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