LongTest.cpp 7.0 KB

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