LongTest.cpp 9.4 KB

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