aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/bit_io/bitinout_ut.cpp
blob: 2a9502260569cedd553aa7a181bad3cb968c6739 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "bitinput.h"
#include "bitoutput.h"

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

#include <util/stream/buffer.h>
#include <util/generic/buffer.h>

namespace NBitIO {
    static const char BITS_REF[] =
        "00100010 01000000 00000000 00100111 11011111 01100111 11010101 00010100 "
        "00100010 01100011 11100011 00110000 11011011 11011111 01001100 00110101 "
        "10011110 01011111 01010000 00000110 00011011 00100110 00010100 01110011 "
        "00001010 10101010 10101010 10101010 10101010 10101010 10101010 10101010 "
        "10110101 01010101 01010101 01010101 01010101 01010101 01010101 01010101 "
        "01000000";

    inline ui64 Bits(ui64 bytes) {
        return bytes << 3ULL;
    }

    inline TString PrintBits(const char* a, const char* b, bool reverse = false) { 
        TString s; 
        TStringOutput out(s);
        for (const char* it = a; it != b; ++it) {
            if (it != a)
                out << ' ';

            ui8 byte = *it;

            if (reverse)
                byte = ReverseBits(byte);

            for (ui32 mask = 1; mask < 0xff; mask <<= 1) {
                out << ((byte & mask) ? '1' : '0');
            }
        }

        return s;
    }

    template <typename T>
    inline TString PrintBits(T t, ui32 bits = Bits(sizeof(T))) { 
        return PrintBits((char*)&t, ((char*)&t) + BytesUp(bits));
    }
}

class TBitIOTest: public TTestBase {
    UNIT_TEST_SUITE(TBitIOTest);
    UNIT_TEST(TestBitIO)
    UNIT_TEST_SUITE_END();

private:
    using TBi = NBitIO::TBitInput;
    using TVec = TVector<char>; 

    void static CheckBits(const TVec& v, const TString& ref, const TString& rem) { 
        UNIT_ASSERT_VALUES_EQUAL_C(NBitIO::PrintBits(v.begin(), v.end()), ref, rem);
    }

    void DoRead(TBi& b, ui32& t) {
        b.Read(t, 1, 0);   // 1
        b.ReadK<3>(t, 1);  // 4
        b.Read(t, 5, 4);   // 9
        b.ReadK<14>(t, 9); // 23
        b.Read(t, 1, 23);  // 24
        b.ReadK<5>(t, 24); // 29
        b.Read(t, 3, 29);  // 32
    }

    template <typename TBo>
    void DoWrite(TBo& b, ui32 t) {
        b.Write(t, 1, 0);  //1
        b.Write(t, 3, 1);  //4
        b.Write(t, 5, 4);  //9
        b.Write(t, 14, 9); //23
        b.Write(t, 1, 23); //24
        b.Write(t, 5, 24); //29
        b.Write(t, 3, 29); //32
    }

    template <typename TBo>
    void DoWrite1(TBo& out, const TString& rem) { 
        out.Write(0x0C, 3);
        UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 1u, (rem + ", " + ToString(__LINE__)));
        out.Write(0x18, 4);
        UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 1u, (rem + ", " + ToString(__LINE__)));
        out.Write(0x0C, 3);
        UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 2u, (rem + ", " + ToString(__LINE__)));
        out.Write(0x30000, 17);
        UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 4u, (rem + ", " + ToString(__LINE__)));
        out.Write(0x0C, 3);
        UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 4u, (rem + ", " + ToString(__LINE__)));
    }

    template <typename TBo>
    void DoWrite2(TBo& out, const TString& rem) { 
        out.Write(0x0C, 3);
        UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 8u, (rem + ", " + ToString(__LINE__)));

        out.Write(0x42, 7);
        UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 9u, (rem + ", " + ToString(__LINE__)));

        DoWrite(out, 1637415112);
        UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 13u, (rem + ", " + ToString(__LINE__)));

        DoWrite(out, 897998715);
        UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 17u, (rem + ", " + ToString(__LINE__)));

        DoWrite(out, 201416527);
        UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 21u, (rem + ", " + ToString(__LINE__)));

        DoWrite(out, 432344219);
        UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 25u, (rem + ", " + ToString(__LINE__)));

        out.Write(0xAAAAAAAAAAAAAAAAULL, 64);
        UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 33u, (rem + ", " + ToString(__LINE__)));

        out.Write(0x5555555555555555ULL, 64);
        UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 41u, (rem + ", " + ToString(__LINE__)));
    }

    void DoBitOutput(NBitIO::TBitOutputYVector& out, const TString& rem) { 
        DoWrite1(out, rem);

        out.WriteWords<8>(0xabcdef);
        UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 8u, (rem + ", " + ToString(__LINE__)));

        DoWrite2(out, rem);
    }

    void DoBitOutput(NBitIO::TBitOutputArray& out, const TString& rem) { 
        DoWrite1(out, rem);

        out.WriteWords<8>(0xabcdef);
        UNIT_ASSERT_VALUES_EQUAL_C(out.GetOffset(), 8u, (rem + ", " + ToString(__LINE__)));

        DoWrite2(out, rem);
    }

    void DoBitInput(TBi& in, const TString& rem) { 
        UNIT_ASSERT(!in.Eof());

        {
            ui64 val;

            val = 0;
            UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 0u, (rem + ": " + NBitIO::PrintBits(val)));

            UNIT_ASSERT_C(in.Read(val, 3), (rem + ": " + NBitIO::PrintBits(val)).data());

            UNIT_ASSERT_VALUES_EQUAL_C(val, 0x4u, (rem + ": " + NBitIO::PrintBits(val)));
            UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 1u, (rem + ": " + NBitIO::PrintBits(val)));
            UNIT_ASSERT_C(!in.Eof(), (rem + ", " + ToString(__LINE__)).data());

            val = 0;
            UNIT_ASSERT_C(in.Read(val, 4), (rem + ": " + NBitIO::PrintBits(val)).data());

            UNIT_ASSERT_VALUES_EQUAL_C(val, 0x8u, (rem + ": " + NBitIO::PrintBits(val)));
            UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 1u, (rem + ": " + NBitIO::PrintBits(val)));
            UNIT_ASSERT_C(!in.Eof(), (rem + ", " + ToString(__LINE__)).data());

            val = 0;
            UNIT_ASSERT_C(in.Read(val, 3), (rem + ": " + NBitIO::PrintBits(val)).data());

            UNIT_ASSERT_VALUES_EQUAL_C(val, 0x4u, (rem + ": " + NBitIO::PrintBits(val)));
            UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 2u, (rem + ": " + NBitIO::PrintBits(val)));
            UNIT_ASSERT_C(!in.Eof(), (rem + ", " + ToString(__LINE__)).data());

            val = 0;
            UNIT_ASSERT_C(in.Read(val, 17), (rem + ": " + NBitIO::PrintBits(val)).data());

            UNIT_ASSERT_VALUES_EQUAL_C(val, 0x10000u, (rem + ": " + NBitIO::PrintBits(val)));
            UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 4u, (rem + ": " + NBitIO::PrintBits(val)));
            UNIT_ASSERT_C(!in.Eof(), (rem + ", " + ToString(__LINE__)).data());

            UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 4u, (rem + ": " + NBitIO::PrintBits(val)));

            {
                ui32 rt = 0;
                in.ReadRandom(30, rt, 10, 20);
                UNIT_ASSERT_STRINGS_EQUAL(NBitIO::PrintBits(rt).data(), "00000000 00000000 00001111 01111100");
            }
            val = 0;
            UNIT_ASSERT_C(in.Read(val, 3), (rem + ": " + NBitIO::PrintBits(val)).data());

            UNIT_ASSERT_VALUES_EQUAL_C(val, 0x4u, (rem + ": " + NBitIO::PrintBits(val)));
            UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 4u, (rem + ": " + NBitIO::PrintBits(val)));
            UNIT_ASSERT_C(!in.Eof(), (rem + ", " + ToString(__LINE__)).data());

            val = 0;
            UNIT_ASSERT_C(in.ReadWords<8>(val), (rem + ": " + NBitIO::PrintBits(val)).data());

            UNIT_ASSERT_VALUES_EQUAL_C(val, 0xabcdefU, (rem + ": " + NBitIO::PrintBits(val)));
            UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 8u, (rem + ": " + NBitIO::PrintBits(val)));
            UNIT_ASSERT_C(!in.Eof(), (rem + ", " + ToString(__LINE__)).data());

            val = 0;
            UNIT_ASSERT_C(in.Read(val, 3), (rem + ", " + ToString(__LINE__)).data());

            UNIT_ASSERT_VALUES_EQUAL_C(val, 0x4u, (rem + ": " + NBitIO::PrintBits(val)));
            UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 8u, (rem + ": " + NBitIO::PrintBits(val)));
            UNIT_ASSERT_C(!in.Eof(), (rem + ", " + ToString(__LINE__)).data());

            val = 0;
            in.Read(val, 7);
            UNIT_ASSERT_VALUES_EQUAL_C(val, 0x42u, (rem + ": " + NBitIO::PrintBits(val)));
        }

        {
            ui32 v = 0;

            DoRead(in, v);
            UNIT_ASSERT_VALUES_EQUAL_C(v, 1637415112ul, (rem + ": " + NBitIO::PrintBits(v)));
            DoRead(in, v);
            UNIT_ASSERT_VALUES_EQUAL_C(v, 897998715u, (rem + ": " + NBitIO::PrintBits(v)));
            DoRead(in, v);
            UNIT_ASSERT_VALUES_EQUAL_C(v, 201416527u, (rem + ": " + NBitIO::PrintBits(v)));
            DoRead(in, v);
            UNIT_ASSERT_VALUES_EQUAL_C(v, 432344219u, (rem + ": " + NBitIO::PrintBits(v)));

            UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 25u, (rem + ": " + NBitIO::PrintBits(v)));
        }

        {
            ui64 v8 = 0;
            in.ReadSafe(v8, 64);

            UNIT_ASSERT_VALUES_EQUAL_C(v8, 0xAAAAAAAAAAAAAAAAULL, (rem + ": " + NBitIO::PrintBits(v8)));
            UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 33u, (rem + ": " + NBitIO::PrintBits(v8)));

            v8 = 0;
            in.ReadK<64>(v8);

            UNIT_ASSERT_VALUES_EQUAL_C(v8, 0x5555555555555555ULL, (rem + ": " + NBitIO::PrintBits(v8)));
            UNIT_ASSERT_VALUES_EQUAL_C(in.GetOffset(), 41u, (rem + ": " + NBitIO::PrintBits(v8)));
        }

        ui32 v = 0;
        UNIT_ASSERT_C(!in.Eof(), (rem + ", " + ToString(__LINE__)).data());
        UNIT_ASSERT_C(in.Read(v, 5), (rem + ", " + ToString(__LINE__)).data());
        UNIT_ASSERT_C(in.Eof(), (rem + ", " + ToString(__LINE__)).data());
    }

    void TestBitIO() {
        {
            TVec vec;

            {
                NBitIO::TBitOutputYVector out(&vec);
                DoBitOutput(out, ToString(__LINE__));
            }

            CheckBits(vec, NBitIO::BITS_REF, ToString(__LINE__).data());

            {
                TBi in(vec);
                DoBitInput(in, ToString(__LINE__));
            }
        }
        {
            TVec vec;
            vec.resize(41, 0);
            {
                NBitIO::TBitOutputArray out(vec.begin(), vec.size());
                DoBitOutput(out, ToString(__LINE__));
            }

            CheckBits(vec, NBitIO::BITS_REF, ToString(__LINE__).data());

            {
                TBi in(vec);
                DoBitInput(in, ToString(__LINE__));
            }
        }
    }
};

UNIT_TEST_SUITE_REGISTRATION(TBitIOTest);