BitSet.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <Tests/Framework/Framework.h>
  6. #include <AnKi/Util/BitSet.h>
  7. #include <bitset>
  8. using namespace anki;
  9. template<U32 N, typename T>
  10. static void randomBitsets(BitSet<N, T>& bitset, std::bitset<N>& stlBitset)
  11. {
  12. for(U32 bit = 0; bit < N; ++bit)
  13. {
  14. const Bool set = rand() & 1;
  15. bitset.set(bit, set);
  16. stlBitset[bit] = set;
  17. }
  18. }
  19. template<U32 N, typename T>
  20. static void fuzzyTest()
  21. {
  22. BitSet<N, T> bitset = {false};
  23. std::bitset<N> stlBitset;
  24. for(U32 i = 0; i < 10000; ++i)
  25. {
  26. const U32 bit = rand() % N;
  27. const U32 set = rand() & 1;
  28. bitset.set(bit, set);
  29. stlBitset[bit] = set;
  30. const U32 mode = rand() % 10;
  31. BitSet<N, T> otherBitset = {false};
  32. std::bitset<N> otherStlBitset;
  33. randomBitsets<N, T>(otherBitset, otherStlBitset);
  34. if(mode == 0)
  35. {
  36. stlBitset.flip();
  37. bitset = ~bitset;
  38. }
  39. else if(mode == 1)
  40. {
  41. bitset |= otherBitset;
  42. stlBitset |= otherStlBitset;
  43. }
  44. else if(mode == 2)
  45. {
  46. bitset = bitset | otherBitset;
  47. stlBitset = stlBitset | otherStlBitset;
  48. }
  49. else if(mode == 3)
  50. {
  51. bitset &= otherBitset;
  52. stlBitset &= otherStlBitset;
  53. }
  54. else if(mode == 4)
  55. {
  56. bitset = bitset & otherBitset;
  57. stlBitset = stlBitset & otherStlBitset;
  58. }
  59. else if(mode == 5)
  60. {
  61. bitset ^= otherBitset;
  62. stlBitset ^= otherStlBitset;
  63. }
  64. else if(mode == 6)
  65. {
  66. bitset = bitset ^ otherBitset;
  67. stlBitset = stlBitset ^ otherStlBitset;
  68. }
  69. }
  70. for(U32 bit = 0; bit < N; ++bit)
  71. {
  72. const Bool set = bitset.get(bit);
  73. const Bool stlSet = stlBitset[bit];
  74. ANKI_TEST_EXPECT_EQ(set, stlSet);
  75. }
  76. }
  77. template<typename TChunkType>
  78. static void checkMsb()
  79. {
  80. const U N = 256;
  81. BitSet<N, TChunkType> a = {false};
  82. ANKI_TEST_EXPECT_EQ(a.getMostSignificantBit(), kMaxU32);
  83. a.set(32);
  84. ANKI_TEST_EXPECT_EQ(a.getMostSignificantBit(), 32);
  85. a.set(33);
  86. ANKI_TEST_EXPECT_EQ(a.getMostSignificantBit(), 33);
  87. a.set(68);
  88. ANKI_TEST_EXPECT_EQ(a.getMostSignificantBit(), 68);
  89. a.unset(68);
  90. ANKI_TEST_EXPECT_EQ(a.getMostSignificantBit(), 33);
  91. a.unsetAll();
  92. a.set(255);
  93. ANKI_TEST_EXPECT_EQ(a.getMostSignificantBit(), 255);
  94. }
  95. template<typename TChunkType>
  96. static void checkLsb()
  97. {
  98. constexpr U32 kCount = 256;
  99. BitSet<kCount, TChunkType> a = {false};
  100. ANKI_TEST_EXPECT_EQ(a.getLeastSignificantBit(), kMaxU32);
  101. a.set(33);
  102. ANKI_TEST_EXPECT_EQ(a.getLeastSignificantBit(), 33);
  103. a.set(34);
  104. ANKI_TEST_EXPECT_EQ(a.getLeastSignificantBit(), 33);
  105. a.set(2);
  106. ANKI_TEST_EXPECT_EQ(a.getLeastSignificantBit(), 2);
  107. a.unset(2);
  108. ANKI_TEST_EXPECT_EQ(a.getLeastSignificantBit(), 33);
  109. a.setAll();
  110. ANKI_TEST_EXPECT_EQ(a.getLeastSignificantBit(), 0);
  111. }
  112. template<typename TChunkType>
  113. static void checkSetNLeastSignificant()
  114. {
  115. constexpr U32 kCount = 256;
  116. BitSet<kCount, TChunkType> a = {true};
  117. for(U32 n = 1; n <= kCount; ++n)
  118. {
  119. a.setAll();
  120. a.unsetNLeastSignificantBits(n);
  121. for(U i = 0; i < kCount; ++i)
  122. {
  123. ANKI_TEST_EXPECT_EQ(a.get(i), (i < n) ? false : true);
  124. }
  125. }
  126. }
  127. ANKI_TEST(Util, BitSet)
  128. {
  129. {
  130. #define ANKI_BITSET_TEST(N) \
  131. fuzzyTest<N, U8>(); \
  132. fuzzyTest<N, U16>(); \
  133. fuzzyTest<N, U32>(); \
  134. fuzzyTest<N, U64>();
  135. ANKI_BITSET_TEST(4)
  136. ANKI_BITSET_TEST(9)
  137. ANKI_BITSET_TEST(33)
  138. ANKI_BITSET_TEST(250)
  139. ANKI_BITSET_TEST(30)
  140. #undef ANKI_BITSET_TEST
  141. }
  142. checkMsb<U8>();
  143. checkMsb<U16>();
  144. checkMsb<U32>();
  145. checkMsb<U64>();
  146. checkLsb<U8>();
  147. checkLsb<U16>();
  148. checkLsb<U32>();
  149. checkLsb<U64>();
  150. checkSetNLeastSignificant<U8>();
  151. checkSetNLeastSignificant<U16>();
  152. checkSetNLeastSignificant<U32>();
  153. checkSetNLeastSignificant<U64>();
  154. }