IntegerTest.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. // implementation
  197. TEST_F(IntegerTest, parse)
  198. {
  199. ls_std::Integer x {};
  200. x.parse("1989");
  201. ASSERT_EQ(1989, x);
  202. x.parse("-17");
  203. ASSERT_EQ(-17, x);
  204. }
  205. }