BitSet.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (C) 2009-2021, 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. ANKI_TEST(Util, BitSet)
  78. {
  79. {
  80. #define ANKI_BITSET_TEST(N) \
  81. fuzzyTest<N, U8>(); \
  82. fuzzyTest<N, U16>(); \
  83. fuzzyTest<N, U32>(); \
  84. fuzzyTest<N, U64>();
  85. ANKI_BITSET_TEST(4)
  86. ANKI_BITSET_TEST(9)
  87. ANKI_BITSET_TEST(33)
  88. ANKI_BITSET_TEST(250)
  89. ANKI_BITSET_TEST(30)
  90. #undef ANKI_BITSET_TEST
  91. }
  92. {
  93. const U N = 256;
  94. BitSet<N, U64> a = {false};
  95. ANKI_TEST_EXPECT_EQ(a.getMostSignificantBit(), MAX_U32);
  96. a.set(32);
  97. ANKI_TEST_EXPECT_EQ(a.getMostSignificantBit(), 32);
  98. a.set(33);
  99. ANKI_TEST_EXPECT_EQ(a.getMostSignificantBit(), 33);
  100. a.set(68);
  101. ANKI_TEST_EXPECT_EQ(a.getMostSignificantBit(), 68);
  102. a.unsetAll();
  103. a.set(255);
  104. ANKI_TEST_EXPECT_EQ(a.getMostSignificantBit(), 255);
  105. }
  106. }