]>
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; | |
56 | for (unsigned int i = 0; i < testLength; ++i) | |
57 | first_pull.push_back(PopFromSrvRecs(base1)); | |
58 | EXPECT_TRUE(base1.empty()); | |
59 | EXPECT_FALSE(first_pull.empty()); | |
60 | EXPECT_EQ(testLength, first_pull.size()); | |
61 | ||
62 | std::vector<SrvRec> second_pull; | |
63 | for (unsigned int i = 0; i < testLength; ++i) | |
64 | second_pull.push_back(PopFromSrvRecs(base2)); | |
65 | EXPECT_FALSE(base2.empty()); | |
66 | EXPECT_FALSE(second_pull.empty()); | |
67 | EXPECT_EQ(testLength, second_pull.size()); | |
68 | ||
69 | EXPECT_EQ(first_pull.size(), second_pull.size()); | |
70 | EXPECT_TRUE(std::all_of(first_pull.begin(), first_pull.end(), [](SrvRec const &R) { return R.priority == 20; })); | |
71 | EXPECT_TRUE(std::all_of(second_pull.begin(), second_pull.end(), [](SrvRec const &R) { return R.priority == 20; })); | |
72 | EXPECT_FALSE(std::equal(first_pull.begin(), first_pull.end(), second_pull.begin())); | |
c29dbdff | 73 | |
9bfb1136 | 74 | EXPECT_TRUE(std::all_of(base2.begin(), base2.end(), [](SrvRec const &R) { return R.priority == 30; })); |
c29dbdff | 75 | } |