LongTest.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-17
  6. * Changed: 2020-09-04
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include "../../../source/boxing/Long.hpp"
  11. #include "../../../source/serialization/boxing/SerializableJSONLong.hpp"
  12. #include "../../TestHelper.hpp"
  13. #include "../../../source/io/File.hpp"
  14. #include "../../../source/io/FileWriter.hpp"
  15. #include "../../../source/io/StorableFile.hpp"
  16. namespace {
  17. class LongTest : public ::testing::Test {
  18. protected:
  19. LongTest() = default;
  20. ~LongTest() override = default;
  21. void SetUp() override
  22. {}
  23. void TearDown() override
  24. {}
  25. };
  26. // assignment operators
  27. TEST_F(LongTest, operatorAssignment)
  28. {
  29. ls_std::Long x {13};
  30. ASSERT_EQ(13, x);
  31. x = 44;
  32. ASSERT_EQ(44, x);
  33. ls_std::Long y {3};
  34. x = y;
  35. ASSERT_EQ(3, x);
  36. }
  37. // arithmetic operators
  38. TEST_F(LongTest, operatorHyphen)
  39. {
  40. ls_std::Long x {13};
  41. ls_std::Long y {-13};
  42. ASSERT_EQ(-13, -x);
  43. ASSERT_EQ(13, -y);
  44. }
  45. TEST_F(LongTest, operatorAddition)
  46. {
  47. ls_std::Long x {13};
  48. ls_std::Long y {7};
  49. ls_std::Long z {-15};
  50. ASSERT_EQ(20, x + y);
  51. ASSERT_EQ(8, 1 + y);
  52. ASSERT_EQ(5, x + y + z);
  53. ASSERT_EQ(15, x + (ls_std::long_type) 2);
  54. ASSERT_EQ(0, x + (ls_std::long_type) 2 + z);
  55. }
  56. TEST_F(LongTest, operatorMultiplication)
  57. {
  58. ls_std::Long x {3};
  59. ls_std::Long y {7};
  60. ls_std::Long z {5};
  61. ASSERT_EQ(21, x * y);
  62. ASSERT_EQ(105, x * y * z);
  63. ASSERT_EQ(6, x * (ls_std::long_type) 2);
  64. ASSERT_EQ(71, 1 + y * (ls_std::long_type) 2 * z);
  65. }
  66. TEST_F(LongTest, operatorSubtraction)
  67. {
  68. ls_std::Long x {51};
  69. ls_std::Long y {17};
  70. ASSERT_EQ(34, x - y);
  71. ASSERT_EQ(30, x - (ls_std::long_type) 21);
  72. ASSERT_EQ(13, x - (ls_std::long_type) 21 - y);
  73. }
  74. TEST_F(LongTest, operatorDivision)
  75. {
  76. ls_std::Long x {81};
  77. ls_std::Long y {9};
  78. ASSERT_EQ(9, x / y);
  79. ASSERT_EQ(3, 9 / 3);
  80. ASSERT_EQ(3, x / y / 3);
  81. ASSERT_EQ(1, 19 / 10);
  82. }
  83. TEST_F(LongTest, operatorModulus)
  84. {
  85. ls_std::Long x {85};
  86. ls_std::Long y {9};
  87. ASSERT_EQ(4, x % y);
  88. ASSERT_EQ(1, x % (ls_std::long_type) 3);
  89. }
  90. // compound operators
  91. TEST_F(LongTest, operatorAddEqual)
  92. {
  93. ls_std::Long x {4};
  94. ls_std::Long y {2};
  95. ASSERT_EQ(4, x);
  96. x += y;
  97. ASSERT_EQ(6, x);
  98. x += 4;
  99. ASSERT_EQ(10, x);
  100. x += -5;
  101. ASSERT_EQ(5, x);
  102. }
  103. TEST_F(LongTest, operatorSubEqual)
  104. {
  105. ls_std::Long x {14};
  106. ls_std::Long y {2};
  107. ASSERT_EQ(14, x);
  108. x -= y;
  109. ASSERT_EQ(12, x);
  110. x -= 6;
  111. ASSERT_EQ(6, x);
  112. x -= -3;
  113. ASSERT_EQ(9, x);
  114. }
  115. TEST_F(LongTest, operatorMulEqual)
  116. {
  117. ls_std::Long x {6};
  118. ls_std::Long y {3};
  119. ASSERT_EQ(6, x);
  120. x *= y;
  121. ASSERT_EQ(18, x);
  122. x *= 2;
  123. ASSERT_EQ(36, x);
  124. }
  125. TEST_F(LongTest, operatorDivEqual)
  126. {
  127. ls_std::Long x {12};
  128. ls_std::Long y {3};
  129. ASSERT_EQ(12, x);
  130. x /= y;
  131. ASSERT_EQ(4, x);
  132. x /= 2;
  133. ASSERT_EQ(2, x);
  134. }
  135. // comparison operators
  136. TEST_F(LongTest, operatorEqual)
  137. {
  138. ls_std::Long x {12};
  139. ls_std::Long y {3};
  140. ls_std::Long z {3};
  141. ASSERT_TRUE(x == (ls_std::long_type) 12);
  142. ASSERT_TRUE(y == z);
  143. ASSERT_TRUE(z == y);
  144. }
  145. TEST_F(LongTest, operatorNotEqual)
  146. {
  147. ls_std::Long x {12};
  148. ls_std::Long y {3};
  149. ls_std::Long z {3};
  150. ASSERT_TRUE(x != (ls_std::long_type) 14);
  151. ASSERT_TRUE(y != x);
  152. ASSERT_TRUE(z != x);
  153. }
  154. TEST_F(LongTest, operatorGreaterThan)
  155. {
  156. ls_std::Long x {12};
  157. ls_std::Long y {3};
  158. ASSERT_TRUE(x > (ls_std::long_type) 4);
  159. ASSERT_TRUE(x > y);
  160. }
  161. TEST_F(LongTest, operatorGreaterThanNegative)
  162. {
  163. ls_std::Long x {12};
  164. ls_std::Long y {3};
  165. ASSERT_FALSE(x > (ls_std::long_type) 14);
  166. ASSERT_FALSE(x > (y + (ls_std::long_type) 20));
  167. }
  168. TEST_F(LongTest, operatorGreaterThanEqual)
  169. {
  170. ls_std::Long x {12};
  171. ls_std::Long y {12};
  172. ASSERT_TRUE(x >= (ls_std::long_type) 12);
  173. ASSERT_TRUE(x >= y);
  174. }
  175. TEST_F(LongTest, operatorGreaterThanEqualNegative)
  176. {
  177. ls_std::Long x {12};
  178. ls_std::Long y {13};
  179. ASSERT_FALSE(x >= (ls_std::long_type) 13);
  180. ASSERT_FALSE(x >= y);
  181. }
  182. TEST_F(LongTest, operatorLessThan)
  183. {
  184. ls_std::Long x {10};
  185. ls_std::Long y {12};
  186. ASSERT_TRUE(x < (ls_std::long_type) 12);
  187. ASSERT_TRUE(x < y);
  188. }
  189. TEST_F(LongTest, operatorLessThanNegative)
  190. {
  191. ls_std::Long x {10};
  192. ls_std::Long y {10};
  193. ASSERT_FALSE(x < (ls_std::long_type) 10);
  194. ASSERT_FALSE(x < y);
  195. }
  196. TEST_F(LongTest, operatorLessThanEqual)
  197. {
  198. ls_std::Long x {10};
  199. ls_std::Long y {10};
  200. ASSERT_TRUE(x <= (ls_std::long_type) 11);
  201. ASSERT_TRUE(x <= y);
  202. }
  203. TEST_F(LongTest, operatorLessThanEqualNegative)
  204. {
  205. ls_std::Long x {11};
  206. ls_std::Long y {10};
  207. ASSERT_FALSE(x <= (ls_std::long_type) 10);
  208. ASSERT_FALSE(x <= y);
  209. }
  210. // logical operators
  211. TEST_F(LongTest, operatorNot)
  212. {
  213. ls_std::Long x {};
  214. ASSERT_TRUE(!x);
  215. }
  216. TEST_F(LongTest, operatorNotNegative)
  217. {
  218. ls_std::Long x {10};
  219. ASSERT_FALSE(!x);
  220. }
  221. TEST_F(LongTest, operatorAnd)
  222. {
  223. ls_std::Long x {1};
  224. ls_std::Long y {1};
  225. ASSERT_TRUE(x && (ls_std::long_type) 1);
  226. ASSERT_TRUE(x && true);
  227. ASSERT_TRUE(x && y);
  228. }
  229. TEST_F(LongTest, operatorAndNegative)
  230. {
  231. ls_std::Long x {};
  232. ls_std::Long y {1};
  233. ASSERT_FALSE(x && (ls_std::long_type) 1);
  234. ASSERT_FALSE(x && true);
  235. ASSERT_FALSE(x && y);
  236. }
  237. TEST_F(LongTest, operatorOr)
  238. {
  239. ls_std::Long x {};
  240. ls_std::Long y {1};
  241. ASSERT_TRUE(x || (ls_std::long_type) 1);
  242. ASSERT_TRUE(x || true);
  243. ASSERT_TRUE(x || y);
  244. }
  245. TEST_F(LongTest, operatorOrNegative)
  246. {
  247. ls_std::Long x {};
  248. ls_std::Long y {};
  249. ASSERT_FALSE(x || (ls_std::long_type) 0);
  250. ASSERT_FALSE(x || false);
  251. ASSERT_FALSE(x || y);
  252. }
  253. // increment / decrement operator
  254. TEST_F(LongTest, operatorIncrement)
  255. {
  256. ls_std::Long x {};
  257. ++x;
  258. ASSERT_EQ(1, x);
  259. ++x;
  260. ASSERT_EQ(2, x);
  261. }
  262. TEST_F(LongTest, operatorDecrement)
  263. {
  264. ls_std::Long x {};
  265. --x;
  266. ASSERT_EQ(-1, x);
  267. --x;
  268. ASSERT_EQ(-2, x);
  269. }
  270. // implementation
  271. TEST_F(LongTest, load)
  272. {
  273. // preparation
  274. std::shared_ptr<ls_std::Long> x = std::make_shared<ls_std::Long>();
  275. std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_long.json";
  276. ls_std::File file {path};
  277. file.createNewFile();
  278. ls_std::FileWriter writer {file};
  279. writer.write(R"({"value":1990})");
  280. auto serializable = std::make_shared<ls_std::SerializableJSONLong>(x);
  281. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  282. auto storable = std::make_shared<ls_std::StorableFile>(path);
  283. x->setStorable(std::dynamic_pointer_cast<ls_std::IStorable>(storable));
  284. // check
  285. x->load();
  286. ASSERT_EQ(1990, *x);
  287. file.remove();
  288. }
  289. TEST_F(LongTest, marshal)
  290. {
  291. std::shared_ptr<ls_std::Long> x = std::make_shared<ls_std::Long>(3);
  292. auto serializable = std::make_shared<ls_std::SerializableJSONLong>(x);
  293. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  294. ASSERT_STREQ(R"({"value":3})", x->marshal().c_str());
  295. *x = 17;
  296. ASSERT_STREQ(R"({"value":17})", x->marshal().c_str());
  297. }
  298. TEST_F(LongTest, parse)
  299. {
  300. ls_std::Long x {};
  301. x.parse("1989");
  302. ASSERT_EQ(1989, x);
  303. x.parse("-17");
  304. ASSERT_EQ(-17, x);
  305. }
  306. TEST_F(LongTest, toString)
  307. {
  308. ls_std::Long x {112};
  309. ASSERT_STREQ("112", x.toString().c_str());
  310. }
  311. TEST_F(LongTest, unmarshal)
  312. {
  313. std::shared_ptr<ls_std::Long> x = std::make_shared<ls_std::Long>(13);
  314. ASSERT_EQ(13, *x);
  315. auto serializable = std::make_shared<ls_std::SerializableJSONLong>(x);
  316. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  317. x->unmarshal(R"({"value":1989})");
  318. ASSERT_EQ(1989, *x);
  319. }
  320. // additional functionality
  321. TEST_F(LongTest, getValue)
  322. {
  323. ls_std::Long x {3};
  324. ASSERT_EQ(3, x.getValue());
  325. }
  326. // additional testing
  327. TEST_F(LongTest, constApproach)
  328. {
  329. const ls_std::Long x {3};
  330. ASSERT_EQ(3, x);
  331. // x = 4; // wouldn't work
  332. }
  333. }