IntegerTest.cpp 8.1 KB

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