blob: 3fba941dd8b00eda9cf8d0ac31fbbb27a5b1193f [file] [log] [blame]
Laurence Lundbladef156fb82018-10-01 09:47:03 -07001
2/*==============================================================================
3 run_tests.c -- test aggregator and results reporting
4
Laurence Lundbladed92a6162018-11-01 11:38:35 +07005 Copyright (c) 2018, Laurence Lundblade.
6 All rights reserved.
Laurence Lundbladef156fb82018-10-01 09:47:03 -07007
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07008Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are
10met:
11 * Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above
14 copyright notice, this list of conditions and the following
15 disclaimer in the documentation and/or other materials provided
16 with the distribution.
17 * The name "Laurence Lundblade" may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
Laurence Lundbladef156fb82018-10-01 09:47:03 -070020
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070021THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
22WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
24ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
Laurence Lundblade88b6ece2018-12-04 12:27:19 +090031IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ==============================================================================*/
Laurence Lundbladef156fb82018-10-01 09:47:03 -070033// Created by Laurence Lundblade on 9/30/18.
34
35
36#include "run_tests.h"
37#include "UsefulBuf.h"
38#include <stdbool.h>
39
Laurence Lundblade2d85ce42018-10-12 14:12:47 +080040#include "float_tests.h"
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080041#include "qcbor_decode_tests.h"
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053042#include "qcbor_encode_tests.h"
43#include "UsefulBuf_Tests.h"
Laurence Lundblade471a3fd2018-10-18 21:27:45 +053044#include "qcbor_decode_malloc_tests.h"
Laurence Lundbladef156fb82018-10-01 09:47:03 -070045
46// Used to test the test runner
47int fail_test()
48{
49 return -44;
50}
51
52
53/*
54 Convert a number up to 999999999 to a string. This is so sprintf doesn't
55 have to be linked in so as to minimized dependencies even in test code.
Laurence Lundblade1544c482018-12-05 20:52:35 +090056 */
Laurence Lundbladef156fb82018-10-01 09:47:03 -070057const char *NumToString(int32_t nNum, UsefulBuf StringMem)
58{
Laurence Lundblade570fab52018-10-13 18:28:27 +080059 const int32_t nMax = 1000000000;
Laurence Lundbladef156fb82018-10-01 09:47:03 -070060
61 UsefulOutBuf OutBuf;
62 UsefulOutBuf_Init(&OutBuf, StringMem);
63
64 if(nNum < 0) {
65 UsefulOutBuf_AppendByte(&OutBuf, '-');
66 nNum = -nNum;
67 }
Laurence Lundblade570fab52018-10-13 18:28:27 +080068 if(nNum > nMax-1) {
Laurence Lundbladef156fb82018-10-01 09:47:03 -070069 return "XXX";
70 }
71
72 bool bDidSomeOutput = false;
Laurence Lundblade570fab52018-10-13 18:28:27 +080073 for(int n = nMax; n > 0; n/=10) {
Laurence Lundbladef156fb82018-10-01 09:47:03 -070074 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 Lundblade471a3fd2018-10-18 21:27:45 +0530118 TEST_ENTRY(MallocAllStringsTest),
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530119 TEST_ENTRY(AllocAllStringsTest),
120 TEST_ENTRY(IndefiniteLengthNestTest),
121 TEST_ENTRY(NestedMapTestIndefLen),
122 TEST_ENTRY(ParseSimpleTest),
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800123 TEST_ENTRY(EncodeRawTest),
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530124 TEST_ENTRY(RTICResultsTest),
125 TEST_ENTRY(MapEncodeTest),
126 TEST_ENTRY(ArrayNestingTest1),
127 TEST_ENTRY(ArrayNestingTest2),
128 TEST_ENTRY(ArrayNestingTest3),
129 TEST_ENTRY(EncodeDateTest),
130 TEST_ENTRY(SimpleValuesTest1),
131 TEST_ENTRY(IntegerValuesTest1),
132 TEST_ENTRY(AllAddMethodsTest),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800133 TEST_ENTRY(ParseTooDeepArrayTest),
134 TEST_ENTRY(ComprehensiveInputTest),
135 TEST_ENTRY(ParseMapTest),
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530136 TEST_ENTRY(IndefiniteLengthArrayMapTest),
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530137 TEST_ENTRY(BasicEncodeTest),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800138 TEST_ENTRY(NestedMapTest),
139 TEST_ENTRY(BignumParseTest),
140 TEST_ENTRY(OptTagParseTest),
141 TEST_ENTRY(DateParseTest),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800142 TEST_ENTRY(ShortBufferParseTest2),
143 TEST_ENTRY(ShortBufferParseTest),
144 TEST_ENTRY(ParseDeepArrayTest),
145 TEST_ENTRY(SimpleArrayTest),
146 TEST_ENTRY(IntegerValuesParseTest),
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530147 TEST_ENTRY(MemPoolTest),
148 TEST_ENTRY(IndefiniteLengthStringTest),
Laurence Lundbladebb474be2018-10-22 11:53:21 +0530149 TEST_ENTRY(HalfPrecisionDecodeBasicTests),
Laurence Lundbladebb474be2018-10-22 11:53:21 +0530150 TEST_ENTRY(DoubleAsSmallestTest),
151 TEST_ENTRY(HalfPrecisionAgainstRFCCodeTest),
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530152 TEST_ENTRY(BstrWrapTest),
153 TEST_ENTRY(BstrWrapErrorTest),
154 TEST_ENTRY(BstrWrapNestTest),
155 TEST_ENTRY(CoseSign1TBSTest),
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -0800156 TEST_ENTRY(EncodeErrorTests),
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700157 //TEST_ENTRY(fail_test),
158};
159
160
Laurence Lundblade88b6ece2018-12-04 12:27:19 +0900161int run_tests(const char *szTestName, outputstring output, void *poutCtx, int *pNumTestsRun)
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700162{
163 int nTestsFailed = 0;
164 int nTestsRun = 0;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530165 UsefulBuf_MAKE_STACK_UB(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++) {
Laurence Lundblade88b6ece2018-12-04 12:27:19 +0900171 if(szTestName && strcmp(szTestName, t2->szTestName)) {
172 continue;
173 }
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530174 const char * x = (t2->test_fun)();
175 nTestsRun++;
176 if(output) {
177 (*output)(t2->szTestName, poutCtx);
178 }
179
180 if(x) {
181 if(output) {
182 (*output)(" FAILED (returned ", poutCtx);
183 (*output)(x, poutCtx);
184 (*output)(")\n", poutCtx);
185 }
186 nTestsFailed++;
187 } else {
188 if(output) {
189 (*output)( " PASSED\n", poutCtx);
190 }
191 }
192 }
193
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700194
195 test_entry *t;
196 const test_entry *s_tests_end = s_tests + sizeof(s_tests)/sizeof(test_entry);
197
198 for(t = s_tests; t < s_tests_end; t++) {
Laurence Lundblade88b6ece2018-12-04 12:27:19 +0900199 if(szTestName && strcmp(szTestName, t->szTestName)) {
200 continue;
201 }
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700202 int x = (t->test_fun)();
203 nTestsRun++;
204 if(output) {
205 (*output)(t->szTestName, poutCtx);
206 }
207
208 if(x) {
209 if(output) {
210 (*output)(" FAILED (returned ", poutCtx);
211 (*output)(NumToString(x, StringStorage), poutCtx);
212 (*output)(")\n", poutCtx);
213 }
214 nTestsFailed++;
215 } else {
216 if(output) {
217 (*output)( " PASSED\n", poutCtx);
218 }
219 }
220 }
221
222 if(pNumTestsRun) {
223 *pNumTestsRun = nTestsRun;
224 }
225
226 if(output) {
227 (*output)( "SUMMARY: ", poutCtx);
228 (*output)( NumToString(nTestsRun, StringStorage), poutCtx);
229 (*output)( " tests run; ", poutCtx);
230 (*output)( NumToString(nTestsFailed, StringStorage), poutCtx);
231 (*output)( " tests failed\n", poutCtx);
232 }
233
234 return nTestsFailed;
235}