srvrecs_test.cc 2.7 KB

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