Laurence Lundblade | 6ed3422 | 2018-12-18 09:46:23 -0800 | [diff] [blame] | 1 | /*============================================================================== |
| 2 | run_tests.c -- test aggregator and results reporting |
| 3 | |
| 4 | Copyright (c) 2018-2019, Laurence Lundblade. All rights reserved. |
| 5 | |
| 6 | SPDX-License-Identifier: BSD-3-Clause |
| 7 | |
| 8 | See BSD-3-Clause license in README.md |
| 9 | |
| 10 | Created on 9/30/18 |
| 11 | ==============================================================================*/ |
| 12 | |
| 13 | #include "run_tests.h" |
| 14 | #include "UsefulBuf.h" |
| 15 | #include <stdbool.h> |
| 16 | |
| 17 | #include "float_tests.h" |
| 18 | #include "qcbor_decode_tests.h" |
| 19 | #include "qcbor_encode_tests.h" |
| 20 | #include "UsefulBuf_Tests.h" |
| 21 | |
| 22 | |
Laurence Lundblade | 6ed3422 | 2018-12-18 09:46:23 -0800 | [diff] [blame] | 23 | /* |
Laurence Lundblade | 234fe42 | 2019-12-02 13:04:34 -0800 | [diff] [blame^] | 24 | Test configuration |
| 25 | */ |
Laurence Lundblade | 6ed3422 | 2018-12-18 09:46:23 -0800 | [diff] [blame] | 26 | |
| 27 | typedef int (test_fun_t)(void); |
| 28 | typedef const char * (test_fun2_t)(void); |
| 29 | |
| 30 | |
| 31 | #define TEST_ENTRY(test_name) {#test_name, test_name, true} |
| 32 | #define TEST_ENTRY_DISABLED(test_name) {#test_name, test_name, false} |
| 33 | |
| 34 | typedef struct { |
| 35 | const char *szTestName; |
| 36 | test_fun_t *test_fun; |
| 37 | bool bEnabled; |
| 38 | } test_entry; |
| 39 | |
| 40 | typedef struct { |
| 41 | const char *szTestName; |
| 42 | test_fun2_t *test_fun; |
| 43 | bool bEnabled; |
| 44 | } test_entry2; |
| 45 | |
Laurence Lundblade | 234fe42 | 2019-12-02 13:04:34 -0800 | [diff] [blame^] | 46 | |
| 47 | static test_entry2 s_tests2[] = { |
Laurence Lundblade | 6ed3422 | 2018-12-18 09:46:23 -0800 | [diff] [blame] | 48 | TEST_ENTRY(UBUTest_CopyUtil), |
| 49 | TEST_ENTRY(UOBTest_NonAdversarial), |
| 50 | TEST_ENTRY(TestBasicSanity), |
| 51 | TEST_ENTRY(UOBTest_BoundaryConditionsTest), |
| 52 | TEST_ENTRY(UBMacroConversionsTest), |
| 53 | TEST_ENTRY(UBUtilTests), |
| 54 | TEST_ENTRY(UIBTest_IntegerFormat) |
| 55 | }; |
| 56 | |
| 57 | |
Laurence Lundblade | 234fe42 | 2019-12-02 13:04:34 -0800 | [diff] [blame^] | 58 | static test_entry s_tests[] = { |
| 59 | TEST_ENTRY(EmptyMapsAndArraysTest), |
| 60 | TEST_ENTRY(NotWellFormedTests), |
Laurence Lundblade | 6ed3422 | 2018-12-18 09:46:23 -0800 | [diff] [blame] | 61 | TEST_ENTRY(ParseMapAsArrayTest), |
Laurence Lundblade | d425fb3 | 2019-02-18 10:56:18 -0800 | [diff] [blame] | 62 | TEST_ENTRY(AllocAllStringsTest), |
Laurence Lundblade | 6ed3422 | 2018-12-18 09:46:23 -0800 | [diff] [blame] | 63 | TEST_ENTRY(IndefiniteLengthNestTest), |
| 64 | TEST_ENTRY(NestedMapTestIndefLen), |
| 65 | TEST_ENTRY(ParseSimpleTest), |
Laurence Lundblade | 234fe42 | 2019-12-02 13:04:34 -0800 | [diff] [blame^] | 66 | TEST_ENTRY(DecodeFailureTests), |
Laurence Lundblade | 6ed3422 | 2018-12-18 09:46:23 -0800 | [diff] [blame] | 67 | TEST_ENTRY(EncodeRawTest), |
| 68 | TEST_ENTRY(RTICResultsTest), |
| 69 | TEST_ENTRY(MapEncodeTest), |
| 70 | TEST_ENTRY(ArrayNestingTest1), |
| 71 | TEST_ENTRY(ArrayNestingTest2), |
| 72 | TEST_ENTRY(ArrayNestingTest3), |
| 73 | TEST_ENTRY(EncodeDateTest), |
| 74 | TEST_ENTRY(SimpleValuesTest1), |
| 75 | TEST_ENTRY(IntegerValuesTest1), |
| 76 | TEST_ENTRY(AllAddMethodsTest), |
| 77 | TEST_ENTRY(ParseTooDeepArrayTest), |
| 78 | TEST_ENTRY(ComprehensiveInputTest), |
| 79 | TEST_ENTRY(ParseMapTest), |
Laurence Lundblade | d425fb3 | 2019-02-18 10:56:18 -0800 | [diff] [blame] | 80 | TEST_ENTRY(IndefiniteLengthArrayMapTest), |
Laurence Lundblade | 6ed3422 | 2018-12-18 09:46:23 -0800 | [diff] [blame] | 81 | TEST_ENTRY(BasicEncodeTest), |
| 82 | TEST_ENTRY(NestedMapTest), |
| 83 | TEST_ENTRY(BignumParseTest), |
| 84 | TEST_ENTRY(OptTagParseTest), |
| 85 | TEST_ENTRY(DateParseTest), |
| 86 | TEST_ENTRY(ShortBufferParseTest2), |
| 87 | TEST_ENTRY(ShortBufferParseTest), |
| 88 | TEST_ENTRY(ParseDeepArrayTest), |
| 89 | TEST_ENTRY(SimpleArrayTest), |
| 90 | TEST_ENTRY(IntegerValuesParseTest), |
Laurence Lundblade | d425fb3 | 2019-02-18 10:56:18 -0800 | [diff] [blame] | 91 | TEST_ENTRY(MemPoolTest), |
| 92 | TEST_ENTRY(IndefiniteLengthStringTest), |
Laurence Lundblade | 6ed3422 | 2018-12-18 09:46:23 -0800 | [diff] [blame] | 93 | TEST_ENTRY(HalfPrecisionDecodeBasicTests), |
| 94 | TEST_ENTRY(DoubleAsSmallestTest), |
| 95 | TEST_ENTRY(HalfPrecisionAgainstRFCCodeTest), |
| 96 | TEST_ENTRY(BstrWrapTest), |
| 97 | TEST_ENTRY(BstrWrapErrorTest), |
| 98 | TEST_ENTRY(BstrWrapNestTest), |
| 99 | TEST_ENTRY(CoseSign1TBSTest), |
| 100 | TEST_ENTRY(StringDecoderModeFailTest), |
| 101 | TEST_ENTRY_DISABLED(BigComprehensiveInputTest), |
| 102 | TEST_ENTRY(EncodeErrorTests), |
Laurence Lundblade | d425fb3 | 2019-02-18 10:56:18 -0800 | [diff] [blame] | 103 | TEST_ENTRY(SetUpAllocatorTest), |
Laurence Lundblade | 234fe42 | 2019-12-02 13:04:34 -0800 | [diff] [blame^] | 104 | TEST_ENTRY(SimpleValuesIndefiniteLengthTest1), |
| 105 | TEST_ENTRY(EncodeLengthThirtyoneTest), |
Laurence Lundblade | 6ed3422 | 2018-12-18 09:46:23 -0800 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | |
Laurence Lundblade | 234fe42 | 2019-12-02 13:04:34 -0800 | [diff] [blame^] | 109 | |
| 110 | |
| 111 | /* |
| 112 | Convert a number up to 999999999 to a string. This is so sprintf doesn't |
| 113 | have to be linked in so as to minimized dependencies even in test code. |
| 114 | |
| 115 | StringMem should be 12 bytes long, 9 for digits, 1 for minus and |
| 116 | 1 for \0 termination. |
| 117 | */ |
| 118 | static const char *NumToString(int32_t nNum, UsefulBuf StringMem) |
| 119 | { |
| 120 | const int32_t nMax = 1000000000; |
| 121 | |
| 122 | UsefulOutBuf OutBuf; |
| 123 | UsefulOutBuf_Init(&OutBuf, StringMem); |
| 124 | |
| 125 | if(nNum < 0) { |
| 126 | UsefulOutBuf_AppendByte(&OutBuf, '-'); |
| 127 | nNum = -nNum; |
| 128 | } |
| 129 | if(nNum > nMax-1) { |
| 130 | return "XXX"; |
| 131 | } |
| 132 | |
| 133 | bool bDidSomeOutput = false; |
| 134 | for(int n = nMax; n > 0; n/=10) { |
| 135 | int x = nNum/n; |
| 136 | if(x || bDidSomeOutput){ |
| 137 | bDidSomeOutput = true; |
| 138 | UsefulOutBuf_AppendByte(&OutBuf, '0' + x); |
| 139 | nNum -= x * n; |
| 140 | } |
| 141 | } |
| 142 | if(!bDidSomeOutput){ |
| 143 | UsefulOutBuf_AppendByte(&OutBuf, '0'); |
| 144 | } |
| 145 | UsefulOutBuf_AppendByte(&OutBuf, '\0'); |
| 146 | |
| 147 | return UsefulOutBuf_GetError(&OutBuf) ? "" : StringMem.ptr; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | /* |
| 152 | Public function. See run_test.h. |
| 153 | */ |
Laurence Lundblade | 6ed3422 | 2018-12-18 09:46:23 -0800 | [diff] [blame] | 154 | int RunTests(const char *szTestNames[], OutputStringCB pfOutput, void *poutCtx, int *pNumTestsRun) |
| 155 | { |
| 156 | int nTestsFailed = 0; |
| 157 | int nTestsRun = 0; |
Laurence Lundblade | 234fe42 | 2019-12-02 13:04:34 -0800 | [diff] [blame^] | 158 | UsefulBuf_MAKE_STACK_UB(StringStorage, 12); |
Laurence Lundblade | 6ed3422 | 2018-12-18 09:46:23 -0800 | [diff] [blame] | 159 | |
| 160 | test_entry2 *t2; |
| 161 | const test_entry2 *s_tests2_end = s_tests2 + sizeof(s_tests2)/sizeof(test_entry2); |
| 162 | |
| 163 | for(t2 = s_tests2; t2 < s_tests2_end; t2++) { |
| 164 | if(szTestNames[0]) { |
| 165 | // Some tests have been named |
| 166 | const char **szRequestedNames; |
| 167 | for(szRequestedNames = szTestNames; *szRequestedNames; szRequestedNames++) { |
| 168 | if(!strcmp(t2->szTestName, *szRequestedNames)) { |
| 169 | break; // Name matched |
| 170 | } |
| 171 | } |
| 172 | if(*szRequestedNames == NULL) { |
| 173 | // Didn't match this test |
| 174 | continue; |
| 175 | } |
| 176 | } else { |
| 177 | // no tests named, but don't run "disabled" tests |
| 178 | if(!t2->bEnabled) { |
| 179 | // Don't run disabled tests when all tests are being run |
| 180 | // as indicated by no specific test names being given |
| 181 | continue; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | const char * szTestResult = (t2->test_fun)(); |
| 186 | nTestsRun++; |
| 187 | if(pfOutput) { |
| 188 | (*pfOutput)(t2->szTestName, poutCtx, 0); |
| 189 | } |
| 190 | |
| 191 | if(szTestResult) { |
| 192 | if(pfOutput) { |
| 193 | (*pfOutput)(" FAILED (returned ", poutCtx, 0); |
| 194 | (*pfOutput)(szTestResult, poutCtx, 0); |
| 195 | (*pfOutput)(")", poutCtx, 1); |
| 196 | } |
| 197 | nTestsFailed++; |
| 198 | } else { |
| 199 | if(pfOutput) { |
| 200 | (*pfOutput)( " PASSED", poutCtx, 1); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | |
| 206 | test_entry *t; |
| 207 | const test_entry *s_tests_end = s_tests + sizeof(s_tests)/sizeof(test_entry); |
| 208 | |
| 209 | for(t = s_tests; t < s_tests_end; t++) { |
| 210 | if(szTestNames[0]) { |
| 211 | // Some tests have been named |
| 212 | const char **szRequestedNames; |
| 213 | for(szRequestedNames = szTestNames; *szRequestedNames; szRequestedNames++) { |
| 214 | if(!strcmp(t->szTestName, *szRequestedNames)) { |
| 215 | break; // Name matched |
| 216 | } |
| 217 | } |
| 218 | if(*szRequestedNames == NULL) { |
| 219 | // Didn't match this test |
| 220 | continue; |
| 221 | } |
| 222 | } else { |
| 223 | // no tests named, but don't run "disabled" tests |
| 224 | if(!t->bEnabled) { |
| 225 | // Don't run disabled tests when all tests are being run |
| 226 | // as indicated by no specific test names being given |
| 227 | continue; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | int nTestResult = (t->test_fun)(); |
| 232 | nTestsRun++; |
| 233 | if(pfOutput) { |
| 234 | (*pfOutput)(t->szTestName, poutCtx, 0); |
| 235 | } |
| 236 | |
| 237 | if(nTestResult) { |
| 238 | if(pfOutput) { |
| 239 | (*pfOutput)(" FAILED (returned ", poutCtx, 0); |
| 240 | (*pfOutput)(NumToString(nTestResult, StringStorage), poutCtx, 0); |
| 241 | (*pfOutput)(")", poutCtx, 1); |
| 242 | } |
| 243 | nTestsFailed++; |
| 244 | } else { |
| 245 | if(pfOutput) { |
| 246 | (*pfOutput)( " PASSED", poutCtx, 1); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | if(pNumTestsRun) { |
| 252 | *pNumTestsRun = nTestsRun; |
| 253 | } |
| 254 | |
| 255 | if(pfOutput) { |
| 256 | (*pfOutput)( "SUMMARY: ", poutCtx, 0); |
| 257 | (*pfOutput)( NumToString(nTestsRun, StringStorage), poutCtx, 0); |
| 258 | (*pfOutput)( " tests run; ", poutCtx, 0); |
| 259 | (*pfOutput)( NumToString(nTestsFailed, StringStorage), poutCtx, 0); |
| 260 | (*pfOutput)( " tests failed", poutCtx, 1); |
| 261 | } |
| 262 | |
| 263 | return nTestsFailed; |
| 264 | } |
| 265 | |
| 266 | |
Laurence Lundblade | 234fe42 | 2019-12-02 13:04:34 -0800 | [diff] [blame^] | 267 | #include "qcbor.h" // For size printing |
Laurence Lundblade | 6ed3422 | 2018-12-18 09:46:23 -0800 | [diff] [blame] | 268 | |
Laurence Lundblade | 234fe42 | 2019-12-02 13:04:34 -0800 | [diff] [blame^] | 269 | /* |
| 270 | Public function. See run_test.h. |
| 271 | */ |
Laurence Lundblade | 6ed3422 | 2018-12-18 09:46:23 -0800 | [diff] [blame] | 272 | static void PrintSize(const char *szWhat, uint32_t uSize, OutputStringCB pfOutput, void *pOutCtx) |
| 273 | { |
| 274 | UsefulBuf_MAKE_STACK_UB(buffer, 20); |
| 275 | |
| 276 | (*pfOutput)(szWhat, pOutCtx, 0); |
| 277 | (*pfOutput)(" ", pOutCtx, 0); |
| 278 | (*pfOutput)(NumToString(uSize, buffer), pOutCtx, 0); |
| 279 | (*pfOutput)("", pOutCtx, 1); |
| 280 | } |
| 281 | |
Laurence Lundblade | 234fe42 | 2019-12-02 13:04:34 -0800 | [diff] [blame^] | 282 | |
| 283 | /* |
| 284 | Public function. See run_test.h. |
| 285 | */ |
Laurence Lundblade | 6ed3422 | 2018-12-18 09:46:23 -0800 | [diff] [blame] | 286 | void PrintSizes(OutputStringCB pfOutput, void *pOutCtx) |
| 287 | { |
| 288 | // Type and size of return from sizeof() varies. These will never be large so cast is safe |
| 289 | PrintSize("sizeof(QCBORTrackNesting)", (uint32_t)sizeof(QCBORTrackNesting), pfOutput, pOutCtx); |
| 290 | PrintSize("sizeof(QCBOREncodeContext)", (uint32_t)sizeof(QCBOREncodeContext), pfOutput, pOutCtx); |
| 291 | PrintSize("sizeof(QCBORDecodeNesting)", (uint32_t)sizeof(QCBORDecodeNesting), pfOutput, pOutCtx); |
| 292 | PrintSize("sizeof(QCBORDecodeContext)", (uint32_t)sizeof(QCBORDecodeContext), pfOutput, pOutCtx); |
| 293 | PrintSize("sizeof(QCBORItem)", (uint32_t)sizeof(QCBORItem), pfOutput, pOutCtx); |
Laurence Lundblade | 6ed3422 | 2018-12-18 09:46:23 -0800 | [diff] [blame] | 294 | PrintSize("sizeof(QCBORTagListIn)", (uint32_t)sizeof(QCBORTagListIn), pfOutput, pOutCtx); |
| 295 | PrintSize("sizeof(QCBORTagListOut)", (uint32_t)sizeof(QCBORTagListOut), pfOutput, pOutCtx); |
| 296 | (*pfOutput)("", pOutCtx, 1); |
| 297 | } |