IntegerTest.cpp 7.7 KB

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