forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestNullable.cpp
309 lines (248 loc) · 11.1 KB
/
TestNullable.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
*
* Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <array>
#include <inttypes.h>
#include <stdint.h>
#include <string.h>
#include <app/data-model/Nullable.h>
#include <lib/support/Span.h>
#include <lib/support/UnitTestRegistration.h>
#include <nlunit-test.h>
using namespace chip;
using namespace chip::app::DataModel;
namespace {
// Counts calls to constructor and destructor, to determine if the right
// semantics applied in cases where destruction is expected.
struct CtorDtorCounter
{
CtorDtorCounter(int i) : m(i) { ++created; }
~CtorDtorCounter() { ++destroyed; }
CtorDtorCounter(const CtorDtorCounter & o) : m(o.m) { ++created; }
CtorDtorCounter & operator=(const CtorDtorCounter &) = default;
CtorDtorCounter(CtorDtorCounter && o) : m(o.m) { ++created; }
CtorDtorCounter & operator=(CtorDtorCounter &&) = default;
bool operator==(const CtorDtorCounter & o) const { return m == o.m; }
bool operator!=(const CtorDtorCounter & o) const { return m != o.m; }
int m;
static void ResetCounter()
{
created = 0;
destroyed = 0;
}
static int created;
static int destroyed;
};
struct MovableCtorDtorCounter : public CtorDtorCounter
{
public:
MovableCtorDtorCounter(int i) : CtorDtorCounter(i) {}
MovableCtorDtorCounter(const MovableCtorDtorCounter & o) = delete;
MovableCtorDtorCounter & operator=(const MovableCtorDtorCounter &) = delete;
MovableCtorDtorCounter(MovableCtorDtorCounter && o) = default;
MovableCtorDtorCounter & operator=(MovableCtorDtorCounter &&) = default;
using CtorDtorCounter::operator==;
using CtorDtorCounter::operator!=;
};
int CtorDtorCounter::created = 0;
int CtorDtorCounter::destroyed = 0;
} // namespace
static void TestBasic(nlTestSuite * inSuite, void * inContext)
{
// Set up our test CtorDtorCounter objects, which will mess with counts, before we reset the
// counts.
CtorDtorCounter c100(100), c101(101), c102(102);
CtorDtorCounter::ResetCounter();
{
auto testNullable = MakeNullable<CtorDtorCounter>(100);
NL_TEST_ASSERT(inSuite, CtorDtorCounter::created == 1 && CtorDtorCounter::destroyed == 0);
NL_TEST_ASSERT(inSuite, !testNullable.IsNull() && testNullable.Value().m == 100);
NL_TEST_ASSERT(inSuite, testNullable == c100);
NL_TEST_ASSERT(inSuite, testNullable != c101);
NL_TEST_ASSERT(inSuite, testNullable != c102);
testNullable.SetNull();
NL_TEST_ASSERT(inSuite, CtorDtorCounter::created == 1 && CtorDtorCounter::destroyed == 1);
NL_TEST_ASSERT(inSuite, !!testNullable.IsNull());
NL_TEST_ASSERT(inSuite, testNullable != c100);
NL_TEST_ASSERT(inSuite, testNullable != c101);
NL_TEST_ASSERT(inSuite, testNullable != c102);
testNullable.SetNonNull(CtorDtorCounter(101));
NL_TEST_ASSERT(inSuite, CtorDtorCounter::created == 3 && CtorDtorCounter::destroyed == 2);
NL_TEST_ASSERT(inSuite, !testNullable.IsNull() && testNullable.Value().m == 101);
NL_TEST_ASSERT(inSuite, testNullable != c100);
NL_TEST_ASSERT(inSuite, testNullable == c101);
NL_TEST_ASSERT(inSuite, testNullable != c102);
testNullable.SetNonNull(102);
NL_TEST_ASSERT(inSuite, CtorDtorCounter::created == 4 && CtorDtorCounter::destroyed == 3);
NL_TEST_ASSERT(inSuite, !testNullable.IsNull() && testNullable.Value().m == 102);
NL_TEST_ASSERT(inSuite, testNullable != c100);
NL_TEST_ASSERT(inSuite, testNullable != c101);
NL_TEST_ASSERT(inSuite, testNullable == c102);
}
// Our test CtorDtorCounter objects are still in scope here.
NL_TEST_ASSERT(inSuite, CtorDtorCounter::created == 4 && CtorDtorCounter::destroyed == 4);
}
static void TestMake(nlTestSuite * inSuite, void * inContext)
{
CtorDtorCounter::ResetCounter();
{
auto testNullable = MakeNullable<CtorDtorCounter>(200);
NL_TEST_ASSERT(inSuite, CtorDtorCounter::created == 1 && CtorDtorCounter::destroyed == 0);
NL_TEST_ASSERT(inSuite, !testNullable.IsNull() && testNullable.Value().m == 200);
}
NL_TEST_ASSERT(inSuite, CtorDtorCounter::created == 1 && CtorDtorCounter::destroyed == 1);
}
static void TestCopy(nlTestSuite * inSuite, void * inContext)
{
CtorDtorCounter::ResetCounter();
{
auto testSrc = MakeNullable<CtorDtorCounter>(300);
NL_TEST_ASSERT(inSuite, CtorDtorCounter::created == 1 && CtorDtorCounter::destroyed == 0);
NL_TEST_ASSERT(inSuite, !testSrc.IsNull() && testSrc.Value().m == 300);
{
Nullable<CtorDtorCounter> testDst(testSrc);
NL_TEST_ASSERT(inSuite, CtorDtorCounter::created == 2 && CtorDtorCounter::destroyed == 0);
NL_TEST_ASSERT(inSuite, !testDst.IsNull() && testDst.Value().m == 300);
}
NL_TEST_ASSERT(inSuite, CtorDtorCounter::created == 2 && CtorDtorCounter::destroyed == 1);
{
Nullable<CtorDtorCounter> testDst;
NL_TEST_ASSERT(inSuite, CtorDtorCounter::created == 2 && CtorDtorCounter::destroyed == 1);
NL_TEST_ASSERT(inSuite, !!testDst.IsNull());
testDst = testSrc;
NL_TEST_ASSERT(inSuite, CtorDtorCounter::created == 3 && CtorDtorCounter::destroyed == 1);
NL_TEST_ASSERT(inSuite, !testDst.IsNull() && testDst.Value().m == 300);
}
NL_TEST_ASSERT(inSuite, CtorDtorCounter::created == 3 && CtorDtorCounter::destroyed == 2);
}
NL_TEST_ASSERT(inSuite, CtorDtorCounter::created == 3 && CtorDtorCounter::destroyed == 3);
}
static void TestMove(nlTestSuite * inSuite, void * inContext)
{
CtorDtorCounter::ResetCounter();
{
auto testSrc = MakeNullable<MovableCtorDtorCounter>(400); // construct
Nullable<MovableCtorDtorCounter> testDst(std::move(testSrc)); // move construct
NL_TEST_ASSERT(inSuite, CtorDtorCounter::created == 2 && CtorDtorCounter::destroyed == 0);
NL_TEST_ASSERT(inSuite, !testDst.IsNull() && testDst.Value().m == 400);
// destroy both testsSrc and testDst
}
NL_TEST_ASSERT(inSuite, CtorDtorCounter::created == 2 && CtorDtorCounter::destroyed == 2);
CtorDtorCounter::ResetCounter();
{
Nullable<MovableCtorDtorCounter> testDst; // no object construction
NL_TEST_ASSERT(inSuite, CtorDtorCounter::created == 0 && CtorDtorCounter::destroyed == 0);
NL_TEST_ASSERT(inSuite, !!testDst.IsNull());
auto testSrc = MakeNullable<MovableCtorDtorCounter>(401); // construct object
testDst = std::move(testSrc); // construct a copy
NL_TEST_ASSERT(inSuite, CtorDtorCounter::created == 2 && CtorDtorCounter::destroyed == 0);
NL_TEST_ASSERT(inSuite, !testDst.IsNull() && testDst.Value().m == 401);
}
NL_TEST_ASSERT(inSuite, CtorDtorCounter::created == 2 && CtorDtorCounter::destroyed == 2);
}
static void TestUpdate(nlTestSuite * inSuite, void * inContext)
{
using SmallArray = std::array<uint8_t, 3>;
// Arrays
{
auto nullable1 = MakeNullable<SmallArray>({ 1, 2, 3 });
auto nullable2 = MakeNullable<SmallArray>({ 1, 2, 3 });
NL_TEST_ASSERT(inSuite, !nullable1.IsNull());
NL_TEST_ASSERT(inSuite, !nullable2.IsNull());
NL_TEST_ASSERT(inSuite, nullable1 == nullable2);
// No-op on change to same.
NL_TEST_ASSERT(inSuite, nullable1.Update(nullable2) == false);
NL_TEST_ASSERT(inSuite, nullable1 == nullable2);
nullable1.Value()[0] = 100;
NL_TEST_ASSERT(inSuite, nullable1 != nullable2);
NL_TEST_ASSERT(inSuite, nullable2.Update(nullable1) == true);
NL_TEST_ASSERT(inSuite, nullable1 == nullable2);
}
// Structs
{
struct SomeObject
{
uint8_t a;
uint8_t b;
bool operator==(const SomeObject & other) const { return (a == other.a) && (b == other.b); }
};
auto nullable1 = MakeNullable<SomeObject>({ 1, 2 });
auto nullable2 = MakeNullable<SomeObject>({ 1, 2 });
NL_TEST_ASSERT(inSuite, !nullable1.IsNull());
NL_TEST_ASSERT(inSuite, !nullable2.IsNull());
NL_TEST_ASSERT(inSuite, nullable1 == nullable2);
// No-op on change to same.
NL_TEST_ASSERT(inSuite, nullable1.Update(nullable2) == false);
NL_TEST_ASSERT(inSuite, nullable1 == nullable2);
nullable1.Value().a = 100;
NL_TEST_ASSERT(inSuite, nullable1 != nullable2);
NL_TEST_ASSERT(inSuite, nullable2.Update(nullable1) == true);
NL_TEST_ASSERT(inSuite, nullable1 == nullable2);
}
// Scalar cases
{
auto nullable1 = MakeNullable(static_cast<uint8_t>(1));
NL_TEST_ASSERT(inSuite, !nullable1.IsNull());
// Non-null to non-null same value
NL_TEST_ASSERT(inSuite, nullable1.Update(nullable1) == false);
NL_TEST_ASSERT(inSuite, !nullable1.IsNull());
// Non-null to null
NL_TEST_ASSERT(inSuite, nullable1.Update(NullNullable) == true);
NL_TEST_ASSERT(inSuite, nullable1.IsNull());
// Null to null
NL_TEST_ASSERT(inSuite, nullable1.Update(NullNullable) == false);
NL_TEST_ASSERT(inSuite, nullable1.IsNull());
// Null to non-null
NL_TEST_ASSERT(inSuite, nullable1.Update(MakeNullable(static_cast<uint8_t>(1))) == true);
NL_TEST_ASSERT(inSuite, !nullable1.IsNull());
NL_TEST_ASSERT(inSuite, nullable1.Value() == 1);
// Non-null to non-null different value
NL_TEST_ASSERT(inSuite, nullable1.Update(MakeNullable(static_cast<uint8_t>(2))) == true);
NL_TEST_ASSERT(inSuite, !nullable1.IsNull());
NL_TEST_ASSERT(inSuite, nullable1.Value() == 2);
// Non-null to extent of range --> changes to "invalid" value in range.
NL_TEST_ASSERT(inSuite, nullable1.Update(MakeNullable(static_cast<uint8_t>(255))) == true);
NL_TEST_ASSERT(inSuite, !nullable1.IsNull());
NL_TEST_ASSERT(inSuite, nullable1.Value() == 255);
}
}
// clang-format off
static const nlTest sTests[] =
{
NL_TEST_DEF("NullableBasic", TestBasic),
NL_TEST_DEF("NullableMake", TestMake),
NL_TEST_DEF("NullableCopy", TestCopy),
NL_TEST_DEF("NullableMove", TestMove),
NL_TEST_DEF("Nullable Update operation", TestUpdate),
NL_TEST_SENTINEL()
};
// clang-format on
int TestNullable()
{
// clang-format off
nlTestSuite theSuite =
{
"Test for Nullable abstraction",
&sTests[0],
nullptr,
nullptr
};
// clang-format on
nlTestRunner(&theSuite, nullptr);
return (nlTestRunnerStats(&theSuite));
}
CHIP_REGISTER_TEST_SUITE(TestNullable)