IntegerTest.cpp 6.3 KB

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