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