]>
git.saurik.com Git - apt.git/blob - test/libapt/srvrecs_test.cc
3 #include <apt-pkg/srvrec.h>
4 #include <apt-pkg/strutl.h>
10 #include <gtest/gtest.h>
12 TEST(SrvRecTest
, PopFromSrvRecs
)
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);
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");
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);
30 SrvRec
const result3
= PopFromSrvRecs(Meep
);
31 EXPECT_EQ(result3
.target
, "baz");
32 EXPECT_TRUE(Meep
.empty());
35 TEST(SrvRecTest
,Randomness
)
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
)
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);
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());
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());
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());
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()));
74 EXPECT_TRUE(std::all_of(base2
.begin(), base2
.end(), [](SrvRec
const &R
) { return R
.priority
== 30; }));