LongTest.cpp 7.7 KB

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