string_pool_ut.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "string_pool.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. using namespace NMonitoring;
  4. Y_UNIT_TEST_SUITE(TStringPoolTest) {
  5. Y_UNIT_TEST(PutIfAbsent) {
  6. TStringPoolBuilder strPool;
  7. strPool.SetSorted(true);
  8. auto* h1 = strPool.PutIfAbsent("one");
  9. auto* h2 = strPool.PutIfAbsent("two");
  10. auto* h3 = strPool.PutIfAbsent("two");
  11. UNIT_ASSERT(h1 != h2);
  12. UNIT_ASSERT(h2 == h3);
  13. UNIT_ASSERT_VALUES_EQUAL(h1->Frequency, 1);
  14. UNIT_ASSERT_VALUES_EQUAL(h1->Index, 0);
  15. UNIT_ASSERT_VALUES_EQUAL(h2->Frequency, 2);
  16. UNIT_ASSERT_VALUES_EQUAL(h2->Index, 1);
  17. UNIT_ASSERT_VALUES_EQUAL(strPool.BytesSize(), 6);
  18. UNIT_ASSERT_VALUES_EQUAL(strPool.Count(), 2);
  19. }
  20. Y_UNIT_TEST(SortByFrequency) {
  21. TStringPoolBuilder strPool;
  22. strPool.SetSorted(true);
  23. auto* h1 = strPool.PutIfAbsent("one");
  24. auto* h2 = strPool.PutIfAbsent("two");
  25. auto* h3 = strPool.PutIfAbsent("two");
  26. UNIT_ASSERT(h1 != h2);
  27. UNIT_ASSERT(h2 == h3);
  28. strPool.Build();
  29. UNIT_ASSERT_VALUES_EQUAL(h1->Frequency, 1);
  30. UNIT_ASSERT_VALUES_EQUAL(h1->Index, 1);
  31. UNIT_ASSERT_VALUES_EQUAL(h2->Frequency, 2);
  32. UNIT_ASSERT_VALUES_EQUAL(h2->Index, 0);
  33. UNIT_ASSERT_VALUES_EQUAL(strPool.BytesSize(), 6);
  34. UNIT_ASSERT_VALUES_EQUAL(strPool.Count(), 2);
  35. }
  36. Y_UNIT_TEST(ForEach) {
  37. TStringPoolBuilder strPool;
  38. strPool.SetSorted(true);
  39. strPool.PutIfAbsent("one");
  40. strPool.PutIfAbsent("two");
  41. strPool.PutIfAbsent("two");
  42. strPool.PutIfAbsent("three");
  43. strPool.PutIfAbsent("three");
  44. strPool.PutIfAbsent("three");
  45. UNIT_ASSERT_VALUES_EQUAL(strPool.BytesSize(), 11);
  46. UNIT_ASSERT_VALUES_EQUAL(strPool.Count(), 3);
  47. strPool.Build();
  48. TVector<TString> strings;
  49. TVector<ui32> indexes;
  50. TVector<ui32> frequences;
  51. strPool.ForEach([&](TStringBuf str, ui32 index, ui32 freq) {
  52. strings.emplace_back(str);
  53. indexes.push_back(index);
  54. frequences.push_back(freq);
  55. });
  56. TVector<TString> expectedStrings = {"three", "two", "one"};
  57. UNIT_ASSERT_EQUAL(strings, expectedStrings);
  58. TVector<ui32> expectedIndexes = {0, 1, 2};
  59. UNIT_ASSERT_EQUAL(indexes, expectedIndexes);
  60. TVector<ui32> expectedFrequences = {3, 2, 1};
  61. UNIT_ASSERT_EQUAL(frequences, expectedFrequences);
  62. }
  63. }