3 * Copyright (c) 2020 Project CHIP Authors
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <support/CHIPMem.h>
20 #include <support/CHIPMemString.h>
21 #include <support/SerializableIntegerSet.h>
22 #include <support/UnitTestRegistration.h>
24 #include <nlunit-test.h>
28 void TestSerializableIntegerSet(nlTestSuite * inSuite, void * inContext)
30 chip::SerializableU64Set<8> set;
31 NL_TEST_ASSERT(inSuite, !set.Contains(123));
33 NL_TEST_ASSERT(inSuite, set.Insert(123) == CHIP_NO_ERROR);
34 NL_TEST_ASSERT(inSuite, set.Contains(123));
36 NL_TEST_ASSERT(inSuite, set.Insert(123) == CHIP_NO_ERROR);
37 NL_TEST_ASSERT(inSuite, set.Contains(123));
40 NL_TEST_ASSERT(inSuite, !set.Contains(123));
42 for (uint64_t i = 1; i <= 8; i++)
44 NL_TEST_ASSERT(inSuite, set.Insert(i) == CHIP_NO_ERROR);
47 NL_TEST_ASSERT(inSuite, set.Insert(9) != CHIP_NO_ERROR);
49 for (uint64_t i = 1; i <= 8; i++)
51 NL_TEST_ASSERT(inSuite, set.Contains(i));
54 size_t size = set.SerializedSize();
55 NL_TEST_ASSERT(inSuite, set.MaxSerializedSize() == size);
57 for (uint64_t i = 1; i <= 7; i++)
60 NL_TEST_ASSERT(inSuite, set.SerializedSize() == size);
64 NL_TEST_ASSERT(inSuite, set.SerializedSize() == CHIP_MAX_SERIALIZED_SIZE_U64(0));
67 void TestSerializableIntegerSetNonZero(nlTestSuite * inSuite, void * inContext)
69 chip::SerializableU64Set<8, 2> set;
70 NL_TEST_ASSERT(inSuite, !set.Contains(123));
72 NL_TEST_ASSERT(inSuite, set.Insert(123) == CHIP_NO_ERROR);
73 NL_TEST_ASSERT(inSuite, set.Contains(123));
75 NL_TEST_ASSERT(inSuite, set.Insert(123) == CHIP_NO_ERROR);
76 NL_TEST_ASSERT(inSuite, set.Contains(123));
79 NL_TEST_ASSERT(inSuite, !set.Contains(123));
81 for (uint64_t i = 0; i <= 1; i++)
83 NL_TEST_ASSERT(inSuite, set.Insert(i) == CHIP_NO_ERROR);
86 // Try inserting empty value
87 NL_TEST_ASSERT(inSuite, set.Insert(2) != CHIP_NO_ERROR);
89 for (uint64_t i = 3; i <= 7; i++)
91 NL_TEST_ASSERT(inSuite, set.Insert(i) == CHIP_NO_ERROR);
94 for (uint64_t i = 0; i <= 1; i++)
96 NL_TEST_ASSERT(inSuite, set.Contains(i));
99 for (uint64_t i = 3; i <= 7; i++)
101 NL_TEST_ASSERT(inSuite, set.Contains(i));
104 for (uint64_t i = 0; i <= 6; i++)
110 NL_TEST_ASSERT(inSuite, set.SerializedSize() == CHIP_MAX_SERIALIZED_SIZE_U64(0));
113 void TestSerializableIntegerSetSerialize(nlTestSuite * inSuite, void * inContext)
115 chip::SerializableU64Set<8> set;
117 for (uint64_t i = 1; i <= 6; i++)
119 NL_TEST_ASSERT(inSuite, set.Insert(i) == CHIP_NO_ERROR);
122 char * buf = nullptr;
125 NL_TEST_ASSERT(inSuite, set.SerializeBase64(buf, size) == nullptr);
126 NL_TEST_ASSERT(inSuite, size != 0);
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);
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);
137 chip::SerializableU64Set<8> set2;
138 NL_TEST_ASSERT(inSuite, set2.DeserializeBase64(buf2.Get(), size2) == CHIP_NO_ERROR);
140 for (uint64_t i = 1; i <= 6; i++)
142 NL_TEST_ASSERT(inSuite, set.Contains(i));
144 NL_TEST_ASSERT(inSuite, !set.Contains(7));
147 int Setup(void * inContext)
149 CHIP_ERROR error = chip::Platform::MemoryInit();
150 if (error != CHIP_NO_ERROR)
155 int Teardown(void * inContext)
157 chip::Platform::MemoryShutdown();
163 #define NL_TEST_DEF_FN(fn) NL_TEST_DEF("Test " #fn, fn)
165 * Test Suite. It lists all the test functions.
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() //
174 int TestSerializableIntegerSet(void)
176 nlTestSuite theSuite = { "CHIP SerializableIntegerSet tests", &sTests[0], Setup, Teardown };
178 // Run test suit againt one context.
179 nlTestRunner(&theSuite, nullptr);
180 return nlTestRunnerStats(&theSuite);
183 CHIP_REGISTER_TEST_SUITE(TestSerializableIntegerSet)