blob: c86baa590d9f9d5c5b4d4c0f7ae23c7058fe039a [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
36#include "half_precision_test.h"
37#include "basic_test.h"
38#include "bstrwrap_tests.h"
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080039#include "mempool_test.h"
40#include "qcbor_decode_tests.h"
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053041#include "qcbor_encode_tests.h"
42#include "UsefulBuf_Tests.h"
Laurence Lundbladef156fb82018-10-01 09:47:03 -070043
44// Used to test the test runner
45int fail_test()
46{
47 return -44;
48}
49
50
51/*
52 Convert a number up to 999999999 to a string. This is so sprintf doesn't
53 have to be linked in so as to minimized dependencies even in test code.
54
55 This function does pointer math. TODO: test this.
56 */
57const char *NumToString(int32_t nNum, UsefulBuf StringMem)
58{
59 const uint32_t uMax = 1000000000;
60
61 UsefulOutBuf OutBuf;
62 UsefulOutBuf_Init(&OutBuf, StringMem);
63
64 if(nNum < 0) {
65 UsefulOutBuf_AppendByte(&OutBuf, '-');
66 nNum = -nNum;
67 }
68 if(nNum > uMax-1) {
69 return "XXX";
70 }
71
72 bool bDidSomeOutput = false;
73 for(int n = uMax; n > 0; n/=10) {
74 int x = nNum/n;
75 if(x || bDidSomeOutput){
76 bDidSomeOutput = true;
77 UsefulOutBuf_AppendByte(&OutBuf, '0' + x);
78 nNum -= x * n;
79 }
80 }
81 if(!bDidSomeOutput){
82 UsefulOutBuf_AppendByte(&OutBuf, '0');
83 }
84 UsefulOutBuf_AppendByte(&OutBuf, '\0');
85
86 return UsefulOutBuf_GetError(&OutBuf) ? "" : StringMem.ptr;
87}
88
89
90
91typedef int (test_fun_t)(void);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053092typedef const char * (test_fun2_t)(void);
93
Laurence Lundbladef156fb82018-10-01 09:47:03 -070094
95#define TEST_ENTRY(test_name) {#test_name, test_name}
96typedef struct {
97 const char *szTestName;
98 test_fun_t *test_fun;
99} test_entry;
100
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530101typedef struct {
102 const char *szTestName;
103 test_fun2_t *test_fun;
104} test_entry2;
105
106test_entry2 s_tests2[] = {
107 TEST_ENTRY(UBCopyUtilTest),
108 TEST_ENTRY(NonAdversarialUOBTest),
109 TEST_ENTRY(TestBasicSanity),
110 TEST_ENTRY(BoundaryConditionsTest),
111 TEST_ENTRY(UBMacroConversionsTest),
112 TEST_ENTRY(UBUtilTests),
113 TEST_ENTRY(UBIntegerFormatTests)
114};
115
116
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700117test_entry s_tests[] = {
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 Lundblade041ffa52018-10-07 11:43:51 +0700130 TEST_ENTRY(indefinite_length_decode_test),
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700131 TEST_ENTRY(basic_test_one),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800132 TEST_ENTRY(NestedMapTest),
133 TEST_ENTRY(BignumParseTest),
134 TEST_ENTRY(OptTagParseTest),
135 TEST_ENTRY(DateParseTest),
136 TEST_ENTRY(ParseSimpleTest),
137 TEST_ENTRY(ShortBufferParseTest2),
138 TEST_ENTRY(ShortBufferParseTest),
139 TEST_ENTRY(ParseDeepArrayTest),
140 TEST_ENTRY(SimpleArrayTest),
141 TEST_ENTRY(IntegerValuesParseTest),
142 TEST_ENTRY(mempool_test),
143 TEST_ENTRY(indefinite_length_decode_string_test),
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700144 TEST_ENTRY(half_precision_encode_basic),
145 TEST_ENTRY(half_precision_decode_basic),
146 TEST_ENTRY(half_precision_to_float_transitive_test),
147 TEST_ENTRY(double_as_smallest_encode_basic),
148 TEST_ENTRY(half_precision_to_float_vs_rfc_test),
149 TEST_ENTRY(bstrwraptest),
150 TEST_ENTRY(bstr_wrap_error_test),
151 TEST_ENTRY(bstr_wrap_nest_test),
152 TEST_ENTRY(cose_sign1_tbs_test),
153 //TEST_ENTRY(fail_test),
154};
155
156
157int run_tests(outputstring output, void *poutCtx, int *pNumTestsRun)
158{
159 int nTestsFailed = 0;
160 int nTestsRun = 0;
161 UsefulBuf_MakeStackUB(StringStorage, 5);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530162
163 test_entry2 *t2;
164 const test_entry2 *s_tests2_end = s_tests2 + sizeof(s_tests2)/sizeof(test_entry2);
165
166 for(t2 = s_tests2; t2 < s_tests2_end; t2++) {
167 const char * x = (t2->test_fun)();
168 nTestsRun++;
169 if(output) {
170 (*output)(t2->szTestName, poutCtx);
171 }
172
173 if(x) {
174 if(output) {
175 (*output)(" FAILED (returned ", poutCtx);
176 (*output)(x, poutCtx);
177 (*output)(")\n", poutCtx);
178 }
179 nTestsFailed++;
180 } else {
181 if(output) {
182 (*output)( " PASSED\n", poutCtx);
183 }
184 }
185 }
186
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700187
188 test_entry *t;
189 const test_entry *s_tests_end = s_tests + sizeof(s_tests)/sizeof(test_entry);
190
191 for(t = s_tests; t < s_tests_end; t++) {
192 int x = (t->test_fun)();
193 nTestsRun++;
194 if(output) {
195 (*output)(t->szTestName, poutCtx);
196 }
197
198 if(x) {
199 if(output) {
200 (*output)(" FAILED (returned ", poutCtx);
201 (*output)(NumToString(x, StringStorage), poutCtx);
202 (*output)(")\n", poutCtx);
203 }
204 nTestsFailed++;
205 } else {
206 if(output) {
207 (*output)( " PASSED\n", poutCtx);
208 }
209 }
210 }
211
212 if(pNumTestsRun) {
213 *pNumTestsRun = nTestsRun;
214 }
215
216 if(output) {
217 (*output)( "SUMMARY: ", poutCtx);
218 (*output)( NumToString(nTestsRun, StringStorage), poutCtx);
219 (*output)( " tests run; ", poutCtx);
220 (*output)( NumToString(nTestsFailed, StringStorage), poutCtx);
221 (*output)( " tests failed\n", poutCtx);
222 }
223
224 return nTestsFailed;
225}