blob: eabce48b3e756150966708ee3b49267886b1f561 [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 Lundbladed92a6162018-11-01 11:38:35 +07008 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are
10 met:
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 * Neither the name of The Linux Foundation nor the names of its
18 contributors, nor the name "Laurence Lundblade" may be used to
19 endorse or promote products derived from this software without
20 specific prior written permission.
Laurence Lundbladef156fb82018-10-01 09:47:03 -070021
Laurence Lundbladed92a6162018-11-01 11:38:35 +070022 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
23 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
25 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
26 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladef156fb82018-10-01 09:47:03 -070033 ==============================================================================*/
34// Created by Laurence Lundblade on 9/30/18.
35
36
37#include "run_tests.h"
38#include "UsefulBuf.h"
39#include <stdbool.h>
40
Laurence Lundblade2d85ce42018-10-12 14:12:47 +080041#include "float_tests.h"
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080042#include "qcbor_decode_tests.h"
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053043#include "qcbor_encode_tests.h"
44#include "UsefulBuf_Tests.h"
Laurence Lundblade471a3fd2018-10-18 21:27:45 +053045#include "qcbor_decode_malloc_tests.h"
Laurence Lundbladef156fb82018-10-01 09:47:03 -070046
47// Used to test the test runner
48int fail_test()
49{
50 return -44;
51}
52
53
54/*
55 Convert a number up to 999999999 to a string. This is so sprintf doesn't
56 have to be linked in so as to minimized dependencies even in test code.
57
58 This function does pointer math. TODO: test this.
59 */
60const char *NumToString(int32_t nNum, UsefulBuf StringMem)
61{
Laurence Lundblade570fab52018-10-13 18:28:27 +080062 const int32_t nMax = 1000000000;
Laurence Lundbladef156fb82018-10-01 09:47:03 -070063
64 UsefulOutBuf OutBuf;
65 UsefulOutBuf_Init(&OutBuf, StringMem);
66
67 if(nNum < 0) {
68 UsefulOutBuf_AppendByte(&OutBuf, '-');
69 nNum = -nNum;
70 }
Laurence Lundblade570fab52018-10-13 18:28:27 +080071 if(nNum > nMax-1) {
Laurence Lundbladef156fb82018-10-01 09:47:03 -070072 return "XXX";
73 }
74
75 bool bDidSomeOutput = false;
Laurence Lundblade570fab52018-10-13 18:28:27 +080076 for(int n = nMax; n > 0; n/=10) {
Laurence Lundbladef156fb82018-10-01 09:47:03 -070077 int x = nNum/n;
78 if(x || bDidSomeOutput){
79 bDidSomeOutput = true;
80 UsefulOutBuf_AppendByte(&OutBuf, '0' + x);
81 nNum -= x * n;
82 }
83 }
84 if(!bDidSomeOutput){
85 UsefulOutBuf_AppendByte(&OutBuf, '0');
86 }
87 UsefulOutBuf_AppendByte(&OutBuf, '\0');
88
89 return UsefulOutBuf_GetError(&OutBuf) ? "" : StringMem.ptr;
90}
91
92
93
94typedef int (test_fun_t)(void);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053095typedef const char * (test_fun2_t)(void);
96
Laurence Lundbladef156fb82018-10-01 09:47:03 -070097
98#define TEST_ENTRY(test_name) {#test_name, test_name}
99typedef struct {
100 const char *szTestName;
101 test_fun_t *test_fun;
102} test_entry;
103
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530104typedef struct {
105 const char *szTestName;
106 test_fun2_t *test_fun;
107} test_entry2;
108
109test_entry2 s_tests2[] = {
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800110 TEST_ENTRY(UBUTest_CopyUtil),
111 TEST_ENTRY(UOBTest_NonAdversarial),
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530112 TEST_ENTRY(TestBasicSanity),
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800113 TEST_ENTRY(UOBTest_BoundaryConditionsTest),
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530114 TEST_ENTRY(UBMacroConversionsTest),
115 TEST_ENTRY(UBUtilTests),
Laurence Lundblade7566b9f2018-10-12 09:13:32 +0800116 TEST_ENTRY(UIBTest_IntegerFormat)
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530117};
118
119
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700120test_entry s_tests[] = {
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530121 TEST_ENTRY(MallocAllStringsTest),
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530122 TEST_ENTRY(AllocAllStringsTest),
123 TEST_ENTRY(IndefiniteLengthNestTest),
124 TEST_ENTRY(NestedMapTestIndefLen),
125 TEST_ENTRY(ParseSimpleTest),
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800126 TEST_ENTRY(EncodeRawTest),
Laurence Lundblade2d85ce42018-10-12 14:12:47 +0800127 TEST_ENTRY(FloatValuesTest1),
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530128 TEST_ENTRY(RTICResultsTest),
129 TEST_ENTRY(MapEncodeTest),
130 TEST_ENTRY(ArrayNestingTest1),
131 TEST_ENTRY(ArrayNestingTest2),
132 TEST_ENTRY(ArrayNestingTest3),
133 TEST_ENTRY(EncodeDateTest),
134 TEST_ENTRY(SimpleValuesTest1),
135 TEST_ENTRY(IntegerValuesTest1),
136 TEST_ENTRY(AllAddMethodsTest),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800137 TEST_ENTRY(ParseTooDeepArrayTest),
138 TEST_ENTRY(ComprehensiveInputTest),
139 TEST_ENTRY(ParseMapTest),
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530140 TEST_ENTRY(IndefiniteLengthArrayMapTest),
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530141 TEST_ENTRY(BasicEncodeTest),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800142 TEST_ENTRY(NestedMapTest),
143 TEST_ENTRY(BignumParseTest),
144 TEST_ENTRY(OptTagParseTest),
145 TEST_ENTRY(DateParseTest),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800146 TEST_ENTRY(ShortBufferParseTest2),
147 TEST_ENTRY(ShortBufferParseTest),
148 TEST_ENTRY(ParseDeepArrayTest),
149 TEST_ENTRY(SimpleArrayTest),
150 TEST_ENTRY(IntegerValuesParseTest),
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530151 TEST_ENTRY(MemPoolTest),
152 TEST_ENTRY(IndefiniteLengthStringTest),
Laurence Lundbladebb474be2018-10-22 11:53:21 +0530153 TEST_ENTRY(HalfPrecisionEncodeBasicTests),
154 TEST_ENTRY(HalfPrecisionDecodeBasicTests),
155 TEST_ENTRY(HalfPrecisionTransitiveTest),
156 TEST_ENTRY(DoubleAsSmallestTest),
157 TEST_ENTRY(HalfPrecisionAgainstRFCCodeTest),
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530158 TEST_ENTRY(BstrWrapTest),
159 TEST_ENTRY(BstrWrapErrorTest),
160 TEST_ENTRY(BstrWrapNestTest),
161 TEST_ENTRY(CoseSign1TBSTest),
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700162 //TEST_ENTRY(fail_test),
163};
164
165
166int run_tests(outputstring output, void *poutCtx, int *pNumTestsRun)
167{
168 int nTestsFailed = 0;
169 int nTestsRun = 0;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530170 UsefulBuf_MAKE_STACK_UB(StringStorage, 5);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530171
172 test_entry2 *t2;
173 const test_entry2 *s_tests2_end = s_tests2 + sizeof(s_tests2)/sizeof(test_entry2);
174
175 for(t2 = s_tests2; t2 < s_tests2_end; t2++) {
176 const char * x = (t2->test_fun)();
177 nTestsRun++;
178 if(output) {
179 (*output)(t2->szTestName, poutCtx);
180 }
181
182 if(x) {
183 if(output) {
184 (*output)(" FAILED (returned ", poutCtx);
185 (*output)(x, poutCtx);
186 (*output)(")\n", poutCtx);
187 }
188 nTestsFailed++;
189 } else {
190 if(output) {
191 (*output)( " PASSED\n", poutCtx);
192 }
193 }
194 }
195
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700196
197 test_entry *t;
198 const test_entry *s_tests_end = s_tests + sizeof(s_tests)/sizeof(test_entry);
199
200 for(t = s_tests; t < s_tests_end; t++) {
201 int x = (t->test_fun)();
202 nTestsRun++;
203 if(output) {
204 (*output)(t->szTestName, poutCtx);
205 }
206
207 if(x) {
208 if(output) {
209 (*output)(" FAILED (returned ", poutCtx);
210 (*output)(NumToString(x, StringStorage), poutCtx);
211 (*output)(")\n", poutCtx);
212 }
213 nTestsFailed++;
214 } else {
215 if(output) {
216 (*output)( " PASSED\n", poutCtx);
217 }
218 }
219 }
220
221 if(pNumTestsRun) {
222 *pNumTestsRun = nTestsRun;
223 }
224
225 if(output) {
226 (*output)( "SUMMARY: ", poutCtx);
227 (*output)( NumToString(nTestsRun, StringStorage), poutCtx);
228 (*output)( " tests run; ", poutCtx);
229 (*output)( NumToString(nTestsFailed, StringStorage), poutCtx);
230 (*output)( " tests failed\n", poutCtx);
231 }
232
233 return nTestsFailed;
234}