sharded_set_ut.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <library/cpp/yt/containers/sharded_set.h>
  2. #include <library/cpp/testing/gtest/gtest.h>
  3. #include <random>
  4. namespace NYT {
  5. namespace {
  6. ////////////////////////////////////////////////////////////////////////////////
  7. struct TIntToShard
  8. {
  9. int operator()(int value) const
  10. {
  11. return value % 16;
  12. }
  13. };
  14. using TSet = TShardedSet<int, 16, TIntToShard>;
  15. ////////////////////////////////////////////////////////////////////////////////
  16. TEST(TShardedSetTest, Insert)
  17. {
  18. TSet set;
  19. for (int i = 0; i < 4; i++) {
  20. set.insert(i);
  21. }
  22. for (int i = 0; i < 4; i++) {
  23. set.insert(i);
  24. }
  25. EXPECT_EQ(4u, set.size());
  26. for (int i = 0; i < 4; i++)
  27. EXPECT_EQ(1u, set.count(i));
  28. EXPECT_EQ(0u, set.count(4));
  29. }
  30. TEST(TShardedSetTest, Erase)
  31. {
  32. TSet set;
  33. for (int i = 0; i < 8; i++) {
  34. set.insert(i);
  35. }
  36. EXPECT_EQ(8u, set.size());
  37. // Remove elements one by one and check if all other elements are still there.
  38. for (int i = 0; i < 8; i++) {
  39. EXPECT_EQ(1u, set.count(i));
  40. EXPECT_TRUE(set.erase(i));
  41. EXPECT_EQ(0u, set.count(i));
  42. EXPECT_EQ(8u - i - 1, set.size());
  43. for (int j = i + 1; j < 8; j++) {
  44. EXPECT_EQ(1u, set.count(j));
  45. }
  46. }
  47. EXPECT_EQ(0u, set.count(8));
  48. }
  49. TEST(TShardedSetTest, StressTest)
  50. {
  51. TSet set;
  52. constexpr int Iterations = 1'000'000;
  53. constexpr int Values = 128;
  54. THashSet<int> values;
  55. auto checkEverything = [&] {
  56. EXPECT_EQ(values.size(), set.size());
  57. EXPECT_EQ(values.empty(), set.empty());
  58. EXPECT_EQ(values, THashSet<int>(set.begin(), set.end()));
  59. std::array<THashSet<int>, 16> shards;
  60. for (int value : values) {
  61. shards[value % 16].insert(value);
  62. }
  63. for (int shardIndex = 0; shardIndex < 16; ++shardIndex) {
  64. EXPECT_EQ(shards[shardIndex], set.Shard(shardIndex));
  65. }
  66. for (int value = 0; value < Values; ++value) {
  67. EXPECT_EQ(values.contains(value), set.contains(value));
  68. EXPECT_EQ(values.count(value), set.count(value));
  69. }
  70. };
  71. std::mt19937_64 rng(42);
  72. for (int iteration = 0; iteration < Iterations; ++iteration) {
  73. if (rng() % 100 == 0) {
  74. set.clear();
  75. values.clear();
  76. checkEverything();
  77. }
  78. int value = rng() % Values;
  79. if (rng() % 2 == 0) {
  80. set.insert(value);
  81. values.insert(value);
  82. } else {
  83. set.erase(value);
  84. values.erase(value);
  85. }
  86. checkEverything();
  87. }
  88. }
  89. ////////////////////////////////////////////////////////////////////////////////
  90. } // namespace
  91. } // namespace NYT