FloatTest.cpp 7.0 KB

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