FloatTest.cpp 7.6 KB

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