FloatTest.cpp 6.5 KB

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