LongTest.cpp 9.4 KB

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