FloatTest.cpp 6.5 KB

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