56 #if LOG_ERRORS_TO_FILE
57 static std::ofstream cerr_log_file(
"SpartaTester_errors.log");
58 if (cerr_log_file.is_open()) {
59 cerr_.rdbuf(cerr_log_file.rdbuf());
61 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED
62 <<
"WARNING: Could not open SpartaTester_errors.log for writing. "
63 <<
"Errors will be printed to std::cerr."
69 bool expectAllReached(uint32_t expected_reached,
const uint32_t line,
const char * file) {
71 if(methods_reached_.size() != expected_reached)
73 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Test failed to execute the "
74 << expected_reached <<
" expected methods at least once." <<
"\n"
75 "Instead, " << methods_reached_.size() <<
" were reached."
78 cerr_ <<
"The test only reached the following: " << std::endl;
79 cerr_ << SPARTA_CURRENT_COLOR_GREEN;
80 for(
auto s : methods_reached_)
82 cerr_ <<
"-> " <<s <<
"\n";
84 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"' FAILED on line "
92 bool expect(
bool val,
const char * test_type,
const uint32_t line,
const char * file) {
95 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Test '" << test_type <<
"' FAILED on line "
104 bool expectEqual(uint8_t v1, uint8_t v2,
bool expected,
const char * test_type,
const uint32_t line,
const char * file) {
106 if((v1 == v2) != expected) {
107 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Test '" << test_type <<
"' FAILED on line "
108 << line <<
" in file " << file
109 <<
". Value: '" << (uint32_t)v1;
111 cerr_ <<
"' should equal '";
113 cerr_ <<
"' should NOT equal '";
124 bool expectEqual(
const T& v1,
const T& v2,
bool expected,
const char * test_type,
const uint32_t line,
const char * file) {
126 if((v1 == v2) != expected) {
127 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Test '" << test_type <<
"' FAILED on line "
128 << line <<
" in file " << file
129 <<
". Value: '" << v1;
131 cerr_ <<
"' should equal '";
133 cerr_ <<
"' should NOT equal '";
143 template<
typename T,
typename U=T>
144 bool expectEqual(
const T& v1,
const U& v2,
bool expected,
const char * test_type,
const uint32_t line,
const char * file) {
146 if(compare<T,U>(v1,v2) != expected) {
147 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Test '" << test_type <<
"' FAILED on line "
148 << line <<
" in file " << file
149 <<
". Value: '" << v1;
151 cerr_ <<
"' should equal '";
153 cerr_ <<
"' should NOT equal '";
163 template<
typename T,
typename U>
164 typename std::enable_if<std::is_integral<T>::value
165 && std::is_integral<U>::value
166 && (std::is_signed<T>::value != std::is_signed<U>::value),
bool>::type
167 compare(
const T& t,
const U& u) {
172 template<
typename T,
typename U>
173 typename std::enable_if<!(std::is_integral<T>::value
174 && std::is_integral<U>::value
175 && (std::is_signed<T>::value != std::is_signed<U>::value)),
bool>::type
176 compare(
const T& t,
const U& u) {
183 bool expectEqual(
const T& v1,
const std::nullptr_t,
bool expected,
const char * test_type,
const uint32_t line,
const char * file) {
185 if((v1 ==
nullptr) != expected) {
186 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Test '" << test_type <<
"' FAILED on line "
187 << line <<
" in file " << file
188 <<
". Value: '" << v1;
190 cerr_ <<
"' should equal '";
192 cerr_ <<
"' should NOT equal '";
203 bool expectEqual(
const std::nullptr_t,
const T& v1,
bool expected,
const char * test_type,
const uint32_t line,
const char * file) {
205 if((
nullptr == v1) != expected) {
206 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Test '" << test_type <<
"' FAILED on line "
207 << line <<
" in file " << file
208 <<
". Value: '" << v1;
210 cerr_ <<
"' should equal '";
212 cerr_ <<
"' should NOT equal '";
222 typename std::enable_if<
223 std::is_floating_point<T>::value,
225 expectEqualWithinTolerance(
const T & v1,
const T & v2,
const T & tol,
226 const char * test_type,
const uint32_t line,
230 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Test '"
231 << test_type <<
"' FAILED on line "
232 << line <<
" in file " << file
233 <<
". Negative tolerance supplied."
238 ret = utils::approximatelyEqual(v1, v2, tol);
240 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Test '"
241 << test_type <<
"' FAILED on line "
242 << line <<
" in file " << file
243 <<
". Value: '" << v1 <<
"' should be equal to '"
244 << v2 <<
"' within tolerance '" << tol <<
"'";
251 void throwTestFailed(
const char * test_type,
const uint32_t line,
const char * file,
const char * exception_what=
"") {
252 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Throw Test Fail:'" << test_type <<
"' FAILED on line "
253 << line <<
" in file " << file << std::endl;
254 if(exception_what != 0 && strlen(exception_what) != 0) {
255 cerr_ <<
" Exception: " << exception_what << std::endl;
280 void expectFilesEqual(
const std::string& a,
const std::string& b,
bool expected, uint32_t line,
const char * file,
const bool ignore_commented_lines =
true) {
281 std::ifstream fa, fb;
282 std::stringstream err;
284 fa.open(a, std::ios_base::in);
285 }
catch(std::exception&){
289 err <<
"Could not open file \"" << a <<
"\"";
290 fileComparisonFailed(a, b, line, file, err.str());
293 fb.open(b, std::ios_base::in);
294 }
catch(std::exception&){
298 err <<
"Could not open file \"" << b <<
"\"";
299 fileComparisonFailed(a, b, line, file, err.str());
302 if(!fa.fail() && !fb.fail()){
303 uint32_t line_num = 0;
304 uint32_t last_line_pos = 0;
306 bool was_newline =
true;
313 if(was_newline && ignore_commented_lines){
342 if(!fa.good() || !fb.good()){
343 if((fa.good() != fb.good()) && expected ==
true){
344 std::stringstream msg;
345 msg <<
"Files were different lengths: ";
347 msg << a <<
" was shorter than " << b <<
" at char '" << chn <<
"' #" << pos;
349 msg << b <<
" was shorter than " << a <<
" at char '" << cho <<
"' #" << pos;
351 fileComparisonFailed(a, b, line, file, msg.str());
358 err <<
"Files differed at pos " << pos <<
" (line "
359 << line_num <<
", col " << pos - last_line_pos
360 <<
") with chars: '" << cho <<
"' != '" << chn <<
"'";
361 if(expected ==
true){
362 fileComparisonFailed(a, b, line, file, err.str());
374 if(expected ==
false){
375 fileComparisonFailed(a, b, line, file,
"Files were the same");
380 void fileComparisonFailed(
const std::string& a,
const std::string& b,
const uint32_t line,
const char * file,
const std::string& error) {
381 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"File comparison test between \"" << a <<
"\" and \"" << b <<
"\" FAILED on line "
382 << line <<
" in file " << file << std::endl;
383 cerr_ <<
" Exception: " << error << std::endl;
388 void reachedMethod(
const std::string& method_title)
390 methods_reached_.insert(method_title);
392 static SpartaTester * getInstance() {
393 static SpartaTester inst;
397 static std::unique_ptr<SpartaTester> makeTesterWithUserCError(std::ostream & cerr) {
398 return std::unique_ptr<SpartaTester>(
new SpartaTester(0, cerr));
401 static uint32_t getErrorCode(
const SpartaTester * tester = getInstance()) {
402 return tester->num_errors_;
406 SpartaTester(
const uint32_t num_errors,
407 std::ostream & cerr) :
408 num_errors_(num_errors),
413 uint32_t num_errors_;
414 std::set<std::string> methods_reached_;
415 std::ostream & cerr_;