DoubleTest.cpp 6.8 KB

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