blob: 41c70e0f37435a78b48b01f64a5e11e7074a0405 [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 Lundblade471a3fd2018-10-18 21:27:45 +053040#include "qcbor_decode_malloc_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{
Laurence Lundblade570fab52018-10-13 18:28:27 +080057 const int32_t nMax = 1000000000;
Laurence Lundbladef156fb82018-10-01 09:47:03 -070058
59 UsefulOutBuf OutBuf;
60 UsefulOutBuf_Init(&OutBuf, StringMem);
61
62 if(nNum < 0) {
63 UsefulOutBuf_AppendByte(&OutBuf, '-');
64 nNum = -nNum;
65 }
Laurence Lundblade570fab52018-10-13 18:28:27 +080066 if(nNum > nMax-1) {
Laurence Lundbladef156fb82018-10-01 09:47:03 -070067 return "XXX";
68 }
69
70 bool bDidSomeOutput = false;
Laurence Lundblade570fab52018-10-13 18:28:27 +080071 for(int n = nMax; n > 0; n/=10) {
Laurence Lundbladef156fb82018-10-01 09:47:03 -070072 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 Lundblade471a3fd2018-10-18 21:27:45 +0530116 TEST_ENTRY(MallocAllStringsTest),
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530117 TEST_ENTRY(AllocAllStringsTest),
118 TEST_ENTRY(IndefiniteLengthNestTest),
119 TEST_ENTRY(NestedMapTestIndefLen),
120 TEST_ENTRY(ParseSimpleTest),
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800121 TEST_ENTRY(EncodeRawTest),
Laurence Lundblade2d85ce42018-10-12 14:12:47 +0800122 TEST_ENTRY(FloatValuesTest1),
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530123 TEST_ENTRY(RTICResultsTest),
124 TEST_ENTRY(MapEncodeTest),
125 TEST_ENTRY(ArrayNestingTest1),
126 TEST_ENTRY(ArrayNestingTest2),
127 TEST_ENTRY(ArrayNestingTest3),
128 TEST_ENTRY(EncodeDateTest),
129 TEST_ENTRY(SimpleValuesTest1),
130 TEST_ENTRY(IntegerValuesTest1),
131 TEST_ENTRY(AllAddMethodsTest),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800132 TEST_ENTRY(ParseTooDeepArrayTest),
133 TEST_ENTRY(ComprehensiveInputTest),
134 TEST_ENTRY(ParseMapTest),
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530135 TEST_ENTRY(IndefiniteLengthArrayMapTest),
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +0800136 TEST_ENTRY(basic_encode_test),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800137 TEST_ENTRY(NestedMapTest),
138 TEST_ENTRY(BignumParseTest),
139 TEST_ENTRY(OptTagParseTest),
140 TEST_ENTRY(DateParseTest),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800141 TEST_ENTRY(ShortBufferParseTest2),
142 TEST_ENTRY(ShortBufferParseTest),
143 TEST_ENTRY(ParseDeepArrayTest),
144 TEST_ENTRY(SimpleArrayTest),
145 TEST_ENTRY(IntegerValuesParseTest),
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530146 TEST_ENTRY(MemPoolTest),
147 TEST_ENTRY(IndefiniteLengthStringTest),
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700148 TEST_ENTRY(half_precision_encode_basic),
149 TEST_ENTRY(half_precision_decode_basic),
150 TEST_ENTRY(half_precision_to_float_transitive_test),
151 TEST_ENTRY(double_as_smallest_encode_basic),
152 TEST_ENTRY(half_precision_to_float_vs_rfc_test),
153 TEST_ENTRY(bstrwraptest),
154 TEST_ENTRY(bstr_wrap_error_test),
155 TEST_ENTRY(bstr_wrap_nest_test),
156 TEST_ENTRY(cose_sign1_tbs_test),
157 //TEST_ENTRY(fail_test),
158};
159
160
161int run_tests(outputstring output, void *poutCtx, int *pNumTestsRun)
162{
163 int nTestsFailed = 0;
164 int nTestsRun = 0;
165 UsefulBuf_MakeStackUB(StringStorage, 5);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530166
167 test_entry2 *t2;
168 const test_entry2 *s_tests2_end = s_tests2 + sizeof(s_tests2)/sizeof(test_entry2);
169
170 for(t2 = s_tests2; t2 < s_tests2_end; t2++) {
171 const char * x = (t2->test_fun)();
172 nTestsRun++;
173 if(output) {
174 (*output)(t2->szTestName, poutCtx);
175 }
176
177 if(x) {
178 if(output) {
179 (*output)(" FAILED (returned ", poutCtx);
180 (*output)(x, poutCtx);
181 (*output)(")\n", poutCtx);
182 }
183 nTestsFailed++;
184 } else {
185 if(output) {
186 (*output)( " PASSED\n", poutCtx);
187 }
188 }
189 }
190
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700191
192 test_entry *t;
193 const test_entry *s_tests_end = s_tests + sizeof(s_tests)/sizeof(test_entry);
194
195 for(t = s_tests; t < s_tests_end; t++) {
196 int x = (t->test_fun)();
197 nTestsRun++;
198 if(output) {
199 (*output)(t->szTestName, poutCtx);
200 }
201
202 if(x) {
203 if(output) {
204 (*output)(" FAILED (returned ", poutCtx);
205 (*output)(NumToString(x, StringStorage), poutCtx);
206 (*output)(")\n", poutCtx);
207 }
208 nTestsFailed++;
209 } else {
210 if(output) {
211 (*output)( " PASSED\n", poutCtx);
212 }
213 }
214 }
215
216 if(pNumTestsRun) {
217 *pNumTestsRun = nTestsRun;
218 }
219
220 if(output) {
221 (*output)( "SUMMARY: ", poutCtx);
222 (*output)( NumToString(nTestsRun, StringStorage), poutCtx);
223 (*output)( " tests run; ", poutCtx);
224 (*output)( NumToString(nTestsFailed, StringStorage), poutCtx);
225 (*output)( " tests failed\n", poutCtx);
226 }
227
228 return nTestsFailed;
229}