aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/enumbitset/enumbitset_ut.cpp
blob: fdc68bf28c17395df8a814ce38be1adf7d734325 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "enumbitset.h"

#include <library/cpp/testing/unittest/registar.h> 

#include <util/stream/str.h>
#include <util/stream/file.h>

enum ETestEnum {
    TE_FIRST = 0,
    TE_1,
    TE_2,
    TE_3,
    TE_4,
    TE_MIDDLE = TE_4 + 64,
    TE_5,
    TE_6,
    TE_7,
    TE_MAX,

    TE_OVERFLOW, // to test overflow
};
using TTestBitSet = TEnumBitSet<ETestEnum, TE_FIRST, TE_MAX>;

Y_UNIT_TEST_SUITE(TEnumBitSetTest) {
    Y_UNIT_TEST(TestMainFunctions) {
        auto ebs = TTestBitSet(TE_FIRST, TE_MIDDLE);

        UNIT_ASSERT(ebs.SafeTest(TE_FIRST));
        UNIT_ASSERT(ebs.SafeTest(TE_MIDDLE));

        ebs.SafeSet(TE_5);
        UNIT_ASSERT(ebs.SafeTest(TE_5));
        UNIT_ASSERT(!ebs.SafeTest(TE_7));

        ebs.SafeSet(TE_OVERFLOW);
        UNIT_ASSERT(!ebs.SafeTest(TE_OVERFLOW));
    }

    Y_UNIT_TEST(TestEmpty) {
        TTestBitSet mask;
        UNIT_ASSERT(mask.Empty());
        if (mask)
            UNIT_ASSERT(false && "should be empty");

        mask.Set(TE_FIRST);
        UNIT_ASSERT(!mask.Empty());
        UNIT_ASSERT(mask.Count() == 1);
        if (!mask)
            UNIT_ASSERT(false && "should not be empty");
    }

    Y_UNIT_TEST(TestIter) {
        TTestBitSet mask = TTestBitSet(TE_1, TE_3, TE_7);

        TTestBitSet mask2;
        for (auto elem : mask) {
            mask2.Set(elem);
        }

        UNIT_ASSERT(mask == mask2);
    }

    Y_UNIT_TEST(TestSerialization) {
        auto ebs = TTestBitSet(TE_MIDDLE, TE_6, TE_7);

        TStringStream ss;
        ebs.Save(&ss);

        auto ebs2 = TTestBitSet();
        ebs2.Load(&ss);
        UNIT_ASSERT_EQUAL(ebs, ebs2);
    }

    Y_UNIT_TEST(TestStringRepresentation) {
        auto ebs = TTestBitSet(TE_MIDDLE, TE_6, TE_7);

        UNIT_ASSERT_EQUAL(ebs.ToString(), "D00000000000000000");

        auto ebs2 = TTestBitSet();
        ebs2.FromString("D00000000000000000");
        UNIT_ASSERT_EQUAL(ebs, ebs2);
    }
}