FloatTest.cpp 7.3 KB

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