CSlimTools.cc
5.08 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
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
184
185
186
187
188
/**
* CSlimTools.cc
*
* Created on: 17 oct. 2012
* Author: AKKA IS
*/
#include "CSlimTools.hh"
#include <math.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <boost/tokenizer.hpp>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
using namespace boost;
CSlimTools::CSlimTools() {
}
CSlimTools::~CSlimTools() {
}
#define MaxAnswer 250
char* CSlimTools::intToString(int i) {
stringstream ss;
static char str[MaxAnswer];
ss << i;
strncpy(str, ss.str().c_str(), MaxAnswer - 1);
str[MaxAnswer - 1] = '\0';
return str;
}
/**
* compare line "data val1 val2 ... valN
* @return "0" if lineRigjht == LineLeft
*/
char * CSlimTools::compareLine(string lineLeft, string lineRight, double epsilon, int lineNumber, double& maxEpsilon) {
static char str[MaxAnswer];
str[0] = '\0';
boost::char_separator<char> sep(" ");
boost::tokenizer<boost::char_separator<char> > tokLeft(lineLeft, sep);
boost::tokenizer<boost::char_separator<char> > tokRight(lineRight, sep);
boost::tokenizer<boost::char_separator<char> >::iterator itLeft = tokLeft.begin();
boost::tokenizer<boost::char_separator<char> >::iterator itRight = tokRight.begin();
bool test = true;
if (itLeft != tokLeft.end() && itRight != tokRight.end()) {
string sLeft = (*itLeft);
string sRight = (*itRight);
if(sLeft !=sRight ) {
stringstream ss;
cout << "line "<< lineNumber <<": "<< sLeft << "' != '" << sRight << endl;
ss << "line "<< lineNumber <<": "<< sLeft << "' != '" << sRight;
strncpy(str, ss.str().c_str(), MaxAnswer - 1);
str[MaxAnswer - 1] = '\0';
test = false;
}
++itLeft;
++itRight;
while (itLeft != tokLeft.end() && itRight != tokRight.end()) {
string sLeft = (*itLeft);
string sRight = (*itRight);
double valLeft = atof(sLeft.c_str());
double valRight = atof(sRight.c_str());
double result = 0;
bool lTest;
if (valLeft != 0.0) {
lTest = ((result = fabs((valLeft - valRight) / valLeft)) <= epsilon);
} else if (valRight != 0.0) {
lTest = ((result = fabs((valLeft - valRight) / valRight)) <= epsilon);
}
else {
lTest = true;
}
lTest = lTest
|| ( sLeft == sRight)
|| (sLeft == "nan" && sRight =="inf")
|| (sRight == "nan" && sLeft =="inf")
|| (sLeft == "nan" && sRight =="-inf")
|| (sRight == "nan" && sLeft =="-inf")
|| (sLeft == "nan" && sRight =="-nan")
|| (sRight == "nan" && sLeft =="-nan");
if(!lTest) {
stringstream ss;
cout << "line "<< lineNumber <<": | ("<< sLeft << "' - '" << sRight<< ") / "<< sLeft << " | = " << fabs((valLeft - valRight)/ valLeft) << " >= " << epsilon << endl;
ss << "| ("<< sLeft << "' - '" << sRight<< ") / "<< sLeft << " | = " << fabs((valLeft - valRight)/ valLeft) << " > " << epsilon;
if ( result > maxEpsilon) {
strncpy(str, ss.str().c_str(), MaxAnswer - 1);
str[MaxAnswer - 1] = '\0';
maxEpsilon=result;
test = false;
}
}
++itLeft;
++itRight;
}
}
if (itLeft != tokLeft.end() || itRight != tokRight.end()) {
cout << "line "<< lineNumber <<" nb columns different" << endl;
strncpy(str, "nb columns different", MaxAnswer - 1);
test = false;
}
if(test) {
strcpy(str, "0");
}
return str;
}
char *CSlimTools::compareFile(std::string fileNameLeft,
std::string fileNameRight, double epsilon) {
static char str[MaxAnswer];
ifstream fileLeft(fileNameLeft.c_str());
ifstream fileRight(fileNameRight.c_str());
bool ret = true;
if (fileLeft.good() && fileRight.good()) {
int lineNumber = 0;
double maxEpsilon=0;
while (fileLeft.good() && fileRight.good()) {
string lineLeft;
string lineRight;
++lineNumber;
getline(fileLeft, lineLeft);
getline(fileRight, lineRight);
char *tmp =compareLine(lineLeft, lineRight, epsilon, lineNumber, maxEpsilon);
if((tmp[0] != '0' || tmp[1] != '\0')) {
stringstream ss;
ss << "line " << lineNumber << ": " << tmp;
strncpy(str, ss.str().c_str(), MaxAnswer - 1);
str[MaxAnswer - 1] = '\0';
ret = false;
}
}
if (!(fileLeft.eof()) || !(fileRight.eof())) {
cout << "line "<< lineNumber <<" nb lines different" << endl;
strcpy(str, "nb lines different");
ret = false;
}
}
else {
if(fileRight.good()) {
cout << "file '"<< fileNameLeft <<"' not found" << endl;
strcpy(str, (std::string("file '")+ fileNameLeft +"' not found").c_str() );
ret = false;
}
else {
cout << "file '"<< fileNameRight <<"' not found" << endl;
strcpy(str, (std::string("file '")+ fileNameRight +"' not found").c_str() );
ret = false;
}
}
if (ret) {
return const_cast<char *>("0");
} else {
return str;
}
}
char* CSlimTools::traitReturnSystem(int status) {
stringstream ss;
static char str[MaxAnswer];
if (WIFEXITED(status)) {
int res = WEXITSTATUS(status);
ss << (res > 124 ? res - 256 : res);
} else {
if (WIFSIGNALED(status)) {
ss << "End cause by signal " << WTERMSIG(status);
}
if (WCOREDUMP(status)) {
ss << " a core dump is generated";
}
}
strncpy(str, ss.str().c_str(), MaxAnswer - 1);
str[MaxAnswer - 1] = '\0';
return str;
}