IntegerTest.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. // stream operators / input stream not testable by using automated tests
  210. // TODO: not compilable
  211. // TEST_F(IntegerTest, operatorOutputStream)
  212. // {
  213. // ls_std::Integer x {10};
  214. // std::ostringstream _stream {};
  215. // _stream << x;
  216. //
  217. // ASSERT_STREQ("10", _stream.str().c_str());
  218. // }
  219. // logical operators
  220. TEST_F(IntegerTest, operatorNot)
  221. {
  222. ls_std::Integer x {};
  223. ASSERT_TRUE(!x);
  224. }
  225. TEST_F(IntegerTest, operatorNotNegative)
  226. {
  227. ls_std::Integer x {10};
  228. ASSERT_FALSE(!x);
  229. }
  230. TEST_F(IntegerTest, operatorAnd)
  231. {
  232. ls_std::Integer x {1};
  233. ls_std::Integer y {1};
  234. ASSERT_TRUE(x && 1);
  235. ASSERT_TRUE(x && true);
  236. ASSERT_TRUE(x && y);
  237. }
  238. TEST_F(IntegerTest, operatorAndNegative)
  239. {
  240. ls_std::Integer x {};
  241. ls_std::Integer y {1};
  242. ASSERT_FALSE(x && 1);
  243. ASSERT_FALSE(x && true);
  244. ASSERT_FALSE(x && y);
  245. }
  246. TEST_F(IntegerTest, operatorOr)
  247. {
  248. ls_std::Integer x {};
  249. ls_std::Integer y {1};
  250. ASSERT_TRUE(x || 1);
  251. ASSERT_TRUE(x || true);
  252. ASSERT_TRUE(x || y);
  253. }
  254. TEST_F(IntegerTest, operatorOrNegative)
  255. {
  256. ls_std::Integer x {};
  257. ls_std::Integer y {};
  258. ASSERT_FALSE(x || 0);
  259. ASSERT_FALSE(x || false);
  260. ASSERT_FALSE(x || y);
  261. }
  262. // increment / decrement operator
  263. TEST_F(IntegerTest, operatorIncrement)
  264. {
  265. ls_std::Integer x {};
  266. ++x;
  267. ASSERT_EQ(1, x);
  268. ++x;
  269. ASSERT_EQ(2, x);
  270. }
  271. TEST_F(IntegerTest, operatorDecrement)
  272. {
  273. ls_std::Integer x {};
  274. --x;
  275. ASSERT_EQ(-1, x);
  276. --x;
  277. ASSERT_EQ(-2, x);
  278. }
  279. // implementation
  280. TEST_F(IntegerTest, load)
  281. {
  282. // preparation
  283. std::shared_ptr<ls_std::Integer> x = std::make_shared<ls_std::Integer>();
  284. std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_integer.json";
  285. ls_std::File file {path};
  286. file.createNewFile();
  287. ls_std::FileWriter writer {file};
  288. writer.write(R"({"class":"Integer","value":1990})");
  289. auto serializable = std::make_shared<ls_std::SerializableJSONInteger>(x);
  290. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  291. auto storable = std::make_shared<ls_std::StorableFile>(path);
  292. x->setStorable(std::dynamic_pointer_cast<ls_std::IStorable>(storable));
  293. // check
  294. x->load();
  295. ASSERT_EQ(1990, *x);
  296. file.remove();
  297. }
  298. TEST_F(IntegerTest, marshal)
  299. {
  300. std::shared_ptr<ls_std::Integer> x = std::make_shared<ls_std::Integer>(3);
  301. auto serializable = std::make_shared<ls_std::SerializableJSONInteger>(x);
  302. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  303. ASSERT_STREQ(R"({"class":"Integer","value":3})", x->marshal().c_str());
  304. *x = 17;
  305. ASSERT_STREQ(R"({"class":"Integer","value":17})", x->marshal().c_str());
  306. }
  307. TEST_F(IntegerTest, parse)
  308. {
  309. ls_std::Integer x {};
  310. x.parse("1989");
  311. ASSERT_EQ(1989, x);
  312. x.parse("-17");
  313. ASSERT_EQ(-17, x);
  314. }
  315. TEST_F(IntegerTest, toString)
  316. {
  317. ls_std::Integer x {112};
  318. ASSERT_STREQ("112", x.toString().c_str());
  319. }
  320. TEST_F(IntegerTest, unmarshal)
  321. {
  322. std::shared_ptr<ls_std::Integer> x = std::make_shared<ls_std::Integer>(13);
  323. ASSERT_EQ(13, *x);
  324. auto serializable = std::make_shared<ls_std::SerializableJSONInteger>(x);
  325. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  326. x->unmarshal(R"({"class":"Integer","value":1989})");
  327. ASSERT_EQ(1989, *x);
  328. }
  329. // additional functionality
  330. TEST_F(IntegerTest, getValue)
  331. {
  332. ls_std::Integer x {3};
  333. ASSERT_EQ(3, x.getValue());
  334. }
  335. // additional testing
  336. TEST_F(IntegerTest, constApproach)
  337. {
  338. const ls_std::Integer x {3};
  339. ASSERT_EQ(3, x);
  340. // x = 4; // wouldn't work
  341. }
  342. }