Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 1 | |
| 2 | /*============================================================================== |
| 3 | run_tests.c -- test aggregator and results reporting |
| 4 | |
| 5 | Copyright 2018 Laurence Lundblade |
| 6 | |
| 7 | Permission is hereby granted, free of charge, to any person obtaining |
| 8 | a copy of this software and associated documentation files (the |
| 9 | "Software"), to deal in the Software without restriction, including |
| 10 | without limitation the rights to use, copy, modify, merge, publish, |
| 11 | distribute, sublicense, and/or sell copies of the Software, and to |
| 12 | permit persons to whom the Software is furnished to do so, subject to |
| 13 | the following conditions: |
| 14 | |
| 15 | The above copyright notice and this permission notice shall be included |
| 16 | in all copies or substantial portions of the Software. |
| 17 | |
| 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 22 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 23 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 24 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | SOFTWARE. |
| 26 | |
| 27 | (This is the MIT license) |
| 28 | ==============================================================================*/ |
| 29 | // Created by Laurence Lundblade on 9/30/18. |
| 30 | |
| 31 | |
| 32 | #include "run_tests.h" |
| 33 | #include "UsefulBuf.h" |
| 34 | #include <stdbool.h> |
| 35 | |
Laurence Lundblade | 2d85ce4 | 2018-10-12 14:12:47 +0800 | [diff] [blame] | 36 | #include "float_tests.h" |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 37 | #include "qcbor_decode_tests.h" |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 38 | #include "qcbor_encode_tests.h" |
| 39 | #include "UsefulBuf_Tests.h" |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 40 | |
| 41 | // Used to test the test runner |
| 42 | int fail_test() |
| 43 | { |
| 44 | return -44; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /* |
| 49 | Convert a number up to 999999999 to a string. This is so sprintf doesn't |
| 50 | have to be linked in so as to minimized dependencies even in test code. |
| 51 | |
| 52 | This function does pointer math. TODO: test this. |
| 53 | */ |
| 54 | const char *NumToString(int32_t nNum, UsefulBuf StringMem) |
| 55 | { |
| 56 | const uint32_t uMax = 1000000000; |
| 57 | |
| 58 | UsefulOutBuf OutBuf; |
| 59 | UsefulOutBuf_Init(&OutBuf, StringMem); |
| 60 | |
| 61 | if(nNum < 0) { |
| 62 | UsefulOutBuf_AppendByte(&OutBuf, '-'); |
| 63 | nNum = -nNum; |
| 64 | } |
| 65 | if(nNum > uMax-1) { |
| 66 | return "XXX"; |
| 67 | } |
| 68 | |
| 69 | bool bDidSomeOutput = false; |
| 70 | for(int n = uMax; n > 0; n/=10) { |
| 71 | int x = nNum/n; |
| 72 | if(x || bDidSomeOutput){ |
| 73 | bDidSomeOutput = true; |
| 74 | UsefulOutBuf_AppendByte(&OutBuf, '0' + x); |
| 75 | nNum -= x * n; |
| 76 | } |
| 77 | } |
| 78 | if(!bDidSomeOutput){ |
| 79 | UsefulOutBuf_AppendByte(&OutBuf, '0'); |
| 80 | } |
| 81 | UsefulOutBuf_AppendByte(&OutBuf, '\0'); |
| 82 | |
| 83 | return UsefulOutBuf_GetError(&OutBuf) ? "" : StringMem.ptr; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | |
| 88 | typedef int (test_fun_t)(void); |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 89 | typedef const char * (test_fun2_t)(void); |
| 90 | |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 91 | |
| 92 | #define TEST_ENTRY(test_name) {#test_name, test_name} |
| 93 | typedef struct { |
| 94 | const char *szTestName; |
| 95 | test_fun_t *test_fun; |
| 96 | } test_entry; |
| 97 | |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 98 | typedef struct { |
| 99 | const char *szTestName; |
| 100 | test_fun2_t *test_fun; |
| 101 | } test_entry2; |
| 102 | |
| 103 | test_entry2 s_tests2[] = { |
Laurence Lundblade | 7566b9f | 2018-10-12 09:13:32 +0800 | [diff] [blame] | 104 | TEST_ENTRY(UBUTest_CopyUtil), |
| 105 | TEST_ENTRY(UOBTest_NonAdversarial), |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 106 | TEST_ENTRY(TestBasicSanity), |
Laurence Lundblade | 7566b9f | 2018-10-12 09:13:32 +0800 | [diff] [blame] | 107 | TEST_ENTRY(UOBTest_BoundaryConditionsTest), |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 108 | TEST_ENTRY(UBMacroConversionsTest), |
| 109 | TEST_ENTRY(UBUtilTests), |
Laurence Lundblade | 7566b9f | 2018-10-12 09:13:32 +0800 | [diff] [blame] | 110 | TEST_ENTRY(UIBTest_IntegerFormat) |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 114 | test_entry s_tests[] = { |
Laurence Lundblade | 2d85ce4 | 2018-10-12 14:12:47 +0800 | [diff] [blame] | 115 | TEST_ENTRY(FloatValuesTest1), |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 116 | TEST_ENTRY(RTICResultsTest), |
| 117 | TEST_ENTRY(MapEncodeTest), |
| 118 | TEST_ENTRY(ArrayNestingTest1), |
| 119 | TEST_ENTRY(ArrayNestingTest2), |
| 120 | TEST_ENTRY(ArrayNestingTest3), |
| 121 | TEST_ENTRY(EncodeDateTest), |
| 122 | TEST_ENTRY(SimpleValuesTest1), |
| 123 | TEST_ENTRY(IntegerValuesTest1), |
| 124 | TEST_ENTRY(AllAddMethodsTest), |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 125 | TEST_ENTRY(ParseTooDeepArrayTest), |
| 126 | TEST_ENTRY(ComprehensiveInputTest), |
| 127 | TEST_ENTRY(ParseMapTest), |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 128 | TEST_ENTRY(indefinite_length_decode_test), |
Laurence Lundblade | 4d1ecba | 2018-10-12 21:22:30 +0800 | [diff] [blame^] | 129 | TEST_ENTRY(basic_encode_test), |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 130 | TEST_ENTRY(NestedMapTest), |
| 131 | TEST_ENTRY(BignumParseTest), |
| 132 | TEST_ENTRY(OptTagParseTest), |
| 133 | TEST_ENTRY(DateParseTest), |
| 134 | TEST_ENTRY(ParseSimpleTest), |
| 135 | TEST_ENTRY(ShortBufferParseTest2), |
| 136 | TEST_ENTRY(ShortBufferParseTest), |
| 137 | TEST_ENTRY(ParseDeepArrayTest), |
| 138 | TEST_ENTRY(SimpleArrayTest), |
| 139 | TEST_ENTRY(IntegerValuesParseTest), |
| 140 | TEST_ENTRY(mempool_test), |
| 141 | TEST_ENTRY(indefinite_length_decode_string_test), |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 142 | TEST_ENTRY(half_precision_encode_basic), |
| 143 | TEST_ENTRY(half_precision_decode_basic), |
| 144 | TEST_ENTRY(half_precision_to_float_transitive_test), |
| 145 | TEST_ENTRY(double_as_smallest_encode_basic), |
| 146 | TEST_ENTRY(half_precision_to_float_vs_rfc_test), |
| 147 | TEST_ENTRY(bstrwraptest), |
| 148 | TEST_ENTRY(bstr_wrap_error_test), |
| 149 | TEST_ENTRY(bstr_wrap_nest_test), |
| 150 | TEST_ENTRY(cose_sign1_tbs_test), |
| 151 | //TEST_ENTRY(fail_test), |
| 152 | }; |
| 153 | |
| 154 | |
| 155 | int run_tests(outputstring output, void *poutCtx, int *pNumTestsRun) |
| 156 | { |
| 157 | int nTestsFailed = 0; |
| 158 | int nTestsRun = 0; |
| 159 | UsefulBuf_MakeStackUB(StringStorage, 5); |
Laurence Lundblade | dc6e28e | 2018-10-11 19:19:27 +0530 | [diff] [blame] | 160 | |
| 161 | test_entry2 *t2; |
| 162 | const test_entry2 *s_tests2_end = s_tests2 + sizeof(s_tests2)/sizeof(test_entry2); |
| 163 | |
| 164 | for(t2 = s_tests2; t2 < s_tests2_end; t2++) { |
| 165 | const char * x = (t2->test_fun)(); |
| 166 | nTestsRun++; |
| 167 | if(output) { |
| 168 | (*output)(t2->szTestName, poutCtx); |
| 169 | } |
| 170 | |
| 171 | if(x) { |
| 172 | if(output) { |
| 173 | (*output)(" FAILED (returned ", poutCtx); |
| 174 | (*output)(x, poutCtx); |
| 175 | (*output)(")\n", poutCtx); |
| 176 | } |
| 177 | nTestsFailed++; |
| 178 | } else { |
| 179 | if(output) { |
| 180 | (*output)( " PASSED\n", poutCtx); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 185 | |
| 186 | test_entry *t; |
| 187 | const test_entry *s_tests_end = s_tests + sizeof(s_tests)/sizeof(test_entry); |
| 188 | |
| 189 | for(t = s_tests; t < s_tests_end; t++) { |
| 190 | int x = (t->test_fun)(); |
| 191 | nTestsRun++; |
| 192 | if(output) { |
| 193 | (*output)(t->szTestName, poutCtx); |
| 194 | } |
| 195 | |
| 196 | if(x) { |
| 197 | if(output) { |
| 198 | (*output)(" FAILED (returned ", poutCtx); |
| 199 | (*output)(NumToString(x, StringStorage), poutCtx); |
| 200 | (*output)(")\n", poutCtx); |
| 201 | } |
| 202 | nTestsFailed++; |
| 203 | } else { |
| 204 | if(output) { |
| 205 | (*output)( " PASSED\n", poutCtx); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if(pNumTestsRun) { |
| 211 | *pNumTestsRun = nTestsRun; |
| 212 | } |
| 213 | |
| 214 | if(output) { |
| 215 | (*output)( "SUMMARY: ", poutCtx); |
| 216 | (*output)( NumToString(nTestsRun, StringStorage), poutCtx); |
| 217 | (*output)( " tests run; ", poutCtx); |
| 218 | (*output)( NumToString(nTestsFailed, StringStorage), poutCtx); |
| 219 | (*output)( " tests failed\n", poutCtx); |
| 220 | } |
| 221 | |
| 222 | return nTestsFailed; |
| 223 | } |