BitSet.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (C) 2009-2019, 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. ANKI_TEST(Util, BitSet)
  8. {
  9. {
  10. BitSet<4, U8> a = {false};
  11. ANKI_TEST_EXPECT_EQ(a.get(3), false);
  12. a.set(3);
  13. ANKI_TEST_EXPECT_EQ(a.get(3), true);
  14. }
  15. {
  16. BitSet<9, U8> a = {true};
  17. ANKI_TEST_EXPECT_EQ(a.get(8), true);
  18. a.set(8, false);
  19. ANKI_TEST_EXPECT_EQ(a.get(8), false);
  20. }
  21. {
  22. BitSet<4, U64> a = {true};
  23. ANKI_TEST_EXPECT_EQ(a.get(3), true);
  24. a.set(3, false);
  25. ANKI_TEST_EXPECT_EQ(a.get(3), false);
  26. }
  27. {
  28. const U N = 120;
  29. BitSet<N, U64> a = {false};
  30. for(U i = 0; i < 1000; ++i)
  31. {
  32. U r = rand() % N;
  33. ANKI_TEST_EXPECT_EQ(a.get(r), false);
  34. a.set(r, true);
  35. ANKI_TEST_EXPECT_EQ(a.get(r), true);
  36. for(U n = 0; n < N; ++n)
  37. {
  38. if(n != r)
  39. {
  40. ANKI_TEST_EXPECT_EQ(a.get(n), false);
  41. }
  42. }
  43. a.set(r, false);
  44. }
  45. }
  46. {
  47. const U N = 256;
  48. BitSet<N, U64> a = {false};
  49. ANKI_TEST_EXPECT_EQ(a.getMostSignificantBit(), MAX_U);
  50. a.set(32);
  51. ANKI_TEST_EXPECT_EQ(a.getMostSignificantBit(), 32);
  52. a.set(33);
  53. ANKI_TEST_EXPECT_EQ(a.getMostSignificantBit(), 33);
  54. a.set(68);
  55. ANKI_TEST_EXPECT_EQ(a.getMostSignificantBit(), 68);
  56. a.unsetAll();
  57. a.set(255);
  58. ANKI_TEST_EXPECT_EQ(a.getMostSignificantBit(), 255);
  59. }
  60. }