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