LongTest.cpp 7.9 KB

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