FloatTest.cpp 6.2 KB

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