blob: ad3f7b78bfe1f89271dbffbd97e8a99c38556e1f [file] [log] [blame]
Laurence Lundbladef156fb82018-10-01 09:47:03 -07001
2/*==============================================================================
3 run_tests.c -- test aggregator and results reporting
4
5 Copyright 2018 Laurence Lundblade
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14
15 The above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 SOFTWARE.
26
27 (This is the MIT license)
28 ==============================================================================*/
29// Created by Laurence Lundblade on 9/30/18.
30
31
32#include "run_tests.h"
33#include "UsefulBuf.h"
34#include <stdbool.h>
35
Laurence Lundblade2d85ce42018-10-12 14:12:47 +080036#include "float_tests.h"
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080037#include "qcbor_decode_tests.h"
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053038#include "qcbor_encode_tests.h"
39#include "UsefulBuf_Tests.h"
Laurence Lundbladef156fb82018-10-01 09:47:03 -070040
41// Used to test the test runner
42int fail_test()
43{
44 return -44;
45}
46
47
48/*
49 Convert a number up to 999999999 to a string. This is so sprintf doesn't
50 have to be linked in so as to minimized dependencies even in test code.
51
52 This function does pointer math. TODO: test this.
53 */
54const char *NumToString(int32_t nNum, UsefulBuf StringMem)
55{
Laurence Lundblade570fab52018-10-13 18:28:27 +080056 const int32_t nMax = 1000000000;
Laurence Lundbladef156fb82018-10-01 09:47:03 -070057
58 UsefulOutBuf OutBuf;
59 UsefulOutBuf_Init(&OutBuf, StringMem);
60
61 if(nNum < 0) {
62 UsefulOutBuf_AppendByte(&OutBuf, '-');
63 nNum = -nNum;
64 }
Laurence Lundblade570fab52018-10-13 18:28:27 +080065 if(nNum > nMax-1) {
Laurence Lundbladef156fb82018-10-01 09:47:03 -070066 return "XXX";
67 }
68
69 bool bDidSomeOutput = false;
Laurence Lundblade570fab52018-10-13 18:28:27 +080070 for(int n = nMax; n > 0; n/=10) {
Laurence Lundbladef156fb82018-10-01 09:47:03 -070071 int x = nNum/n;
72 if(x || bDidSomeOutput){
73 bDidSomeOutput = true;
74 UsefulOutBuf_AppendByte(&OutBuf, '0' + x);
75 nNum -= x * n;
76 }
77 }
78 if(!bDidSomeOutput){
79 UsefulOutBuf_AppendByte(&OutBuf, '0');
80 }
81 UsefulOutBuf_AppendByte(&OutBuf, '\0');
82
83 return UsefulOutBuf_GetError(&OutBuf) ? "" : StringMem.ptr;
84}
85
86
87
88typedef int (test_fun_t)(void);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053089typedef const char * (test_fun2_t)(void);
90
Laurence Lundbladef156fb82018-10-01 09:47:03 -070091
92#define TEST_ENTRY(test_name) {#test_name, test_name}
93typedef struct {
94 const char *szTestName;
95 test_fun_t *test_fun;
96} test_entry;
97
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053098typedef struct {
99 const char *szTestName;
100 test_fun2_t *test_fun;
101} test_entry2;
102
103test_entry2 s_tests2[] = {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800104 TEST_ENTRY(UBUTest_CopyUtil),
105 TEST_ENTRY(UOBTest_NonAdversarial),
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530106 TEST_ENTRY(TestBasicSanity),
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800107 TEST_ENTRY(UOBTest_BoundaryConditionsTest),
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530108 TEST_ENTRY(UBMacroConversionsTest),
109 TEST_ENTRY(UBUtilTests),
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800110 TEST_ENTRY(UIBTest_IntegerFormat)
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530111};
112
113
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700114test_entry s_tests[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +0800115 TEST_ENTRY(indeflen_nest_test),
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800116 TEST_ENTRY(EncodeRawTest),
Laurence Lundblade2d85ce42018-10-12 14:12:47 +0800117 TEST_ENTRY(FloatValuesTest1),
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530118 TEST_ENTRY(RTICResultsTest),
119 TEST_ENTRY(MapEncodeTest),
120 TEST_ENTRY(ArrayNestingTest1),
121 TEST_ENTRY(ArrayNestingTest2),
122 TEST_ENTRY(ArrayNestingTest3),
123 TEST_ENTRY(EncodeDateTest),
124 TEST_ENTRY(SimpleValuesTest1),
125 TEST_ENTRY(IntegerValuesTest1),
126 TEST_ENTRY(AllAddMethodsTest),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800127 TEST_ENTRY(ParseTooDeepArrayTest),
128 TEST_ENTRY(ComprehensiveInputTest),
129 TEST_ENTRY(ParseMapTest),
Laurence Lundblade742df4a2018-10-13 20:07:17 +0800130 TEST_ENTRY(NestedMapTestIndefLen),
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700131 TEST_ENTRY(indefinite_length_decode_test),
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800132 TEST_ENTRY(basic_encode_test),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800133 TEST_ENTRY(NestedMapTest),
134 TEST_ENTRY(BignumParseTest),
135 TEST_ENTRY(OptTagParseTest),
136 TEST_ENTRY(DateParseTest),
137 TEST_ENTRY(ParseSimpleTest),
138 TEST_ENTRY(ShortBufferParseTest2),
139 TEST_ENTRY(ShortBufferParseTest),
140 TEST_ENTRY(ParseDeepArrayTest),
141 TEST_ENTRY(SimpleArrayTest),
142 TEST_ENTRY(IntegerValuesParseTest),
143 TEST_ENTRY(mempool_test),
144 TEST_ENTRY(indefinite_length_decode_string_test),
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700145 TEST_ENTRY(half_precision_encode_basic),
146 TEST_ENTRY(half_precision_decode_basic),
147 TEST_ENTRY(half_precision_to_float_transitive_test),
148 TEST_ENTRY(double_as_smallest_encode_basic),
149 TEST_ENTRY(half_precision_to_float_vs_rfc_test),
150 TEST_ENTRY(bstrwraptest),
151 TEST_ENTRY(bstr_wrap_error_test),
152 TEST_ENTRY(bstr_wrap_nest_test),
153 TEST_ENTRY(cose_sign1_tbs_test),
154 //TEST_ENTRY(fail_test),
155};
156
157
158int run_tests(outputstring output, void *poutCtx, int *pNumTestsRun)
159{
160 int nTestsFailed = 0;
161 int nTestsRun = 0;
162 UsefulBuf_MakeStackUB(StringStorage, 5);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530163
164 test_entry2 *t2;
165 const test_entry2 *s_tests2_end = s_tests2 + sizeof(s_tests2)/sizeof(test_entry2);
166
167 for(t2 = s_tests2; t2 < s_tests2_end; t2++) {
168 const char * x = (t2->test_fun)();
169 nTestsRun++;
170 if(output) {
171 (*output)(t2->szTestName, poutCtx);
172 }
173
174 if(x) {
175 if(output) {
176 (*output)(" FAILED (returned ", poutCtx);
177 (*output)(x, poutCtx);
178 (*output)(")\n", poutCtx);
179 }
180 nTestsFailed++;
181 } else {
182 if(output) {
183 (*output)( " PASSED\n", poutCtx);
184 }
185 }
186 }
187
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700188
189 test_entry *t;
190 const test_entry *s_tests_end = s_tests + sizeof(s_tests)/sizeof(test_entry);
191
192 for(t = s_tests; t < s_tests_end; t++) {
193 int x = (t->test_fun)();
194 nTestsRun++;
195 if(output) {
196 (*output)(t->szTestName, poutCtx);
197 }
198
199 if(x) {
200 if(output) {
201 (*output)(" FAILED (returned ", poutCtx);
202 (*output)(NumToString(x, StringStorage), poutCtx);
203 (*output)(")\n", poutCtx);
204 }
205 nTestsFailed++;
206 } else {
207 if(output) {
208 (*output)( " PASSED\n", poutCtx);
209 }
210 }
211 }
212
213 if(pNumTestsRun) {
214 *pNumTestsRun = nTestsRun;
215 }
216
217 if(output) {
218 (*output)( "SUMMARY: ", poutCtx);
219 (*output)( NumToString(nTestsRun, StringStorage), poutCtx);
220 (*output)( " tests run; ", poutCtx);
221 (*output)( NumToString(nTestsFailed, StringStorage), poutCtx);
222 (*output)( " tests failed\n", poutCtx);
223 }
224
225 return nTestsFailed;
226}