bititerator_ut.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "bititerator.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/generic/vector.h>
  4. Y_UNIT_TEST_SUITE(TBitIteratorTest) {
  5. TVector<ui16> GenWords() {
  6. TVector<ui16> words(1, 0);
  7. for (ui16 word = 1; word; ++word)
  8. words.push_back(word);
  9. return words;
  10. }
  11. template <typename TWord>
  12. void AssertPeekRead(TBitIterator<TWord> & iter, ui8 count, TWord expected) {
  13. auto peek = iter.Peek(count);
  14. auto read = iter.Read(count);
  15. UNIT_ASSERT_EQUAL(peek, read);
  16. UNIT_ASSERT_EQUAL(peek, expected);
  17. }
  18. Y_UNIT_TEST(TestNextAndPeek) {
  19. const auto& words = GenWords();
  20. TBitIterator<ui16> iter(words.data());
  21. ui16 word = 0;
  22. for (int i = 0; i != (1 << 16); ++i, ++word) {
  23. for (int bit = 0; bit != 16; ++bit) {
  24. auto peek = iter.Peek();
  25. auto next = iter.Next();
  26. UNIT_ASSERT_EQUAL(peek, next);
  27. UNIT_ASSERT_EQUAL(peek, (word >> bit) & 1);
  28. }
  29. UNIT_ASSERT_EQUAL(iter.NextWord(), words.data() + i + 1);
  30. }
  31. UNIT_ASSERT_EQUAL(iter.NextWord(), words.data() + words.size());
  32. }
  33. Y_UNIT_TEST(TestAlignedReadAndPeek) {
  34. const auto& words = GenWords();
  35. TBitIterator<ui16> iter(words.data());
  36. ui16 word = 0;
  37. for (int i = 0; i != (1 << 16); ++i, ++word) {
  38. AssertPeekRead(iter, 16, word);
  39. UNIT_ASSERT_EQUAL(iter.NextWord(), words.data() + i + 1);
  40. }
  41. UNIT_ASSERT_EQUAL(iter.NextWord(), words.data() + words.size());
  42. }
  43. Y_UNIT_TEST(TestForward) {
  44. TVector<ui32> words;
  45. words.push_back((1 << 10) | (1 << 20) | (1 << 25));
  46. words.push_back(1 | (1 << 5) | (1 << 6) | (1 << 30));
  47. for (int i = 0; i < 3; ++i)
  48. words.push_back(0);
  49. words.push_back(1 << 10);
  50. TBitIterator<ui32> iter(words.data());
  51. UNIT_ASSERT(!iter.Next());
  52. UNIT_ASSERT(!iter.Next());
  53. UNIT_ASSERT(!iter.Next());
  54. iter.Forward(6);
  55. UNIT_ASSERT(!iter.Next());
  56. UNIT_ASSERT(iter.Next());
  57. UNIT_ASSERT(!iter.Next());
  58. iter.Forward(8);
  59. UNIT_ASSERT(iter.Next());
  60. iter.Forward(4);
  61. UNIT_ASSERT(iter.Next());
  62. iter.Forward(5);
  63. UNIT_ASSERT(!iter.Next());
  64. UNIT_ASSERT(iter.Next());
  65. iter.Forward(4);
  66. UNIT_ASSERT(iter.Next());
  67. iter.Reset(words.data());
  68. iter.Forward(38);
  69. UNIT_ASSERT(iter.Next());
  70. UNIT_ASSERT(!iter.Next());
  71. UNIT_ASSERT_EQUAL(iter.NextWord(), words.data() + 2);
  72. iter.Forward(24 + 32 * 3 + 9);
  73. UNIT_ASSERT(!iter.Next());
  74. UNIT_ASSERT(iter.Next());
  75. UNIT_ASSERT(!iter.Next());
  76. UNIT_ASSERT_EQUAL(iter.NextWord(), words.data() + 6);
  77. }
  78. Y_UNIT_TEST(TestUnalignedReadAndPeek) {
  79. TVector<ui32> words;
  80. words.push_back((1 << 10) | (1 << 20) | (1 << 25));
  81. words.push_back(1 | (1 << 5) | (1 << 6) | (1 << 30));
  82. for (int i = 0; i < 5; ++i)
  83. words.push_back(1 | (1 << 10));
  84. TBitIterator<ui32> iter(words.data());
  85. AssertPeekRead(iter, 5, ui32(0));
  86. AssertPeekRead(iter, 7, ui32(1 << 5));
  87. AssertPeekRead(iter, 21, ui32((1 << 8) | (1 << 13) | (1 << 20)));
  88. AssertPeekRead(iter, 32, (words[1] >> 1) | (1 << 31));
  89. iter.Forward(8);
  90. UNIT_ASSERT(!iter.Next());
  91. UNIT_ASSERT(iter.Next());
  92. UNIT_ASSERT(!iter.Next());
  93. }
  94. }