]>
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 auto const startingClock
= clock();
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());
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())
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());
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; }));
79 if (startingClock
!= -1 && startingClock
!= clock())
80 EXPECT_FALSE(std::equal(first_pull
.begin(), first_pull
.end(), second_pull
.begin()));
82 EXPECT_TRUE(std::all_of(base2
.begin(), base2
.end(), [](SrvRec
const &R
) { return R
.priority
== 30; }));