IntegerTest.cpp 5.9 KB

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