-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
131 lines (116 loc) · 2.55 KB
/
test.c
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
/**
* @file
*
* Implementation of useful test routines.
*
* @ingroup relic
*/
#include "test.h"
/*
* Turn on colors for pretty printing.
*/
#define COLOR
#ifdef _MSC_VER
#undef DOUBLE
#include <Windows.h>
/**
* Stores the default color used in the console.
*/
static int default_color;
#endif
/*============================================================================*/
/* Private definitions */
/*============================================================================*/
/**
* Color of the string printed when the test fails (red).
*/
#ifdef _MSC_VER
#define FAIL_COLOR 12
#else
#define FAIL_COLOR 31
#endif
/**
* Color of the string printed when the test passes (green).
*/
#ifdef _MSC_VER
#define PASS_COLOR 10
#else
#define PASS_COLOR 32
#endif
/**
* Command to set terminal colors.
*/
#define CMD_SET 27
/**
* Command to reset terminal colors.
*/
#define CMD_RESET 0
/**
* Print with bright attribute.
*/
#define CMD_ATTR 1
/**
* Copies default color to global variable.
*/
static void cache_default_color(void) {
#ifdef _MSC_VER
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE m_hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(m_hConsole, &csbi);
default_color = (csbi.wAttributes & 255);
#endif
}
/**
* Changes font to test failure mode.
*/
static void fail_font(void) {
cache_default_color();
#ifdef COLOR
#ifdef _MSC_VER
HANDLE m_hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(m_hConsole, FAIL_COLOR);
#else
printf("%c[%d;%dm", CMD_SET, CMD_ATTR, FAIL_COLOR);
#endif
#endif
}
/**
* Changes font to test pass mode.
*/
static void pass_font(void) {
cache_default_color();
#ifdef COLOR
#ifdef _MSC_VER
HANDLE m_hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(m_hConsole, PASS_COLOR);
#else
printf("%c[%d;%dm", CMD_SET, CMD_ATTR, PASS_COLOR);
#endif
#endif
}
/**
* Resets font to default.
*/
static void reset_font(void) {
#ifdef COLOR
#ifdef _MSC_VER
HANDLE m_hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(m_hConsole, default_color);
#else
printf("%c[%dm", CMD_SET, CMD_RESET);
#endif
#endif
}
/*============================================================================*/
/* Public definitions */
/*============================================================================*/
void test_fail(void) {
fail_font();
printf("[FAIL]\n");
reset_font();
}
void test_pass(void) {
pass_font();
printf("[PASS]\n");
reset_font();
}