-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTestCommon.cc
More file actions
121 lines (99 loc) · 4.34 KB
/
TestCommon.cc
File metadata and controls
121 lines (99 loc) · 4.34 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
116
117
118
119
120
121
#include "TestCommon.h"
//----------------------------------------------------------------
// Method to check whether two DataFrames are equal
// @param data1: first DataFrame to check
// @param data2: second DataFrame to check
// @param fatalErr: set to nonzero if dimensions are not the same
// @return a list of the rows of the dataframe that were different
//----------------------------------------------------------------
std::vector< int > CheckDataFrameEquality ( DataFrame< double > data1,
DataFrame< double > data2,
int & fatalErr ) {
// to keep track of the rows with different content
std::vector< int > badRows;
// to hold each row for comparison
std::valarray< double > row1;
std::valarray< double > row2;
// check if dimensions are equal
if ( data1.NRows() != data2.NRows() or
data1.NColumns() != data2.NColumns() ) {
fatalErr = 1;
}
// check if any rows were different in the dataframes passed in
for ( size_t rowIdx = 0; rowIdx < data1.NRows(); rowIdx++ ) {
for ( size_t colIdx = 0; colIdx < data1.NColumns(); colIdx++ ) {
float diff = std::abs(data1(rowIdx,colIdx) - data2(rowIdx,colIdx));
if ( diff >= EPSILON ) {
badRows.push_back( rowIdx );
break;
}
}
}
return badRows;
}
//----------------------------------------------------------------
// Run DataFrame comparison
// @param testName: description of the test being run
// @param data1: first DataFrame to check
// @param data2: second DataFrame to check
// @return: none, just prints to stdout
//----------------------------------------------------------------
void MakeTest (std::string testName, DataFrame< double > data1,
DataFrame< double > data2 ) {
int fatalErr = 0;
//the rows that were different in the test
std::vector< int > badRows = CheckDataFrameEquality(data1, data2, fatalErr);
if ( fatalErr ) {
std::cout << RED_TEXT << "FATAL ERR: dimensions of block 1: "
<< data1.NRows() << "x" << data1.NColumns()
<< " do not equal dimensions of block 2: "
<< data2.NRows() << "x " << data2.NColumns() << std::endl;
}
// print info on test name and test results
std::cout << STR_LINE_SEP << std::endl;
std::cout << "Test: " << testName << std::endl;
std::cout << STR_LINE_SEP << std::endl;
if ( badRows.empty() ) {
std::cout << GREEN_TEXT;
std::cout << TAB_CHAR << "PASSED. EPSILON: " << EPSILON;
}
else {
int numBadRows =
std::count_if( badRows.begin(),
badRows.end(), [](int i){ return i != 0; } );
if ( numBadRows ) {
std::cout << YELLOW_TEXT;
std::cout << TAB_CHAR << "FAILED. EPSILON: " << EPSILON;
}
#ifdef PRINT_DIFFERENCE_IN_RESULTS
std::cout << TAB_CHAR << TAB_CHAR << "Block 1 column names: ";
for (auto colName : data1.ColumnNames()) {
std::cout << std::setw(10) << colName << TAB_CHAR;
}
std::cout << std::endl;
std::cout << TAB_CHAR << TAB_CHAR << "Block 2 column names: ";
for (auto colName : data2.ColumnNames()) {
std::cout << std::setw(10) << colName << TAB_CHAR;
}
std::cout << std::endl;
std::cout << TAB_CHAR << "first 10 different rows:" << std::endl;
for (auto iterate = badRows.begin();
iterate != badRows.end();
++iterate) {
std::valarray< double > badRow1 = data1.Row( *iterate );
std::valarray< double > badRow2 = data2.Row( *iterate );
std::cout << TAB_CHAR << TAB_CHAR << "Block 1 row "<<*iterate<<": ";
for (double elem : badRow1) {
std::cout << std::setw(10) << TAB_CHAR << elem << " ";
}
std::cout << std::endl;
std::cout << TAB_CHAR << TAB_CHAR << "Block 2 row "<<*iterate<<": ";
for (double elem : badRow2) {
std::cout << std::setw(10) << TAB_CHAR << elem << " ";
}
std::cout << std::endl << std::endl;
}
#endif
}
std::cout << RESET_TEXT << std::endl << std::flush;
}