-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutst.h
More file actions
120 lines (105 loc) · 7.86 KB
/
utst.h
File metadata and controls
120 lines (105 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//========================================================================
// UTST
//========================================================================
// A very simple set of macros for unit testing.
//
// Author: Yanghui Ou
// Date : September 13, 2019
//
#ifndef UTST_H
#define UTST_H
#include <cstdlib>
#include <cstdio>
#include <cassert>
#include <cmath>
#define RED "\033[31m"
#define GREEN "\033[32m"
#define RESET "\033[0m"
// __n > 0 means zoom-in mode, __n == 0 means zoon-out mode
static int __n = 1;
//------------------------------------------------------------------------
// UTST_ASSERT_TRUE( expr_ )
//------------------------------------------------------------------------
// Checks to see if the expression is true.
#define UTST_ASSERT_TRUE( expr_ ) \
if ( !( expr_ ) ) { \
if ( __n == 0 ) \
std::printf( "\n" ); \
std::printf( " - [ " RED "FAILED" RESET " ] Line %d\n", __LINE__ ); \
exit( 1 ); \
} \
else if ( __n > 0 ) { \
std::printf( " - [ " GREEN "passed" RESET " ] Line %d\n", __LINE__ ); \
} \
else { \
std::printf( GREEN "." RESET ); \
}
//------------------------------------------------------------------------
// UTST_ASSERT_FALSE( expr_ )
//------------------------------------------------------------------------
// Checks to see if the expression is false.
#define UTST_ASSERT_FALSE( expr_ ) \
if ( ( expr_ ) ) { \
if ( __n == 0 ) \
std::printf( "\n" ); \
std::printf( " - [ " RED "FAILED" RESET " ] Line %d\n", __LINE__ ); \
exit( 1 ); \
} \
else if ( __n > 0 ) { \
std::printf( " - [ " GREEN "passed" RESET " ] Line %d\n", __LINE__ ); \
} \
else { \
std::printf( GREEN "." RESET ); \
}
//------------------------------------------------------------------------
// UTST_ASSERT_INT_EQ( expr0_, expr1_ )
//------------------------------------------------------------------------
// Checks to see if the two expressions are equal using the != operator.
#define UTST_ASSERT_INT_EQ( expr0_, expr1_ ) \
if ( (int)( expr0_ ) != (int)( expr1_ ) ) { \
if ( __n == 0 ) \
std::printf( "\n" ); \
std::printf( " - [ " RED "FAILED" RESET " ] Line %d: %d != %d\n", __LINE__, (int)( expr0_ ), (int)( expr1_ ) ); \
exit( 1 ); \
} \
else if ( __n > 0 ) { \
std::printf( " - [ " GREEN "passed" RESET " ] Line %d: %d == %d\n", __LINE__, (int)( expr0_ ), (int)( expr1_ ) ); \
} \
else { \
std::printf( GREEN "." RESET ); \
}
//------------------------------------------------------------------------
// UTST_ASSERT_FLOAT_EQ( expr0_, expr1_ )
//------------------------------------------------------------------------
// Checks to see if the two expressions are within eps_ of each other.
#define UTST_ASSERT_FLOAT_EQ( expr0_, expr1_, eps_ ) \
if ( fabs( ( expr0_ ) - ( expr1_ ) ) > (double)( eps_ ) ) { \
if ( __n == 0 ) \
std::printf( "\n" ); \
std::printf( " - [ " RED "FAILED" RESET " ] Line %d: %f != %f\n", __LINE__, (double)( expr0_ ), (double)( expr1_ ) ); \
exit( 1 ); \
} \
else if ( __n > 0 ) { \
std::printf( " - [ " GREEN "passed" RESET " ] Line %d: %f == %f\n", __LINE__, (double)( expr0_ ), (double)( expr1_ ) ); \
} \
else { \
std::printf( GREEN "." RESET ); \
}
//------------------------------------------------------------------------
// UTST_ASSERT_CHAR_EQ( expr0_, expr1_ )
//------------------------------------------------------------------------
// Checks to see if the two expressions are equal using the != operator.
#define UTST_ASSERT_CHAR_EQ( expr0_, expr1_ ) \
if ( (char)( expr0_ ) != (char)( expr1_ ) ) { \
if ( __n == 0 ) \
std::printf( "\n" ); \
std::printf( " - [ " RED "FAILED" RESET " ] Line %d: %c != %c\n", __LINE__, (char)( expr0_ ), (char)( expr1_ ) ); \
exit( 1 ); \
} \
else if ( __n > 0 ) { \
std::printf( " - [ " GREEN "passed" RESET " ] Line %d: %c == %c\n", __LINE__, (char)( expr0_ ), (char)( expr1_ ) ); \
} \
else { \
std::printf( GREEN "." RESET ); \
}
#endif