Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / shell / tests / TestShell.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_shell_vector
39 {
40     const char * line;
41     const char ** argv;
42 };
43
44 static const struct test_shell_vector test_vector_shell_tokenizer[] = {
45     { .line = "hello how are you?", .argv = (const char *[]){ "hello", "how", "are", "you?" } },
46     { .line = "hey yo yo", .argv = (const char *[]){ "hey", "yo", "yo" } },
47     { .line = "This  has  double  spaces", .argv = (const char *[]){ "This", "has", "double", "spaces" } },
48     { .line = " leading space", .argv = (const char *[]){ "leading", "space" } },
49     { .line = "trailing space ", .argv = (const char *[]){ "trailing", "space", "" } },
50     { .line = "no_space", .argv = (const char *[]){ "no_space" } },
51     { .line = "escaped\\ space", .argv = (const char *[]){ "escaped space" } },
52     { .line = "escape\\\\", .argv = (const char *[]){ "escape\\" } },
53     { .line = "extended\\ escaped\\ space and\\ more", .argv = (const char *[]){ "extended escaped space", "and more" } },
54     { .line = " ", .argv = (const char *[]){ "" } },
55     { .line = "", .argv = (const char *[]){} },
56 };
57
58 // =================================
59 //      Unit tests
60 // =================================
61
62 #define TEST_SHELL_MAX_TOKENS 5
63
64 static void TestShell_Tokenizer(nlTestSuite * inSuite, void * inContext)
65 {
66     int numOfTestVectors = ArraySize(test_vector_shell_tokenizer);
67     int numOfTestsRan    = 0;
68     const struct test_shell_vector * test_params;
69     char * argv[TEST_SHELL_MAX_TOKENS];
70     int argc = TEST_SHELL_MAX_TOKENS;
71     int count;
72
73     char line[128];
74
75     for (int vectorIndex = 0; vectorIndex < numOfTestVectors; vectorIndex++)
76     {
77         test_params = &test_vector_shell_tokenizer[vectorIndex];
78         strcpy(line, test_params->line);
79
80         count = shell_line_tokenize(line, argv, argc);
81
82         for (int i = 0; i < count; i++)
83         {
84             NL_TEST_ASSERT(inSuite, strcmp(argv[i], test_params->argv[i]) == 0);
85         }
86         numOfTestsRan++;
87     }
88     NL_TEST_ASSERT(inSuite, numOfTestsRan > 0);
89 }
90
91 /**
92  *   Test Suite. It lists all the test functions.
93  */
94 static const nlTest sTests[] = {
95
96     NL_TEST_DEF("Test Shell: TestShell_Tokenizer", TestShell_Tokenizer),
97
98     NL_TEST_SENTINEL()
99 };
100
101 int TestShell(void)
102 {
103     nlTestSuite theSuite = { "CHIP Shell tests", &sTests[0], nullptr, nullptr };
104
105     // Run test suit againt one context.
106     nlTestRunner(&theSuite, nullptr);
107     return nlTestRunnerStats(&theSuite);
108 }
109
110 CHIP_REGISTER_TEST_SUITE(TestShell)