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