blob: ef5a16f0e90a86bf779fe647046546daac9d6835 [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 Lundblade1ef8b2d2018-12-14 23:13:34 -0800161 TEST_ENTRY(EncodeErrorTests),
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700162 //TEST_ENTRY(fail_test),
163};
164
165
Laurence Lundblade88b6ece2018-12-04 12:27:19 +0900166int run_tests(const char *szTestName, outputstring output, void *poutCtx, int *pNumTestsRun)
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700167{
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);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800174
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530175 for(t2 = s_tests2; t2 < s_tests2_end; t2++) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800176 if(!t2->bEnabled && !szTestName) {
177 // Don't run disabled tests when all tests are being run
178 // as indicated by no specific test name being given
179 continue;
180 }
Laurence Lundblade88b6ece2018-12-04 12:27:19 +0900181 if(szTestName && strcmp(szTestName, t2->szTestName)) {
182 continue;
183 }
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800184 const char * szTestResult = (t2->test_fun)();
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530185 nTestsRun++;
186 if(output) {
187 (*output)(t2->szTestName, poutCtx);
188 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800189
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800190 if(szTestResult) {
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530191 if(output) {
192 (*output)(" FAILED (returned ", poutCtx);
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800193 (*output)(szTestResult, poutCtx);
Laurence Lundbladedc6e28e2018-10-11 19:19:27 +0530194 (*output)(")\n", poutCtx);
195 }
196 nTestsFailed++;
197 } else {
198 if(output) {
199 (*output)( " PASSED\n", poutCtx);
200 }
201 }
202 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800203
204
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700205 test_entry *t;
206 const test_entry *s_tests_end = s_tests + sizeof(s_tests)/sizeof(test_entry);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800207
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700208 for(t = s_tests; t < s_tests_end; t++) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800209 if(!t->bEnabled && !szTestName) {
210 // Don't run disabled tests when all tests are being run
211 // as indicated by no specific test name being given
212 continue;
213 }
Laurence Lundblade88b6ece2018-12-04 12:27:19 +0900214 if(szTestName && strcmp(szTestName, t->szTestName)) {
215 continue;
216 }
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800217 int nTestResult = (t->test_fun)();
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700218 nTestsRun++;
219 if(output) {
220 (*output)(t->szTestName, poutCtx);
221 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800222
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800223 if(nTestResult) {
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700224 if(output) {
225 (*output)(" FAILED (returned ", poutCtx);
Laurence Lundbladea2e29072018-12-30 09:20:06 -0800226 (*output)(NumToString(nTestResult, StringStorage), poutCtx);
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700227 (*output)(")\n", poutCtx);
228 }
229 nTestsFailed++;
230 } else {
231 if(output) {
232 (*output)( " PASSED\n", poutCtx);
233 }
234 }
235 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800236
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700237 if(pNumTestsRun) {
238 *pNumTestsRun = nTestsRun;
239 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800240
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700241 if(output) {
242 (*output)( "SUMMARY: ", poutCtx);
243 (*output)( NumToString(nTestsRun, StringStorage), poutCtx);
244 (*output)( " tests run; ", poutCtx);
245 (*output)( NumToString(nTestsFailed, StringStorage), poutCtx);
246 (*output)( " tests failed\n", poutCtx);
247 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800248
Laurence Lundbladef156fb82018-10-01 09:47:03 -0700249 return nTestsFailed;
250}