IntegerTest.cpp 9.1 KB

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