IntegerTest.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-09
  6. * Changed: 2023-02-22
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls-std/ls-std-boxing.hpp>
  11. #include <ls-std/ls-std-core.hpp>
  12. using ls::std::boxing::Integer;
  13. using ls::std::core::IllegalArithmeticOperationException;
  14. using testing::Test;
  15. namespace
  16. {
  17. class IntegerTest : public Test
  18. {
  19. protected:
  20. IntegerTest() = default;
  21. ~IntegerTest() override = default;
  22. void SetUp() override
  23. {}
  24. void TearDown() override
  25. {}
  26. };
  27. // assignment operators
  28. TEST_F(IntegerTest, operator_assignment_with_reference)
  29. {
  30. Integer x{};
  31. Integer y{3};
  32. x = y;
  33. ASSERT_EQ(3, x.getValue());
  34. }
  35. TEST_F(IntegerTest, operator_assignment_with_value)
  36. {
  37. Integer x{};
  38. x = 44;
  39. ASSERT_EQ(44, x.getValue());
  40. }
  41. // arithmetic operators
  42. TEST_F(IntegerTest, operator_negative)
  43. {
  44. Integer x{13};
  45. Integer y{-13};
  46. ASSERT_EQ(-13, -x);
  47. ASSERT_EQ(13, -y);
  48. }
  49. TEST_F(IntegerTest, operator_add_with_reference)
  50. {
  51. Integer x{13};
  52. Integer y{7};
  53. ASSERT_EQ(20, x + y);
  54. }
  55. TEST_F(IntegerTest, operator_add_with_value)
  56. {
  57. Integer x{13};
  58. ASSERT_EQ(20, x + 7);
  59. }
  60. TEST_F(IntegerTest, operator_mul_with_reference)
  61. {
  62. Integer x{3};
  63. Integer y{7};
  64. ASSERT_EQ(21, x * y);
  65. }
  66. TEST_F(IntegerTest, operator_mul_with_value)
  67. {
  68. Integer x{3};
  69. ASSERT_EQ(21, x * 7);
  70. }
  71. TEST_F(IntegerTest, operator_sub_with_reference)
  72. {
  73. Integer x{51};
  74. Integer y{17};
  75. ASSERT_EQ(34, x - y);
  76. }
  77. TEST_F(IntegerTest, operator_sub_with_value)
  78. {
  79. Integer x{51};
  80. ASSERT_EQ(34, x - 17);
  81. }
  82. TEST_F(IntegerTest, operator_div_with_reference)
  83. {
  84. Integer x{81};
  85. Integer y{9};
  86. ASSERT_EQ(9, x / y);
  87. }
  88. TEST_F(IntegerTest, operator_div_with_value)
  89. {
  90. Integer x{81};
  91. ASSERT_EQ(9, x / 9);
  92. }
  93. TEST_F(IntegerTest, operator_div_by_zero_with_reference)
  94. {
  95. EXPECT_THROW(
  96. {
  97. try
  98. {
  99. Integer x{9};
  100. Integer y{0};
  101. x = x / y;
  102. }
  103. catch (const IllegalArithmeticOperationException &_exception)
  104. {
  105. throw;
  106. }
  107. },
  108. IllegalArithmeticOperationException);
  109. }
  110. TEST_F(IntegerTest, operator_div_by_zero_with_value)
  111. {
  112. EXPECT_THROW(
  113. {
  114. try
  115. {
  116. Integer x{9};
  117. x = x / 0;
  118. }
  119. catch (const IllegalArithmeticOperationException &_exception)
  120. {
  121. throw;
  122. }
  123. },
  124. IllegalArithmeticOperationException);
  125. }
  126. TEST_F(IntegerTest, operator_mod_with_reference)
  127. {
  128. Integer x{85};
  129. Integer y{9};
  130. ASSERT_EQ(4, x % y);
  131. }
  132. TEST_F(IntegerTest, operator_mod_with_value)
  133. {
  134. Integer x{85};
  135. ASSERT_EQ(4, x % 9);
  136. }
  137. // compound operators
  138. TEST_F(IntegerTest, operator_add_assign_with_reference)
  139. {
  140. Integer x{4};
  141. Integer y{2};
  142. x += y;
  143. ASSERT_EQ(6, x.getValue());
  144. }
  145. TEST_F(IntegerTest, operator_add_assign_with_value)
  146. {
  147. Integer x{4};
  148. x += 2;
  149. ASSERT_EQ(6, x.getValue());
  150. }
  151. TEST_F(IntegerTest, operator_sub_assign_with_reference)
  152. {
  153. Integer x{14};
  154. Integer y{2};
  155. x -= y;
  156. ASSERT_EQ(12, x.getValue());
  157. }
  158. TEST_F(IntegerTest, operator_sub_assign_with_value)
  159. {
  160. Integer x{14};
  161. x -= 2;
  162. ASSERT_EQ(12, x.getValue());
  163. }
  164. TEST_F(IntegerTest, operator_mul_assign_with_reference)
  165. {
  166. Integer x{6};
  167. Integer y{3};
  168. x *= y;
  169. ASSERT_EQ(18, x.getValue());
  170. }
  171. TEST_F(IntegerTest, operator_mul_assign_with_value)
  172. {
  173. Integer x{6};
  174. x *= 3;
  175. ASSERT_EQ(18, x.getValue());
  176. }
  177. TEST_F(IntegerTest, operator_div_assign_with_reference)
  178. {
  179. Integer x{12};
  180. Integer y{3};
  181. x /= y;
  182. ASSERT_EQ(4, x.getValue());
  183. }
  184. TEST_F(IntegerTest, operator_div_assign_with_value)
  185. {
  186. Integer x{12};
  187. x /= 3;
  188. ASSERT_EQ(4, x.getValue());
  189. }
  190. TEST_F(IntegerTest, operator_div_assign_by_zero_with_reference)
  191. {
  192. EXPECT_THROW(
  193. {
  194. try
  195. {
  196. Integer x{9};
  197. Integer y{0};
  198. x = x /= y;
  199. }
  200. catch (const IllegalArithmeticOperationException &_exception)
  201. {
  202. throw;
  203. }
  204. },
  205. IllegalArithmeticOperationException);
  206. }
  207. TEST_F(IntegerTest, operator_div_assign_by_zero_with_value)
  208. {
  209. EXPECT_THROW(
  210. {
  211. try
  212. {
  213. Integer x{9};
  214. x = x /= 0;
  215. }
  216. catch (const IllegalArithmeticOperationException &_exception)
  217. {
  218. throw;
  219. }
  220. },
  221. IllegalArithmeticOperationException);
  222. }
  223. // comparison operators
  224. TEST_F(IntegerTest, operator_equals_with_reference)
  225. {
  226. Integer x{12};
  227. Integer y{12};
  228. ASSERT_TRUE(x == y);
  229. }
  230. TEST_F(IntegerTest, operator_equals_with_value)
  231. {
  232. Integer x{12};
  233. ASSERT_TRUE(x == 12);
  234. }
  235. TEST_F(IntegerTest, operator_not_equals_with_reference)
  236. {
  237. Integer x{12};
  238. Integer y{3};
  239. ASSERT_TRUE(x != y);
  240. }
  241. TEST_F(IntegerTest, operator_not_equals_with_value)
  242. {
  243. Integer x{12};
  244. ASSERT_TRUE(x != 3);
  245. }
  246. TEST_F(IntegerTest, operator_greater_than_with_reference)
  247. {
  248. Integer x{12};
  249. Integer y{3};
  250. ASSERT_TRUE(x > y);
  251. }
  252. TEST_F(IntegerTest, operator_greater_than_with_value)
  253. {
  254. Integer x{12};
  255. ASSERT_TRUE(x > 3);
  256. }
  257. TEST_F(IntegerTest, operator_greater_than_equals_with_reference)
  258. {
  259. Integer x{12};
  260. Integer y{12};
  261. ASSERT_TRUE(x >= y);
  262. }
  263. TEST_F(IntegerTest, operator_greater_than_equals_with_value)
  264. {
  265. Integer x{12};
  266. ASSERT_TRUE(x >= 12);
  267. }
  268. TEST_F(IntegerTest, operator_less_than_with_reference)
  269. {
  270. Integer x{10};
  271. Integer y{12};
  272. ASSERT_TRUE(x < y);
  273. }
  274. TEST_F(IntegerTest, operator_less_than_with_value)
  275. {
  276. Integer x{10};
  277. ASSERT_TRUE(x < 12);
  278. }
  279. TEST_F(IntegerTest, operator_less_than_equals_with_reference)
  280. {
  281. Integer x{10};
  282. Integer y{10};
  283. ASSERT_TRUE(x <= y);
  284. }
  285. TEST_F(IntegerTest, operator_less_than_equals_with_value)
  286. {
  287. Integer x{10};
  288. ASSERT_TRUE(x <= 10);
  289. }
  290. // logical operators
  291. TEST_F(IntegerTest, operator_negation)
  292. {
  293. Integer x{};
  294. ASSERT_TRUE(!x);
  295. }
  296. TEST_F(IntegerTest, operator_and_with_reference)
  297. {
  298. Integer x{1};
  299. Integer y{1};
  300. ASSERT_TRUE(x && y);
  301. }
  302. TEST_F(IntegerTest, operator_and_with_value)
  303. {
  304. Integer x{1};
  305. ASSERT_TRUE(x && 1);
  306. }
  307. TEST_F(IntegerTest, operator_and_with_boolean)
  308. {
  309. Integer x{1};
  310. ASSERT_TRUE(x && true);
  311. }
  312. TEST_F(IntegerTest, operator_or_with_reference)
  313. {
  314. Integer x{};
  315. Integer y{1};
  316. ASSERT_TRUE(x || y);
  317. }
  318. TEST_F(IntegerTest, operator_or_with_value)
  319. {
  320. Integer x{};
  321. ASSERT_TRUE(x || 1);
  322. }
  323. TEST_F(IntegerTest, operator_or_with_boolean)
  324. {
  325. Integer x{};
  326. ASSERT_TRUE(x || true);
  327. }
  328. // increment / decrement operator
  329. TEST_F(IntegerTest, operator_increment)
  330. {
  331. Integer x{};
  332. ++x;
  333. ASSERT_EQ(1, x.getValue());
  334. }
  335. TEST_F(IntegerTest, operator_decrement)
  336. {
  337. Integer x{};
  338. --x;
  339. ASSERT_EQ(-1, x.getValue());
  340. }
  341. // implementation
  342. TEST_F(IntegerTest, parse_with_positive_value)
  343. {
  344. Integer number{};
  345. number.parse("1989");
  346. ASSERT_EQ(1989, number.getValue());
  347. }
  348. TEST_F(IntegerTest, parse_with_negative_value)
  349. {
  350. Integer number{};
  351. number.parse("-13");
  352. ASSERT_EQ(-13, number.getValue());
  353. }
  354. TEST_F(IntegerTest, toString)
  355. {
  356. Integer number{112};
  357. ASSERT_STREQ("112", number.toString().c_str());
  358. }
  359. // additional functionality
  360. TEST_F(IntegerTest, getValue)
  361. {
  362. Integer x{3};
  363. ASSERT_EQ(3, x.getValue());
  364. }
  365. // additional testing
  366. TEST_F(IntegerTest, constApproach)
  367. {
  368. const Integer x{3};
  369. ASSERT_EQ(3, x.getValue());
  370. // x = 4; // wouldn't work
  371. }
  372. }