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 | |
| 36 | #include "half_precision_test.h" |
| 37 | #include "basic_test.h" |
| 38 | #include "bstrwrap_tests.h" |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame^] | 39 | #include "mempool_test.h" |
| 40 | #include "qcbor_decode_tests.h" |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 41 | |
| 42 | // Used to test the test runner |
| 43 | int fail_test() |
| 44 | { |
| 45 | return -44; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /* |
| 50 | Convert a number up to 999999999 to a string. This is so sprintf doesn't |
| 51 | have to be linked in so as to minimized dependencies even in test code. |
| 52 | |
| 53 | This function does pointer math. TODO: test this. |
| 54 | */ |
| 55 | const char *NumToString(int32_t nNum, UsefulBuf StringMem) |
| 56 | { |
| 57 | const uint32_t uMax = 1000000000; |
| 58 | |
| 59 | UsefulOutBuf OutBuf; |
| 60 | UsefulOutBuf_Init(&OutBuf, StringMem); |
| 61 | |
| 62 | if(nNum < 0) { |
| 63 | UsefulOutBuf_AppendByte(&OutBuf, '-'); |
| 64 | nNum = -nNum; |
| 65 | } |
| 66 | if(nNum > uMax-1) { |
| 67 | return "XXX"; |
| 68 | } |
| 69 | |
| 70 | bool bDidSomeOutput = false; |
| 71 | for(int n = uMax; n > 0; n/=10) { |
| 72 | int x = nNum/n; |
| 73 | if(x || bDidSomeOutput){ |
| 74 | bDidSomeOutput = true; |
| 75 | UsefulOutBuf_AppendByte(&OutBuf, '0' + x); |
| 76 | nNum -= x * n; |
| 77 | } |
| 78 | } |
| 79 | if(!bDidSomeOutput){ |
| 80 | UsefulOutBuf_AppendByte(&OutBuf, '0'); |
| 81 | } |
| 82 | UsefulOutBuf_AppendByte(&OutBuf, '\0'); |
| 83 | |
| 84 | return UsefulOutBuf_GetError(&OutBuf) ? "" : StringMem.ptr; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | |
| 89 | typedef int (test_fun_t)(void); |
| 90 | |
| 91 | #define TEST_ENTRY(test_name) {#test_name, test_name} |
| 92 | typedef struct { |
| 93 | const char *szTestName; |
| 94 | test_fun_t *test_fun; |
| 95 | } test_entry; |
| 96 | |
| 97 | test_entry s_tests[] = { |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame^] | 98 | TEST_ENTRY(ParseTooDeepArrayTest), |
| 99 | TEST_ENTRY(ComprehensiveInputTest), |
| 100 | TEST_ENTRY(ParseMapTest), |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 101 | TEST_ENTRY(indefinite_length_decode_test), |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 102 | TEST_ENTRY(basic_test_one), |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame^] | 103 | TEST_ENTRY(NestedMapTest), |
| 104 | TEST_ENTRY(BignumParseTest), |
| 105 | TEST_ENTRY(OptTagParseTest), |
| 106 | TEST_ENTRY(DateParseTest), |
| 107 | TEST_ENTRY(ParseSimpleTest), |
| 108 | TEST_ENTRY(ShortBufferParseTest2), |
| 109 | TEST_ENTRY(ShortBufferParseTest), |
| 110 | TEST_ENTRY(ParseDeepArrayTest), |
| 111 | TEST_ENTRY(SimpleArrayTest), |
| 112 | TEST_ENTRY(IntegerValuesParseTest), |
| 113 | TEST_ENTRY(mempool_test), |
| 114 | TEST_ENTRY(indefinite_length_decode_string_test), |
Laurence Lundblade | f156fb8 | 2018-10-01 09:47:03 -0700 | [diff] [blame] | 115 | TEST_ENTRY(half_precision_encode_basic), |
| 116 | TEST_ENTRY(half_precision_decode_basic), |
| 117 | TEST_ENTRY(half_precision_to_float_transitive_test), |
| 118 | TEST_ENTRY(double_as_smallest_encode_basic), |
| 119 | TEST_ENTRY(half_precision_to_float_vs_rfc_test), |
| 120 | TEST_ENTRY(bstrwraptest), |
| 121 | TEST_ENTRY(bstr_wrap_error_test), |
| 122 | TEST_ENTRY(bstr_wrap_nest_test), |
| 123 | TEST_ENTRY(cose_sign1_tbs_test), |
| 124 | //TEST_ENTRY(fail_test), |
| 125 | }; |
| 126 | |
| 127 | |
| 128 | int run_tests(outputstring output, void *poutCtx, int *pNumTestsRun) |
| 129 | { |
| 130 | int nTestsFailed = 0; |
| 131 | int nTestsRun = 0; |
| 132 | UsefulBuf_MakeStackUB(StringStorage, 5); |
| 133 | |
| 134 | test_entry *t; |
| 135 | const test_entry *s_tests_end = s_tests + sizeof(s_tests)/sizeof(test_entry); |
| 136 | |
| 137 | for(t = s_tests; t < s_tests_end; t++) { |
| 138 | int x = (t->test_fun)(); |
| 139 | nTestsRun++; |
| 140 | if(output) { |
| 141 | (*output)(t->szTestName, poutCtx); |
| 142 | } |
| 143 | |
| 144 | if(x) { |
| 145 | if(output) { |
| 146 | (*output)(" FAILED (returned ", poutCtx); |
| 147 | (*output)(NumToString(x, StringStorage), poutCtx); |
| 148 | (*output)(")\n", poutCtx); |
| 149 | } |
| 150 | nTestsFailed++; |
| 151 | } else { |
| 152 | if(output) { |
| 153 | (*output)( " PASSED\n", poutCtx); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if(pNumTestsRun) { |
| 159 | *pNumTestsRun = nTestsRun; |
| 160 | } |
| 161 | |
| 162 | if(output) { |
| 163 | (*output)( "SUMMARY: ", poutCtx); |
| 164 | (*output)( NumToString(nTestsRun, StringStorage), poutCtx); |
| 165 | (*output)( " tests run; ", poutCtx); |
| 166 | (*output)( NumToString(nTestsFailed, StringStorage), poutCtx); |
| 167 | (*output)( " tests failed\n", poutCtx); |
| 168 | } |
| 169 | |
| 170 | return nTestsFailed; |
| 171 | } |