aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/utility_ut.cpp
blob: 3226322cc50fa99f168bedc48f1875784f5805c6 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "utility.h"
#include "ymath.h"

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

// DO_NOT_STYLE

class TTest {
public:
    inline TTest(int val)
        : Val(val)
    {
    }

    inline void Swap(TTest& t) {
        DoSwap(Val, t.Val);
    }

    int Val;

private:
    TTest(const TTest&);
    TTest& operator=(const TTest&);
};

struct TUnorderedTag {
    TStringBuf Tag;
};

static bool operator<(const TUnorderedTag, const TUnorderedTag) {
    return false;
}

static bool operator>(const TUnorderedTag, const TUnorderedTag) = delete;

Y_UNIT_TEST_SUITE(TUtilityTest) {

    Y_UNIT_TEST(TestSwapPrimitive) {
        int i = 0;
        int j = 1;

        DoSwap(i, j);

        UNIT_ASSERT_EQUAL(i, 1);
        UNIT_ASSERT_EQUAL(j, 0);
    }

    Y_UNIT_TEST(TestSwapClass) {
        TTest i(0);
        TTest j(1);

        DoSwap(i, j);

        UNIT_ASSERT_EQUAL(i.Val, 1);
        UNIT_ASSERT_EQUAL(j.Val, 0);
    }

    Y_UNIT_TEST(TestMaxMin) {
        static_assert(Min(10, 3, 8) == 3, "Min doesn't work");
        static_assert(Max(10, 3, 8) == 10, "Max doesn't work");
        UNIT_ASSERT_EQUAL(Min(10, 3, 8), 3);
        UNIT_ASSERT_EQUAL(Max(3.5, 4.2, 8.1, 99.025, 0.33, 29.0), 99.025);

        UNIT_ASSERT_VALUES_EQUAL(Min(TUnorderedTag{"first"}, TUnorderedTag{"second"}).Tag, "first");
        UNIT_ASSERT_VALUES_EQUAL(Max(TUnorderedTag{"first"}, TUnorderedTag{"second"}).Tag, "first");
        UNIT_ASSERT_VALUES_EQUAL(Min(TUnorderedTag{"first"}, TUnorderedTag{"second"}, TUnorderedTag{"third"}).Tag, "first");
        UNIT_ASSERT_VALUES_EQUAL(Max(TUnorderedTag{"first"}, TUnorderedTag{"second"}, TUnorderedTag{"third"}).Tag, "first");
    }

    Y_UNIT_TEST(TestMean) {
        UNIT_ASSERT_EQUAL(Mean(5), 5);
        UNIT_ASSERT_EQUAL(Mean(1, 2, 3), 2);
        UNIT_ASSERT_EQUAL(Mean(6, 5, 4), 5);
        UNIT_ASSERT_EQUAL(Mean(1, 2), 1.5);
        UNIT_ASSERT(Abs(Mean(1., 2., 7.5) - 3.5) < std::numeric_limits<double>::epsilon());
    }

    Y_UNIT_TEST(TestZeroInitWithDefaultZeros) {
        struct TStructWithPaddingBytes: public TZeroInit<TStructWithPaddingBytes> {
            TStructWithPaddingBytes()
                : TZeroInit<TStructWithPaddingBytes>() {
            }
            bool Field1_ = static_cast<bool>(0);
            // here between Field1_ and Field2_ will be padding bytes
            i64 Field2_ = 0;
        };

        TStructWithPaddingBytes foo{};

        // all bytes must be zeroes, and MSAN will not complain about reading from padding bytes
        const char* const fooPtr = (char*)&foo;
        for (size_t i = 0; i < sizeof(TStructWithPaddingBytes); ++i) {
            const char byte = fooPtr[i];
            UNIT_ASSERT_EQUAL(byte, 0);
        }
    }

    Y_UNIT_TEST(TestZeroInitWithDefaultNonZeros) {
        struct TStructWithPaddingBytes: public TZeroInit<TStructWithPaddingBytes> {
            TStructWithPaddingBytes()
                : TZeroInit<TStructWithPaddingBytes>() {
            }
            bool Field1_ = true;
            // here between Field1_ and Field2_ will be padding bytes
            i64 Field2_ = 100500;
        };

        TStructWithPaddingBytes foo{};

        // check that default values are set correctly
        UNIT_ASSERT_EQUAL(foo.Field1_, true);
        UNIT_ASSERT_EQUAL(foo.Field2_, 100500);

        const char* const fooPtr = (char*)&foo;
        // just reading all bytes, and MSAN must not complain about reading padding bytes
        for (size_t i = 0; i < sizeof(TStructWithPaddingBytes); ++i) {
            const char byte = fooPtr[i];
            UNIT_ASSERT_EQUAL(byte, byte);
        }
    }

    Y_UNIT_TEST(TestClampValNoClamp) {
        double val = 2;
        double lo = 1;
        double hi = 3;
        const double& clamped = ClampVal(val, lo, hi);
        UNIT_ASSERT_EQUAL(clamped, val);
        UNIT_ASSERT_EQUAL(&clamped, &val);
    }

    Y_UNIT_TEST(TestClampValLo) {
        double val = 2;
        double lo = 3;
        double hi = 4;
        const double& clamped = ClampVal(val, lo, hi);
        UNIT_ASSERT_EQUAL(clamped, lo);
        UNIT_ASSERT_EQUAL(&clamped, &lo);
    }

    Y_UNIT_TEST(TestClampValHi) {
        double val = 4;
        double lo = 3;
        double hi = 2;
        const double& clamped = ClampVal(val, lo, hi);
        UNIT_ASSERT_EQUAL(clamped, hi);
        UNIT_ASSERT_EQUAL(&clamped, &hi);
    }

    Y_UNIT_TEST(TestSecureZero) {
        constexpr size_t checkSize = 128;
        char test[checkSize];

        // fill with garbage
        for (size_t i = 0; i < checkSize; ++i) {
            test[i] = i;
        }

        SecureZero(test, checkSize);

        for (size_t i = 0; i < checkSize; ++i) {
            UNIT_ASSERT_EQUAL(test[i], 0);
        }
    }

    Y_UNIT_TEST(TestSecureZeroTemplate) {
        constexpr size_t checkSize = 128;
        char test[checkSize];

        // fill with garbage
        for (size_t i = 0; i < checkSize; ++i) {
            test[i] = i;
        }

        SecureZero(test);

        for (size_t i = 0; i < checkSize; ++i) {
            UNIT_ASSERT_EQUAL(test[i], 0);
        }
    }
};