LongTest.cpp 7.9 KB

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