-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunittest.cpp
183 lines (156 loc) · 5.88 KB
/
unittest.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "fraction.h"
#include "unittest.h"
#include <string>
#include <iostream>
#include <windows.h>
#include <sstream>
#include <iomanip>
#include <vector>
using namespace std;
//returns the current attributes
WORD GetConsoleTextAttribute (HANDLE hCon)
{
CONSOLE_SCREEN_BUFFER_INFO con_info;
GetConsoleScreenBufferInfo(hCon, &con_info);
return con_info.wAttributes;
}
UnitTest::UnitTest(): numErrors(0) { }
//--------------------------------------------------------------
// void test(int expNum, int expDen, Fraction obj, string testNum)
// Purpose: tests an object against 'expNum' and 'expDen' and displays
// both, displaying the actual values in red if they are not the same
// Limitations: none
// Assumptions: none
//--------------------------------------------------------------
void UnitTest::test(int expNum, int expDen, Fraction obj, string testNum)
{
// handles the changing of the console text color; used to make error text red and easy to spot
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
const int saved_colors = GetConsoleTextAttribute(hConsole);
cout << testNum << "\t\t\t";
if (expNum == obj.getNum() && expDen == obj.getDen())
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
cout << "Test passed" << endl;
SetConsoleTextAttribute(hConsole, saved_colors);
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
cout << "Test failed" << endl;
SetConsoleTextAttribute(hConsole, saved_colors);
numErrors++;
}
}
//--------------------------------------------------------------
// void test(bool expected, bool actual, string testNum)
// Purpose: tests 'expected' against 'actual' and displays output in red
// if they are not the same
// Limitations: none
// Assumptions: none
//--------------------------------------------------------------
void UnitTest::test(bool expected, bool actual, string testNum)
{
// handles the changing of the console text color; used to make error text red and easy to spot
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
const int saved_colors = GetConsoleTextAttribute(hConsole);
string exp, act;
if (expected == true) exp = "true";
else exp = "false";
if (actual == true) act = "true";
else act = "false";
cout << testNum << "\t\t\t";
if (expected == actual)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
cout << "Test passed" << endl;
SetConsoleTextAttribute(hConsole, saved_colors);
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
cout << "Test failed" << endl;
SetConsoleTextAttribute(hConsole, saved_colors);
numErrors++;
}
}
void UnitTest::test(int expNum, int expDen, int actNum, int actDen, string testNum)
{
// handles the changing of the console text color; used to make error text red and easy to spot
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
const int saved_colors = GetConsoleTextAttribute(hConsole);
cout << testNum << "\t\t\t";
if (expNum == actNum && expDen == actDen)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
cout << "Test passed" << endl;
SetConsoleTextAttribute(hConsole, saved_colors);
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
cout << "Test failed" << endl;
SetConsoleTextAttribute(hConsole, saved_colors);
numErrors++;
}
}
void UnitTest::test(int exp, int act, string testNum)
{
// handles the changing of the console text color; used to make error text red and easy to spot
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
const int saved_colors = GetConsoleTextAttribute(hConsole);
cout << testNum << "\t\t\t";
if (exp == act)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
cout << "Test passed" << endl;
SetConsoleTextAttribute(hConsole, saved_colors);
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
cout << "Test failed" << endl;
SetConsoleTextAttribute(hConsole, saved_colors);
numErrors++;
}
}
void UnitTest::test(Fraction obj, const char& cstr, string testNum)
{
// handles the changing of the console text color; used to make error text red and easy to spot
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
const int saved_colors = GetConsoleTextAttribute(hConsole);
string str = obj.toString();
cout << testNum << "\t\t\t";
if (cstr == str)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
cout << "Test passed" << endl;
SetConsoleTextAttribute(hConsole, saved_colors);
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
cout << "Test failed" << endl;
SetConsoleTextAttribute(hConsole, saved_colors);
numErrors++;
}
}
//--------------------------------------------------------------
// void errors()
// Purpose: displays the number of errors in grey if there are none and
// in red if there are any
// if they are not the same
// Limitations: none
// Assumptions: none
//--------------------------------------------------------------
void UnitTest::errors()
{
// handles the changing of the console text color; used to make error text red and easy to spot
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
const int saved_colors = GetConsoleTextAttribute(hConsole);
if (numErrors > 0) {
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
}
cout << "\nTotal number of errors: " << numErrors << endl;
SetConsoleTextAttribute(hConsole, saved_colors);
}