UnitTest.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "UnitTest.h"
  2. #include <vector>
  3. #include <iostream>
  4. #include <stdexcept>
  5. #include <ctime>
  6. #include <cstdlib>
  7. std::vector<std::string> Fails;
  8. std::vector<std::string> All;
  9. bool ReturnOnFail = false;
  10. bool Echo = true;
  11. std::string Prefix;
  12. clock_t started = 0;
  13. #if (!defined(CLOCKS_PER_SEC))
  14. #define noTimeFormatting
  15. #endif
  16. std::string timing();
  17. std::string timing(){
  18. clock_t clockticks = clock() - started;
  19. std::stringstream out;
  20. if (CLOCKS_PER_SEC == 1000000){
  21. if (clockticks < 10000){
  22. out << clockticks << " microseconds";
  23. return out.str();
  24. } else if (clockticks < 10000000){
  25. out << clockticks / 1000 << " milliseconds";
  26. return out.str();
  27. }
  28. } else if (CLOCKS_PER_SEC == 1000){
  29. if (clockticks < 10000){
  30. out << clockticks << " milliseconds";
  31. return out.str();
  32. }
  33. } else {
  34. out << clockticks << " clockticks";
  35. return out.str();
  36. }
  37. #ifndef noTimeFormatting
  38. if ((CLOCKS_PER_SEC == 1000000) || (CLOCKS_PER_SEC == 1000)){
  39. clock_t seconds = clockticks / CLOCKS_PER_SEC;
  40. if (seconds < 60){
  41. out << seconds << " seconds";
  42. } else if (seconds < 7200) {
  43. out << seconds / 60 << " minutes";
  44. } else {
  45. out << seconds / 3600 << " hours";
  46. }
  47. return out.str();
  48. }
  49. #endif
  50. }
  51. void UnitTest::SelfCheck(void){
  52. assertTrue(true);
  53. assertFalse(false);
  54. assertEquals(1, 1);
  55. assertNotEquals(1, 0);
  56. assertGreaterThan(1, 0);
  57. assertGreaterThanEqualTo(1, 0);
  58. assertGreaterThanEqualTo(1, 1);
  59. assertLessThan(0, 1);
  60. assertLessThanEqualTo(0, 1);
  61. assertLessThanEqualTo(1, 1);
  62. assertCStringEquals("Hello", "Hello");
  63. assertCStringNotEquals("Hello", "World");
  64. assertCStringEqualsW(L"Hello", L"Hello");
  65. assertCStringNotEqualsW(L"Hello", L"World");
  66. std::vector<std::string> exception_Test;
  67. assertException(std::string res = exception_Test.at(15), std::out_of_range);
  68. }
  69. std::string fix(const std::string & str);
  70. std::string fix(const std::string & str){
  71. std::string fff(str);
  72. size_t pos = fff.find('\n');
  73. while(pos != std::string::npos){
  74. fff = fff.substr(0, pos) + "\\n" + fff.substr(pos + 1);
  75. pos = fff.find('\n', pos + 1);
  76. }
  77. pos = fff.find('\t');
  78. while(pos != std::string::npos){
  79. fff = fff.substr(0, pos) + "\\t" + fff.substr(pos + 1);
  80. pos = fff.find('\t', pos + 1);
  81. }
  82. pos = fff.find('\r');
  83. while(pos != std::string::npos){
  84. fff = fff.substr(0, pos) + "\\r" + fff.substr(pos + 1);
  85. pos = fff.find('\r', pos + 1);
  86. }
  87. pos = fff.find('\"');
  88. while(pos != std::string::npos){
  89. fff = fff.substr(0, pos) + "\\\"" + fff.substr(pos + 1);
  90. pos = fff.find('\"', pos + 2);
  91. }
  92. return fff;
  93. }
  94. void UnitTest::PushFailure(const std::string & fail){
  95. Fails.push_back(fail);
  96. if (test_likely(Echo)) std::cout << fail << std::endl;
  97. All.push_back(std::string("<b style=\"color:#000000;background:#FF0000\">") + fail + "</b><br>");
  98. }
  99. void UnitTest::PushSuccess(const std::string & pass){
  100. All.push_back(std::string("<b style=\"color:#000000;background:#00FF00\">") + pass + "</b><br>");
  101. }
  102. std::string UnitTest::ToString(void){
  103. std::stringstream out;
  104. out << "Number of failed tests: " << Fails.size();
  105. std::string result(out.str());
  106. for(std::vector<std::string>::iterator it = Fails.begin(), end = Fails.end(); it != end; ++it){
  107. result += *it;
  108. result += "\n";
  109. }
  110. return result;
  111. }
  112. std::string UnitTest::ToHTML(void){
  113. std::string result("<html><head><title>Test Suite Results</title></head><body><a style=\"font-size:14\">");
  114. std::stringstream out;
  115. out << "Passed Tests: <c style=\"color:#00CC00\">" << All.size() - Fails.size() << "</c><br>Failed Tests: <c style=\"color:#CC0000\">" << Fails.size() << "</c><br>Total Tests: " << All.size() << "<br>";
  116. if (test_likely(started)){
  117. out << "Elapsed time: " << timing() << "<br><br>";
  118. } else {
  119. out << "<br>";
  120. }
  121. result += out.str();
  122. for(std::vector<std::string>::iterator it = All.begin(), end = All.end(); it != end; ++it){
  123. result += *it;
  124. }
  125. return result + "</a></body></html>";
  126. }
  127. #include <iostream>
  128. #include <cstdio>
  129. void UnitTest::SaveTo(const std::string & location){
  130. FILE * fp = fopen(location.c_str(), "w");
  131. if (test_likely(fp != 0)){
  132. std::string html(ToHTML());
  133. fwrite(html.c_str(), html.length(), 1, fp);
  134. fclose(fp);
  135. system("pwd");
  136. std::cout << "Saved file to " << location << std::endl;
  137. } else {
  138. std::cout << "Couldn't save file" << std::endl;
  139. }
  140. if (test_likely(Echo)) std::cout << "Passed tests: " << All.size() - Fails.size() << std::endl << "Failed tests: " << Fails.size() << std::endl;
  141. }
  142. bool UnitTest::GetReturnOnFail(void){ return ReturnOnFail; }
  143. void UnitTest::SetReturnOnFail(bool option){ ReturnOnFail = option; }
  144. void UnitTest::SetEcho(bool option){ Echo = option; }
  145. void UnitTest::SetPrefix(const std::string & prefix){
  146. std::cout << prefix << std::endl;
  147. Prefix = prefix;
  148. }
  149. std::string UnitTest::GetPrefix(void){ return Prefix; }
  150. void UnitTest::echo_(const std::string & out){
  151. All.push_back(fix(out) + "<br>");
  152. std::cout << out << std::endl;
  153. }
  154. void UnitTest::StartTime(void){
  155. started = clock();
  156. }