FloatTest.cpp 6.1 KB

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