aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/packedtypes/longs_ut.cpp
blob: 8b06c934d2f16168dd1dcb96d484bd44a2ea6f23 (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
#include "longs.h"

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

#include <library/cpp/digest/old_crc/crc.h>
#include <util/string/util.h>
#include <util/stream/output.h>
#include <util/system/hi_lo.h>

Y_UNIT_TEST_SUITE(TLongsTest) {
    Y_UNIT_TEST(TestLongs) {
        i16 x16 = 40;
        i64 x64 = 40;
        i64 y64;
        TString s;

        s += Sprintf("x16=0x%x\n", (int)x16);
        s += Sprintf("LO_8(x16)=0x%x HI_8(x16)=0x%x\n\n", (int)Lo8(x16), (int)Hi8(x16));

        char buf[100];
        memset(buf, 0, 100);
        char* p = buf;
        int l = out_long(x64, buf);
        s += Sprintf("x64=0x%" PRIi64 "\n", x64);
        s += Sprintf("LO_32(x64)=0x%" PRIu32 " HI_32(x64)=0x%" PRIu32 "\n", (ui32)Lo32(x64), (ui32)Hi32(x64));
        s += Sprintf("buf=%s, l=%d: ", buf, l);
        for (int i = 0; i < l; i++) {
            s += Sprintf("0x%02x ", buf[i]);
        }
        s += Sprintf("\n");

        p = buf;
        in_long(y64, p);
        s += Sprintf("x=0x%" PRIi64 " y=0x%" PRIi64 "\n", x64, y64);
        if (x64 != y64) {
            s += Sprintf("Error: y64 != x64\n");
        } else {
            s += Sprintf("OK\n");
        }

        UNIT_ASSERT_EQUAL(Crc<ui64>(s.data(), s.size()), 7251624297500315779ULL); // WTF?
    }

    template <typename TSignedInt>
    void TestOneValue(TSignedInt value) {
        char buffer[sizeof(TSignedInt) + 1];
        auto bytes = out_long(value, buffer);
        TSignedInt readValue = 0;
        auto readBytes = in_long(readValue, buffer);
        UNIT_ASSERT_EQUAL(bytes, readBytes);
        UNIT_ASSERT_EQUAL(bytes, len_long(value));
        UNIT_ASSERT_EQUAL(value, readValue);
    }

    template <typename TSignedInt>
    void TestCornerCasesImpl(int maxPow) {
        for (int i = 0; i <= maxPow; ++i) {
            TestOneValue<TSignedInt>((TSignedInt)(1ull << i));
            TestOneValue<TSignedInt>((TSignedInt)((1ull << i) - 1));
            TestOneValue<TSignedInt>((TSignedInt)((1ull << i) + 1));
        }
    }

    Y_UNIT_TEST(TestCornerCases) {
        TestCornerCasesImpl<i32>(31);
        TestCornerCasesImpl<i64>(63);
    }
}