diff options
author | David Kalnischkies <david@kalnischkies.de> | 2015-09-12 01:23:01 +0200 |
---|---|---|
committer | David Kalnischkies <david@kalnischkies.de> | 2015-09-14 15:22:18 +0200 |
commit | 9bfb1136abfd58e48545304507dedceb2fe87a36 (patch) | |
tree | 586fef2a045bc1bcd3aca7aab9939c53c79311a5 | |
parent | 93a0805bc9afd556e625fb90e98e690b1a847ed4 (diff) |
srv test: do 100 pulls twice and compare list
The previous implementation was still a bit unstable in terms of failing
at times. Lets try if we have more luck with this one.
Git-Dch: Ignore
-rw-r--r-- | apt-pkg/contrib/srvrec.h | 4 | ||||
-rw-r--r-- | test/libapt/srvrecs_test.cc | 88 |
2 files changed, 65 insertions, 27 deletions
diff --git a/apt-pkg/contrib/srvrec.h b/apt-pkg/contrib/srvrec.h index 2adad03e9..920b6bb32 100644 --- a/apt-pkg/contrib/srvrec.h +++ b/apt-pkg/contrib/srvrec.h @@ -12,6 +12,7 @@ #include <arpa/nameser.h> #include <vector> #include <string> +#include <tuple> class SrvRec { @@ -29,6 +30,9 @@ class SrvRec bool operator<(SrvRec const &other) const { return this->priority < other.priority; } + bool operator==(SrvRec const &other) const { + return std::tie(target, priority, weight, port) == std::tie(other.target, other.priority, other.weight, other.port); + } SrvRec(std::string const Target, u_int16_t const Priority, u_int16_t const Weight, u_int16_t const Port) : diff --git a/test/libapt/srvrecs_test.cc b/test/libapt/srvrecs_test.cc index 4b63d2ccd..b3fa8102d 100644 --- a/test/libapt/srvrecs_test.cc +++ b/test/libapt/srvrecs_test.cc @@ -1,41 +1,75 @@ #include <config.h> #include <apt-pkg/srvrec.h> +#include <apt-pkg/strutl.h> -#include <string> +#include <algorithm> #include <iostream> +#include <string> #include <gtest/gtest.h> TEST(SrvRecTest, PopFromSrvRecs) { - // the PopFromSrvRecs() is using a random number so we must - // run it a bunch of times to ensure we are not fooled by randomness - std::set<std::string> selected; - for(size_t i = 0; i < 100; ++i) + std::vector<SrvRec> Meep; + Meep.emplace_back("foo", 20, 0, 80); + Meep.emplace_back("bar", 20, 0, 80); + Meep.emplace_back("baz", 30, 0, 80); + + EXPECT_EQ(Meep.size(), 3); + SrvRec const result = PopFromSrvRecs(Meep); + // ensure that pop removed one element + EXPECT_EQ(Meep.size(), 2); + EXPECT_NE(result.target, "baz"); + + SrvRec const result2 = PopFromSrvRecs(Meep); + EXPECT_NE(result.target, result2.target); + EXPECT_NE(result2.target, "baz"); + EXPECT_EQ(Meep.size(), 1); + + SrvRec const result3 = PopFromSrvRecs(Meep); + EXPECT_EQ(result3.target, "baz"); + EXPECT_TRUE(Meep.empty()); +} + +TEST(SrvRecTest,Randomness) +{ + constexpr unsigned int testLength = 100; + std::vector<SrvRec> base1; + std::vector<SrvRec> base2; + std::vector<SrvRec> base3; + for (unsigned int i = 0; i < testLength; ++i) { - std::vector<SrvRec> Meep; - Meep.emplace_back("foo", 20, 0, 80); - Meep.emplace_back("bar", 20, 0, 80); - Meep.emplace_back("baz", 30, 0, 80); - - EXPECT_EQ(Meep.size(), 3); - SrvRec const result = PopFromSrvRecs(Meep); - selected.insert(result.target); - // ensure that pop removed one element - EXPECT_EQ(Meep.size(), 2); - EXPECT_NE(result.target, "baz"); - - SrvRec const result2 = PopFromSrvRecs(Meep); - EXPECT_NE(result.target, result2.target); - EXPECT_NE(result2.target, "baz"); - EXPECT_EQ(Meep.size(), 1); - - SrvRec const result3 = PopFromSrvRecs(Meep); - EXPECT_EQ(result3.target, "baz"); - EXPECT_TRUE(Meep.empty()); + std::string name; + strprintf(name, "foo%d", i); + base1.emplace_back(name, 20, 0, 80); + base2.emplace_back(name, 20, 0, 80); + base3.emplace_back(name, 30, 0, 80); } + EXPECT_EQ(testLength, base1.size()); + EXPECT_EQ(testLength, base2.size()); + EXPECT_EQ(testLength, base3.size()); + std::move(base3.begin(), base3.end(), std::back_inserter(base2)); + EXPECT_EQ(testLength*2, base2.size()); + + std::vector<SrvRec> first_pull; + for (unsigned int i = 0; i < testLength; ++i) + first_pull.push_back(PopFromSrvRecs(base1)); + EXPECT_TRUE(base1.empty()); + EXPECT_FALSE(first_pull.empty()); + EXPECT_EQ(testLength, first_pull.size()); + + std::vector<SrvRec> second_pull; + for (unsigned int i = 0; i < testLength; ++i) + second_pull.push_back(PopFromSrvRecs(base2)); + EXPECT_FALSE(base2.empty()); + EXPECT_FALSE(second_pull.empty()); + EXPECT_EQ(testLength, second_pull.size()); + + EXPECT_EQ(first_pull.size(), second_pull.size()); + EXPECT_TRUE(std::all_of(first_pull.begin(), first_pull.end(), [](SrvRec const &R) { return R.priority == 20; })); + EXPECT_TRUE(std::all_of(second_pull.begin(), second_pull.end(), [](SrvRec const &R) { return R.priority == 20; })); + EXPECT_FALSE(std::equal(first_pull.begin(), first_pull.end(), second_pull.begin())); - // ensure that after enough runs we end up with both selected - EXPECT_EQ(selected.size(), 2); + EXPECT_TRUE(std::all_of(base2.begin(), base2.end(), [](SrvRec const &R) { return R.priority == 30; })); } |