LongTest.cpp 7.1 KB

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