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