IntegerTest.cpp 6.3 KB

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