-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmy_integer_test.cpp
86 lines (74 loc) · 2.74 KB
/
my_integer_test.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
#include <cassert>
#include <iostream>
#include "my_type_functions.h"
#include "my_integer.h"
using namespace std;
size_t failures = 0;
size_t successes = 0;
template<typename F>
void test_unary_procedure(F f,
string procedure,
string type,
Domain(F) n,
Codomain(F) expected)
{
if (f(n) != expected) {
++failures;
cout << " test failed for procedure " << procedure;
cout << ", type " << type << endl;
cout << " input: " << n;
cout << "; expected output: " << expected;
cout << "; actual output: " << f(n) << endl;
} else {
++successes;
}
}
template<typename F>
void test_binary_procedure(F f,
string procedure,
string type,
Domain(F) n1,
Domain(F) n2,
Codomain(F) expected)
{
if (f(n1, n2) != expected) {
++failures;
cout << " test failed for procedure " << procedure;
cout << ", type " << type << endl;
cout << " inputs: " << n1 << ", " << n2;
cout << "; expected output: " << expected;
cout << "; actual output: " << f(n1, n2) << endl;
} else {
++successes;
}
}
template<typename I>
void test_procedures_for_signed_type(string type) {
#define UNARY_TEST_CASE(PROCEDURE, INPUT, EXPECTED) test_unary_procedure(PROCEDURE<I>, #PROCEDURE, type, INPUT, EXPECTED);
#define BINARY_TEST_CASE(PROCEDURE, INPUT1, INPUT2, EXPECTED) test_binary_procedure(PROCEDURE<I>, #PROCEDURE, type, INPUT1, INPUT2, EXPECTED);
#include "my_integer_test_cases_signed.h"
#undef UNARY_TEST_CASE
#undef BINARY_TEST_CASE
}
template<typename I>
void test_procedures_for_unsigned_type(string type) {
#define UNARY_TEST_CASE(PROCEDURE, INPUT, EXPECTED) test_unary_procedure(PROCEDURE<I>, #PROCEDURE, type, INPUT, EXPECTED);
#define BINARY_TEST_CASE(PROCEDURE, INPUT1, INPUT2, EXPECTED) test_binary_procedure(PROCEDURE<I>, #PROCEDURE, type, INPUT1, INPUT2, EXPECTED);
#include "my_integer_test_cases_unsigned.h"
#undef UNARY_TEST_CASE
#undef BINARY_TEST_CASE
}
int main() {
test_procedures_for_signed_type<short>("short");
test_procedures_for_signed_type<int>("int");
test_procedures_for_signed_type<long>("long");
test_procedures_for_unsigned_type<unsigned short>("unsigned short");
test_procedures_for_unsigned_type<unsigned int>("unsigned int");
test_procedures_for_unsigned_type<unsigned long>("unsigned long");
cout << successes << " tests passed" << endl;
if (failures) {
cout << failures << " tests failed" << endl;
} else {
cout << "all tests passed!" << endl;
}
}