LongTest.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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-17
  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 + (long) 2);
  49. ASSERT_EQ(0, x + (long) 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 * (long) 2);
  59. ASSERT_EQ(71, 1 + y * (long) 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 - (long) 21);
  67. ASSERT_EQ(13, x - (long) 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 % (long) 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 == (long) 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 != (long) 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 > (long) 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 > (long) 14);
  161. ASSERT_FALSE(x > (y + (long) 20));
  162. }
  163. TEST_F(LongTest, operatorGreaterThanEqual)
  164. {
  165. ls_std::Long x {12};
  166. ls_std::Long y {12};
  167. ASSERT_TRUE(x >= (long) 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 >= (long) 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 < (long) 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 < (long) 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 <= (long) 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 <= (long) 10);
  203. ASSERT_FALSE(x <= y);
  204. }
  205. // stream operators / input stream not testable by using automated tests
  206. // TODO: not compilable
  207. // TEST_F(LongTest, operatorOutputStream)
  208. // {
  209. // ls_std::Long x {10};
  210. // std::ostringstream _stream {};
  211. // _stream << x;
  212. //
  213. // ASSERT_STREQ("10", _stream.str().c_str());
  214. // }
  215. // logical operators
  216. TEST_F(LongTest, operatorNot)
  217. {
  218. ls_std::Long x {};
  219. ASSERT_TRUE(!x);
  220. }
  221. TEST_F(LongTest, operatorNotNegative)
  222. {
  223. ls_std::Long x {10};
  224. ASSERT_FALSE(!x);
  225. }
  226. TEST_F(LongTest, operatorAnd)
  227. {
  228. ls_std::Long x {1};
  229. ls_std::Long y {1};
  230. ASSERT_TRUE(x && (long) 1);
  231. ASSERT_TRUE(x && true);
  232. ASSERT_TRUE(x && y);
  233. }
  234. TEST_F(LongTest, operatorAndNegative)
  235. {
  236. ls_std::Long x {};
  237. ls_std::Long y {1};
  238. ASSERT_FALSE(x && (long) 1);
  239. ASSERT_FALSE(x && true);
  240. ASSERT_FALSE(x && y);
  241. }
  242. TEST_F(LongTest, operatorOr)
  243. {
  244. ls_std::Long x {};
  245. ls_std::Long y {1};
  246. ASSERT_TRUE(x || (long) 1);
  247. ASSERT_TRUE(x || true);
  248. ASSERT_TRUE(x || y);
  249. }
  250. TEST_F(LongTest, operatorOrNegative)
  251. {
  252. ls_std::Long x {};
  253. ls_std::Long y {};
  254. ASSERT_FALSE(x || (long) 0);
  255. ASSERT_FALSE(x || false);
  256. ASSERT_FALSE(x || y);
  257. }
  258. // increment / decrement operator
  259. TEST_F(LongTest, operatorIncrement)
  260. {
  261. ls_std::Long x {};
  262. ++x;
  263. ASSERT_EQ(1, x);
  264. ++x;
  265. ASSERT_EQ(2, x);
  266. }
  267. TEST_F(LongTest, operatorDecrement)
  268. {
  269. ls_std::Long x {};
  270. --x;
  271. ASSERT_EQ(-1, x);
  272. --x;
  273. ASSERT_EQ(-2, x);
  274. }
  275. // implementation
  276. TEST_F(LongTest, parse)
  277. {
  278. ls_std::Long x {};
  279. x.parse("1989");
  280. ASSERT_EQ(1989, x);
  281. x.parse("-17");
  282. ASSERT_EQ(-17, x);
  283. }
  284. TEST_F(LongTest, toString)
  285. {
  286. ls_std::Long x {112};
  287. ASSERT_STREQ("112", x.toString().c_str());
  288. }
  289. // additional functionality
  290. TEST_F(LongTest, getValue)
  291. {
  292. ls_std::Long x {3};
  293. ASSERT_EQ(3, x.getValue());
  294. }
  295. // additional testing
  296. TEST_F(LongTest, constApproach)
  297. {
  298. const ls_std::Long x {3};
  299. ASSERT_EQ(3, x);
  300. // x = 4; // wouldn't work
  301. }
  302. }