LongTest.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-17
  6. * Changed: 2020-08-26
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include "../../../source/boxing/Long.hpp"
  11. namespace {
  12. class LongTest : public ::testing::Test {
  13. protected:
  14. LongTest() = default;
  15. ~LongTest() override = default;
  16. void SetUp() override
  17. {}
  18. void TearDown() override
  19. {}
  20. };
  21. // assignment operators
  22. TEST_F(LongTest, operatorAssignment)
  23. {
  24. ls_std::Long x {13};
  25. ASSERT_EQ(13, x);
  26. x = 44;
  27. ASSERT_EQ(44, x);
  28. ls_std::Long y {3};
  29. x = y;
  30. ASSERT_EQ(3, x);
  31. }
  32. // arithmetic operators
  33. TEST_F(LongTest, operatorHyphen)
  34. {
  35. ls_std::Long x {13};
  36. ls_std::Long y {-13};
  37. ASSERT_EQ(-13, -x);
  38. ASSERT_EQ(13, -y);
  39. }
  40. TEST_F(LongTest, operatorAddition)
  41. {
  42. ls_std::Long x {13};
  43. ls_std::Long y {7};
  44. ls_std::Long z {-15};
  45. ASSERT_EQ(20, x + y);
  46. ASSERT_EQ(8, 1 + y);
  47. ASSERT_EQ(5, x + y + z);
  48. ASSERT_EQ(15, x + (ls_std::long_type) 2);
  49. ASSERT_EQ(0, x + (ls_std::long_type) 2 + z);
  50. }
  51. TEST_F(LongTest, operatorMultiplication)
  52. {
  53. ls_std::Long x {3};
  54. ls_std::Long y {7};
  55. ls_std::Long z {5};
  56. ASSERT_EQ(21, x * y);
  57. ASSERT_EQ(105, x * y * z);
  58. ASSERT_EQ(6, x * (ls_std::long_type) 2);
  59. ASSERT_EQ(71, 1 + y * (ls_std::long_type) 2 * z);
  60. }
  61. TEST_F(LongTest, operatorSubtraction)
  62. {
  63. ls_std::Long x {51};
  64. ls_std::Long y {17};
  65. ASSERT_EQ(34, x - y);
  66. ASSERT_EQ(30, x - (ls_std::long_type) 21);
  67. ASSERT_EQ(13, x - (ls_std::long_type) 21 - y);
  68. }
  69. TEST_F(LongTest, operatorDivision)
  70. {
  71. ls_std::Long x {81};
  72. ls_std::Long y {9};
  73. ASSERT_EQ(9, x / y);
  74. ASSERT_EQ(3, 9 / 3);
  75. ASSERT_EQ(3, x / y / 3);
  76. ASSERT_EQ(1, 19 / 10);
  77. }
  78. TEST_F(LongTest, operatorModulus)
  79. {
  80. ls_std::Long x {85};
  81. ls_std::Long y {9};
  82. ASSERT_EQ(4, x % y);
  83. ASSERT_EQ(1, x % (ls_std::long_type) 3);
  84. }
  85. // compound operators
  86. TEST_F(LongTest, operatorAddEqual)
  87. {
  88. ls_std::Long x {4};
  89. ls_std::Long y {2};
  90. ASSERT_EQ(4, x);
  91. x += y;
  92. ASSERT_EQ(6, x);
  93. x += 4;
  94. ASSERT_EQ(10, x);
  95. x += -5;
  96. ASSERT_EQ(5, x);
  97. }
  98. TEST_F(LongTest, operatorSubEqual)
  99. {
  100. ls_std::Long x {14};
  101. ls_std::Long y {2};
  102. ASSERT_EQ(14, x);
  103. x -= y;
  104. ASSERT_EQ(12, x);
  105. x -= 6;
  106. ASSERT_EQ(6, x);
  107. x -= -3;
  108. ASSERT_EQ(9, x);
  109. }
  110. TEST_F(LongTest, operatorMulEqual)
  111. {
  112. ls_std::Long x {6};
  113. ls_std::Long y {3};
  114. ASSERT_EQ(6, x);
  115. x *= y;
  116. ASSERT_EQ(18, x);
  117. x *= 2;
  118. ASSERT_EQ(36, x);
  119. }
  120. TEST_F(LongTest, operatorDivEqual)
  121. {
  122. ls_std::Long x {12};
  123. ls_std::Long y {3};
  124. ASSERT_EQ(12, x);
  125. x /= y;
  126. ASSERT_EQ(4, x);
  127. x /= 2;
  128. ASSERT_EQ(2, x);
  129. }
  130. // comparison operators
  131. TEST_F(LongTest, operatorEqual)
  132. {
  133. ls_std::Long x {12};
  134. ls_std::Long y {3};
  135. ls_std::Long z {3};
  136. ASSERT_TRUE(x == (ls_std::long_type) 12);
  137. ASSERT_TRUE(y == z);
  138. ASSERT_TRUE(z == y);
  139. }
  140. TEST_F(LongTest, operatorNotEqual)
  141. {
  142. ls_std::Long x {12};
  143. ls_std::Long y {3};
  144. ls_std::Long z {3};
  145. ASSERT_TRUE(x != (ls_std::long_type) 14);
  146. ASSERT_TRUE(y != x);
  147. ASSERT_TRUE(z != x);
  148. }
  149. TEST_F(LongTest, operatorGreaterThan)
  150. {
  151. ls_std::Long x {12};
  152. ls_std::Long y {3};
  153. ASSERT_TRUE(x > (ls_std::long_type) 4);
  154. ASSERT_TRUE(x > y);
  155. }
  156. TEST_F(LongTest, operatorGreaterThanNegative)
  157. {
  158. ls_std::Long x {12};
  159. ls_std::Long y {3};
  160. ASSERT_FALSE(x > (ls_std::long_type) 14);
  161. ASSERT_FALSE(x > (y + (ls_std::long_type) 20));
  162. }
  163. TEST_F(LongTest, operatorGreaterThanEqual)
  164. {
  165. ls_std::Long x {12};
  166. ls_std::Long y {12};
  167. ASSERT_TRUE(x >= (ls_std::long_type) 12);
  168. ASSERT_TRUE(x >= y);
  169. }
  170. TEST_F(LongTest, operatorGreaterThanEqualNegative)
  171. {
  172. ls_std::Long x {12};
  173. ls_std::Long y {13};
  174. ASSERT_FALSE(x >= (ls_std::long_type) 13);
  175. ASSERT_FALSE(x >= y);
  176. }
  177. TEST_F(LongTest, operatorLessThan)
  178. {
  179. ls_std::Long x {10};
  180. ls_std::Long y {12};
  181. ASSERT_TRUE(x < (ls_std::long_type) 12);
  182. ASSERT_TRUE(x < y);
  183. }
  184. TEST_F(LongTest, operatorLessThanNegative)
  185. {
  186. ls_std::Long x {10};
  187. ls_std::Long y {10};
  188. ASSERT_FALSE(x < (ls_std::long_type) 10);
  189. ASSERT_FALSE(x < y);
  190. }
  191. TEST_F(LongTest, operatorLessThanEqual)
  192. {
  193. ls_std::Long x {10};
  194. ls_std::Long y {10};
  195. ASSERT_TRUE(x <= (ls_std::long_type) 11);
  196. ASSERT_TRUE(x <= y);
  197. }
  198. TEST_F(LongTest, operatorLessThanEqualNegative)
  199. {
  200. ls_std::Long x {11};
  201. ls_std::Long y {10};
  202. ASSERT_FALSE(x <= (ls_std::long_type) 10);
  203. ASSERT_FALSE(x <= y);
  204. }
  205. // logical operators
  206. TEST_F(LongTest, operatorNot)
  207. {
  208. ls_std::Long x {};
  209. ASSERT_TRUE(!x);
  210. }
  211. TEST_F(LongTest, operatorNotNegative)
  212. {
  213. ls_std::Long x {10};
  214. ASSERT_FALSE(!x);
  215. }
  216. TEST_F(LongTest, operatorAnd)
  217. {
  218. ls_std::Long x {1};
  219. ls_std::Long y {1};
  220. ASSERT_TRUE(x && (ls_std::long_type) 1);
  221. ASSERT_TRUE(x && true);
  222. ASSERT_TRUE(x && y);
  223. }
  224. TEST_F(LongTest, operatorAndNegative)
  225. {
  226. ls_std::Long x {};
  227. ls_std::Long y {1};
  228. ASSERT_FALSE(x && (ls_std::long_type) 1);
  229. ASSERT_FALSE(x && true);
  230. ASSERT_FALSE(x && y);
  231. }
  232. TEST_F(LongTest, operatorOr)
  233. {
  234. ls_std::Long x {};
  235. ls_std::Long y {1};
  236. ASSERT_TRUE(x || (ls_std::long_type) 1);
  237. ASSERT_TRUE(x || true);
  238. ASSERT_TRUE(x || y);
  239. }
  240. TEST_F(LongTest, operatorOrNegative)
  241. {
  242. ls_std::Long x {};
  243. ls_std::Long y {};
  244. ASSERT_FALSE(x || (ls_std::long_type) 0);
  245. ASSERT_FALSE(x || false);
  246. ASSERT_FALSE(x || y);
  247. }
  248. // increment / decrement operator
  249. TEST_F(LongTest, operatorIncrement)
  250. {
  251. ls_std::Long x {};
  252. ++x;
  253. ASSERT_EQ(1, x);
  254. ++x;
  255. ASSERT_EQ(2, x);
  256. }
  257. TEST_F(LongTest, operatorDecrement)
  258. {
  259. ls_std::Long x {};
  260. --x;
  261. ASSERT_EQ(-1, x);
  262. --x;
  263. ASSERT_EQ(-2, x);
  264. }
  265. // implementation
  266. TEST_F(LongTest, parse)
  267. {
  268. ls_std::Long x {};
  269. x.parse("1989");
  270. ASSERT_EQ(1989, x);
  271. x.parse("-17");
  272. ASSERT_EQ(-17, x);
  273. }
  274. TEST_F(LongTest, toString)
  275. {
  276. ls_std::Long x {112};
  277. ASSERT_STREQ("112", x.toString().c_str());
  278. }
  279. // additional functionality
  280. TEST_F(LongTest, getValue)
  281. {
  282. ls_std::Long x {3};
  283. ASSERT_EQ(3, x.getValue());
  284. }
  285. // additional testing
  286. TEST_F(LongTest, constApproach)
  287. {
  288. const ls_std::Long x {3};
  289. ASSERT_EQ(3, x);
  290. // x = 4; // wouldn't work
  291. }
  292. }