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