Newer
Older
c-interpreter / modules / libkcpp / src / kcpp_assert.cpp
Nomura Kei on 9 Aug 2023 4 KB UPDATE
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Assert
  4. //
  5.  
  6. #include <sstream>
  7.  
  8. #include <kcpp_assert.hpp>
  9.  
  10. #undef assertEquals
  11. #undef assertTrue
  12. #undef assertFalse
  13. #undef assertNull
  14. #undef assertNotNull
  15. #undef fail
  16.  
  17. namespace kcpp
  18. {
  19.  
  20. AssertError::AssertError() noexcept : Error(), errorFile(""), errorFunc(""), errorLine(0)
  21. {
  22. // NOP
  23. }
  24.  
  25. AssertError::AssertError(const AssertError& t) noexcept : Error(t), errorFile(t.errorFile), errorFunc(t.errorFunc), errorLine(t.errorLine)
  26. {
  27. // NOP
  28. }
  29.  
  30. AssertError::AssertError(const std::string& msg) noexcept : Error(msg), errorFile(""), errorFunc(""), errorLine(0)
  31. {
  32. // NOP
  33. }
  34.  
  35. AssertError::AssertError(const std::string& msg, const char* file, const char* func, int line) noexcept
  36. : Error(msg), errorFile(file), errorFunc(func), errorLine(line)
  37. {
  38. // NOP
  39. }
  40.  
  41. AssertError::~AssertError() noexcept
  42. {
  43. // NOP
  44. }
  45.  
  46. const std::string& AssertError::getFile() const noexcept
  47. {
  48. return errorFile;
  49. }
  50.  
  51. const std::string& AssertError::getFunc() const noexcept
  52. {
  53. return errorFunc;
  54. }
  55.  
  56. int AssertError::getLine() const noexcept
  57. {
  58. return errorLine;
  59. }
  60.  
  61.  
  62. namespace Assert
  63. {
  64. void assertEquals(bool expected, bool actual, const char* file, const char* func, int line)
  65. {
  66. if (expected != actual)
  67. {
  68. const char* msg = (expected)
  69. ? "expected:<true> but was:<false>"
  70. : "expected:<false> but was:<true>";
  71. throw AssertError(msg, file, func, line);
  72. }
  73. }
  74.  
  75. void assertEquals(char expected, char actual, const char* file, const char* func, int line)
  76. {
  77. if (expected != actual)
  78. {
  79. std::ostringstream msg;
  80. msg << "expected:<" << expected << "> but was:<" << actual << ">";
  81. throw AssertError(msg.str(), file, func, line);
  82. }
  83. }
  84.  
  85. void assertEquals(int expected, int actual, const char* file, const char* func, int line)
  86. {
  87. if (expected != actual)
  88. {
  89. std::ostringstream msg;
  90. msg << "expected:<" << expected << "> but was:<" << actual << ">";
  91. throw AssertError(msg.str(), file, func, line);
  92. }
  93. }
  94.  
  95. void assertEquals(long expected, long actual, const char* file, const char* func, int line)
  96. {
  97. if (expected != actual)
  98. {
  99. std::ostringstream msg;
  100. msg << "expected:<" << expected << "> but was:<" << actual << ">";
  101. throw AssertError(msg.str(), file, func, line);
  102. }
  103. }
  104.  
  105. void assertEquals(double expected, double actual, const char* file, const char* func, int line)
  106. {
  107. if (expected != actual)
  108. {
  109. std::ostringstream msg;
  110. msg << "expected:<" << expected << "> but was:<" << actual << ">";
  111. throw AssertError(msg.str(), file, func, line);
  112. }
  113. }
  114.  
  115. void assertEquals(const std::string& expected, const std::string& actual, const char* file, const char* func, int line)
  116. {
  117. if (expected != actual)
  118. {
  119. std::ostringstream msg;
  120. msg << "expected:<" << expected << "> but was:<" << actual << ">";
  121. throw AssertError(msg.str(), file, func, line);
  122. }
  123. }
  124.  
  125. void assertEquals(const char* expected, const std::string& actual, const char* file, const char* func, int line)
  126. {
  127. std::string expectedStr = expected;
  128. assertEquals(expectedStr, actual, file, func, line);
  129. }
  130.  
  131. void assertEquals(const std::string& expected, const char* actual, const char* file, const char* func, int line)
  132. {
  133. std::string actualStr = actual;
  134. assertEquals(expected, actualStr, file, func, line);
  135. }
  136.  
  137. void assertEquals(const char* expected, const char* actual, const char* file, const char* func, int line)
  138. {
  139. std::string expectedStr = expected;
  140. std::string actualStr = actual;
  141. assertEquals(expectedStr, actualStr, file, func, line);
  142. }
  143.  
  144. void assertTrue(bool condition, const char* file, const char* func, int line)
  145. {
  146. assertEquals(true, condition, file, func, line);
  147. }
  148.  
  149. void assertFalse(bool condition, const char* file, const char* func, int line)
  150. {
  151. assertEquals(false, condition, file, func, line);
  152. }
  153.  
  154. void assertNull(void* obj, const char* file, const char* func, int line)
  155. {
  156. if (obj != nullptr)
  157. {
  158. throw AssertError("expected:<null> but was:<not null>", file, func, line);
  159. }
  160. }
  161.  
  162. void assertNotNull(void* obj, const char* file, const char* func, int line)
  163. {
  164. if (obj == nullptr)
  165. {
  166. throw AssertError("expected:<not null> but was:<not>", file, func, line);
  167. }
  168. }
  169.  
  170. void fail(const char* file, const char* func, int line)
  171. {
  172. throw AssertError("fail()", file, func, line);
  173. }
  174.  
  175. }
  176.  
  177. }