FloatTest.cpp 6.2 KB

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