IntegerTest.cpp 9.1 KB

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