DoubleTest.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2021-07-01
  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. static std::pair<std::shared_ptr<ls_std::File>, std::shared_ptr<ls_std::Double>> createPersistentTestDouble()
  24. {
  25. std::shared_ptr<ls_std::Double> number = std::make_shared<ls_std::Double>();
  26. std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_double.json";
  27. std::shared_ptr<ls_std::File> file = std::make_shared<ls_std::File>(path);
  28. file->createNewFile();
  29. ls_std::FileWriter writer{*file};
  30. writer.write(R"({"value":3.14159})");
  31. auto serializable = std::make_shared<ls_std::SerializableJsonDouble>(number);
  32. number->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  33. auto storable = std::make_shared<ls_std::StorableFile>(path);
  34. number->setStorable(std::dynamic_pointer_cast<ls_std::IStorable>(storable));
  35. return std::pair<std::shared_ptr<ls_std::File>, std::shared_ptr<ls_std::Double>>(file, number);
  36. }
  37. };
  38. // assignment operators
  39. TEST_F(DoubleTest, operator_assignment)
  40. {
  41. ls_std::Double x{};
  42. x = 44.22;
  43. ASSERT_EQ(44.22, x);
  44. }
  45. // arithmetic operators
  46. TEST_F(DoubleTest, operator_negative)
  47. {
  48. ls_std::Double x{3.25};
  49. ASSERT_DOUBLE_EQ(-3.25, -x);
  50. }
  51. TEST_F(DoubleTest, operator_addition_with_reference)
  52. {
  53. ls_std::Double x{3.1415};
  54. ls_std::Double y{2.223};
  55. ls_std::Double z{x + y};
  56. ASSERT_DOUBLE_EQ(5.3645, z);
  57. }
  58. TEST_F(DoubleTest, operator_addition_with_value)
  59. {
  60. ls_std::Double x{3.1415};
  61. ASSERT_DOUBLE_EQ(5.3645, x + 2.223);
  62. }
  63. TEST_F(DoubleTest, operator_multiplication_with_reference)
  64. {
  65. ls_std::Double x{3.14};
  66. ls_std::Double y{2.22};
  67. ls_std::Double z{x * y};
  68. ASSERT_DOUBLE_EQ(6.9708, z);
  69. }
  70. TEST_F(DoubleTest, operator_multiplication_with_value)
  71. {
  72. ls_std::Double x{3.14};
  73. ASSERT_DOUBLE_EQ(6.9708, x * 2.22);
  74. }
  75. TEST_F(DoubleTest, operator_substraction_with_reference)
  76. {
  77. ls_std::Double x{3.1415};
  78. ls_std::Double y{2.225};
  79. ls_std::Double z{x - y};
  80. ASSERT_DOUBLE_EQ(0.9165, z);
  81. }
  82. TEST_F(DoubleTest, operator_substraction_with_value)
  83. {
  84. ls_std::Double x{3.1415};
  85. ASSERT_DOUBLE_EQ(0.9165, x - 2.225);
  86. }
  87. TEST_F(DoubleTest, operator_division_with_reference)
  88. {
  89. ls_std::Double x{2.25};
  90. ls_std::Double y{0.5};
  91. ls_std::Double z{x / y};
  92. ASSERT_DOUBLE_EQ(4.5, z);
  93. }
  94. TEST_F(DoubleTest, operator_division_with_value)
  95. {
  96. ls_std::Double x{2.25};
  97. ASSERT_DOUBLE_EQ(4.5, x / 0.5);
  98. }
  99. // compound operators
  100. TEST_F(DoubleTest, operator_add_assign_with_reference)
  101. {
  102. ls_std::Double x{2.25000000};
  103. ls_std::Double y{3.14000000};
  104. x += y;
  105. ASSERT_DOUBLE_EQ(5.39000000, x);
  106. }
  107. TEST_F(DoubleTest, operator_add_assign_with_value)
  108. {
  109. ls_std::Double x{2.25000000};
  110. x += 3.14000000;
  111. ASSERT_DOUBLE_EQ(5.39000000, x);
  112. }
  113. TEST_F(DoubleTest, operator_sub_assign_with_reference)
  114. {
  115. ls_std::Double x{2.25};
  116. ls_std::Double y{0.04};
  117. x -= y;
  118. ASSERT_DOUBLE_EQ(2.21, x);
  119. }
  120. TEST_F(DoubleTest, operator_sub_assign_with_value)
  121. {
  122. ls_std::Double x{2.25};
  123. x -= 0.04;
  124. ASSERT_DOUBLE_EQ(2.21, x);
  125. }
  126. TEST_F(DoubleTest, operator_mul_assign_with_reference)
  127. {
  128. ls_std::Double x{2.25000000};
  129. ls_std::Double y{0.04000000};
  130. x *= y;
  131. ASSERT_DOUBLE_EQ(0.09000000, x);
  132. }
  133. TEST_F(DoubleTest, operator_mul_assign_with_value)
  134. {
  135. ls_std::Double x{2.25000000};
  136. x *= 0.04000000;
  137. ASSERT_DOUBLE_EQ(0.09000000, x);
  138. }
  139. TEST_F(DoubleTest, operator_division_assign_with_reference)
  140. {
  141. ls_std::Double x{2.25};
  142. ls_std::Double y{0.05};
  143. x /= y;
  144. ASSERT_DOUBLE_EQ(45.0, x);
  145. }
  146. TEST_F(DoubleTest, operator_division_assign_with_value)
  147. {
  148. ls_std::Double x{2.25};
  149. x /= 0.05;
  150. ASSERT_DOUBLE_EQ(45.0, x);
  151. }
  152. // comparison operators
  153. TEST_F(DoubleTest, operator_equals_with_reference)
  154. {
  155. ls_std::Double x{3.14159};
  156. ls_std::Double y{3.14159};
  157. ASSERT_TRUE(x == y);
  158. ASSERT_TRUE(y == x);
  159. }
  160. TEST_F(DoubleTest, operator_equals_with_value)
  161. {
  162. ls_std::Double x{3.14159};
  163. ASSERT_TRUE(x == 3.14159);
  164. ASSERT_TRUE(3.14159 == x);
  165. }
  166. TEST_F(DoubleTest, operator_not_equal_with_reference)
  167. {
  168. ls_std::Double x{3.1415};
  169. ls_std::Double y{3.1414};
  170. ASSERT_TRUE(x != y);
  171. ASSERT_TRUE(y != x);
  172. }
  173. TEST_F(DoubleTest, operator_not_equal_with_value)
  174. {
  175. ls_std::Double x{3.1415};
  176. ASSERT_TRUE(x != 3.1414);
  177. ASSERT_TRUE(3.1414 != x);
  178. }
  179. TEST_F(DoubleTest, operator_greater_than_with_reference)
  180. {
  181. ls_std::Double x{3.1415};
  182. ls_std::Double y{3.1414};
  183. ASSERT_TRUE(x > y);
  184. }
  185. TEST_F(DoubleTest, operator_greater_than_with_value)
  186. {
  187. ls_std::Double x{3.1415};
  188. ls_std::Double y{3.1414};
  189. ASSERT_TRUE(x > 3.1414);
  190. }
  191. TEST_F(DoubleTest, operator_greater_than_equals_with_reference)
  192. {
  193. ls_std::Double x{3.1414};
  194. ls_std::Double y{3.1414};
  195. ls_std::Double z{3.1415};
  196. ASSERT_TRUE(x >= y);
  197. ASSERT_TRUE(z >= y);
  198. }
  199. TEST_F(DoubleTest, operator_greater_than_equals_with_value)
  200. {
  201. ls_std::Double x{3.1414};
  202. ASSERT_TRUE(x >= 3.1414);
  203. }
  204. TEST_F(DoubleTest, operator_less_than_with_reference)
  205. {
  206. ls_std::Double x{3.1413};
  207. ls_std::Double y{3.1414};
  208. ASSERT_TRUE(x < y);
  209. }
  210. TEST_F(DoubleTest, operator_less_than_with_value)
  211. {
  212. ls_std::Double x{3.1413};
  213. ls_std::Double y{3.1414};
  214. ASSERT_TRUE(x < 3.1414);
  215. }
  216. TEST_F(DoubleTest, operator_less_than_equals_with_reference)
  217. {
  218. ls_std::Double x{3.1414};
  219. ls_std::Double y{3.1414};
  220. ls_std::Double z{3.1415};
  221. ASSERT_TRUE(x <= y);
  222. ASSERT_TRUE(x <= z);
  223. }
  224. TEST_F(DoubleTest, operator_less_than_equals_with_value)
  225. {
  226. ls_std::Double x{3.1414};
  227. ASSERT_TRUE(x <= 3.1414);
  228. }
  229. // increment / decrement operator
  230. TEST_F(DoubleTest, operator_increment)
  231. {
  232. ls_std::Double x{3.1415};
  233. ++x;
  234. ASSERT_DOUBLE_EQ(4.1415, x);
  235. }
  236. TEST_F(DoubleTest, operator_decrement)
  237. {
  238. ls_std::Double x{3.1415};
  239. --x;
  240. ASSERT_DOUBLE_EQ(2.1415, x);
  241. }
  242. // implementation
  243. TEST_F(DoubleTest, load)
  244. {
  245. // preparation
  246. auto storableDouble = createPersistentTestDouble();
  247. // check
  248. storableDouble.second->load();
  249. ASSERT_DOUBLE_EQ(3.14159, *storableDouble.second);
  250. storableDouble.first->remove();
  251. }
  252. TEST_F(DoubleTest, marshal)
  253. {
  254. std::shared_ptr<ls_std::Double> number = std::make_shared<ls_std::Double>(3.14159);
  255. auto serializable = std::make_shared<ls_std::SerializableJsonDouble>(number);
  256. number->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  257. ls_std::String jsonString{number->marshal()};
  258. ASSERT_TRUE(jsonString.contains(R"({"value":3.14159)"));
  259. }
  260. TEST_F(DoubleTest, parse_with_positive_value)
  261. {
  262. ls_std::Double x{};
  263. x.parse("3.1415");
  264. ASSERT_DOUBLE_EQ(3.1415, x);
  265. }
  266. TEST_F(DoubleTest, parse_with_negative_value)
  267. {
  268. ls_std::Double x{};
  269. x.parse("-2.1415");
  270. ASSERT_DOUBLE_EQ(-2.1415, x);
  271. }
  272. TEST_F(DoubleTest, toString)
  273. {
  274. ls_std::Double x{13.1543};
  275. ASSERT_TRUE(x.toString().find("13.1543") != std::string::npos);
  276. }
  277. TEST_F(DoubleTest, unmarshal)
  278. {
  279. std::shared_ptr<ls_std::Double> number = std::make_shared<ls_std::Double>(3.14159);
  280. auto serializable = std::make_shared<ls_std::SerializableJsonDouble>(number);
  281. number->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  282. number->unmarshal(R"({"value":17.4132})");
  283. ASSERT_DOUBLE_EQ(17.4132, *number);
  284. }
  285. // additional functionality
  286. TEST_F(DoubleTest, getEpsilon)
  287. {
  288. ls_std::Double x{};
  289. ASSERT_DOUBLE_EQ(0.00000001, x.getEpsilon());
  290. }
  291. TEST_F(DoubleTest, getValue)
  292. {
  293. ls_std::Double x{3.1415};
  294. ASSERT_DOUBLE_EQ(3.1415, x.getValue());
  295. }
  296. TEST_F(DoubleTest, setEpsilon)
  297. {
  298. ls_std::Double x{};
  299. x.setEpsilon(0.01);
  300. ASSERT_DOUBLE_EQ(0.01, x.getEpsilon());
  301. }
  302. TEST_F(DoubleTest, setEpsilon_invalid_value)
  303. {
  304. ls_std::Double x{};
  305. EXPECT_THROW({
  306. try
  307. {
  308. x.setEpsilon(0.0);
  309. }
  310. catch (const ls_std::IllegalArgumentException &_exception)
  311. {
  312. throw;
  313. }
  314. }, ls_std::IllegalArgumentException);
  315. }
  316. TEST_F(DoubleTest, setSerializable_no_reference)
  317. {
  318. ls_std::Double x{};
  319. EXPECT_THROW({
  320. try
  321. {
  322. x.setSerializable(nullptr);
  323. }
  324. catch (const ls_std::IllegalArgumentException &_exception)
  325. {
  326. throw;
  327. }
  328. }, ls_std::IllegalArgumentException);
  329. }
  330. TEST_F(DoubleTest, setStorable_no_reference)
  331. {
  332. ls_std::Double x{};
  333. EXPECT_THROW({
  334. try
  335. {
  336. x.setStorable(nullptr);
  337. }
  338. catch (const ls_std::IllegalArgumentException &_exception)
  339. {
  340. throw;
  341. }
  342. }, ls_std::IllegalArgumentException);
  343. }
  344. }