blob: 8a63d785672927fd6081d18fee5687accc462e23 [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 Lundbladea44d5062018-10-17 18:45:12 +0530115 TEST_ENTRY(AllocAllStringsTest),
116 TEST_ENTRY(IndefiniteLengthNestTest),
117 TEST_ENTRY(NestedMapTestIndefLen),
118 TEST_ENTRY(ParseSimpleTest),
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800119 TEST_ENTRY(EncodeRawTest),
Laurence Lundblade2d85ce42018-10-12 14:12:47 +0800120 TEST_ENTRY(FloatValuesTest1),
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530121 TEST_ENTRY(RTICResultsTest),
122 TEST_ENTRY(MapEncodeTest),
123 TEST_ENTRY(ArrayNestingTest1),
124 TEST_ENTRY(ArrayNestingTest2),
125 TEST_ENTRY(ArrayNestingTest3),
126 TEST_ENTRY(EncodeDateTest),
127 TEST_ENTRY(SimpleValuesTest1),
128 TEST_ENTRY(IntegerValuesTest1),
129 TEST_ENTRY(AllAddMethodsTest),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800130 TEST_ENTRY(ParseTooDeepArrayTest),
131 TEST_ENTRY(ComprehensiveInputTest),
132 TEST_ENTRY(ParseMapTest),
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530133 TEST_ENTRY(IndefiniteLengthArrayMapTest),
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800134 TEST_ENTRY(basic_encode_test),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800135 TEST_ENTRY(NestedMapTest),
136 TEST_ENTRY(BignumParseTest),
137 TEST_ENTRY(OptTagParseTest),
138 TEST_ENTRY(DateParseTest),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800139 TEST_ENTRY(ShortBufferParseTest2),
140 TEST_ENTRY(ShortBufferParseTest),
141 TEST_ENTRY(ParseDeepArrayTest),
142 TEST_ENTRY(SimpleArrayTest),
143 TEST_ENTRY(IntegerValuesParseTest),
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530144 TEST_ENTRY(MemPoolTest),
145 TEST_ENTRY(IndefiniteLengthStringTest),
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700146 TEST_ENTRY(half_precision_encode_basic),
147 TEST_ENTRY(half_precision_decode_basic),
148 TEST_ENTRY(half_precision_to_float_transitive_test),
149 TEST_ENTRY(double_as_smallest_encode_basic),
150 TEST_ENTRY(half_precision_to_float_vs_rfc_test),
151 TEST_ENTRY(bstrwraptest),
152 TEST_ENTRY(bstr_wrap_error_test),
153 TEST_ENTRY(bstr_wrap_nest_test),
154 TEST_ENTRY(cose_sign1_tbs_test),
155 //TEST_ENTRY(fail_test),
156};
157
158
159int run_tests(outputstring output, void *poutCtx, int *pNumTestsRun)
160{
161 int nTestsFailed = 0;
162 int nTestsRun = 0;
163 UsefulBuf_MakeStackUB(StringStorage, 5);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530164
165 test_entry2 *t2;
166 const test_entry2 *s_tests2_end = s_tests2 + sizeof(s_tests2)/sizeof(test_entry2);
167
168 for(t2 = s_tests2; t2 < s_tests2_end; t2++) {
169 const char * x = (t2->test_fun)();
170 nTestsRun++;
171 if(output) {
172 (*output)(t2->szTestName, poutCtx);
173 }
174
175 if(x) {
176 if(output) {
177 (*output)(" FAILED (returned ", poutCtx);
178 (*output)(x, poutCtx);
179 (*output)(")\n", poutCtx);
180 }
181 nTestsFailed++;
182 } else {
183 if(output) {
184 (*output)( " PASSED\n", poutCtx);
185 }
186 }
187 }
188
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700189
190 test_entry *t;
191 const test_entry *s_tests_end = s_tests + sizeof(s_tests)/sizeof(test_entry);
192
193 for(t = s_tests; t < s_tests_end; t++) {
194 int x = (t->test_fun)();
195 nTestsRun++;
196 if(output) {
197 (*output)(t->szTestName, poutCtx);
198 }
199
200 if(x) {
201 if(output) {
202 (*output)(" FAILED (returned ", poutCtx);
203 (*output)(NumToString(x, StringStorage), poutCtx);
204 (*output)(")\n", poutCtx);
205 }
206 nTestsFailed++;
207 } else {
208 if(output) {
209 (*output)( " PASSED\n", poutCtx);
210 }
211 }
212 }
213
214 if(pNumTestsRun) {
215 *pNumTestsRun = nTestsRun;
216 }
217
218 if(output) {
219 (*output)( "SUMMARY: ", poutCtx);
220 (*output)( NumToString(nTestsRun, StringStorage), poutCtx);
221 (*output)( " tests run; ", poutCtx);
222 (*output)( NumToString(nTestsFailed, StringStorage), poutCtx);
223 (*output)( " tests failed\n", poutCtx);
224 }
225
226 return nTestsFailed;
227}