utest.h
Go to the documentation of this file.
1 /*
2  *
3  * utest.h
4  *
5  * Declarations for a simple unit testing framework.
6  *
7  * Devon Powell
8  * 31 August 2015
9  *
10  * This program was prepared by Los Alamos National Security, LLC at Los Alamos National
11  * Laboratory (LANL) under contract No. DE-AC52-06NA25396 with the U.S. Department of Energy (DOE).
12  * All rights in the program are reserved by the DOE and Los Alamos National Security, LLC.
13  * Permission is granted to the public to copy and use this software without charge, provided that
14  * this Notice and any statement of authorship are reproduced on all copies. Neither the U.S.
15  * Government nor LANS makes any warranty, express or implied, or assumes any liability
16  * or responsibility for the use of this software.
17  *
18  */
19 
20 #ifndef _UTEST_H_
21 #define _UTEST_H_
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 
28 
29 
30 // -- functions to be defined by the user -- //
31 
32 // setup() will run prior to the tests,
33 // and can be left as an empty function if desired.
34 void setup();
35 
36 // register_all_tests() should be defined and filled with calls
37 // to register_test(), one for each defined unit test.
38 void register_all_tests();
39 
40 
41 
42 // -- assertion and expectation macros to be used inside user code -- //
43 
44 // pass/fail/warn state IDs
45 #define PASS 2
46 #define WARN 1
47 #define FAIL 0
48 
49 // fails if the argument is false
50 #define ASSERT_TRUE(x) { \
51  if(!(x)) { \
52  test_state = FAIL; \
53  printf("\x1b[31mASSERT_TRUE( %d ) failed:\x1b[0m\n %s, line %d.\n", \
54  x, __FILE__, __LINE__); \
55  return; \
56  } \
57 } \
58 
59 // fails if the argument is true
60 #define ASSERT_FALSE(x) { \
61  if((x)) { \
62  test_state = FAIL; \
63  printf("\x1b[31mASSERT_FALSE( %d ) failed:\x1b[0m\n %s, line %d.\n", \
64  x, __FILE__, __LINE__); \
65  return; \
66  } \
67 } \
68 
69 // warns if the argument is false
70 #define EXPECT_TRUE(x) { \
71  if(!(x)) { \
72  test_state = WARN; \
73  printf("\x1b[33mEXPECT_TRUE( %d ) failed:\x1b[0m\n %s, line %d.\n", \
74  x, __FILE__, __LINE__); \
75  } \
76 } \
77 
78 // warns if the argument is true
79 #define EXPECT_FALSE(x) { \
80  if((x)) { \
81  test_state = WARN; \
82  printf("\x1b[33mEXPECT_FALSE( %d ) failed:\x1b[0m\n %s, line %d.\n", \
83  x, __FILE__, __LINE__); \
84  } \
85 } \
86 
87 // fails if two floating-point numbers are not within some fractional tolerance
88 #define ASSERT_EQ(x, y, tol) { \
89  double err = fabs(1.0-(x)/(y)); \
90  if(err > (tol)) { \
91  test_state = FAIL; \
92  printf("\x1b[31mASSERT_EQ( %.3e , %.3e , %.3e ) failed ( err = %.3e ):\x1b[0m\n %s, line %d.\n", \
93  x, y, tol, err, __FILE__, __LINE__); \
94  return; \
95  } \
96 } \
97 
98 // warns if two floating-point numbers are not within some fractional tolerance
99 #define EXPECT_EQ(x, y, tol) { \
100  double err = fabs(1.0-(x)/(y)); \
101  if(err > (tol)) { \
102  test_state = WARN; \
103  printf("\x1b[33mEXPECT_EQ( %.3e , %.3e , %.3e ) failed ( err = %.3e ):\x1b[0m\n %s, line %d.\n", \
104  x, y, tol, err, __FILE__, __LINE__); \
105  } \
106 } \
107 
108 
109 // fails if x >= y
110 #define ASSERT_LT(x, y) { \
111  if((x) >= (y)) { \
112  test_state = FAIL; \
113  printf("\x1b[31mASSERT_LT( %.3e , %.3e ) failed:\x1b[0m\n %s, line %d.\n", \
114  x, y, __FILE__, __LINE__); \
115  return; \
116  } \
117 } \
118 
119 // warns if x >= y
120 #define EXPECT_LT(x, y) { \
121  if((x) >= (y)) { \
122  test_state = WARN; \
123  printf("\x1b[33mEXPECT_LT( %.3e , %.3e ) failed:\x1b[0m\n %s, line %d.\n", \
124  x, y, __FILE__, __LINE__); \
125  } \
126 } \
127 
128 
129 
130 
131 
132 // -- other -- //
133 
134 // use this to register a test by passing
135 // a function pointer and a test name
136 void register_test(void (*testfn)(void), char* name);
137 
138 // the state of the current test
139 extern int test_state;
140 
141 
142 #endif // _UTEST_H_
void register_all_tests()
void register_test(void(*testfn)(void), char *name)
int test_state
void setup()