]>
Commit | Line | Data |
---|---|---|
c29dbdff MV |
1 | #include <config.h> |
2 | ||
3 | #include <apt-pkg/srvrec.h> | |
9bfb1136 | 4 | #include <apt-pkg/strutl.h> |
c29dbdff | 5 | |
9bfb1136 | 6 | #include <algorithm> |
c29dbdff | 7 | #include <iostream> |
9bfb1136 | 8 | #include <string> |
c29dbdff MV |
9 | |
10 | #include <gtest/gtest.h> | |
11 | ||
12 | TEST(SrvRecTest, PopFromSrvRecs) | |
13 | { | |
9bfb1136 DK |
14 | std::vector<SrvRec> Meep; |
15 | Meep.emplace_back("foo", 20, 0, 80); | |
16 | Meep.emplace_back("bar", 20, 0, 80); | |
17 | Meep.emplace_back("baz", 30, 0, 80); | |
18 | ||
19 | EXPECT_EQ(Meep.size(), 3); | |
20 | SrvRec const result = PopFromSrvRecs(Meep); | |
21 | // ensure that pop removed one element | |
22 | EXPECT_EQ(Meep.size(), 2); | |
23 | EXPECT_NE(result.target, "baz"); | |
24 | ||
25 | SrvRec const result2 = PopFromSrvRecs(Meep); | |
26 | EXPECT_NE(result.target, result2.target); | |
27 | EXPECT_NE(result2.target, "baz"); | |
28 | EXPECT_EQ(Meep.size(), 1); | |
29 | ||
30 | SrvRec const result3 = PopFromSrvRecs(Meep); | |
31 | EXPECT_EQ(result3.target, "baz"); | |
32 | EXPECT_TRUE(Meep.empty()); | |
33 | } | |
34 | ||
35 | TEST(SrvRecTest,Randomness) | |
36 | { | |
37 | constexpr unsigned int testLength = 100; | |
38 | std::vector<SrvRec> base1; | |
39 | std::vector<SrvRec> base2; | |
40 | std::vector<SrvRec> base3; | |
41 | for (unsigned int i = 0; i < testLength; ++i) | |
c29dbdff | 42 | { |
9bfb1136 DK |
43 | std::string name; |
44 | strprintf(name, "foo%d", i); | |
45 | base1.emplace_back(name, 20, 0, 80); | |
46 | base2.emplace_back(name, 20, 0, 80); | |
47 | base3.emplace_back(name, 30, 0, 80); | |
c29dbdff | 48 | } |
9bfb1136 DK |
49 | EXPECT_EQ(testLength, base1.size()); |
50 | EXPECT_EQ(testLength, base2.size()); | |
51 | EXPECT_EQ(testLength, base3.size()); | |
52 | std::move(base3.begin(), base3.end(), std::back_inserter(base2)); | |
53 | EXPECT_EQ(testLength*2, base2.size()); | |
54 | ||
55 | std::vector<SrvRec> first_pull; | |
379a36f4 | 56 | auto const startingClock = clock(); |
9bfb1136 DK |
57 | for (unsigned int i = 0; i < testLength; ++i) |
58 | first_pull.push_back(PopFromSrvRecs(base1)); | |
59 | EXPECT_TRUE(base1.empty()); | |
60 | EXPECT_FALSE(first_pull.empty()); | |
61 | EXPECT_EQ(testLength, first_pull.size()); | |
62 | ||
379a36f4 DK |
63 | // busy-wait for a cpu-clock change as we use it as "random" value |
64 | if (startingClock != -1) | |
65 | for (int i = 0; i < 100000; ++i) | |
66 | if (startingClock != clock()) | |
67 | break; | |
68 | ||
9bfb1136 DK |
69 | std::vector<SrvRec> second_pull; |
70 | for (unsigned int i = 0; i < testLength; ++i) | |
71 | second_pull.push_back(PopFromSrvRecs(base2)); | |
72 | EXPECT_FALSE(base2.empty()); | |
73 | EXPECT_FALSE(second_pull.empty()); | |
74 | EXPECT_EQ(testLength, second_pull.size()); | |
75 | ||
76 | EXPECT_EQ(first_pull.size(), second_pull.size()); | |
77 | EXPECT_TRUE(std::all_of(first_pull.begin(), first_pull.end(), [](SrvRec const &R) { return R.priority == 20; })); | |
78 | EXPECT_TRUE(std::all_of(second_pull.begin(), second_pull.end(), [](SrvRec const &R) { return R.priority == 20; })); | |
379a36f4 DK |
79 | if (startingClock != -1 && startingClock != clock()) |
80 | EXPECT_FALSE(std::equal(first_pull.begin(), first_pull.end(), second_pull.begin())); | |
c29dbdff | 81 | |
9bfb1136 | 82 | EXPECT_TRUE(std::all_of(base2.begin(), base2.end(), [](SrvRec const &R) { return R.priority == 30; })); |
c29dbdff | 83 | } |