IntegerTest.cpp 7.4 KB

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