IntegerTest.cpp 7.8 KB

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