DoubleTest.cpp 6.7 KB

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