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
|
#include "shared_data.h"
#include <library/cpp/testing/unittest/registar.h>
#include <util/generic/hash.h>
#include <util/generic/deque.h>
#include <util/system/sys_alloc.h>
namespace NActors {
Y_UNIT_TEST_SUITE(TSharedDataTest) {
Y_UNIT_TEST(BasicBehavior) {
auto data = TSharedData::Copy("Hello", 5);
UNIT_ASSERT(data.IsPrivate());
UNIT_ASSERT(!data.IsShared());
UNIT_ASSERT_VALUES_EQUAL(data.size(), 5u);
UNIT_ASSERT_VALUES_EQUAL(data.end() - data.begin(), 5u);
UNIT_ASSERT_VALUES_EQUAL(data.mutable_end() - data.mutable_begin(), 5u);
UNIT_ASSERT(data.begin() == data.data());
UNIT_ASSERT(data.mutable_data() == data.data());
UNIT_ASSERT(data.mutable_begin() == data.mutable_data());
UNIT_ASSERT_VALUES_EQUAL(data.ToString(), TString("Hello"));
UNIT_ASSERT_VALUES_EQUAL(::memcmp(data.data(), "Hello", 5), 0);
auto link = data;
UNIT_ASSERT(!link.IsPrivate());
UNIT_ASSERT(!data.IsPrivate());
UNIT_ASSERT(link.IsShared());
UNIT_ASSERT(data.IsShared());
UNIT_ASSERT(link.data() == data.data());
UNIT_ASSERT(link.size() == data.size());
link = { };
UNIT_ASSERT(link.IsPrivate());
UNIT_ASSERT(data.IsPrivate());
UNIT_ASSERT(!link.IsShared());
UNIT_ASSERT(!data.IsShared());
UNIT_ASSERT_VALUES_EQUAL(TString(data.Slice()), TString("Hello"));
UNIT_ASSERT_VALUES_EQUAL(TString(data.Slice(1)), TString("ello"));
UNIT_ASSERT_VALUES_EQUAL(TString(data.Slice(1, 3)), TString("ell"));
UNIT_ASSERT_VALUES_EQUAL(TString(data.Slice(1, 100)), TString("ello"));
UNIT_ASSERT_VALUES_EQUAL(TString(data.Slice(0, 4)), TString("Hell"));
}
Y_UNIT_TEST(TrimBehavior) {
auto data = TSharedData::Uninitialized(42);
UNIT_ASSERT_VALUES_EQUAL(data.size(), 42u);
UNIT_ASSERT(data.data() != nullptr);
// Trim to non-zero does not change addresses
const char* ptr1 = data.data();
data.Trim(31);
const char* ptr2 = data.data();
UNIT_ASSERT_VALUES_EQUAL(data.size(), 31u);
UNIT_ASSERT(ptr1 == ptr2);
// Trim to zero releases underlying data
data.Trim(0);
UNIT_ASSERT_VALUES_EQUAL(data.size(), 0u);
UNIT_ASSERT(data.data() == nullptr);
}
class TCustomOwner : public TSharedData::IOwner {
using THeader = TSharedData::THeader;
public:
TSharedData Allocate(size_t size) {
char* raw = reinterpret_cast<char*>(y_allocate(sizeof(THeader) + size));
THeader* header = reinterpret_cast<THeader*>(raw);
header->RefCount = 1;
header->Owner = this;
char* data = raw + sizeof(THeader);
Y_VERIFY(Allocated_.insert(data).second);
return TSharedData::AttachUnsafe(data, size);
}
void Deallocate(char* data) noexcept {
Y_VERIFY(Allocated_.erase(data) > 0);
char* raw = data - sizeof(THeader);
y_deallocate(raw);
Deallocated_.push_back(data);
}
char* NextDeallocated() {
char* result = nullptr;
if (Deallocated_) {
result = Deallocated_.front();
Deallocated_.pop_front();
}
return result;
}
private:
THashSet<void*> Allocated_;
TDeque<char*> Deallocated_;
};
Y_UNIT_TEST(CustomOwner) {
TCustomOwner owner;
const char* ptr;
// Test destructor releases data
{
auto data = owner.Allocate(42);
UNIT_ASSERT_VALUES_EQUAL(data.size(), 42u);
ptr = data.data();
UNIT_ASSERT(owner.NextDeallocated() == nullptr);
}
UNIT_ASSERT(owner.NextDeallocated() == ptr);
UNIT_ASSERT(owner.NextDeallocated() == nullptr);
// Test assignment releases data
{
auto data = owner.Allocate(42);
UNIT_ASSERT_VALUES_EQUAL(data.size(), 42u);
ptr = data.data();
UNIT_ASSERT(owner.NextDeallocated() == nullptr);
data = { };
}
UNIT_ASSERT(owner.NextDeallocated() == ptr);
UNIT_ASSERT(owner.NextDeallocated() == nullptr);
// Test copies keep references correctly
{
auto data = owner.Allocate(42);
UNIT_ASSERT_VALUES_EQUAL(data.size(), 42u);
ptr = data.data();
auto copy = data;
UNIT_ASSERT_VALUES_EQUAL(copy.size(), 42u);
UNIT_ASSERT(copy.data() == ptr);
data = { };
UNIT_ASSERT_VALUES_EQUAL(data.size(), 0u);
UNIT_ASSERT(data.data() == nullptr);
UNIT_ASSERT(owner.NextDeallocated() == nullptr);
}
UNIT_ASSERT(owner.NextDeallocated() == ptr);
UNIT_ASSERT(owner.NextDeallocated() == nullptr);
// Test assignment releases correct data
{
auto data1 = owner.Allocate(42);
UNIT_ASSERT_VALUES_EQUAL(data1.size(), 42u);
auto data2 = owner.Allocate(31);
UNIT_ASSERT_VALUES_EQUAL(data2.size(), 31u);
ptr = data1.data();
UNIT_ASSERT(owner.NextDeallocated() == nullptr);
data1 = data2;
UNIT_ASSERT(owner.NextDeallocated() == ptr);
UNIT_ASSERT(owner.NextDeallocated() == nullptr);
ptr = data2.data();
UNIT_ASSERT_VALUES_EQUAL(data1.size(), 31u);
UNIT_ASSERT(data1.data() == ptr);
}
UNIT_ASSERT(owner.NextDeallocated() == ptr);
UNIT_ASSERT(owner.NextDeallocated() == nullptr);
// Test moves don't produce dangling references
{
auto data = owner.Allocate(42);
UNIT_ASSERT_VALUES_EQUAL(data.size(), 42u);
ptr = data.data();
auto moved = std::move(data);
UNIT_ASSERT_VALUES_EQUAL(moved.size(), 42u);
UNIT_ASSERT(moved.data() == ptr);
UNIT_ASSERT_VALUES_EQUAL(data.size(), 0u);
UNIT_ASSERT(data.data() == nullptr);
UNIT_ASSERT(owner.NextDeallocated() == nullptr);
}
UNIT_ASSERT(owner.NextDeallocated() == ptr);
UNIT_ASSERT(owner.NextDeallocated() == nullptr);
}
}
}
|