aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/mem_ut.cpp
blob: ba4dfb869b54e2f6ed5bb3bf47669d4d9b33a146 (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
#include "mem.h" 
 
#include <library/cpp/testing/unittest/registar.h>
 
Y_UNIT_TEST_SUITE(TestMemIO) {
    Y_UNIT_TEST(TestReadTo) {
        TString s("0123456789abc");
        TMemoryInput in(s); 
 
        TString t;
        UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, '7'), 8); 
        UNIT_ASSERT_VALUES_EQUAL(t, "0123456"); 
        UNIT_ASSERT_VALUES_EQUAL(in.ReadTo(t, 'z'), 5); 
        UNIT_ASSERT_VALUES_EQUAL(t, "89abc"); 
    } 

    Y_UNIT_TEST(NextAndUndo) {
        char buffer[20];
        TMemoryOutput output(buffer, sizeof(buffer));
        char* ptr = nullptr;
        size_t bufferSize = output.Next(&ptr);
        UNIT_ASSERT_GE(bufferSize, 1);
        *ptr = '1';
        if (bufferSize > 1) {
            output.Undo(bufferSize - 1);
        }

        bufferSize = output.Next(&ptr);
        UNIT_ASSERT_GE(bufferSize, 2);
        *ptr = '2';
        *(ptr + 1) = '2';
        if (bufferSize > 2) {
            output.Undo(bufferSize - 2);
        }

        bufferSize = output.Next(&ptr);
        UNIT_ASSERT_GE(bufferSize, 3);
        *ptr = '3';
        *(ptr + 1) = '3';
        *(ptr + 2) = '3';
        if (bufferSize > 3) {
            output.Undo(bufferSize - 3);
        }

        output.Finish();

        const char* const result = "1"
                                   "22"
                                   "333";
        UNIT_ASSERT(0 == memcmp(buffer, result, strlen(result)));
    }

    Y_UNIT_TEST(Write) {
        char buffer[20];
        TMemoryOutput output(buffer, sizeof(buffer));
        output << "1"
               << "22"
               << "333"
               << "4444"
               << "55555";

        const char* const result = "1"
                                   "22"
                                   "333"
                                   "4444"
                                   "55555";
        UNIT_ASSERT(0 == memcmp(buffer, result, strlen(result)));
    }

    Y_UNIT_TEST(WriteChars) {
        char buffer[20];
        TMemoryOutput output(buffer, sizeof(buffer));
        output << '1' << '2' << '3' << '4' << '5' << '6' << '7' << '8' << '9' << '0';

        const char* const result = "1234567890";
        UNIT_ASSERT(0 == memcmp(buffer, result, strlen(result)));
    }
}