DoubleTest.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2021-07-12
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. #include <TestHelper.hpp>
  12. namespace
  13. {
  14. class DoubleTest : public ::testing::Test
  15. {
  16. protected:
  17. DoubleTest() = default;
  18. ~DoubleTest() override = default;
  19. void SetUp() override
  20. {}
  21. void TearDown() override
  22. {}
  23. };
  24. // assignment operators
  25. TEST_F(DoubleTest, operator_assignment)
  26. {
  27. ls_std::Double x{};
  28. x = 44.22;
  29. ASSERT_EQ(44.22, x);
  30. }
  31. // arithmetic operators
  32. TEST_F(DoubleTest, operator_negative)
  33. {
  34. ls_std::Double x{3.25};
  35. ASSERT_DOUBLE_EQ(-3.25, -x);
  36. }
  37. TEST_F(DoubleTest, operator_addition_with_reference)
  38. {
  39. ls_std::Double x{3.1415};
  40. ls_std::Double y{2.223};
  41. ls_std::Double z{x + y};
  42. ASSERT_DOUBLE_EQ(5.3645, z);
  43. }
  44. TEST_F(DoubleTest, operator_addition_with_value)
  45. {
  46. ls_std::Double x{3.1415};
  47. ASSERT_DOUBLE_EQ(5.3645, x + 2.223);
  48. }
  49. TEST_F(DoubleTest, operator_multiplication_with_reference)
  50. {
  51. ls_std::Double x{3.14};
  52. ls_std::Double y{2.22};
  53. ls_std::Double z{x * y};
  54. ASSERT_DOUBLE_EQ(6.9708, z);
  55. }
  56. TEST_F(DoubleTest, operator_multiplication_with_value)
  57. {
  58. ls_std::Double x{3.14};
  59. ASSERT_DOUBLE_EQ(6.9708, x * 2.22);
  60. }
  61. TEST_F(DoubleTest, operator_substraction_with_reference)
  62. {
  63. ls_std::Double x{3.1415};
  64. ls_std::Double y{2.225};
  65. ls_std::Double z{x - y};
  66. ASSERT_DOUBLE_EQ(0.9165, z);
  67. }
  68. TEST_F(DoubleTest, operator_substraction_with_value)
  69. {
  70. ls_std::Double x{3.1415};
  71. ASSERT_DOUBLE_EQ(0.9165, x - 2.225);
  72. }
  73. TEST_F(DoubleTest, operator_division_with_reference)
  74. {
  75. ls_std::Double x{2.25};
  76. ls_std::Double y{0.5};
  77. ls_std::Double z{x / y};
  78. ASSERT_DOUBLE_EQ(4.5, z);
  79. }
  80. TEST_F(DoubleTest, operator_division_with_value)
  81. {
  82. ls_std::Double x{2.25};
  83. ASSERT_DOUBLE_EQ(4.5, x / 0.5);
  84. }
  85. // compound operators
  86. TEST_F(DoubleTest, operator_add_assign_with_reference)
  87. {
  88. ls_std::Double x{2.25000000};
  89. ls_std::Double y{3.14000000};
  90. x += y;
  91. ASSERT_DOUBLE_EQ(5.39000000, x);
  92. }
  93. TEST_F(DoubleTest, operator_add_assign_with_value)
  94. {
  95. ls_std::Double x{2.25000000};
  96. x += 3.14000000;
  97. ASSERT_DOUBLE_EQ(5.39000000, x);
  98. }
  99. TEST_F(DoubleTest, operator_sub_assign_with_reference)
  100. {
  101. ls_std::Double x{2.25};
  102. ls_std::Double y{0.04};
  103. x -= y;
  104. ASSERT_DOUBLE_EQ(2.21, x);
  105. }
  106. TEST_F(DoubleTest, operator_sub_assign_with_value)
  107. {
  108. ls_std::Double x{2.25};
  109. x -= 0.04;
  110. ASSERT_DOUBLE_EQ(2.21, x);
  111. }
  112. TEST_F(DoubleTest, operator_mul_assign_with_reference)
  113. {
  114. ls_std::Double x{2.25000000};
  115. ls_std::Double y{0.04000000};
  116. x *= y;
  117. ASSERT_DOUBLE_EQ(0.09000000, x);
  118. }
  119. TEST_F(DoubleTest, operator_mul_assign_with_value)
  120. {
  121. ls_std::Double x{2.25000000};
  122. x *= 0.04000000;
  123. ASSERT_DOUBLE_EQ(0.09000000, x);
  124. }
  125. TEST_F(DoubleTest, operator_division_assign_with_reference)
  126. {
  127. ls_std::Double x{2.25};
  128. ls_std::Double y{0.05};
  129. x /= y;
  130. ASSERT_DOUBLE_EQ(45.0, x);
  131. }
  132. TEST_F(DoubleTest, operator_division_assign_with_value)
  133. {
  134. ls_std::Double x{2.25};
  135. x /= 0.05;
  136. ASSERT_DOUBLE_EQ(45.0, x);
  137. }
  138. // comparison operators
  139. TEST_F(DoubleTest, operator_equals_with_reference)
  140. {
  141. ls_std::Double x{3.14159};
  142. ls_std::Double y{3.14159};
  143. ASSERT_TRUE(x == y);
  144. ASSERT_TRUE(y == x);
  145. }
  146. TEST_F(DoubleTest, operator_equals_with_value)
  147. {
  148. ls_std::Double x{3.14159};
  149. ASSERT_TRUE(x == 3.14159);
  150. ASSERT_TRUE(3.14159 == x);
  151. }
  152. TEST_F(DoubleTest, operator_not_equal_with_reference)
  153. {
  154. ls_std::Double x{3.1415};
  155. ls_std::Double y{3.1414};
  156. ASSERT_TRUE(x != y);
  157. ASSERT_TRUE(y != x);
  158. }
  159. TEST_F(DoubleTest, operator_not_equal_with_value)
  160. {
  161. ls_std::Double x{3.1415};
  162. ASSERT_TRUE(x != 3.1414);
  163. ASSERT_TRUE(3.1414 != x);
  164. }
  165. TEST_F(DoubleTest, operator_greater_than_with_reference)
  166. {
  167. ls_std::Double x{3.1415};
  168. ls_std::Double y{3.1414};
  169. ASSERT_TRUE(x > y);
  170. }
  171. TEST_F(DoubleTest, operator_greater_than_with_value)
  172. {
  173. ls_std::Double x{3.1415};
  174. ls_std::Double y{3.1414};
  175. ASSERT_TRUE(x > 3.1414);
  176. }
  177. TEST_F(DoubleTest, operator_greater_than_equals_with_reference)
  178. {
  179. ls_std::Double x{3.1414};
  180. ls_std::Double y{3.1414};
  181. ls_std::Double z{3.1415};
  182. ASSERT_TRUE(x >= y);
  183. ASSERT_TRUE(z >= y);
  184. }
  185. TEST_F(DoubleTest, operator_greater_than_equals_with_value)
  186. {
  187. ls_std::Double x{3.1414};
  188. ASSERT_TRUE(x >= 3.1414);
  189. }
  190. TEST_F(DoubleTest, operator_less_than_with_reference)
  191. {
  192. ls_std::Double x{3.1413};
  193. ls_std::Double y{3.1414};
  194. ASSERT_TRUE(x < y);
  195. }
  196. TEST_F(DoubleTest, operator_less_than_with_value)
  197. {
  198. ls_std::Double x{3.1413};
  199. ls_std::Double y{3.1414};
  200. ASSERT_TRUE(x < 3.1414);
  201. }
  202. TEST_F(DoubleTest, operator_less_than_equals_with_reference)
  203. {
  204. ls_std::Double x{3.1414};
  205. ls_std::Double y{3.1414};
  206. ls_std::Double z{3.1415};
  207. ASSERT_TRUE(x <= y);
  208. ASSERT_TRUE(x <= z);
  209. }
  210. TEST_F(DoubleTest, operator_less_than_equals_with_value)
  211. {
  212. ls_std::Double x{3.1414};
  213. ASSERT_TRUE(x <= 3.1414);
  214. }
  215. // increment / decrement operator
  216. TEST_F(DoubleTest, operator_increment)
  217. {
  218. ls_std::Double x{3.1415};
  219. ++x;
  220. ASSERT_DOUBLE_EQ(4.1415, x);
  221. }
  222. TEST_F(DoubleTest, operator_decrement)
  223. {
  224. ls_std::Double x{3.1415};
  225. --x;
  226. ASSERT_DOUBLE_EQ(2.1415, x);
  227. }
  228. // implementation
  229. TEST_F(DoubleTest, parse_with_positive_value)
  230. {
  231. ls_std::Double x{};
  232. x.parse("3.1415");
  233. ASSERT_DOUBLE_EQ(3.1415, x);
  234. }
  235. TEST_F(DoubleTest, parse_with_negative_value)
  236. {
  237. ls_std::Double x{};
  238. x.parse("-2.1415");
  239. ASSERT_DOUBLE_EQ(-2.1415, x);
  240. }
  241. TEST_F(DoubleTest, toString)
  242. {
  243. ls_std::Double x{13.1543};
  244. ASSERT_TRUE(x.toString().find("13.1543") != std::string::npos);
  245. }
  246. // additional functionality
  247. TEST_F(DoubleTest, getEpsilon)
  248. {
  249. ls_std::Double x{};
  250. ASSERT_DOUBLE_EQ(0.00000001, x.getEpsilon());
  251. }
  252. TEST_F(DoubleTest, getValue)
  253. {
  254. ls_std::Double x{3.1415};
  255. ASSERT_DOUBLE_EQ(3.1415, x.getValue());
  256. }
  257. TEST_F(DoubleTest, setEpsilon)
  258. {
  259. ls_std::Double x{};
  260. x.setEpsilon(0.01);
  261. ASSERT_DOUBLE_EQ(0.01, x.getEpsilon());
  262. }
  263. TEST_F(DoubleTest, setEpsilon_invalid_value)
  264. {
  265. ls_std::Double x{};
  266. EXPECT_THROW({
  267. try
  268. {
  269. x.setEpsilon(0.0);
  270. }
  271. catch (const ls_std::IllegalArgumentException &_exception)
  272. {
  273. throw;
  274. }
  275. }, ls_std::IllegalArgumentException);
  276. }
  277. }