Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / nlunit-test / repo / src / nlunit-test.c
1 /**
2  *    Copyright 2012-2016 Nest Labs Inc. All Rights Reserved.
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 /**
18  *    @file
19  *      This file implements functions that effect a simple, portable
20  *      unit test suite framework.
21  *
22  */
23
24 #include <stdio.h>
25 #include <stddef.h>
26 #include <string.h>
27
28 #include <nlunit-test.h>
29
30 /* Function Prototypes */
31
32 static void def_log_name(struct _nlTestSuite* inSuite);
33 static void def_log_initialize(struct _nlTestSuite * inSuite, int inResult, int inWidth);
34 static void def_log_terminate(struct _nlTestSuite * inSuite, int inResult, int inWidth);
35 static void def_log_setup(struct _nlTestSuite* inSuite, int inResult, int inWidth);
36 static void def_log_test(struct _nlTestSuite *inSuite, int inWidth, int inIndex);
37 static void def_log_teardown(struct _nlTestSuite* inSuite, int inResult, int inWidth);
38 static void def_log_statTest(struct _nlTestSuite* inSuite);
39 static void def_log_statAssert(struct _nlTestSuite* inSuite);
40 static void csv_log_name(struct _nlTestSuite* inSuite);
41 static void csv_log_initialize(struct _nlTestSuite * inSuite, int inResult, int inWidth);
42 static void csv_log_terminate(struct _nlTestSuite * inSuite, int inResult, int inWidth);
43 static void csv_log_setup(struct _nlTestSuite* inSuite, int inResult, int inWidth);
44 static void csv_log_test(struct _nlTestSuite *inSuite, int inWidth, int inIndex);
45 static void csv_log_teardown(struct _nlTestSuite* inSuite, int inResult, int inWidth);
46 static void csv_log_statTest(struct _nlTestSuite* inSuite);
47 static void csv_log_statAssert(struct _nlTestSuite* inSuite);
48
49 /* Global Variables */
50
51 static nl_test_output_logger_t nl_test_logger_default = {
52     def_log_name,
53     def_log_initialize,
54     def_log_terminate,
55     def_log_setup,
56     def_log_test,
57     def_log_teardown,
58     def_log_statTest,
59     def_log_statAssert,
60 };
61
62 static nl_test_output_logger_t nl_test_logger_csv = {
63     csv_log_name,
64     csv_log_initialize,
65     csv_log_terminate,
66     csv_log_setup,
67     csv_log_test,
68     csv_log_teardown,
69     csv_log_statTest,
70     csv_log_statAssert,
71 };
72
73 /* Global Output Style Variable */
74
75 static const nl_test_output_logger_t *logger_output = &nl_test_logger_default;
76
77 static int isSentinel(const nlTest* inTests, size_t inIndex)
78 {
79     if (inTests[inIndex].name == NULL
80         && inTests[inIndex].function == NULL)
81     {
82         return 1;
83     }
84
85     return 0;
86 }
87
88 /* Human-readable (Default) Output Functions */
89
90 static void def_log_name(struct _nlTestSuite* inSuite)
91 {
92     printf("[ %s ]\n", inSuite->name);
93 }
94
95 static void def_log_initialize(struct _nlTestSuite * inSuite, int inResult, int inWidth)
96 {
97     printf("[ %s : %-*s ] : %s\n", inSuite->name, inWidth, "Initialize", inResult==FAILURE ? "FAILED" : "PASSED" );
98 }
99 static void def_log_terminate(struct _nlTestSuite * inSuite, int inResult, int inWidth)
100 {
101     printf("[ %s : %-*s ] : %s\n", inSuite->name, inWidth,"Terminate", inResult==FAILURE ? "FAILED" : "PASSED" );
102 }
103
104 static void def_log_setup(struct _nlTestSuite* inSuite, int inResult, int inWidth)
105 {
106     printf("[ %s : %-*s ] : %s\n", inSuite->name, inWidth, "Setup", inResult == FAILURE ? "FAILED" : "PASSED");
107 }
108
109 static void def_log_test(struct _nlTestSuite *inSuite, int inWidth, int inIndex)
110 {
111     printf("[ %s : %-*s ] : %s\n", inSuite->name, inWidth, inSuite->tests[inIndex].name, inSuite->flagError ? "FAILED" : "PASSED");
112 }
113
114 static void def_log_teardown(struct _nlTestSuite* inSuite, int inResult, int inWidth)
115 {
116     printf("[ %s : %-*s ] : %s\n", inSuite->name, inWidth, "TearDown", inResult == FAILURE ? "FAILED" : "PASSED");
117 }
118
119 static void def_log_statTest(struct _nlTestSuite* inSuite)
120 {
121     printf("Failed Tests:   %d / %d\n", inSuite->failedTests, inSuite->runTests);
122 }
123
124 static void def_log_statAssert(struct _nlTestSuite* inSuite)
125 {
126     printf("Failed Asserts: %d / %d\n", inSuite->failedAssertions, inSuite->performedAssertions);
127 }
128
129 /* CSV Output Functions */
130
131 static void csv_log_name(struct _nlTestSuite* inSuite)
132 {
133     printf("'#0:','%s'\n", inSuite->name);
134 }
135
136 static void csv_log_initialize(struct _nlTestSuite * inSuite, int inResult, int inWidth)
137 {
138     printf("'#1:','%-*s','%s'\n", inWidth, "Initialize", inResult==FAILURE ? "FAILED" : "PASSED" );
139 }
140
141 static void csv_log_terminate(struct _nlTestSuite * inSuite, int inResult, int inWidth)
142 {
143     printf("'#5:','%-*s','%s'\n", inWidth,"Terminate", inResult==FAILURE ? "FAILED" : "PASSED" );
144 }
145
146 static void csv_log_setup(struct _nlTestSuite* inSuite, int inResult, int inWidth)
147 {
148     printf("'#2:','%-*s','%s'\n", inWidth, "Setup", inResult == FAILURE ? "FAILED" : "PASSED");
149 }
150
151 static void csv_log_test(struct _nlTestSuite *inSuite, int inWidth, int inIndex)
152 {
153     printf("'#3:','%-*s','%s'\n", inWidth, inSuite->tests[inIndex].name, inSuite->flagError ? "FAILED" : "PASSED");
154 }
155
156 static void csv_log_teardown(struct _nlTestSuite* inSuite, int inResult, int inWidth)
157 {
158     printf("'#4:','%-*s','%s'\n", inWidth, "Teardown", inResult == FAILURE ? "FAILED" : "PASSED");
159 }
160
161 static void csv_log_statTest(struct _nlTestSuite* inSuite)
162 {
163     printf("'#6:','%d','%d'\n", inSuite->failedTests, inSuite->runTests);
164 }
165
166 static void csv_log_statAssert(struct _nlTestSuite* inSuite)
167 {
168     printf("'#7:','%d','%d'\n", inSuite->failedAssertions, inSuite->performedAssertions);
169 }
170
171 void nlTestRunner(struct _nlTestSuite* inSuite, void* inContext)
172 {
173     int i = 0;
174     size_t len, max = 0;
175
176     logger_output->PrintName(inSuite);
177
178     /* Determine the maximum test name length */
179
180     for (i = 0; i < kTestSuiteMaxTests; i++)
181     {
182         if (isSentinel(inSuite->tests, i))
183             break;
184
185         len = strlen(inSuite->tests[i].name);
186         if (len > max)
187             max = len;
188     }
189     
190     inSuite->runTests = 0;
191     inSuite->failedTests = 0;
192     inSuite->performedAssertions = 0;
193     inSuite->failedAssertions = 0;
194
195     /* Run the tests and display the test and summary result */
196     if (inSuite->setup != NULL)
197     {
198         int resSetup = inSuite->setup(inContext);
199         logger_output->PrintSetup(inSuite,resSetup, max);
200     }
201     for (i = 0; i < kTestSuiteMaxTests; i++)
202     {
203         if (isSentinel(inSuite->tests, i))
204             break;
205
206         if (inSuite->initialize != NULL)
207         {
208             int resInitialize = inSuite->initialize(inContext);
209             logger_output->PrintInitialize(inSuite,resInitialize, max);
210         }
211
212         inSuite->flagError = false;
213         inSuite->tests[i].function(inSuite, inContext);
214         inSuite->runTests += 1;
215         if (inSuite->flagError)
216             inSuite->failedTests += 1;
217
218         logger_output->PrintTest(inSuite, max, i);
219
220         if (inSuite->terminate != NULL)
221         {
222             int resTerminate = inSuite->terminate(inContext);
223             logger_output->PrintTerminate(inSuite,resTerminate, max);
224         }
225     }
226     if (inSuite->tear_down != NULL)
227     {
228         int resTeardown = inSuite->tear_down(inContext);
229         logger_output->PrintTeardown(inSuite,resTeardown, max);
230     }
231 }
232
233 int nlTestRunnerStats(struct _nlTestSuite* inSuite)
234 {
235     logger_output->PrintStatTests(inSuite);
236     logger_output->PrintStatAsserts(inSuite);
237
238     return 0 - inSuite->failedTests;
239 }
240
241 void nlTestSetOutputStyle(nlTestOutputStyle inStyle)
242 {
243     if (inStyle == OUTPUT_DEF)
244     {
245         logger_output = &nl_test_logger_default;
246     }
247     else if (inStyle == OUTPUT_CSV)
248     {
249         logger_output = &nl_test_logger_csv;
250     }
251 }
252
253 void nlTestSetLogger(const nlTestOutputLogger* inLogger)
254 {
255     logger_output = inLogger;
256 }
257