Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 1 | |
| 2 | /*============================================================================== |
| 3 | run_tests.c -- test aggregator and results reporting |
| 4 | |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 5 | Copyright (c) 2018, Laurence Lundblade. |
| 6 | All rights reserved. |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 7 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 8 | 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 | * 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 Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 20 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 21 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 22 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 24 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 25 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 28 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 29 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 30 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
Laurence Lundblade | 88b6ece | 2018-12-04 12:27:19 +0900 | [diff] [blame] | 31 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | ==============================================================================*/ |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 33 | // 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 Lundblade | 2d85ce4 | 2018-10-12 14:12:47 +0800 | [diff] [blame] | 40 | #include "float_tests.h" |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 41 | #include "qcbor_decode_tests.h" |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 42 | #include "qcbor_encode_tests.h" |
| 43 | #include "UsefulBuf_Tests.h" |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 44 | #include "qcbor_decode_malloc_tests.h" |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 45 | |
| 46 | // Used to test the test runner |
| 47 | int fail_test() |
| 48 | { |
| 49 | return -44; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | /* |
| 54 | Convert a number up to 999999999 to a string. This is so sprintf doesn't |
| 55 | have to be linked in so as to minimized dependencies even in test code. |
Laurence Lundblade | 1544c48 | 2018-12-05 20:52:35 +0900 | [diff] [blame] | 56 | */ |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 57 | const char *NumToString(int32_t nNum, UsefulBuf StringMem) |
| 58 | { |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 59 | const int32_t nMax = 1000000000; |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 60 | |
| 61 | UsefulOutBuf OutBuf; |
| 62 | UsefulOutBuf_Init(&OutBuf, StringMem); |
| 63 | |
| 64 | if(nNum < 0) { |
| 65 | UsefulOutBuf_AppendByte(&OutBuf, '-'); |
| 66 | nNum = -nNum; |
| 67 | } |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 68 | if(nNum > nMax-1) { |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 69 | return "XXX"; |
| 70 | } |
| 71 | |
| 72 | bool bDidSomeOutput = false; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 73 | for(int n = nMax; n > 0; n/=10) { |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 74 | int x = nNum/n; |
| 75 | if(x || bDidSomeOutput){ |
| 76 | bDidSomeOutput = true; |
| 77 | UsefulOutBuf_AppendByte(&OutBuf, '0' + x); |
| 78 | nNum -= x * n; |
| 79 | } |
| 80 | } |
| 81 | if(!bDidSomeOutput){ |
| 82 | UsefulOutBuf_AppendByte(&OutBuf, '0'); |
| 83 | } |
| 84 | UsefulOutBuf_AppendByte(&OutBuf, '\0'); |
| 85 | |
| 86 | return UsefulOutBuf_GetError(&OutBuf) ? "" : StringMem.ptr; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | |
| 91 | typedef int (test_fun_t)(void); |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 92 | typedef const char * (test_fun2_t)(void); |
| 93 | |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 94 | |
| 95 | #define TEST_ENTRY(test_name) {#test_name, test_name} |
| 96 | typedef struct { |
| 97 | const char *szTestName; |
| 98 | test_fun_t *test_fun; |
| 99 | } test_entry; |
| 100 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 101 | typedef struct { |
| 102 | const char *szTestName; |
| 103 | test_fun2_t *test_fun; |
| 104 | } test_entry2; |
| 105 | |
| 106 | test_entry2 s_tests2[] = { |
Laurence Lundblade | 7566b9f | 2018-10-12 09:13:32 +0800 | [diff] [blame] | 107 | TEST_ENTRY(UBUTest_CopyUtil), |
| 108 | TEST_ENTRY(UOBTest_NonAdversarial), |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 109 | TEST_ENTRY(TestBasicSanity), |
Laurence Lundblade | 7566b9f | 2018-10-12 09:13:32 +0800 | [diff] [blame] | 110 | TEST_ENTRY(UOBTest_BoundaryConditionsTest), |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 111 | TEST_ENTRY(UBMacroConversionsTest), |
| 112 | TEST_ENTRY(UBUtilTests), |
Laurence Lundblade | 7566b9f | 2018-10-12 09:13:32 +0800 | [diff] [blame] | 113 | TEST_ENTRY(UIBTest_IntegerFormat) |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 117 | test_entry s_tests[] = { |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 118 | TEST_ENTRY(MallocAllStringsTest), |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 119 | TEST_ENTRY(AllocAllStringsTest), |
| 120 | TEST_ENTRY(IndefiniteLengthNestTest), |
| 121 | TEST_ENTRY(NestedMapTestIndefLen), |
| 122 | TEST_ENTRY(ParseSimpleTest), |
Laurence Lundblade | 0fb6c6d | 2018-10-12 22:02:05 +0800 | [diff] [blame] | 123 | TEST_ENTRY(EncodeRawTest), |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 124 | TEST_ENTRY(RTICResultsTest), |
| 125 | TEST_ENTRY(MapEncodeTest), |
| 126 | TEST_ENTRY(ArrayNestingTest1), |
| 127 | TEST_ENTRY(ArrayNestingTest2), |
| 128 | TEST_ENTRY(ArrayNestingTest3), |
| 129 | TEST_ENTRY(EncodeDateTest), |
| 130 | TEST_ENTRY(SimpleValuesTest1), |
| 131 | TEST_ENTRY(IntegerValuesTest1), |
| 132 | TEST_ENTRY(AllAddMethodsTest), |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 133 | TEST_ENTRY(ParseTooDeepArrayTest), |
| 134 | TEST_ENTRY(ComprehensiveInputTest), |
| 135 | TEST_ENTRY(ParseMapTest), |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 136 | TEST_ENTRY(IndefiniteLengthArrayMapTest), |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 137 | TEST_ENTRY(BasicEncodeTest), |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 138 | TEST_ENTRY(NestedMapTest), |
| 139 | TEST_ENTRY(BignumParseTest), |
| 140 | TEST_ENTRY(OptTagParseTest), |
| 141 | TEST_ENTRY(DateParseTest), |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 142 | TEST_ENTRY(ShortBufferParseTest2), |
| 143 | TEST_ENTRY(ShortBufferParseTest), |
| 144 | TEST_ENTRY(ParseDeepArrayTest), |
| 145 | TEST_ENTRY(SimpleArrayTest), |
| 146 | TEST_ENTRY(IntegerValuesParseTest), |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 147 | TEST_ENTRY(MemPoolTest), |
| 148 | TEST_ENTRY(IndefiniteLengthStringTest), |
Laurence Lundblade | bb474be | 2018-10-22 11:53:21 +0530 | [diff] [blame] | 149 | TEST_ENTRY(HalfPrecisionDecodeBasicTests), |
Laurence Lundblade | bb474be | 2018-10-22 11:53:21 +0530 | [diff] [blame] | 150 | TEST_ENTRY(DoubleAsSmallestTest), |
| 151 | TEST_ENTRY(HalfPrecisionAgainstRFCCodeTest), |
Laurence Lundblade | 369b90a | 2018-10-22 02:04:37 +0530 | [diff] [blame] | 152 | TEST_ENTRY(BstrWrapTest), |
| 153 | TEST_ENTRY(BstrWrapErrorTest), |
| 154 | TEST_ENTRY(BstrWrapNestTest), |
| 155 | TEST_ENTRY(CoseSign1TBSTest), |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 156 | //TEST_ENTRY(fail_test), |
| 157 | }; |
| 158 | |
| 159 | |
Laurence Lundblade | 88b6ece | 2018-12-04 12:27:19 +0900 | [diff] [blame] | 160 | int run_tests(const char *szTestName, outputstring output, void *poutCtx, int *pNumTestsRun) |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 161 | { |
| 162 | int nTestsFailed = 0; |
| 163 | int nTestsRun = 0; |
Laurence Lundblade | 4fe9f31 | 2018-10-22 10:22:39 +0530 | [diff] [blame] | 164 | UsefulBuf_MAKE_STACK_UB(StringStorage, 5); |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 165 | |
| 166 | test_entry2 *t2; |
| 167 | const test_entry2 *s_tests2_end = s_tests2 + sizeof(s_tests2)/sizeof(test_entry2); |
| 168 | |
| 169 | for(t2 = s_tests2; t2 < s_tests2_end; t2++) { |
Laurence Lundblade | 88b6ece | 2018-12-04 12:27:19 +0900 | [diff] [blame] | 170 | if(szTestName && strcmp(szTestName, t2->szTestName)) { |
| 171 | continue; |
| 172 | } |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 173 | const char * x = (t2->test_fun)(); |
| 174 | nTestsRun++; |
| 175 | if(output) { |
| 176 | (*output)(t2->szTestName, poutCtx); |
| 177 | } |
| 178 | |
| 179 | if(x) { |
| 180 | if(output) { |
| 181 | (*output)(" FAILED (returned ", poutCtx); |
| 182 | (*output)(x, poutCtx); |
| 183 | (*output)(")\n", poutCtx); |
| 184 | } |
| 185 | nTestsFailed++; |
| 186 | } else { |
| 187 | if(output) { |
| 188 | (*output)( " PASSED\n", poutCtx); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 193 | |
| 194 | test_entry *t; |
| 195 | const test_entry *s_tests_end = s_tests + sizeof(s_tests)/sizeof(test_entry); |
| 196 | |
| 197 | for(t = s_tests; t < s_tests_end; t++) { |
Laurence Lundblade | 88b6ece | 2018-12-04 12:27:19 +0900 | [diff] [blame] | 198 | if(szTestName && strcmp(szTestName, t->szTestName)) { |
| 199 | continue; |
| 200 | } |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 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 | } |