FloatTest.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2020-08-14
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include "../../../source/boxing/Float.hpp"
  11. namespace {
  12. class FloatTest : public ::testing::Test {
  13. protected:
  14. FloatTest() = default;
  15. ~FloatTest() override = default;
  16. void SetUp() override {}
  17. void TearDown() override {}
  18. };
  19. // assignment operators
  20. TEST_F(FloatTest, operatorAssignment)
  21. {
  22. ls_std::Float x {13.023f};
  23. ASSERT_EQ(13.023f, x);
  24. x = 44.22f;
  25. ASSERT_EQ(44.22f, x);
  26. ls_std::Float y {3.0f};
  27. x = y;
  28. ASSERT_EQ(3.0f, x);
  29. }
  30. // arithmetic operators
  31. TEST_F(FloatTest, operatorHyphen)
  32. {
  33. ls_std::Float x {3.25f};
  34. ASSERT_FLOAT_EQ(-3.25f, -x);
  35. }
  36. TEST_F(FloatTest, operatorAddition)
  37. {
  38. ls_std::Float x {3.1415f};
  39. ls_std::Float y {2.223f};
  40. ls_std::Float z {x + y};
  41. ASSERT_FLOAT_EQ(5.3645f, z);
  42. ASSERT_FLOAT_EQ(5.3645f, x + 2.223f);
  43. }
  44. TEST_F(FloatTest, operatorMultiplication)
  45. {
  46. ls_std::Float x {3.14f};
  47. ls_std::Float y {2.22f};
  48. ls_std::Float z {x * y};
  49. ASSERT_FLOAT_EQ(6.9708f, z);
  50. ASSERT_FLOAT_EQ(6.9708f, x * 2.22f);
  51. }
  52. TEST_F(FloatTest, operatorSubstraction)
  53. {
  54. ls_std::Float x {3.1415f};
  55. ls_std::Float y {2.225f};
  56. ls_std::Float z {x - y};
  57. ASSERT_FLOAT_EQ(0.9165f, z);
  58. ASSERT_FLOAT_EQ(0.9165f, x - 2.225f);
  59. }
  60. TEST_F(FloatTest, operatorDivision)
  61. {
  62. ls_std::Float x {2.25f};
  63. ls_std::Float y {0.5f};
  64. ls_std::Float z {x / y};
  65. ASSERT_FLOAT_EQ(4.5f, z);
  66. ASSERT_FLOAT_EQ(4.5f, x / 0.5f);
  67. }
  68. // compound operators
  69. TEST_F(FloatTest, operatorAddEqual)
  70. {
  71. ls_std::Float x {2.25f};
  72. ls_std::Float y {-0.39f};
  73. ASSERT_FLOAT_EQ(2.25f, x);
  74. x += 3.14f;
  75. ASSERT_FLOAT_EQ(5.39f, x);
  76. x += y;
  77. ASSERT_FLOAT_EQ(5.0f, x);
  78. }
  79. TEST_F(FloatTest, operatorSubEqual)
  80. {
  81. ls_std::Float x {2.25f};
  82. ls_std::Float y {-0.04f};
  83. ASSERT_FLOAT_EQ(2.25f, x);
  84. x -= 1.14f;
  85. ASSERT_FLOAT_EQ(1.11f, x);
  86. x -= y;
  87. ASSERT_FLOAT_EQ(1.15f, x);
  88. }
  89. TEST_F(FloatTest, operatorMulEqual)
  90. {
  91. ls_std::Float x {2.25f};
  92. ls_std::Float y {0.04f};
  93. ASSERT_FLOAT_EQ(2.25f, x);
  94. x *= 1.14f;
  95. ASSERT_FLOAT_EQ(2.565f, x);
  96. x *= y;
  97. ASSERT_FLOAT_EQ(0.1026f, x);
  98. }
  99. TEST_F(FloatTest, operatorDivEqual)
  100. {
  101. ls_std::Float x {2.25f};
  102. ls_std::Float y {1.5f};
  103. ASSERT_FLOAT_EQ(2.25f, x);
  104. x /= 0.05f;
  105. ASSERT_FLOAT_EQ(45.0f, x);
  106. x /= y;
  107. ASSERT_FLOAT_EQ(30.0f, x);
  108. }
  109. // comparison operators
  110. TEST_F(FloatTest, operatorEqual)
  111. {
  112. ls_std::Float x {3.14159f};
  113. ls_std::Float y {3.14159f};
  114. ASSERT_TRUE(x == y);
  115. ASSERT_TRUE(y == x);
  116. ASSERT_TRUE(x == 3.14159f);
  117. ASSERT_TRUE(3.14159f == x);
  118. }
  119. TEST_F(FloatTest, operatorNotEqual)
  120. {
  121. ls_std::Float x {3.1415f};
  122. ls_std::Float y {3.1414f};
  123. ASSERT_TRUE(x != y);
  124. ASSERT_TRUE(y != x);
  125. ASSERT_TRUE(x != 3.1414f);
  126. ASSERT_TRUE(3.1414 != x);
  127. }
  128. TEST_F(FloatTest, operatorGreaterThan)
  129. {
  130. ls_std::Float x {3.1415f};
  131. ls_std::Float y {3.1414f};
  132. ASSERT_TRUE(x > y);
  133. ASSERT_TRUE(x > 3.1414f);
  134. }
  135. TEST_F(FloatTest, operatorGreaterThanNegative)
  136. {
  137. ls_std::Float x {3.1414f};
  138. ls_std::Float y {3.1414f};
  139. ASSERT_FALSE(x > y);
  140. ASSERT_FALSE(x > 3.1414f);
  141. }
  142. TEST_F(FloatTest, operatorGreaterThanEqual)
  143. {
  144. ls_std::Float x {3.1414f};
  145. ls_std::Float y {3.1414f};
  146. ls_std::Float z {3.1415f};
  147. ASSERT_TRUE(x >= y);
  148. ASSERT_TRUE(z >= y);
  149. ASSERT_TRUE(x >= 3.1414f);
  150. ASSERT_TRUE(z >= 3.1414f);
  151. }
  152. TEST_F(FloatTest, operatorGreaterThanEqualNegative)
  153. {
  154. ls_std::Float x {3.1414f};
  155. ls_std::Float y {3.1415f};
  156. ASSERT_FALSE(x >= y);
  157. ASSERT_FALSE(x >= 3.1415f);
  158. }
  159. TEST_F(FloatTest, operatorLessThan)
  160. {
  161. ls_std::Float x {3.1413f};
  162. ls_std::Float y {3.1414f};
  163. ASSERT_TRUE(x < y);
  164. ASSERT_TRUE(x < 3.1414f);
  165. }
  166. TEST_F(FloatTest, operatorLessThanNegative)
  167. {
  168. ls_std::Float x {3.1414f};
  169. ls_std::Float y {3.1414f};
  170. ASSERT_FALSE(x < y);
  171. ASSERT_FALSE(x < 3.1414f);
  172. }
  173. TEST_F(FloatTest, operatorLessThanEqual)
  174. {
  175. ls_std::Float x {3.1414f};
  176. ls_std::Float y {3.1414f};
  177. ls_std::Float z {3.1415f};
  178. ASSERT_TRUE(x <= y);
  179. ASSERT_TRUE(x <= z);
  180. ASSERT_TRUE(x <= 3.1414f);
  181. ASSERT_TRUE(x <= 3.1415f);
  182. }
  183. TEST_F(FloatTest, operatorLessThanEqualNegative)
  184. {
  185. ls_std::Float x {3.1415f};
  186. ls_std::Float y {3.1414f};
  187. ASSERT_FALSE(x <= y);
  188. ASSERT_FALSE(x <= 3.1414f);
  189. }
  190. // increment / decrement operator
  191. TEST_F(FloatTest, operatorIncrement)
  192. {
  193. ls_std::Float x {3.1415f};
  194. ASSERT_FLOAT_EQ(3.1415f, x);
  195. ++x;
  196. ASSERT_FLOAT_EQ(4.1415f, x);
  197. ++x;
  198. ASSERT_FLOAT_EQ(5.1415f, x);
  199. }
  200. TEST_F(FloatTest, operatorDecrement)
  201. {
  202. ls_std::Float x {3.1415f};
  203. ASSERT_FLOAT_EQ(3.1415f, x);
  204. --x;
  205. ASSERT_FLOAT_EQ(2.1415f, x);
  206. --x;
  207. ASSERT_FLOAT_EQ(1.1415f, x);
  208. }
  209. // implementation
  210. TEST_F(FloatTest, parse)
  211. {
  212. ls_std::Float x {};
  213. x.parse("3.1415f");
  214. ASSERT_FLOAT_EQ(3.1415f, x);
  215. x.parse("-2.1415f");
  216. ASSERT_FLOAT_EQ(-2.1415f, x);
  217. }
  218. TEST_F(FloatTest, toString)
  219. {
  220. ls_std::Float x {13.1543f};
  221. ASSERT_TRUE(x.toString().find("13.1543") != std::string::npos);
  222. }
  223. // additional functionality
  224. TEST_F(FloatTest, getEpsilon)
  225. {
  226. ls_std::Float x {};
  227. ASSERT_FLOAT_EQ(0.00001f, x.getEpsilon());
  228. }
  229. TEST_F(FloatTest, getValue)
  230. {
  231. ls_std::Float x {3.1415f};
  232. ASSERT_FLOAT_EQ(3.1415f, x.getValue());
  233. }
  234. TEST_F(FloatTest, setEpsilon)
  235. {
  236. ls_std::Float x {};
  237. x.setEpsilon(0.01f);
  238. ASSERT_FLOAT_EQ(0.01f, x.getEpsilon());
  239. }
  240. }