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