IntegerTest.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-09
  6. * Changed: 2022-11-09
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std_core.hpp>
  11. #include <ls_std/ls_std_boxing.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. try
  96. {
  97. Integer x{9};
  98. Integer y{0};
  99. x = x / y;
  100. } catch (const IllegalArithmeticOperationException &_exception)
  101. {
  102. throw;
  103. }
  104. }, IllegalArithmeticOperationException);
  105. }
  106. TEST_F(IntegerTest, operator_div_by_zero_with_value)
  107. {
  108. EXPECT_THROW({
  109. try
  110. {
  111. Integer x{9};
  112. x = x / 0;
  113. } catch (const IllegalArithmeticOperationException &_exception)
  114. {
  115. throw;
  116. }
  117. }, IllegalArithmeticOperationException);
  118. }
  119. TEST_F(IntegerTest, operator_mod_with_reference)
  120. {
  121. Integer x{85};
  122. Integer y{9};
  123. ASSERT_EQ(4, x % y);
  124. }
  125. TEST_F(IntegerTest, operator_mod_with_value)
  126. {
  127. Integer x{85};
  128. ASSERT_EQ(4, x % 9);
  129. }
  130. // compound operators
  131. TEST_F(IntegerTest, operator_add_assign_with_reference)
  132. {
  133. Integer x{4};
  134. Integer y{2};
  135. x += y;
  136. ASSERT_EQ(6, x);
  137. }
  138. TEST_F(IntegerTest, operator_add_assign_with_value)
  139. {
  140. Integer x{4};
  141. x += 2;
  142. ASSERT_EQ(6, x);
  143. }
  144. TEST_F(IntegerTest, operator_sub_assign_with_reference)
  145. {
  146. Integer x{14};
  147. Integer y{2};
  148. x -= y;
  149. ASSERT_EQ(12, x);
  150. }
  151. TEST_F(IntegerTest, operator_sub_assign_with_value)
  152. {
  153. Integer x{14};
  154. x -= 2;
  155. ASSERT_EQ(12, x);
  156. }
  157. TEST_F(IntegerTest, operator_mul_assign_with_reference)
  158. {
  159. Integer x{6};
  160. Integer y{3};
  161. x *= y;
  162. ASSERT_EQ(18, x);
  163. }
  164. TEST_F(IntegerTest, operator_mul_assign_with_value)
  165. {
  166. Integer x{6};
  167. x *= 3;
  168. ASSERT_EQ(18, x);
  169. }
  170. TEST_F(IntegerTest, operator_div_assign_with_reference)
  171. {
  172. Integer x{12};
  173. Integer y{3};
  174. x /= y;
  175. ASSERT_EQ(4, x);
  176. }
  177. TEST_F(IntegerTest, operator_div_assign_with_value)
  178. {
  179. Integer x{12};
  180. x /= 3;
  181. ASSERT_EQ(4, x);
  182. }
  183. TEST_F(IntegerTest, operator_div_assign_by_zero_with_reference)
  184. {
  185. EXPECT_THROW({
  186. try
  187. {
  188. Integer x{9};
  189. Integer y{0};
  190. x = x /= y;
  191. } catch (const IllegalArithmeticOperationException &_exception)
  192. {
  193. throw;
  194. }
  195. }, IllegalArithmeticOperationException);
  196. }
  197. TEST_F(IntegerTest, operator_div_assign_by_zero_with_value)
  198. {
  199. EXPECT_THROW({
  200. try
  201. {
  202. Integer x{9};
  203. x = x /= 0;
  204. } catch (const IllegalArithmeticOperationException &_exception)
  205. {
  206. throw;
  207. }
  208. }, IllegalArithmeticOperationException);
  209. }
  210. // comparison operators
  211. TEST_F(IntegerTest, operator_equals_with_reference)
  212. {
  213. Integer x{12};
  214. Integer y{12};
  215. ASSERT_TRUE(x == y);
  216. }
  217. TEST_F(IntegerTest, operator_equals_with_value)
  218. {
  219. Integer x{12};
  220. ASSERT_TRUE(x == 12);
  221. }
  222. TEST_F(IntegerTest, operator_not_equals_with_reference)
  223. {
  224. Integer x{12};
  225. Integer y{3};
  226. ASSERT_TRUE(x != y);
  227. }
  228. TEST_F(IntegerTest, operator_not_equals_with_value)
  229. {
  230. Integer x{12};
  231. ASSERT_TRUE(x != 3);
  232. }
  233. TEST_F(IntegerTest, operator_greater_than_with_reference)
  234. {
  235. Integer x{12};
  236. Integer y{3};
  237. ASSERT_TRUE(x > y);
  238. }
  239. TEST_F(IntegerTest, operator_greater_than_with_value)
  240. {
  241. Integer x{12};
  242. ASSERT_TRUE(x > 3);
  243. }
  244. TEST_F(IntegerTest, operator_greater_than_equals_with_reference)
  245. {
  246. Integer x{12};
  247. Integer y{12};
  248. ASSERT_TRUE(x >= y);
  249. }
  250. TEST_F(IntegerTest, operator_greater_than_equals_with_value)
  251. {
  252. Integer x{12};
  253. ASSERT_TRUE(x >= 12);
  254. }
  255. TEST_F(IntegerTest, operator_less_than_with_reference)
  256. {
  257. Integer x{10};
  258. Integer y{12};
  259. ASSERT_TRUE(x < y);
  260. }
  261. TEST_F(IntegerTest, operator_less_than_with_value)
  262. {
  263. Integer x{10};
  264. ASSERT_TRUE(x < 12);
  265. }
  266. TEST_F(IntegerTest, operator_less_than_equals_with_reference)
  267. {
  268. Integer x{10};
  269. Integer y{10};
  270. ASSERT_TRUE(x <= y);
  271. }
  272. TEST_F(IntegerTest, operator_less_than_equals_with_value)
  273. {
  274. Integer x{10};
  275. ASSERT_TRUE(x <= 10);
  276. }
  277. // logical operators
  278. TEST_F(IntegerTest, operator_negation)
  279. {
  280. Integer x{};
  281. ASSERT_TRUE(!x);
  282. }
  283. TEST_F(IntegerTest, operator_and_with_reference)
  284. {
  285. Integer x{1};
  286. Integer y{1};
  287. ASSERT_TRUE(x && y);
  288. }
  289. TEST_F(IntegerTest, operator_and_with_value)
  290. {
  291. Integer x{1};
  292. ASSERT_TRUE(x && 1);
  293. }
  294. TEST_F(IntegerTest, operator_and_with_boolean)
  295. {
  296. Integer x{1};
  297. ASSERT_TRUE(x && true);
  298. }
  299. TEST_F(IntegerTest, operator_or_with_reference)
  300. {
  301. Integer x{};
  302. Integer y{1};
  303. ASSERT_TRUE(x || y);
  304. }
  305. TEST_F(IntegerTest, operator_or_with_value)
  306. {
  307. Integer x{};
  308. ASSERT_TRUE(x || 1);
  309. }
  310. TEST_F(IntegerTest, operator_or_with_boolean)
  311. {
  312. Integer x{};
  313. ASSERT_TRUE(x || true);
  314. }
  315. // increment / decrement operator
  316. TEST_F(IntegerTest, operator_increment)
  317. {
  318. Integer x{};
  319. ++x;
  320. ASSERT_EQ(1, x);
  321. }
  322. TEST_F(IntegerTest, operator_decrement)
  323. {
  324. Integer x{};
  325. --x;
  326. ASSERT_EQ(-1, x);
  327. }
  328. // implementation
  329. TEST_F(IntegerTest, parse_with_positive_value)
  330. {
  331. Integer number{};
  332. number.parse("1989");
  333. ASSERT_EQ(1989, number);
  334. }
  335. TEST_F(IntegerTest, parse_with_negative_value)
  336. {
  337. Integer number{};
  338. number.parse("-13");
  339. ASSERT_EQ(-13, number);
  340. }
  341. TEST_F(IntegerTest, toString)
  342. {
  343. Integer number{112};
  344. ASSERT_STREQ("112", number.toString().c_str());
  345. }
  346. // additional functionality
  347. TEST_F(IntegerTest, getValue)
  348. {
  349. Integer x{3};
  350. ASSERT_EQ(3, x.getValue());
  351. }
  352. // additional testing
  353. TEST_F(IntegerTest, constApproach)
  354. {
  355. const Integer x{3};
  356. ASSERT_EQ(3, x);
  357. // x = 4; // wouldn't work
  358. }
  359. }