enumbitset_ut.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "enumbitset.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <util/stream/str.h>
  4. #include <util/stream/file.h>
  5. enum ETestEnum {
  6. TE_FIRST = 0,
  7. TE_1,
  8. TE_2,
  9. TE_3,
  10. TE_4,
  11. TE_MIDDLE = TE_4 + 64,
  12. TE_5,
  13. TE_6,
  14. TE_7,
  15. TE_MAX,
  16. TE_OVERFLOW, // to test overflow
  17. };
  18. using TTestBitSet = TEnumBitSet<ETestEnum, TE_FIRST, TE_MAX>;
  19. Y_UNIT_TEST_SUITE(TEnumBitSetTest) {
  20. Y_UNIT_TEST(TestMainFunctions) {
  21. auto ebs = TTestBitSet(TE_FIRST, TE_MIDDLE);
  22. UNIT_ASSERT(ebs.SafeTest(TE_FIRST));
  23. UNIT_ASSERT(ebs.SafeTest(TE_MIDDLE));
  24. ebs.SafeSet(TE_5);
  25. UNIT_ASSERT(ebs.SafeTest(TE_5));
  26. UNIT_ASSERT(!ebs.SafeTest(TE_7));
  27. ebs.SafeSet(TE_OVERFLOW);
  28. UNIT_ASSERT(!ebs.SafeTest(TE_OVERFLOW));
  29. }
  30. Y_UNIT_TEST(TestEmpty) {
  31. TTestBitSet mask;
  32. UNIT_ASSERT(mask.Empty());
  33. if (mask)
  34. UNIT_ASSERT(false && "should be empty");
  35. mask.Set(TE_FIRST);
  36. UNIT_ASSERT(!mask.Empty());
  37. UNIT_ASSERT(mask.Count() == 1);
  38. if (!mask)
  39. UNIT_ASSERT(false && "should not be empty");
  40. }
  41. Y_UNIT_TEST(TestIter) {
  42. TTestBitSet mask = TTestBitSet(TE_1, TE_3, TE_7);
  43. TTestBitSet mask2;
  44. for (auto elem : mask) {
  45. mask2.Set(elem);
  46. }
  47. UNIT_ASSERT(mask == mask2);
  48. }
  49. Y_UNIT_TEST(TestSerialization) {
  50. auto ebs = TTestBitSet(TE_MIDDLE, TE_6, TE_7);
  51. TStringStream ss;
  52. ebs.Save(&ss);
  53. auto ebs2 = TTestBitSet();
  54. ebs2.Load(&ss);
  55. UNIT_ASSERT_EQUAL(ebs, ebs2);
  56. }
  57. Y_UNIT_TEST(TestStringRepresentation) {
  58. auto ebs = TTestBitSet(TE_MIDDLE, TE_6, TE_7);
  59. UNIT_ASSERT_EQUAL(ebs.ToString(), "D00000000000000000");
  60. auto ebs2 = TTestBitSet();
  61. ebs2.FromString("D00000000000000000");
  62. UNIT_ASSERT_EQUAL(ebs, ebs2);
  63. }
  64. }