blob: d1f997b354ae72a0de7bb0539a0f91267802f015 [file] [log] [blame]
Laurence Lundbladef156fb82018-10-01 09:47:03 -07001
2/*==============================================================================
3 run_tests.c -- test aggregator and results reporting
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004
Laurence Lundbladed92a6162018-11-01 11:38:35 +07005 Copyright (c) 2018, Laurence Lundblade.
6 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08007
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 Lundblade3aee3a32018-12-17 16:17:45 -080020
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 Lundbladef156fb82018-10-01 09:47:03 -070044
45// Used to test the test runner
46int fail_test()
47{
48 return -44;
49}
50
51
52/*
53 Convert a number up to 999999999 to a string. This is so sprintf doesn't
54 have to be linked in so as to minimized dependencies even in test code.
Laurence Lundblade1544c482018-12-05 20:52:35 +090055 */
Laurence Lundbladef156fb82018-10-01 09:47:03 -070056const char *NumToString(int32_t nNum, UsefulBuf StringMem)
57{
Laurence Lundblade570fab52018-10-13 18:28:27 +080058 const int32_t nMax = 1000000000;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080059
Laurence Lundbladef156fb82018-10-01 09:47:03 -070060 UsefulOutBuf OutBuf;
61 UsefulOutBuf_Init(&OutBuf, StringMem);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080062
Laurence Lundbladef156fb82018-10-01 09:47:03 -070063 if(nNum < 0) {
64 UsefulOutBuf_AppendByte(&OutBuf, '-');
65 nNum = -nNum;
66 }
Laurence Lundblade570fab52018-10-13 18:28:27 +080067 if(nNum > nMax-1) {
Laurence Lundbladef156fb82018-10-01 09:47:03 -070068 return "XXX";
69 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080070
Laurence Lundbladef156fb82018-10-01 09:47:03 -070071 bool bDidSomeOutput = false;
Laurence Lundblade570fab52018-10-13 18:28:27 +080072 for(int n = nMax; n > 0; n/=10) {
Laurence Lundbladef156fb82018-10-01 09:47:03 -070073 int x = nNum/n;
74 if(x || bDidSomeOutput){
75 bDidSomeOutput = true;
76 UsefulOutBuf_AppendByte(&OutBuf, '0' + x);
77 nNum -= x * n;
78 }
79 }
80 if(!bDidSomeOutput){
81 UsefulOutBuf_AppendByte(&OutBuf, '0');
82 }
83 UsefulOutBuf_AppendByte(&OutBuf, '\0');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080084
Laurence Lundbladef156fb82018-10-01 09:47:03 -070085 return UsefulOutBuf_GetError(&OutBuf) ? "" : StringMem.ptr;
86}
87
88
89
90typedef int (test_fun_t)(void);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +053091typedef const char * (test_fun2_t)(void);
92
Laurence Lundbladef156fb82018-10-01 09:47:03 -070093
Laurence Lundbladea2e29072018-12-30 09:20:06 -080094#define TEST_ENTRY(test_name) {#test_name, test_name, true}
95#define TEST_ENTRY_DISABLED(test_name) {#test_name, test_name, false}
96
Laurence Lundbladef156fb82018-10-01 09:47:03 -070097typedef struct {
Laurence Lundbladea2e29072018-12-30 09:20:06 -080098 const char *szTestName;
Laurence Lundbladef156fb82018-10-01 09:47:03 -070099 test_fun_t *test_fun;
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800100 bool bEnabled;
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700101} test_entry;
102
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530103typedef struct {
104 const char *szTestName;
105 test_fun2_t *test_fun;
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800106 bool bEnabled;
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530107} 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 Lundbladeccfb8cd2018-12-07 21:11:30 +0900121 TEST_ENTRY(ParseMapAsArrayTest),
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 Lundbladedc6e28e2018-10-11 19:19:27 +0530127 TEST_ENTRY(RTICResultsTest),
128 TEST_ENTRY(MapEncodeTest),
129 TEST_ENTRY(ArrayNestingTest1),
130 TEST_ENTRY(ArrayNestingTest2),
131 TEST_ENTRY(ArrayNestingTest3),
132 TEST_ENTRY(EncodeDateTest),
133 TEST_ENTRY(SimpleValuesTest1),
134 TEST_ENTRY(IntegerValuesTest1),
135 TEST_ENTRY(AllAddMethodsTest),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800136 TEST_ENTRY(ParseTooDeepArrayTest),
137 TEST_ENTRY(ComprehensiveInputTest),
138 TEST_ENTRY(ParseMapTest),
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530139 TEST_ENTRY(IndefiniteLengthArrayMapTest),
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530140 TEST_ENTRY(BasicEncodeTest),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800141 TEST_ENTRY(NestedMapTest),
142 TEST_ENTRY(BignumParseTest),
143 TEST_ENTRY(OptTagParseTest),
144 TEST_ENTRY(DateParseTest),
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800145 TEST_ENTRY(ShortBufferParseTest2),
146 TEST_ENTRY(ShortBufferParseTest),
147 TEST_ENTRY(ParseDeepArrayTest),
148 TEST_ENTRY(SimpleArrayTest),
149 TEST_ENTRY(IntegerValuesParseTest),
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530150 TEST_ENTRY(MemPoolTest),
151 TEST_ENTRY(IndefiniteLengthStringTest),
Laurence Lundbladebb474be2018-10-22 11:53:21 +0530152 TEST_ENTRY(HalfPrecisionDecodeBasicTests),
Laurence Lundbladebb474be2018-10-22 11:53:21 +0530153 TEST_ENTRY(DoubleAsSmallestTest),
154 TEST_ENTRY(HalfPrecisionAgainstRFCCodeTest),
Laurence Lundblade369b90a2018-10-22 02:04:37 +0530155 TEST_ENTRY(BstrWrapTest),
156 TEST_ENTRY(BstrWrapErrorTest),
157 TEST_ENTRY(BstrWrapNestTest),
158 TEST_ENTRY(CoseSign1TBSTest),
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800159 TEST_ENTRY(StringDecoderModeFailTest),
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800160 TEST_ENTRY_DISABLED(BigComprehensiveInputTest),
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700161 //TEST_ENTRY(fail_test),
162};
163
164
Laurence Lundblade88b6ece2018-12-04 12:27:19 +0900165int run_tests(const char *szTestName, outputstring output, void *poutCtx, int *pNumTestsRun)
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700166{
167 int nTestsFailed = 0;
168 int nTestsRun = 0;
Laurence Lundblade4fe9f312018-10-22 10:22:39 +0530169 UsefulBuf_MAKE_STACK_UB(StringStorage, 5);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530170
171 test_entry2 *t2;
172 const test_entry2 *s_tests2_end = s_tests2 + sizeof(s_tests2)/sizeof(test_entry2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800173
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530174 for(t2 = s_tests2; t2 < s_tests2_end; t2++) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800175 if(!t2->bEnabled && !szTestName) {
176 // Don't run disabled tests when all tests are being run
177 // as indicated by no specific test name being given
178 continue;
179 }
Laurence Lundblade88b6ece2018-12-04 12:27:19 +0900180 if(szTestName && strcmp(szTestName, t2->szTestName)) {
181 continue;
182 }
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800183 const char * szTestResult = (t2->test_fun)();
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530184 nTestsRun++;
185 if(output) {
186 (*output)(t2->szTestName, poutCtx);
187 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800188
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800189 if(szTestResult) {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530190 if(output) {
191 (*output)(" FAILED (returned ", poutCtx);
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800192 (*output)(szTestResult, poutCtx);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530193 (*output)(")\n", poutCtx);
194 }
195 nTestsFailed++;
196 } else {
197 if(output) {
198 (*output)( " PASSED\n", poutCtx);
199 }
200 }
201 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800202
203
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700204 test_entry *t;
205 const test_entry *s_tests_end = s_tests + sizeof(s_tests)/sizeof(test_entry);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800206
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700207 for(t = s_tests; t < s_tests_end; t++) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800208 if(!t->bEnabled && !szTestName) {
209 // Don't run disabled tests when all tests are being run
210 // as indicated by no specific test name being given
211 continue;
212 }
Laurence Lundblade88b6ece2018-12-04 12:27:19 +0900213 if(szTestName && strcmp(szTestName, t->szTestName)) {
214 continue;
215 }
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800216 int nTestResult = (t->test_fun)();
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700217 nTestsRun++;
218 if(output) {
219 (*output)(t->szTestName, poutCtx);
220 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800221
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800222 if(nTestResult) {
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700223 if(output) {
224 (*output)(" FAILED (returned ", poutCtx);
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800225 (*output)(NumToString(nTestResult, StringStorage), poutCtx);
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700226 (*output)(")\n", poutCtx);
227 }
228 nTestsFailed++;
229 } else {
230 if(output) {
231 (*output)( " PASSED\n", poutCtx);
232 }
233 }
234 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800235
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700236 if(pNumTestsRun) {
237 *pNumTestsRun = nTestsRun;
238 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800239
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700240 if(output) {
241 (*output)( "SUMMARY: ", poutCtx);
242 (*output)( NumToString(nTestsRun, StringStorage), poutCtx);
243 (*output)( " tests run; ", poutCtx);
244 (*output)( NumToString(nTestsFailed, StringStorage), poutCtx);
245 (*output)( " tests failed\n", poutCtx);
246 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800247
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700248 return nTestsFailed;
249}