blob: 38341a80f26a21ac61407a99395bd3516490d4e5 [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 Lundbladef156fb82018-10-01 09:47:03 -070037#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[] = {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800107 TEST_ENTRY(UBUTest_CopyUtil),
108 TEST_ENTRY(UOBTest_NonAdversarial),
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530109 TEST_ENTRY(TestBasicSanity),
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800110 TEST_ENTRY(UOBTest_BoundaryConditionsTest),
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530111 TEST_ENTRY(UBMacroConversionsTest),
112 TEST_ENTRY(UBUtilTests),
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800113 TEST_ENTRY(UIBTest_IntegerFormat)
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530114};
115
116
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700117test_entry s_tests[] = {
Laurence Lundblade2d85ce42018-10-12 14:12:47 +0800118 TEST_ENTRY(FloatValuesTest1),
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530119 TEST_ENTRY(RTICResultsTest),
120 TEST_ENTRY(MapEncodeTest),
121 TEST_ENTRY(ArrayNestingTest1),
122 TEST_ENTRY(ArrayNestingTest2),
123 TEST_ENTRY(ArrayNestingTest3),
124 TEST_ENTRY(EncodeDateTest),
125 TEST_ENTRY(SimpleValuesTest1),
126 TEST_ENTRY(IntegerValuesTest1),
127 TEST_ENTRY(AllAddMethodsTest),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800128 TEST_ENTRY(ParseTooDeepArrayTest),
129 TEST_ENTRY(ComprehensiveInputTest),
130 TEST_ENTRY(ParseMapTest),
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700131 TEST_ENTRY(indefinite_length_decode_test),
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700132 TEST_ENTRY(basic_test_one),
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}