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