IntegerTest.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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-22
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include "../../../source/boxing/Integer.hpp"
  11. #include "../../../source/serialization/boxing/SerializableJSONInteger.hpp"
  12. namespace {
  13. class IntegerTest : public ::testing::Test {
  14. protected:
  15. IntegerTest() = default;
  16. ~IntegerTest() override = default;
  17. void SetUp() override
  18. {}
  19. void TearDown() override
  20. {}
  21. };
  22. // assignment operators
  23. TEST_F(IntegerTest, operatorAssignment)
  24. {
  25. ls_std::Integer x {13};
  26. ASSERT_EQ(13, x);
  27. x = 44;
  28. ASSERT_EQ(44, x);
  29. ls_std::Integer y {3};
  30. x = y;
  31. ASSERT_EQ(3, x);
  32. }
  33. // arithmetic operators
  34. TEST_F(IntegerTest, operatorHyphen)
  35. {
  36. ls_std::Integer x {13};
  37. ls_std::Integer y {-13};
  38. ASSERT_EQ(-13, -x);
  39. ASSERT_EQ(13, -y);
  40. }
  41. TEST_F(IntegerTest, operatorAddition)
  42. {
  43. ls_std::Integer x {13};
  44. ls_std::Integer y {7};
  45. ls_std::Integer 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 + 2);
  50. ASSERT_EQ(0, x + 2 + z);
  51. }
  52. TEST_F(IntegerTest, operatorMultiplication)
  53. {
  54. ls_std::Integer x {3};
  55. ls_std::Integer y {7};
  56. ls_std::Integer z {5};
  57. ASSERT_EQ(21, x * y);
  58. ASSERT_EQ(105, x * y * z);
  59. ASSERT_EQ(6, x * 2);
  60. ASSERT_EQ(71, 1 + y * 2 * z);
  61. }
  62. TEST_F(IntegerTest, operatorSubtraction)
  63. {
  64. ls_std::Integer x {51};
  65. ls_std::Integer y {17};
  66. ASSERT_EQ(34, x - y);
  67. ASSERT_EQ(30, x - 21);
  68. ASSERT_EQ(13, x - 21 - y);
  69. }
  70. TEST_F(IntegerTest, operatorDivision)
  71. {
  72. ls_std::Integer x {81};
  73. ls_std::Integer 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(IntegerTest, operatorModulus)
  80. {
  81. ls_std::Integer x {85};
  82. ls_std::Integer y {9};
  83. ASSERT_EQ(4, x % y);
  84. ASSERT_EQ(1, x % 3);
  85. }
  86. // compound operators
  87. TEST_F(IntegerTest, operatorAddEqual)
  88. {
  89. ls_std::Integer x {4};
  90. ls_std::Integer 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(IntegerTest, operatorSubEqual)
  100. {
  101. ls_std::Integer x {14};
  102. ls_std::Integer 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(IntegerTest, operatorMulEqual)
  112. {
  113. ls_std::Integer x {6};
  114. ls_std::Integer 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(IntegerTest, operatorDivEqual)
  122. {
  123. ls_std::Integer x {12};
  124. ls_std::Integer 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(IntegerTest, operatorEqual)
  133. {
  134. ls_std::Integer x {12};
  135. ls_std::Integer y {3};
  136. ls_std::Integer z {3};
  137. ASSERT_TRUE(x == 12);
  138. ASSERT_TRUE(y == z);
  139. ASSERT_TRUE(z == y);
  140. }
  141. TEST_F(IntegerTest, operatorNotEqual)
  142. {
  143. ls_std::Integer x {12};
  144. ls_std::Integer y {3};
  145. ls_std::Integer z {3};
  146. ASSERT_TRUE(x != 14);
  147. ASSERT_TRUE(y != x);
  148. ASSERT_TRUE(z != x);
  149. }
  150. TEST_F(IntegerTest, operatorGreaterThan)
  151. {
  152. ls_std::Integer x {12};
  153. ls_std::Integer y {3};
  154. ASSERT_TRUE(x > 4);
  155. ASSERT_TRUE(x > y);
  156. }
  157. TEST_F(IntegerTest, operatorGreaterThanNegative)
  158. {
  159. ls_std::Integer x {12};
  160. ls_std::Integer y {3};
  161. ASSERT_FALSE(x > 14);
  162. ASSERT_FALSE(x > (y + 20));
  163. }
  164. TEST_F(IntegerTest, operatorGreaterThanEqual)
  165. {
  166. ls_std::Integer x {12};
  167. ls_std::Integer y {12};
  168. ASSERT_TRUE(x >= 12);
  169. ASSERT_TRUE(x >= y);
  170. }
  171. TEST_F(IntegerTest, operatorGreaterThanEqualNegative)
  172. {
  173. ls_std::Integer x {12};
  174. ls_std::Integer y {13};
  175. ASSERT_FALSE(x >= 13);
  176. ASSERT_FALSE(x >= y);
  177. }
  178. TEST_F(IntegerTest, operatorLessThan)
  179. {
  180. ls_std::Integer x {10};
  181. ls_std::Integer y {12};
  182. ASSERT_TRUE(x < 12);
  183. ASSERT_TRUE(x < y);
  184. }
  185. TEST_F(IntegerTest, operatorLessThanNegative)
  186. {
  187. ls_std::Integer x {10};
  188. ls_std::Integer y {10};
  189. ASSERT_FALSE(x < 10);
  190. ASSERT_FALSE(x < y);
  191. }
  192. TEST_F(IntegerTest, operatorLessThanEqual)
  193. {
  194. ls_std::Integer x {10};
  195. ls_std::Integer y {10};
  196. ASSERT_TRUE(x <= 11);
  197. ASSERT_TRUE(x <= y);
  198. }
  199. TEST_F(IntegerTest, operatorLessThanEqualNegative)
  200. {
  201. ls_std::Integer x {11};
  202. ls_std::Integer y {10};
  203. ASSERT_FALSE(x <= 10);
  204. ASSERT_FALSE(x <= y);
  205. }
  206. // stream operators / input stream not testable by using automated tests
  207. // TODO: not compilable
  208. // TEST_F(IntegerTest, operatorOutputStream)
  209. // {
  210. // ls_std::Integer x {10};
  211. // std::ostringstream _stream {};
  212. // _stream << x;
  213. //
  214. // ASSERT_STREQ("10", _stream.str().c_str());
  215. // }
  216. // logical operators
  217. TEST_F(IntegerTest, operatorNot)
  218. {
  219. ls_std::Integer x {};
  220. ASSERT_TRUE(!x);
  221. }
  222. TEST_F(IntegerTest, operatorNotNegative)
  223. {
  224. ls_std::Integer x {10};
  225. ASSERT_FALSE(!x);
  226. }
  227. TEST_F(IntegerTest, operatorAnd)
  228. {
  229. ls_std::Integer x {1};
  230. ls_std::Integer y {1};
  231. ASSERT_TRUE(x && 1);
  232. ASSERT_TRUE(x && true);
  233. ASSERT_TRUE(x && y);
  234. }
  235. TEST_F(IntegerTest, operatorAndNegative)
  236. {
  237. ls_std::Integer x {};
  238. ls_std::Integer y {1};
  239. ASSERT_FALSE(x && 1);
  240. ASSERT_FALSE(x && true);
  241. ASSERT_FALSE(x && y);
  242. }
  243. TEST_F(IntegerTest, operatorOr)
  244. {
  245. ls_std::Integer x {};
  246. ls_std::Integer y {1};
  247. ASSERT_TRUE(x || 1);
  248. ASSERT_TRUE(x || true);
  249. ASSERT_TRUE(x || y);
  250. }
  251. TEST_F(IntegerTest, operatorOrNegative)
  252. {
  253. ls_std::Integer x {};
  254. ls_std::Integer y {};
  255. ASSERT_FALSE(x || 0);
  256. ASSERT_FALSE(x || false);
  257. ASSERT_FALSE(x || y);
  258. }
  259. // increment / decrement operator
  260. TEST_F(IntegerTest, operatorIncrement)
  261. {
  262. ls_std::Integer x {};
  263. ++x;
  264. ASSERT_EQ(1, x);
  265. ++x;
  266. ASSERT_EQ(2, x);
  267. }
  268. TEST_F(IntegerTest, operatorDecrement)
  269. {
  270. ls_std::Integer x {};
  271. --x;
  272. ASSERT_EQ(-1, x);
  273. --x;
  274. ASSERT_EQ(-2, x);
  275. }
  276. // implementation
  277. TEST_F(IntegerTest, marshal)
  278. {
  279. std::shared_ptr<ls_std::Integer> x = std::make_shared<ls_std::Integer>(3);
  280. auto serializable = std::make_shared<ls_std::SerializableJSONInteger>(x);
  281. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  282. ASSERT_STREQ(R"({"class":"Integer","value":3})", x->marshal());
  283. *x = 17;
  284. ASSERT_STREQ(R"({"class":"Integer","value":17})", x->marshal());
  285. }
  286. TEST_F(IntegerTest, parse)
  287. {
  288. ls_std::Integer x {};
  289. x.parse("1989");
  290. ASSERT_EQ(1989, x);
  291. x.parse("-17");
  292. ASSERT_EQ(-17, x);
  293. }
  294. TEST_F(IntegerTest, toString)
  295. {
  296. ls_std::Integer x {112};
  297. ASSERT_STREQ("112", x.toString().c_str());
  298. }
  299. TEST_F(IntegerTest, unmarshal)
  300. {
  301. std::shared_ptr<ls_std::Integer> x = std::make_shared<ls_std::Integer>(13);
  302. ASSERT_EQ(13, *x);
  303. auto serializable = std::make_shared<ls_std::SerializableJSONInteger>(x);
  304. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  305. x->unmarshal(R"({"class":"Integer","value":1989})");
  306. ASSERT_EQ(1989, *x);
  307. }
  308. // additional functionality
  309. TEST_F(IntegerTest, getValue)
  310. {
  311. ls_std::Integer x {3};
  312. ASSERT_EQ(3, x.getValue());
  313. }
  314. // additional testing
  315. TEST_F(IntegerTest, constApproach)
  316. {
  317. const ls_std::Integer x {3};
  318. ASSERT_EQ(3, x);
  319. // x = 4; // wouldn't work
  320. }
  321. }