forked from coder-mike/FixedPoint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautomated_tests.cpp
More file actions
115 lines (109 loc) · 4.6 KB
/
automated_tests.cpp
File metadata and controls
115 lines (109 loc) · 4.6 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
#include <iostream>
#include <fstream>
#include "fixed_point.hpp"
OverflowMode OVERFLOW_MODE = OverflowMode::CLAMP;
#define CURR_TYPE FixedPoint<12, 16>
bool double_equals(double a, double b, double epsilon = 0.01){
return std::abs(a - b) < epsilon;
}
int main(){
// start reading tests.txt
std::ifstream infile("tests.txt");
std::ofstream outfile("results.txt");
char optype;
double num1, num2, num3;
int n, m;
while(infile >> optype){
switch (optype){
case '!':{
infile >> n >> m;
outfile << "Creating FixedPoint<" << n << ", " << m << ">" << std::endl;
break;
}
case '#':{// Assignment test
infile >> num1 >> num2;
auto p1 = new CURR_TYPE;
*p1 = num1;
// now need to check if they are equal
if(double_equals(p1->getValueF(), num2)){
outfile << "OK " << num1 << " == " << num2 << " " << p1->getValueF() << std::endl;
} else {
outfile << "FAIL " << num1 << " != " << num2 << " " << p1->getValueF() << std::endl;
}
break;
}
case '+':{// Addition test
infile >> num1 >> num2 >> num3;
auto p1 = new CURR_TYPE;
*p1 = num1;
auto p2 = new CURR_TYPE;
*p2 = num2;
// now need to check if they are equal
if(double_equals((*p1 + *p2).getValueF(), num3)){
outfile << "OK " << num1 << " + " << num2 << " == " << num3 << "==" << *p1 + *p2 << std::endl;
} else {
outfile << "FAIL " << num1 << " + " << num2 << " == " << *p1 << " + " << *p2 << " == " << *p1+*p2 << " != " << num3 << std::endl;
}
break;
}
case '-':{// Subtraction test
infile >> num1 >> num2 >> num3;
auto p1 = new CURR_TYPE;
*p1 = num1;
auto p2 = new CURR_TYPE;
*p2 = num2;
// now need to check if they are equal
if(double_equals((*p1 - *p2).getValueF(), num3)){
outfile << "OK " << num1 << " - " << num2 << " == " << num3 << "==" << *p1 - *p2 << std::endl;
} else {
outfile << "FAIL " << num1 << " - " << num2 << " == " << *p1 << " - " << *p2 << " == " << *p1-*p2 << " != " << num3 << std::endl;
}
break;
}
case '*':{// Multiplication test
infile >> num1 >> num2 >> num3;
auto p1 = new CURR_TYPE;
*p1 = num1;
auto p2 = new CURR_TYPE;
*p2 = num2;
// now need to check if they are equal
if(double_equals((*p1 * *p2).getValueF(), num3)){
outfile << "OK " << num1 << " * " << num2 << " == " << num3 << "==" << *p1 * *p2 << std::endl;
} else {
outfile << "FAIL " << num1 << " * " << num2 << " == " << *p1 << " * " << *p2 << " == " << *p1 * *p2 << " != " << num3 << std::endl;
}
break;
}
case '/':{// Division test
infile >> num1 >> num2 >> num3;
auto p1 = new CURR_TYPE;
*p1 = num1;
auto p2 = new CURR_TYPE;
*p2 = num2;
// now need to check if they are equal
if(double_equals((*p1 / *p2).getValueF(), num3)){
outfile << "OK " << num1 << " / " << num2 << " == " << num3 << "==" << *p1 / *p2 << std::endl;
} else {
outfile << "FAIL " << num1 << " / " << num2 << " == " << *p1 << " / " << *p2 << " == " << *p1 / *p2 << " != " << num3 << std::endl;
}
break;
}
case '(':{//Unary minus test
infile >> num1 >> num2;
auto p1 = new CURR_TYPE;
*p1 = num1;
// now need to check if they are equal
if(double_equals(-(*p1).getValueF(), num2)){
outfile << "OK -" << num1 << " == " << num2 << "==" << -(*p1) << std::endl;
} else {
outfile << "FAIL -" << num1 << " == " << *p1 << " != " << num2 << std::endl;
}
break;
}
default:{
std::cout << "Unknown optype: " << optype << std::endl;
return 0;
}
}
}
}