LongTest.cpp 7.8 KB

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