FloatTest.cpp 6.7 KB

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