BitSet.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright (C) 2009-2023, 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. ANKI_TEST(Util, BitSet)
  113. {
  114. {
  115. #define ANKI_BITSET_TEST(N) \
  116. fuzzyTest<N, U8>(); \
  117. fuzzyTest<N, U16>(); \
  118. fuzzyTest<N, U32>(); \
  119. fuzzyTest<N, U64>();
  120. ANKI_BITSET_TEST(4)
  121. ANKI_BITSET_TEST(9)
  122. ANKI_BITSET_TEST(33)
  123. ANKI_BITSET_TEST(250)
  124. ANKI_BITSET_TEST(30)
  125. #undef ANKI_BITSET_TEST
  126. }
  127. checkMsb<U8>();
  128. checkMsb<U16>();
  129. checkMsb<U32>();
  130. checkMsb<U64>();
  131. checkLsb<U8>();
  132. checkLsb<U16>();
  133. checkLsb<U32>();
  134. checkLsb<U64>();
  135. }