-
Notifications
You must be signed in to change notification settings - Fork 59
/
ece2400-stdlib.h
142 lines (112 loc) · 4.83 KB
/
ece2400-stdlib.h
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//========================================================================
// ece2400-stdlib.h
//========================================================================
// ECE2400 programming assignment utility functions.
//
// Note:
//
// DO NOT CHANGE THE FOLLOWING CODE!
//
// Author: Yanghui Ou, Peitian Pan
// Date: Sep 2, 2020
#ifndef ECE2400_STDLIB_H
#define ECE2400_STDLIB_H
#include <stdio.h>
#include <math.h>
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define RESET "\033[0m"
// __n > 0: display full [ passed ] line
// __n == 0: do not display anything for passed case
// __n < 0: display a dot for passed case
extern int __n;
// The status of the current test file. Any failed check sets
// this variable to 1.
extern int __failed;
// Temporary variable to save the condition so that we don't
// evaluate the given expressions multiple times.
extern int __failure_condition;
extern int __int_expr0, __int_expr1;
extern double __double_expr0, __double_expr1;
// Helper functions
char* __ece2400_get_file_name(char*);
void __ece2400_fail(char*, int, char*);
void __ece2400_check_and_print_uniop(char*, int, char*);
void __ece2400_check_and_print_int_binop(char*, int, char*, char*);
void __ece2400_check_and_print_double_binop(char*, int, char*, char*);
//------------------------------------------------------------------------
// ECE2400_CHECK_FAIL()
//------------------------------------------------------------------------
// Unconditionally fail a test case.
#define ECE2400_CHECK_FAIL() \
__ece2400_fail( __FILE__, __LINE__, "ECE2400_CHECK_FAIL" ); \
return;
//------------------------------------------------------------------------
// ECE2400_CHECK_TRUE( expr_ )
//------------------------------------------------------------------------
// Checks to see if the expression is true.
#define ECE2400_CHECK_TRUE( expr_ ) \
__int_expr0 = expr_; \
__failure_condition = !__int_expr0; \
__ece2400_check_and_print_uniop( __FILE__, __LINE__, #expr_ ); \
if ( __failure_condition ) return;
//------------------------------------------------------------------------
// ECE2400_CHECK_FALSE( expr_ )
//------------------------------------------------------------------------
// Checks to see if the expression is false.
#define ECE2400_CHECK_FALSE( expr_ ) \
__int_expr0 = expr_; \
__failure_condition = __int_expr0; \
__ece2400_check_and_print_uniop( __FILE__, __LINE__, #expr_ ); \
if ( __failure_condition ) return;
//------------------------------------------------------------------------
// ECE2400_CHECK_INT_EQ( expr0_, expr1_ )
//------------------------------------------------------------------------
// Checks to see if the two expressions are equal using the != operator.
#define ECE2400_CHECK_INT_EQ( expr0_, expr1_ ) \
__int_expr0 = (int)(expr0_); \
__int_expr1 = (int)(expr1_); \
__failure_condition = __int_expr0 != __int_expr1; \
__ece2400_check_and_print_int_binop( __FILE__, __LINE__, #expr0_, #expr1_ ); \
if ( __failure_condition ) return;
//------------------------------------------------------------------------
// ECE2400_CHECK_APPROX_EQ( expr0_, expr1_, pct_ )
//------------------------------------------------------------------------
// Checks to see if the two expressions are within percent of each other.
#define ECE2400_CHECK_APPROX_EQ( expr0_, expr1_, pct_ ) \
__double_expr0 = expr0_; \
__double_expr1 = expr1_; \
__failure_condition = fabs( __double_expr0 - __double_expr1 ) > fabs( (double)(pct_) * __double_expr1 ); \
__ece2400_check_and_print_double_binop( __FILE__, __LINE__, #expr0_, #expr1_ ); \
if ( __failure_condition ) return;
//------------------------------------------------------------------------
// ECE2400_DEBUG( ... ) and ECE2400_DEBUG_NEWLINE
//------------------------------------------------------------------------
// Print out debug info when not in eval build. Note that debug info is
// only dumped to stdout when __n > 0 (i.e., we are looking at a specific
// test function).
#ifndef EVAL
#define ECE2400_DEBUG( ... ) \
if ( __n > 0 ) { \
printf(" - [ " YELLOW "-info-" RESET " ] File %s:%d: ", __ece2400_get_file_name(__FILE__), __LINE__); \
printf(__VA_ARGS__); \
printf("\n"); \
}
#define ECE2400_DEBUG_NEWLINE \
if ( __n > 0 ) { printf("\n"); }
#else
#define ECE2400_DEBUG( ... ) ;
#define ECE2400_DEBUG_NEWLINE ;
#endif // #ifndef EVAL
//------------------------------------------------------------------------
// ece2400_timer_reset
//------------------------------------------------------------------------
// Resets the timer.
void ece2400_timer_reset();
//------------------------------------------------------------------------
// ece2400_timer_get_elapsed
//------------------------------------------------------------------------
// Return the elapased time in seconds.
double ece2400_timer_get_elapsed();
#endif // ECE2400_STDLIB_H