IntegerTest.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. // implementation
  122. TEST_F(IntegerTest, parse)
  123. {
  124. ls_std::Integer x {};
  125. x.parse("1989");
  126. ASSERT_EQ(1989, x);
  127. x.parse("-17");
  128. ASSERT_EQ(-17, x);
  129. }
  130. }