blob: a9317a202a91f7e8189673099d0a4f6afbcb7fd1 [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"
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080038#include "qcbor_decode_tests.h"
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053039#include "qcbor_encode_tests.h"
40#include "UsefulBuf_Tests.h"
Laurence Lundbladef156fb82018-10-01 09:47:03 -070041
42// Used to test the test runner
43int fail_test()
44{
45 return -44;
46}
47
48
49/*
50 Convert a number up to 999999999 to a string. This is so sprintf doesn't
51 have to be linked in so as to minimized dependencies even in test code.
52
53 This function does pointer math. TODO: test this.
54 */
55const char *NumToString(int32_t nNum, UsefulBuf StringMem)
56{
57 const uint32_t uMax = 1000000000;
58
59 UsefulOutBuf OutBuf;
60 UsefulOutBuf_Init(&OutBuf, StringMem);
61
62 if(nNum < 0) {
63 UsefulOutBuf_AppendByte(&OutBuf, '-');
64 nNum = -nNum;
65 }
66 if(nNum > uMax-1) {
67 return "XXX";
68 }
69
70 bool bDidSomeOutput = false;
71 for(int n = uMax; n > 0; n/=10) {
72 int x = nNum/n;
73 if(x || bDidSomeOutput){
74 bDidSomeOutput = true;
75 UsefulOutBuf_AppendByte(&OutBuf, '0' + x);
76 nNum -= x * n;
77 }
78 }
79 if(!bDidSomeOutput){
80 UsefulOutBuf_AppendByte(&OutBuf, '0');
81 }
82 UsefulOutBuf_AppendByte(&OutBuf, '\0');
83
84 return UsefulOutBuf_GetError(&OutBuf) ? "" : StringMem.ptr;
85}
86
87
88
89typedef int (test_fun_t)(void);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053090typedef const char * (test_fun2_t)(void);
91
Laurence Lundbladef156fb82018-10-01 09:47:03 -070092
93#define TEST_ENTRY(test_name) {#test_name, test_name}
94typedef struct {
95 const char *szTestName;
96 test_fun_t *test_fun;
97} test_entry;
98
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053099typedef struct {
100 const char *szTestName;
101 test_fun2_t *test_fun;
102} test_entry2;
103
104test_entry2 s_tests2[] = {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800105 TEST_ENTRY(UBUTest_CopyUtil),
106 TEST_ENTRY(UOBTest_NonAdversarial),
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530107 TEST_ENTRY(TestBasicSanity),
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800108 TEST_ENTRY(UOBTest_BoundaryConditionsTest),
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530109 TEST_ENTRY(UBMacroConversionsTest),
110 TEST_ENTRY(UBUtilTests),
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800111 TEST_ENTRY(UIBTest_IntegerFormat)
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530112};
113
114
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700115test_entry s_tests[] = {
Laurence Lundblade2d85ce42018-10-12 14:12:47 +0800116 TEST_ENTRY(FloatValuesTest1),
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530117 TEST_ENTRY(RTICResultsTest),
118 TEST_ENTRY(MapEncodeTest),
119 TEST_ENTRY(ArrayNestingTest1),
120 TEST_ENTRY(ArrayNestingTest2),
121 TEST_ENTRY(ArrayNestingTest3),
122 TEST_ENTRY(EncodeDateTest),
123 TEST_ENTRY(SimpleValuesTest1),
124 TEST_ENTRY(IntegerValuesTest1),
125 TEST_ENTRY(AllAddMethodsTest),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800126 TEST_ENTRY(ParseTooDeepArrayTest),
127 TEST_ENTRY(ComprehensiveInputTest),
128 TEST_ENTRY(ParseMapTest),
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700129 TEST_ENTRY(indefinite_length_decode_test),
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700130 TEST_ENTRY(basic_test_one),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800131 TEST_ENTRY(NestedMapTest),
132 TEST_ENTRY(BignumParseTest),
133 TEST_ENTRY(OptTagParseTest),
134 TEST_ENTRY(DateParseTest),
135 TEST_ENTRY(ParseSimpleTest),
136 TEST_ENTRY(ShortBufferParseTest2),
137 TEST_ENTRY(ShortBufferParseTest),
138 TEST_ENTRY(ParseDeepArrayTest),
139 TEST_ENTRY(SimpleArrayTest),
140 TEST_ENTRY(IntegerValuesParseTest),
141 TEST_ENTRY(mempool_test),
142 TEST_ENTRY(indefinite_length_decode_string_test),
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700143 TEST_ENTRY(half_precision_encode_basic),
144 TEST_ENTRY(half_precision_decode_basic),
145 TEST_ENTRY(half_precision_to_float_transitive_test),
146 TEST_ENTRY(double_as_smallest_encode_basic),
147 TEST_ENTRY(half_precision_to_float_vs_rfc_test),
148 TEST_ENTRY(bstrwraptest),
149 TEST_ENTRY(bstr_wrap_error_test),
150 TEST_ENTRY(bstr_wrap_nest_test),
151 TEST_ENTRY(cose_sign1_tbs_test),
152 //TEST_ENTRY(fail_test),
153};
154
155
156int run_tests(outputstring output, void *poutCtx, int *pNumTestsRun)
157{
158 int nTestsFailed = 0;
159 int nTestsRun = 0;
160 UsefulBuf_MakeStackUB(StringStorage, 5);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530161
162 test_entry2 *t2;
163 const test_entry2 *s_tests2_end = s_tests2 + sizeof(s_tests2)/sizeof(test_entry2);
164
165 for(t2 = s_tests2; t2 < s_tests2_end; t2++) {
166 const char * x = (t2->test_fun)();
167 nTestsRun++;
168 if(output) {
169 (*output)(t2->szTestName, poutCtx);
170 }
171
172 if(x) {
173 if(output) {
174 (*output)(" FAILED (returned ", poutCtx);
175 (*output)(x, poutCtx);
176 (*output)(")\n", poutCtx);
177 }
178 nTestsFailed++;
179 } else {
180 if(output) {
181 (*output)( " PASSED\n", poutCtx);
182 }
183 }
184 }
185
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700186
187 test_entry *t;
188 const test_entry *s_tests_end = s_tests + sizeof(s_tests)/sizeof(test_entry);
189
190 for(t = s_tests; t < s_tests_end; t++) {
191 int x = (t->test_fun)();
192 nTestsRun++;
193 if(output) {
194 (*output)(t->szTestName, poutCtx);
195 }
196
197 if(x) {
198 if(output) {
199 (*output)(" FAILED (returned ", poutCtx);
200 (*output)(NumToString(x, StringStorage), poutCtx);
201 (*output)(")\n", poutCtx);
202 }
203 nTestsFailed++;
204 } else {
205 if(output) {
206 (*output)( " PASSED\n", poutCtx);
207 }
208 }
209 }
210
211 if(pNumTestsRun) {
212 *pNumTestsRun = nTestsRun;
213 }
214
215 if(output) {
216 (*output)( "SUMMARY: ", poutCtx);
217 (*output)( NumToString(nTestsRun, StringStorage), poutCtx);
218 (*output)( " tests run; ", poutCtx);
219 (*output)( NumToString(nTestsFailed, StringStorage), poutCtx);
220 (*output)( " tests failed\n", poutCtx);
221 }
222
223 return nTestsFailed;
224}