You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

76 lines
2.4 KiB

  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. for (unsigned int i = 0; i < testLength; ++i)
  48. first_pull.push_back(PopFromSrvRecs(base1));
  49. EXPECT_TRUE(base1.empty());
  50. EXPECT_FALSE(first_pull.empty());
  51. EXPECT_EQ(testLength, first_pull.size());
  52. std::vector<SrvRec> second_pull;
  53. for (unsigned int i = 0; i < testLength; ++i)
  54. second_pull.push_back(PopFromSrvRecs(base2));
  55. EXPECT_FALSE(base2.empty());
  56. EXPECT_FALSE(second_pull.empty());
  57. EXPECT_EQ(testLength, second_pull.size());
  58. EXPECT_EQ(first_pull.size(), second_pull.size());
  59. EXPECT_TRUE(std::all_of(first_pull.begin(), first_pull.end(), [](SrvRec const &R) { return R.priority == 20; }));
  60. EXPECT_TRUE(std::all_of(second_pull.begin(), second_pull.end(), [](SrvRec const &R) { return R.priority == 20; }));
  61. EXPECT_FALSE(std::equal(first_pull.begin(), first_pull.end(), second_pull.begin()));
  62. EXPECT_TRUE(std::all_of(base2.begin(), base2.end(), [](SrvRec const &R) { return R.priority == 30; }));
  63. }