57 bool expectAllReached(uint32_t expected_reached,
const uint32_t line,
const char * file) {
59 if(methods_reached_.size() != expected_reached)
61 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Test failed to execute the "
62 << expected_reached <<
" expected methods at least once." <<
"\n"
63 "Instead, " << methods_reached_.size() <<
" were reached."
66 cerr_ <<
"The test only reached the following: " << std::endl;
67 cerr_ << SPARTA_CURRENT_COLOR_GREEN;
68 for(
auto s : methods_reached_)
70 cerr_ <<
"-> " <<s <<
"\n";
72 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"' FAILED on line "
80 bool expect(
bool val,
const char * test_type,
const uint32_t line,
const char * file) {
83 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Test '" << test_type <<
"' FAILED on line "
92 bool expectEqual(uint8_t v1, uint8_t v2,
bool expected,
const char * test_type,
const uint32_t line,
const char * file) {
94 if((v1 == v2) != expected) {
95 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Test '" << test_type <<
"' FAILED on line "
96 << line <<
" in file " << file
97 <<
". Value: '" << (uint32_t)v1;
99 cerr_ <<
"' should equal '";
101 cerr_ <<
"' should NOT equal '";
112 bool expectEqual(
const T& v1,
const T& v2,
bool expected,
const char * test_type,
const uint32_t line,
const char * file) {
114 if((v1 == v2) != expected) {
115 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Test '" << test_type <<
"' FAILED on line "
116 << line <<
" in file " << file
117 <<
". Value: '" << v1;
119 cerr_ <<
"' should equal '";
121 cerr_ <<
"' should NOT equal '";
131 template<
typename T,
typename U=T>
132 bool expectEqual(
const T& v1,
const U& v2,
bool expected,
const char * test_type,
const uint32_t line,
const char * file) {
134 if(compare<T,U>(v1,v2) != expected) {
135 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Test '" << test_type <<
"' FAILED on line "
136 << line <<
" in file " << file
137 <<
". Value: '" << v1;
139 cerr_ <<
"' should equal '";
141 cerr_ <<
"' should NOT equal '";
151 template<
typename T,
typename U>
152 typename std::enable_if<std::is_integral<T>::value
153 && std::is_integral<U>::value
154 && (std::is_signed<T>::value != std::is_signed<U>::value),
bool>::type
155 compare(
const T& t,
const U& u) {
160 template<
typename T,
typename U>
161 typename std::enable_if<!(std::is_integral<T>::value
162 && std::is_integral<U>::value
163 && (std::is_signed<T>::value != std::is_signed<U>::value)),
bool>::type
164 compare(
const T& t,
const U& u) {
171 bool expectEqual(
const T& v1,
const std::nullptr_t,
bool expected,
const char * test_type,
const uint32_t line,
const char * file) {
173 if((v1 ==
nullptr) != expected) {
174 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Test '" << test_type <<
"' FAILED on line "
175 << line <<
" in file " << file
176 <<
". Value: '" << v1;
178 cerr_ <<
"' should equal '";
180 cerr_ <<
"' should NOT equal '";
191 bool expectEqual(
const std::nullptr_t,
const T& v1,
bool expected,
const char * test_type,
const uint32_t line,
const char * file) {
193 if((
nullptr == v1) != expected) {
194 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Test '" << test_type <<
"' FAILED on line "
195 << line <<
" in file " << file
196 <<
". Value: '" << v1;
198 cerr_ <<
"' should equal '";
200 cerr_ <<
"' should NOT equal '";
210 typename std::enable_if<
211 std::is_floating_point<T>::value,
213 expectEqualWithinTolerance(
const T & v1,
const T & v2,
const T & tol,
214 const char * test_type,
const uint32_t line,
218 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Test '"
219 << test_type <<
"' FAILED on line "
220 << line <<
" in file " << file
221 <<
". Negative tolerance supplied."
226 ret = simdb::utils::approximatelyEqual(v1, v2, tol);
228 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Test '"
229 << test_type <<
"' FAILED on line "
230 << line <<
" in file " << file
231 <<
". Value: '" << v1 <<
"' should be equal to '"
232 << v2 <<
"' within tolerance '" << tol <<
"'";
239 void throwTestFailed(
const char * test_type,
const uint32_t line,
const char * file,
const char * exception_what=
"") {
240 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"Throw Test Fail:'" << test_type <<
"' FAILED on line "
241 << line <<
" in file " << file << std::endl;
242 if(exception_what != 0 && strlen(exception_what) != 0) {
243 cerr_ <<
" Exception: " << exception_what << std::endl;
268 void expectFilesEqual(
const std::string& a,
const std::string& b,
bool expected, uint32_t line,
const char * file,
const bool ignore_commented_lines =
true) {
269 std::ifstream fa, fb;
270 std::stringstream err;
272 fa.open(a, std::ios_base::in);
273 }
catch(std::exception&){
277 err <<
"Could not open file \"" << a <<
"\"";
278 fileComparisonFailed(a, b, line, file, err.str());
281 fb.open(b, std::ios_base::in);
282 }
catch(std::exception&){
286 err <<
"Could not open file \"" << b <<
"\"";
287 fileComparisonFailed(a, b, line, file, err.str());
290 if(!fa.fail() && !fb.fail()){
291 uint32_t line_num = 0;
292 uint32_t last_line_pos = 0;
294 bool was_newline =
true;
301 if(was_newline && ignore_commented_lines){
330 if(!fa.good() || !fb.good()){
331 if((fa.good() != fb.good()) && expected ==
true){
332 std::stringstream msg;
333 msg <<
"Files were different lengths: ";
335 msg << a <<
" was shorter than " << b <<
" at char '" << chn <<
"' #" << pos;
337 msg << b <<
" was shorter than " << a <<
" at char '" << cho <<
"' #" << pos;
339 fileComparisonFailed(a, b, line, file, msg.str());
346 err <<
"Files differed at pos " << pos <<
" (line "
347 << line_num <<
", col " << pos - last_line_pos
348 <<
") with chars: '" << cho <<
"' != '" << chn <<
"'";
349 if(expected ==
true){
350 fileComparisonFailed(a, b, line, file, err.str());
362 if(expected ==
false){
363 fileComparisonFailed(a, b, line, file,
"Files were the same");
368 void fileComparisonFailed(
const std::string& a,
const std::string& b,
const uint32_t line,
const char * file,
const std::string& error) {
369 cerr_ << SPARTA_CURRENT_COLOR_BRIGHT_RED <<
"File comparison test between \"" << a <<
"\" and \"" << b <<
"\" FAILED on line "
370 << line <<
" in file " << file << std::endl;
371 cerr_ <<
" Exception: " << error << std::endl;
376 void reachedMethod(
const std::string& method_title)
378 methods_reached_.insert(method_title);
380 static SpartaTester * getInstance() {
381 static SpartaTester inst;
385 static std::unique_ptr<SpartaTester> makeTesterWithUserCError(std::ostream & cerr) {
386 return std::unique_ptr<SpartaTester>(
new SpartaTester(0, cerr));
389 static uint32_t getErrorCode(
const SpartaTester * tester = getInstance()) {
390 return tester->num_errors_;
394 SpartaTester(
const uint32_t num_errors,
395 std::ostream & cerr) :
396 num_errors_(num_errors),
401 uint32_t num_errors_;
402 std::set<std::string> methods_reached_;
403 std::ostream & cerr_;