Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / support / tests / TestStringBuilder.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 #include <support/StringBuilder.h>
18 #include <support/UnitTestRegistration.h>
19
20 #include <nlunit-test.h>
21
22 namespace {
23
24 using namespace chip;
25
26 void TestStringBuilder(nlTestSuite * inSuite, void * inContext)
27 {
28
29     StringBuilder<64> builder;
30
31     NL_TEST_ASSERT(inSuite, builder.Fit());
32     NL_TEST_ASSERT(inSuite, strcmp(builder.c_str(), "") == 0);
33
34     builder.Add("foo");
35     NL_TEST_ASSERT(inSuite, builder.Fit());
36     NL_TEST_ASSERT(inSuite, strcmp(builder.c_str(), "foo") == 0);
37
38     builder.Add("bar");
39     NL_TEST_ASSERT(inSuite, builder.Fit());
40     NL_TEST_ASSERT(inSuite, strcmp(builder.c_str(), "foobar") == 0);
41 }
42
43 void TestIntegerAppend(nlTestSuite * inSuite, void * inContext)
44 {
45
46     StringBuilder<64> builder;
47
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);
51
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);
55 }
56
57 void TestOverflow(nlTestSuite * inSuite, void * inContext)
58 {
59
60     {
61         StringBuilder<4> builder;
62
63         builder.Add("foo");
64         NL_TEST_ASSERT(inSuite, builder.Fit());
65         NL_TEST_ASSERT(inSuite, strcmp(builder.c_str(), "foo") == 0);
66
67         builder.Add("bar");
68         NL_TEST_ASSERT(inSuite, !builder.Fit());
69         NL_TEST_ASSERT(inSuite, strcmp(builder.c_str(), "foo") == 0);
70     }
71
72     {
73         StringBuilder<7> builder;
74
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);
78     }
79 }
80
81 const nlTest sTests[] = {
82     NL_TEST_DEF("TestStringBuilder", TestStringBuilder), //
83     NL_TEST_DEF("TestIntegerAppend", TestIntegerAppend), //
84     NL_TEST_DEF("TestOverflow", TestOverflow),           //
85     NL_TEST_SENTINEL()                                   //
86 };
87
88 } // namespace
89
90 int TestStringBuilder(void)
91 {
92     nlTestSuite theSuite = { "StringBuilder", sTests, nullptr, nullptr };
93     nlTestRunner(&theSuite, nullptr);
94     return nlTestRunnerStats(&theSuite);
95 }
96
97 CHIP_REGISTER_TEST_SUITE(TestStringBuilder)