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
|
#include "accessors.h"
#include <library/cpp/testing/unittest/registar.h>
#include <util/generic/buffer.h>
#include <util/generic/vector.h>
#include <array>
class TAccessorsTest: public TTestBase {
UNIT_TEST_SUITE(TAccessorsTest);
UNIT_TEST(TestAccessors);
UNIT_TEST_SUITE_END();
private:
template <typename T>
void TestRead(const T& t, const char* comm) {
const char* beg = (const char*)NAccessors::Begin(t);
const char* end = (const char*)NAccessors::End(t);
long sz = NAccessors::Size(t) * sizeof(typename TMemoryTraits<T>::TElementType);
UNIT_ASSERT_VALUES_EQUAL_C(end - beg, sz, comm);
}
template <typename T>
void TestWrite(const char* comm) {
typename TMemoryTraits<T>::TElementType val[4] = {'t', 'e', 's', 't'};
T t;
NAccessors::Init(t);
NAccessors::Reserve(t, 6);
size_t sz = NAccessors::Size(t);
UNIT_ASSERT_VALUES_EQUAL_C(0u, sz, comm);
NAccessors::Append(t, 'a');
sz = NAccessors::Size(t);
UNIT_ASSERT_VALUES_EQUAL_C(1u, sz, comm);
NAccessors::Append(t, val, val + 4);
sz = NAccessors::Size(t);
UNIT_ASSERT_VALUES_EQUAL_C(5u, sz, comm);
NAccessors::Clear(t);
sz = NAccessors::Size(t);
UNIT_ASSERT_VALUES_EQUAL_C(0u, sz, comm);
}
void TestAccessors() {
TestRead('a', "char");
TestRead(1, "int");
int t[4] = {0, 1, 2, 3};
TestRead(t, "int[4]");
TStringBuf sbuf = "test";
TestRead(sbuf, "TStringBuf");
TUtf16String wtr;
wtr.resize(10, 1024);
TestRead(wtr, "TUtf16String");
TBuffer buf;
buf.Resize(30);
TestRead(buf, "TBuffer");
TVector<ui64> vec(10, 100);
TestRead(vec, "TVector<ui64>");
TestWrite<TString>("TString");
TestWrite<TVector<char>>("TVector<char>");
TestWrite<TBuffer>("TBuffer");
TestWrite<TVector<ui64>>("TVector<ui64>");
TestWrite<TUtf16String>("TUtf16String");
std::array<TString, 10> sarr;
NAccessors::Init(sarr);
NAccessors::Clear(sarr);
std::array<char, 10> carr;
NAccessors::Init(carr);
NAccessors::Clear(carr);
TestRead(carr, "std::array<char, 10>");
}
};
UNIT_TEST_SUITE_REGISTRATION(TAccessorsTest)
|