3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 #include <support/StringBuilder.h>
18 #include <support/UnitTestRegistration.h>
20 #include <nlunit-test.h>
26 void TestStringBuilder(nlTestSuite * inSuite, void * inContext)
29 StringBuilder<64> builder;
31 NL_TEST_ASSERT(inSuite, builder.Fit());
32 NL_TEST_ASSERT(inSuite, strcmp(builder.c_str(), "") == 0);
35 NL_TEST_ASSERT(inSuite, builder.Fit());
36 NL_TEST_ASSERT(inSuite, strcmp(builder.c_str(), "foo") == 0);
39 NL_TEST_ASSERT(inSuite, builder.Fit());
40 NL_TEST_ASSERT(inSuite, strcmp(builder.c_str(), "foobar") == 0);
43 void TestIntegerAppend(nlTestSuite * inSuite, void * inContext)
46 StringBuilder<64> builder;
48 builder.Add("nr: ").Add(1234);
49 NL_TEST_ASSERT(inSuite, builder.Fit());
50 NL_TEST_ASSERT(inSuite, strcmp(builder.c_str(), "nr: 1234") == 0);
52 builder.Add(", ").Add(-22);
53 NL_TEST_ASSERT(inSuite, builder.Fit());
54 NL_TEST_ASSERT(inSuite, strcmp(builder.c_str(), "nr: 1234, -22") == 0);
57 void TestOverflow(nlTestSuite * inSuite, void * inContext)
61 StringBuilder<4> builder;
64 NL_TEST_ASSERT(inSuite, builder.Fit());
65 NL_TEST_ASSERT(inSuite, strcmp(builder.c_str(), "foo") == 0);
68 NL_TEST_ASSERT(inSuite, !builder.Fit());
69 NL_TEST_ASSERT(inSuite, strcmp(builder.c_str(), "foo") == 0);
73 StringBuilder<7> builder;
75 builder.Add("x: ").Add(12345);
76 NL_TEST_ASSERT(inSuite, !builder.Fit());
77 NL_TEST_ASSERT(inSuite, strcmp(builder.c_str(), "x: 123") == 0);
81 const nlTest sTests[] = {
82 NL_TEST_DEF("TestStringBuilder", TestStringBuilder), //
83 NL_TEST_DEF("TestIntegerAppend", TestIntegerAppend), //
84 NL_TEST_DEF("TestOverflow", TestOverflow), //
90 int TestStringBuilder(void)
92 nlTestSuite theSuite = { "StringBuilder", sTests, nullptr, nullptr };
93 nlTestRunner(&theSuite, nullptr);
94 return nlTestRunnerStats(&theSuite);
97 CHIP_REGISTER_TEST_SUITE(TestStringBuilder)