IntegerTest.cpp 6.4 KB

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