Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / shell / tests / TestStreamerStdio.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
18 #include "TestShell.h"
19
20 #include <shell/shell.h>
21 #include <support/CodeUtils.h>
22 #include <support/UnitTestRegistration.h>
23
24 #include <inttypes.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 using namespace chip;
31 using namespace chip::Shell;
32 using namespace chip::Logging;
33
34 // =================================
35 //      Test Vectors
36 // =================================
37
38 struct test_streamer_vector
39 {
40     const char * output;
41 };
42
43 static const struct test_streamer_vector test_vector_streamer_out[] = {
44     { .output = "prompt>\n" }, { .output = "T123 " }, { .output = "T456\n" }, { .output = "T789 " }, { .output = "T10\n" },
45 };
46
47 // =================================
48 //      Unit tests
49 // =================================
50
51 static void TestStreamer_Output(nlTestSuite * inSuite, void * inContext)
52 {
53     int numOfTestVectors = ArraySize(test_vector_streamer_out);
54     int numOfTestsRan    = 0;
55     const struct test_streamer_vector * test_params;
56
57     const char * output;
58     ssize_t num_chars;
59
60     for (int vectorIndex = 0; vectorIndex < numOfTestVectors; vectorIndex++)
61     {
62         test_params = &test_vector_streamer_out[vectorIndex];
63         output      = test_params->output;
64
65         num_chars = streamer_write(streamer_get(), output, strlen(output));
66         // Let's assume that all our output lengths fit in ssize_t.
67         NL_TEST_ASSERT(inSuite, num_chars == static_cast<ssize_t>(strlen(output)));
68         numOfTestsRan++;
69     }
70     NL_TEST_ASSERT(inSuite, numOfTestsRan > 0);
71 }
72
73 /**
74  *   Test Suite. It lists all the test functions.
75  */
76 static const nlTest sTests[] = {
77
78     NL_TEST_DEF("Test Streamer: TestStreamer_Output", TestStreamer_Output),
79
80     NL_TEST_SENTINEL()
81 };
82
83 int TestStreamerStdio(void)
84 {
85     nlTestSuite theSuite = { "CHIP Streamer tests", &sTests[0], nullptr, nullptr };
86
87     // Run test suit againt one context.
88     nlTestRunner(&theSuite, nullptr);
89     return nlTestRunnerStats(&theSuite);
90 }
91
92 CHIP_REGISTER_TEST_SUITE(TestStreamerStdio)