Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / support / tests / TestSerializableIntegerSet.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    All rights reserved.
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18
19 #include <support/CHIPMem.h>
20 #include <support/CHIPMemString.h>
21 #include <support/SerializableIntegerSet.h>
22 #include <support/UnitTestRegistration.h>
23
24 #include <nlunit-test.h>
25
26 namespace {
27
28 void TestSerializableIntegerSet(nlTestSuite * inSuite, void * inContext)
29 {
30     chip::SerializableU64Set<8> set;
31     NL_TEST_ASSERT(inSuite, !set.Contains(123));
32
33     NL_TEST_ASSERT(inSuite, set.Insert(123) == CHIP_NO_ERROR);
34     NL_TEST_ASSERT(inSuite, set.Contains(123));
35
36     NL_TEST_ASSERT(inSuite, set.Insert(123) == CHIP_NO_ERROR);
37     NL_TEST_ASSERT(inSuite, set.Contains(123));
38
39     set.Remove(123);
40     NL_TEST_ASSERT(inSuite, !set.Contains(123));
41
42     for (uint64_t i = 1; i <= 8; i++)
43     {
44         NL_TEST_ASSERT(inSuite, set.Insert(i) == CHIP_NO_ERROR);
45     }
46
47     NL_TEST_ASSERT(inSuite, set.Insert(9) != CHIP_NO_ERROR);
48
49     for (uint64_t i = 1; i <= 8; i++)
50     {
51         NL_TEST_ASSERT(inSuite, set.Contains(i));
52     }
53
54     size_t size = set.SerializedSize();
55     NL_TEST_ASSERT(inSuite, set.MaxSerializedSize() == size);
56
57     for (uint64_t i = 1; i <= 7; i++)
58     {
59         set.Remove(i);
60         NL_TEST_ASSERT(inSuite, set.SerializedSize() == size);
61     }
62
63     set.Remove(8);
64     NL_TEST_ASSERT(inSuite, set.SerializedSize() == CHIP_MAX_SERIALIZED_SIZE_U64(0));
65 }
66
67 void TestSerializableIntegerSetNonZero(nlTestSuite * inSuite, void * inContext)
68 {
69     chip::SerializableU64Set<8, 2> set;
70     NL_TEST_ASSERT(inSuite, !set.Contains(123));
71
72     NL_TEST_ASSERT(inSuite, set.Insert(123) == CHIP_NO_ERROR);
73     NL_TEST_ASSERT(inSuite, set.Contains(123));
74
75     NL_TEST_ASSERT(inSuite, set.Insert(123) == CHIP_NO_ERROR);
76     NL_TEST_ASSERT(inSuite, set.Contains(123));
77
78     set.Remove(123);
79     NL_TEST_ASSERT(inSuite, !set.Contains(123));
80
81     for (uint64_t i = 0; i <= 1; i++)
82     {
83         NL_TEST_ASSERT(inSuite, set.Insert(i) == CHIP_NO_ERROR);
84     }
85
86     // Try inserting empty value
87     NL_TEST_ASSERT(inSuite, set.Insert(2) != CHIP_NO_ERROR);
88
89     for (uint64_t i = 3; i <= 7; i++)
90     {
91         NL_TEST_ASSERT(inSuite, set.Insert(i) == CHIP_NO_ERROR);
92     }
93
94     for (uint64_t i = 0; i <= 1; i++)
95     {
96         NL_TEST_ASSERT(inSuite, set.Contains(i));
97     }
98
99     for (uint64_t i = 3; i <= 7; i++)
100     {
101         NL_TEST_ASSERT(inSuite, set.Contains(i));
102     }
103
104     for (uint64_t i = 0; i <= 6; i++)
105     {
106         set.Remove(i);
107     }
108
109     set.Remove(7);
110     NL_TEST_ASSERT(inSuite, set.SerializedSize() == CHIP_MAX_SERIALIZED_SIZE_U64(0));
111 }
112
113 void TestSerializableIntegerSetSerialize(nlTestSuite * inSuite, void * inContext)
114 {
115     chip::SerializableU64Set<8> set;
116
117     for (uint64_t i = 1; i <= 6; i++)
118     {
119         NL_TEST_ASSERT(inSuite, set.Insert(i) == CHIP_NO_ERROR);
120     }
121
122     char * buf    = nullptr;
123     uint16_t size = 0;
124
125     NL_TEST_ASSERT(inSuite, set.SerializeBase64(buf, size) == nullptr);
126     NL_TEST_ASSERT(inSuite, size != 0);
127
128     chip::Platform::ScopedMemoryString buf1("", size);
129     NL_TEST_ASSERT(inSuite, set.SerializeBase64(buf1.Get(), size) == buf1.Get());
130     NL_TEST_ASSERT(inSuite, size != 0);
131
132     uint16_t size2 = static_cast<uint16_t>(2 * size);
133     chip::Platform::ScopedMemoryString buf2("", size2);
134     NL_TEST_ASSERT(inSuite, set.SerializeBase64(buf2.Get(), size2) == buf2.Get());
135     NL_TEST_ASSERT(inSuite, size2 == size);
136
137     chip::SerializableU64Set<8> set2;
138     NL_TEST_ASSERT(inSuite, set2.DeserializeBase64(buf2.Get(), size2) == CHIP_NO_ERROR);
139
140     for (uint64_t i = 1; i <= 6; i++)
141     {
142         NL_TEST_ASSERT(inSuite, set.Contains(i));
143     }
144     NL_TEST_ASSERT(inSuite, !set.Contains(7));
145 }
146
147 int Setup(void * inContext)
148 {
149     CHIP_ERROR error = chip::Platform::MemoryInit();
150     if (error != CHIP_NO_ERROR)
151         return FAILURE;
152     return SUCCESS;
153 }
154
155 int Teardown(void * inContext)
156 {
157     chip::Platform::MemoryShutdown();
158     return SUCCESS;
159 }
160
161 } // namespace
162
163 #define NL_TEST_DEF_FN(fn) NL_TEST_DEF("Test " #fn, fn)
164 /**
165  *   Test Suite. It lists all the test functions.
166  */
167 static const nlTest sTests[] = {
168     NL_TEST_DEF_FN(TestSerializableIntegerSet),          //
169     NL_TEST_DEF_FN(TestSerializableIntegerSetNonZero),   //
170     NL_TEST_DEF_FN(TestSerializableIntegerSetSerialize), //
171     NL_TEST_SENTINEL()                                   //
172 };
173
174 int TestSerializableIntegerSet(void)
175 {
176     nlTestSuite theSuite = { "CHIP SerializableIntegerSet tests", &sTests[0], Setup, Teardown };
177
178     // Run test suit againt one context.
179     nlTestRunner(&theSuite, nullptr);
180     return nlTestRunnerStats(&theSuite);
181 }
182
183 CHIP_REGISTER_TEST_SUITE(TestSerializableIntegerSet)