Foenix A2650 OS/f Library
 
Loading...
Searching...
No Matches
minunit.h
1/*
2 * minunit.h
3 *
4 * Created on: Feb 19, 2022
5 * Author: micahbly (not really, see below)
6 * This is an adaption of minunit.h, as found at https://raw.githubusercontent.com/siu/minunit/master/minunit.h
7 * I modified it as follows:
8 * - to run on A2560
9 * - removed cross-platform stuff
10 * - snprintf replaced with sprintf, as snprintf not available in my amiga compilers
11 * - hacked together A2560-compatible time function
12 * - removed the CPU time, leaving just real time.
13 */
14
15
16/*
17 * Copyright (c) 2012 David SiƱuela Pastor, siu.4coders@gmail.com
18 *
19 * Permission is hereby granted, free of charge, to any person obtaining
20 * a copy of this software and associated documentation files (the
21 * "Software"), to deal in the Software without restriction, including
22 * without limitation the rights to use, copy, modify, merge, publish,
23 * distribute, sublicense, and/or sell copies of the Software, and to
24 * permit persons to whom the Software is furnished to do so, subject to
25 * the following conditions:
26 *
27 * The above copyright notice and this permission notice shall be
28 * included in all copies or substantial portions of the Software.
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 */
38
39#ifndef MINUNIT_MINUNIT_H
40#define MINUNIT_MINUNIT_H
41
42
43/* Change POSIX C SOURCE version for pure c99 compilers */
44#if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200112L
45#undef _POSIX_C_SOURCE
46#define _POSIX_C_SOURCE 200112L
47#endif
48
49
50
51// C includes
52#include <stdbool.h>
53#include <string.h>
54#include <stdio.h>
55#include <math.h>
56#include <stdlib.h>
57
58
59// A2560 includes
60#include <mcp/syscalls.h>
61
62
63
64
65/* Maximum length of last message */
66#define MINUNIT_MESSAGE_LEN 1024
67/* Accuracy with which floats are compared */
68#define MINUNIT_EPSILON 1E-12
69
70/* Misc. counters */
71static int minunit_run = 0;
72static int minunit_assert = 0;
73static int minunit_fail = 0;
74static int minunit_status = 0;
75
76/* Timers */
77static long minunit_real_timer = 0;
78static long minunit_proc_timer = 0;
79
80/* Last message */
81static char minunit_last_message[MINUNIT_MESSAGE_LEN];
82
83/* Test setup and teardown function pointers */
84static void (*minunit_setup)(void) = NULL;
85static void (*minunit_teardown)(void) = NULL;
86
87/* Definitions */
88#define MU_TEST(method_name) static void method_name(void)
89#define MU_TEST_SUITE(suite_name) static void suite_name(void)
90
91#define MU__SAFE_BLOCK(block) do {\
92 block\
93} while(0)
94
95/* Run test suite and unset setup and teardown functions */
96#define MU_RUN_SUITE(suite_name) MU__SAFE_BLOCK(\
97 suite_name();\
98 minunit_setup = NULL;\
99 minunit_teardown = NULL;\
100)
101
102/* Configure setup and teardown functions */
103#define MU_SUITE_CONFIGURE(setup_fun, teardown_fun) MU__SAFE_BLOCK(\
104 minunit_setup = setup_fun;\
105 minunit_teardown = teardown_fun;\
106)
107
108/* Test runner */
109#define MU_RUN_TEST(test) MU__SAFE_BLOCK(\
110 if (minunit_real_timer==0 && minunit_proc_timer==0) {\
111 minunit_real_timer = mu_timer_real();\
112 minunit_proc_timer = mu_timer_cpu();\
113 }\
114 if (minunit_setup) (*minunit_setup)();\
115 minunit_status = 0;\
116 test();\
117 minunit_run++;\
118 if (minunit_status) {\
119 minunit_fail++;\
120 printf("F");\
121 printf("\n%s\n", minunit_last_message);\
122 }\
123 fflush(stdout);\
124 if (minunit_teardown) (*minunit_teardown)();\
125)
126
127/* Report */
128#define MU_REPORT() MU__SAFE_BLOCK(\
129 long minunit_end_real_timer;\
130 long minunit_end_proc_timer;\
131 long elapsed_real_time;\
132 printf("\n\n%d tests, %d assertions, %d failures\n", minunit_run, minunit_assert, minunit_fail);\
133 DEBUG_OUT(("\n\n%d tests, %d assertions, %d failures", minunit_run, minunit_assert, minunit_fail));\
134 minunit_end_real_timer = mu_timer_real();\
135 minunit_end_proc_timer = mu_timer_cpu();\
136 elapsed_real_time = minunit_end_real_timer - minunit_real_timer;\
137 printf("\nFinished in %li ticks (real) %.8f seconds (real)\n\n",\
138 elapsed_real_time,\
139 (double)(elapsed_real_time/SYS_TICKS_PER_SEC));\
140 DEBUG_OUT(("Finished in %li ticks (real) %.8f seconds (real)",\
141 minunit_end_real_timer - minunit_real_timer,\
142 (double)(minunit_end_real_timer - minunit_real_timer)/SYS_TICKS_PER_SEC));\
143)
144#define MU_EXIT_CODE minunit_fail
145
146/* Assertions */
147#define mu_check(test) MU__SAFE_BLOCK(\
148 minunit_assert++;\
149 if (!(test)) {\
150 sprintf(minunit_last_message, "%s failed:\n\t%s:%d: %s", __func__, __FILE__, __LINE__, #test);\
151 minunit_status = 1;\
152 return;\
153 } else {\
154 printf(".");\
155 }\
156)
157
158#define mu_fail(message) MU__SAFE_BLOCK(\
159 minunit_assert++;\
160 sprintf(minunit_last_message, "%s failed:\n\t%s:%d: %s", __func__, __FILE__, __LINE__, message);\
161 minunit_status = 1;\
162 return;\
163)
164
165#define mu_assert(test, message) MU__SAFE_BLOCK(\
166 minunit_assert++;\
167 if (!(test)) {\
168 sprintf(minunit_last_message, "%s failed:\n\t%s:%d: %s", __func__, __FILE__, __LINE__, message);\
169 minunit_status = 1;\
170 return;\
171 } else {\
172 printf(".");\
173 }\
174)
175
176#define mu_assert_int_eq(expected, result) MU__SAFE_BLOCK(\
177 int minunit_tmp_e;\
178 int minunit_tmp_r;\
179 minunit_assert++;\
180 minunit_tmp_e = (expected);\
181 minunit_tmp_r = (result);\
182 if (minunit_tmp_e != minunit_tmp_r) {\
183 sprintf(minunit_last_message, "%s failed:\n\t%s:%d: %d expected but was %d", __func__, __FILE__, __LINE__, minunit_tmp_e, minunit_tmp_r);\
184 minunit_status = 1;\
185 return;\
186 } else {\
187 printf(".");\
188 }\
189)
190
191#define mu_assert_double_eq(expected, result) MU__SAFE_BLOCK(\
192 double minunit_tmp_e;\
193 double minunit_tmp_r;\
194 minunit_assert++;\
195 minunit_tmp_e = (expected);\
196 minunit_tmp_r = (result);\
197 if (fabs(minunit_tmp_e-minunit_tmp_r) > MINUNIT_EPSILON) {\
198 int minunit_significant_figures = 1 - log10(MINUNIT_EPSILON);\
199 sprintf(minunit_last_message, "%s failed:\n\t%s:%d: %.*g expected but was %.*g", __func__, __FILE__, __LINE__, minunit_significant_figures, minunit_tmp_e, minunit_significant_figures, minunit_tmp_r);\
200 minunit_status = 1;\
201 return;\
202 } else {\
203 printf(".");\
204 }\
205)
206
207#define mu_assert_string_eq(expected, result) MU__SAFE_BLOCK(\
208 const char* minunit_tmp_e = expected;\
209 const char* minunit_tmp_r = result;\
210 minunit_assert++;\
211 if (!minunit_tmp_e) {\
212 minunit_tmp_e = "<null pointer>";\
213 }\
214 if (!minunit_tmp_r) {\
215 minunit_tmp_r = "<null pointer>";\
216 }\
217 if(strcmp(minunit_tmp_e, minunit_tmp_r)) {\
218 sprintf(minunit_last_message, "%s failed:\n\t%s:%d: '%s' expected but was '%s'", __func__, __FILE__, __LINE__, minunit_tmp_e, minunit_tmp_r);\
219 minunit_status = 1;\
220 return;\
221 } else {\
222 printf(".");\
223 }\
224)
225
226/*
227 * The following two functions were written by David Robert Nadeau
228 * from http://NadeauSoftware.com/ and distributed under the
229 * Creative Commons Attribution 3.0 Unported License
230 */
231
239static long mu_timer_real(void)
240{
241 // A2560
242 long the_ticks;
243
244 the_ticks = sys_time_jiffies();
245
246 return the_ticks;
247}
248
253static long mu_timer_cpu(void)
254{
255 // return ticks? what would be useful from an amiga POV?
256
257 return -1;
258}
259
260
261
262
263
264#endif /* MINUNIT_MINUNIT_H */