aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/mem_copy_ut.cpp
blob: 891827b7213015b1697d15d27d8a7bdceec50027 (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
#include "mem_copy.h"

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

namespace {
    class TAssignBCalled: public yexception {
    };

    struct TB {
        inline TB& operator=(const TB&) {
            throw TAssignBCalled();

            return *this;
        }
    };

    struct TC: public TB {
    };
}

Y_DECLARE_PODTYPE(TB); 

Y_UNIT_TEST_SUITE(TestMemCopy) { 
    Y_UNIT_TEST(Test1) { 
        char buf[] = "123";
        char buf1[sizeof(buf)];

        UNIT_ASSERT_EQUAL(MemCopy(buf1, buf, sizeof(buf)), buf1);

        for (size_t i = 0; i < sizeof(buf); ++i) {
            UNIT_ASSERT_VALUES_EQUAL(buf[i], buf1[i]);
        }
    }

    static int x = 0;

    struct TA {
        inline TA() {
            X = ++x;
        }

        int X;
    };

    Y_UNIT_TEST(Test2) { 
        x = 0;

        TA a1[5];
        TA a2[5];

        UNIT_ASSERT_VALUES_EQUAL(a1[0].X, 1);
        UNIT_ASSERT_VALUES_EQUAL(a2[0].X, 6);

        MemCopy(a2, a1, 5);

        for (size_t i = 0; i < 5; ++i) {
            UNIT_ASSERT_VALUES_EQUAL(a1[i].X, a2[i].X);
        }
    }

    Y_UNIT_TEST(Test3) { 
        TB b1[5];
        TB b2[5];

        MemCopy(b2, b1, 5);
    }

    Y_UNIT_TEST(Test4) { 
        TC c1[5];
        TC c2[5];

        UNIT_ASSERT_EXCEPTION(MemCopy(c2, c1, 5), TAssignBCalled);
    }

    template <class T>
    inline void FillX(T* b, T* e) {
        int tmp = 0;

        while (b != e) {
            (b++)->X = ++tmp;
        }
    }

    Y_UNIT_TEST(Test5) { 
        struct TD {
            int X;
        };

        TD orig[50];

        for (ssize_t i = -15; i < 15; ++i) {
            TD* b = orig + 20;
            TD* e = b + 10;

            FillX(b, e);

            TD* to = b + i;

            MemMove(to, b, e - b - 1);

            for (size_t j = 0; j < (e - b) - (size_t)1; ++j) {
                UNIT_ASSERT_VALUES_EQUAL(to[j].X, j + 1);
            }
        }
    }

    Y_UNIT_TEST(TestEmpty) { 
        char* tmp = nullptr;

        UNIT_ASSERT(MemCopy(tmp, tmp, 0) == nullptr);
        UNIT_ASSERT(MemMove(tmp, tmp, 0) == nullptr);
    }
}