DoubleTest.cpp 7.3 KB

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