39 #ifndef GNSSTK_TESTUTIL_HPP
40 #define GNSSTK_TESTUTIL_HPP
51 #include "build_config.h"
56 #define TUDEF(CLASS,METHOD) gnsstk::TestUtil testFramework(CLASS, METHOD, __FILE__, __LINE__)
59 #define TUCSM(METHOD) testFramework.changeSourceMethod(METHOD)
63 #define TUASSERT(EXPR) \
66 testFramework.assert(EXPR, "Assertion failure: "#EXPR, __LINE__); \
68 catch (gnsstk::Exception &exc) \
70 std::cerr << exc << std::endl; \
71 testFramework.assert(false, "Exception during "#EXPR, __LINE__); \
75 testFramework.assert(false, "Exception during "#EXPR, __LINE__); \
81 #define TUASSERTE(TYPE,EXP,GOT) \
84 testFramework.assert_equals<TYPE>(EXP, GOT, __LINE__); \
86 catch (gnsstk::Exception &exc) \
88 std::cerr << exc << std::endl; \
89 testFramework.assert(false, \
90 "Exception evaluating " #EXP " or " #GOT, \
95 testFramework.assert(false, \
96 "Exception evaluating " #EXP " or " #GOT, \
103 #define TUASSERTFE(EXP,GOT) \
106 testFramework.assert_equals(EXP, GOT, __LINE__); \
108 catch (gnsstk::Exception &exc) \
110 std::cerr << exc << std::endl; \
111 testFramework.assert(false, \
112 "Exception evaluating " #EXP " or " #GOT, \
117 testFramework.assert(false, \
118 "Exception evaluating " #EXP " or " #GOT, \
126 #define TUASSERTFEPS(EXP,GOT,EPS) \
129 testFramework.assert_equals(EXP, GOT, __LINE__, "", EPS); \
131 catch (gnsstk::Exception &exc) \
133 std::cerr << exc << std::endl; \
134 testFramework.assert(false, \
135 "Exception evaluating " #EXP " or " #GOT, \
140 testFramework.assert(false, \
141 "Exception evaluating " #EXP " or " #GOT, \
150 #define TUASSERTFESMRT(EXP,GOT) \
153 testFramework.assert_equals_fps(EXP, GOT, __LINE__, ""); \
155 catch (gnsstk::Exception &exc) \
157 std::cerr << exc << std::endl; \
158 testFramework.assert(false, \
159 "Exception evaluating " #EXP " or " #GOT, \
164 testFramework.assert(false, \
165 "Exception evaluating " #EXP " or " #GOT, \
170 #define TUCMPFILE(F1,F2,SKIP) \
173 testFramework.assert_files_equal(__LINE__, F1, F2, \
174 "File mismatch: "+F1+" "+F2, \
177 catch (gnsstk::Exception &exc) \
179 std::cerr << exc << std::endl; \
180 testFramework.assert(false, \
181 "Exception comparing " #F1 " and " #F2, \
186 testFramework.assert(false, \
187 "Exception comparing " #F1 " and " #F2, \
193 #define TUCATCH(STATEMENT) \
197 TUPASS(#STATEMENT); \
199 catch (gnsstk::Exception &exc) \
201 std::cerr << exc << std::endl; \
202 TUFAIL("Exception"); \
206 TUFAIL("Exception"); \
211 #define TUTHROW(STATEMENT) \
215 TUFAIL("Did not throw Exception"); \
217 catch (gnsstk::Exception &exc) \
219 TUPASS(#STATEMENT); \
223 TUPASS(#STATEMENT); \
228 #define TUFAIL(MSG) testFramework.assert(false, MSG, __LINE__)
230 #define TUPASS(MSG) testFramework.assert(true, MSG, __LINE__)
232 #define TURETURN() return testFramework.countFails()
240 if (
typeid(T) ==
typeid(
long double))
241 return std::string(
"long double");
242 else if (
typeid(T) ==
typeid(
double))
243 return std::string(
"double");
244 else if (
typeid(T) ==
typeid(
float))
245 return std::string(
"float");
280 TestUtil(
const std::string& sourceClassInput =
"Unknown",
281 const std::string& sourceMethodInput =
"Unknown",
282 const std::string& testFileInput =
"Unknown",
283 const int& testLineInput = 0,
284 const int& verbosityInput = 1
296 void assert(
bool testExpression,
297 const std::string& testMsg,
298 const int lineNumber );
314 const std::string& testMsg = std::string());
332 const std::string& testMsg = std::string(),
334 {assert_equals_fp<double>(expected, got, lineNumber, testMsg, epsilon);}
339 const std::string& testMsg = std::string(),
340 long double epsilon=-1)
341 {assert_equals_fp<double>(expected, got, lineNumber, testMsg, epsilon);}
346 const std::string& testMsg = std::string(),
348 {assert_equals_fp<double>(expected, got, lineNumber, testMsg, epsilon);}
350 template <
typename T>
354 const std::string& testMsg = std::string(),
367 template <
typename T>
371 const std::string& testMsg = std::string());
391 const std::string& testMsg = std::string(),
397 const std::string& testMsg = std::string(),
422 const std::string& file1Name,
423 const std::string& file2Name,
424 const std::string& testMsg,
426 bool ignoreLeadingSpaces =
false,
427 bool ignoreTrailingSpaces =
false,
428 std::vector<std::string> ignoreRegex = std::vector<std::string>(0) );
443 const std::string& file1Name,
444 const std::string& file2Name,
445 const std::string& testMsg,
446 unsigned long long from = 0,
447 unsigned long long to = -1);
472 void setTestMessage(
const std::string& testMsg,
const int lineNumber );
492 const std::string& checkFile,
494 bool ignoreLeadingSpaces =
false,
495 bool ignoreTrailingSpaces =
false,
496 std::vector<std::string> ignoreRegex = std::vector<std::string>(0) );
504 const std::string& checkFile,
505 unsigned long long from = 0,
506 unsigned long long to = -1);
559 void fail(
const std::string& failMsg );
563 void fail(
const std::string& failMsg,
const int lineNumber );
579 const std::string& sourceMethodInput,
580 const std::string& testFileInput,
581 const int& testLineInput,
582 const int& verbosityInput )
583 : outputKeyword(
"GNSSTkTest" ),
584 sourceClass( sourceClassInput ),
585 sourceMethod( sourceMethodInput ),
586 testFileName( testFileInput ),
588 testMessage(
"Developer is a lazy slacker" ),
590 verbosity( verbosityInput ),
600 std::string
file_sep = gnsstk::getFileSep();
608 const std::string& testMsg,
609 const int lineNumber )
614 if( testExpression ==
false )
633 const std::string& testMsg)
635 std::string mess(testMsg);
638 std::ostringstream ostr;
639 ostr << std::boolalpha <<
"Expected:'" << expected <<
"' ,But got:'"
643 assert(expected == got, mess, lineNumber);
652 const std::string& testMsg,
655 T
err = std::abs(expected - got);
657 epsilon = std::numeric_limits<T>::epsilon();
659 bool good =
err <= epsilon;
660 std::string mess(testMsg);
663 std::ostringstream ostr;
664 ostr << std::setprecision(20)
665 <<
"abs(" << expected <<
" - " << got <<
") = " <<
err;
673 assert(good, mess, lineNumber);
682 const std::string& testMsg )
684 T
err = std::abs(expected - got);
686 int exp = 1 + (int)std::floor(std::log10(std::fabs(expected)));
688 int digs10 = std::numeric_limits<decltype(got+expected)>::digits10;
691 double epsilon = std::pow(10, exp-digs10);
701 const std::string& testMsg,
705 epsilon = std::numeric_limits<T>::epsilon();
707 std::string mess(testMsg);
708 T mag =
maxabs(expected - got);
711 std::ostringstream ostr;
712 ostr <<
"maxabs(expected-computed) = " << mag;
724 const std::string& testMsg,
728 epsilon = std::numeric_limits<T>::epsilon();
729 std::string mess(testMsg);
730 T mag =
maxabs(expected - got);
733 std::ostringstream ostr;
734 ostr <<
"absmag(expected-computed) = " << mag;
742 const std::string& file1Name,
743 const std::string& file2Name,
744 const std::string& testMsg,
746 bool ignoreLeadingSpaces,
747 bool ignoreTrailingSpaces,
748 std::vector<std::string> ignoreRegex )
751 file1Name, file2Name, numLinesSkip, ignoreLeadingSpaces,
752 ignoreTrailingSpaces, ignoreRegex );
753 assert(eq, testMsg, lineNumber);
759 const std::string& file1Name,
760 const std::string& file2Name,
761 const std::string& testMsg,
762 unsigned long long from,
763 unsigned long long to)
766 assert(eq, testMsg, lineNumber);
808 std::ostringstream conversionStringStream;
809 conversionStringStream << lineNumber;
816 const std::string& checkFile,
818 bool ignoreLeadingSpaces,
819 bool ignoreTrailingSpaces,
820 std::vector<std::string> ignoreRegex )
823 bool filesEqual =
false;
824 std::ifstream refStream;
825 std::ifstream checkStream;
827 std::string checkLine;
829 refStream.open( refFile.c_str() );
830 checkStream.open( checkFile.c_str() );
833 while( !refStream.eof() )
839 if( checkStream.eof() )
842 return( filesEqual );
846 getline( refStream, refLine );
847 getline( checkStream, checkLine );
849 if (lineNumber <= numLinesSkip)
852 if (ignoreLeadingSpaces)
854 std::size_t idx = refLine.find_first_not_of(
" \t\r\n\f\v");
855 if (idx != std::string::npos)
856 refLine.erase(0,idx-1);
857 idx = checkLine.find_first_not_of(
" \t\r\n\f\v");
858 if (idx != std::string::npos)
859 checkLine.erase(0,idx-1);
861 if (ignoreTrailingSpaces)
863 std::size_t idx = refLine.find_last_not_of(
" \t\r\n\f\v");
864 if (idx != std::string::npos)
865 refLine.erase(idx+1);
868 idx = checkLine.find_last_not_of(
" \t\r\n\f\v");
869 if (idx != std::string::npos)
870 checkLine.erase(idx+1);
874 if (!ignoreRegex.empty())
880 for (
int i = 0; i < ignoreRegex.size(); i++)
893 if (refLine != checkLine)
896 return( filesEqual );
902 if( !checkStream.eof() )
906 std::cout <<
"refLine:" << refLine << std::endl
907 <<
"checkLine:" << checkLine << std::endl;
908 return( filesEqual );
913 return( filesEqual );
920 const std::string& checkFile,
921 unsigned long long from,
922 unsigned long long to)
924 static const unsigned bufsize = 4096;
925 unsigned readsize = bufsize;
927 std::vector<char> refBuf(bufsize, 0), checkBuf(bufsize, 0);
928 std::ifstream ref(refFile.c_str()), check(checkFile.c_str());
934 unsigned long long refSize, checkSize, curPos;
935 ref.seekg(0, std::ios_base::end);
936 refSize = ref.tellg();
937 check.seekg(0, std::ios_base::end);
938 checkSize = check.tellg();
939 if (refSize != checkSize)
945 if (!ref.seekg(from, std::ios_base::beg))
949 if (!check.seekg(from, std::ios_base::beg))
956 curPos = ref.tellg();
958 if (((curPos + readsize) > to) || (curPos > refSize))
960 readsize = 1+(to - curPos);
963 ref.read(&refBuf[0], readsize);
964 check.read(&checkBuf[0], readsize);
965 if (refBuf != checkBuf)
987 <<
"failBit=" <<
failBit <<
", "
1032 fail(
const std::string& failMsg,
const int lineNumber )
1070 #endif // GNSSTK_TESTUTIL_HPP