Jelajahi Sumber

Improve Long class test coverage

- add missing test cases
Patrick-Christopher Mattulat 3 tahun lalu
induk
melakukan
53484c3738
2 mengubah file dengan 201 tambahan dan 122 penghapusan
  1. 1 1
      source/ls_std/boxing/Long.cpp
  2. 200 121
      test/cases/boxing/LongTest.cpp

+ 1 - 1
source/ls_std/boxing/Long.cpp

@@ -254,7 +254,7 @@ void ls_std::Long::operator--()
 
 void ls_std::Long::parse(std::string _parseText)
 {
-  this->value = std::stoi(_parseText);
+  this->value = std::stoll(_parseText);
 }
 
 std::string ls_std::Long::toString()

+ 200 - 121
test/cases/boxing/LongTest.cpp

@@ -18,6 +18,7 @@ namespace
     protected:
 
       LongTest() = default;
+
       ~LongTest() override = default;
 
       void SetUp() override
@@ -29,22 +30,26 @@ namespace
 
   // assignment operators
 
-  TEST_F(LongTest, operatorAssignment)
+  TEST_F(LongTest, operator_assignment_with_reference)
   {
     ls_std::Long x{13};
-    ASSERT_EQ(13, x);
-
-    x = 44;
-    ASSERT_EQ(44, x);
-
     ls_std::Long y{3};
     x = y;
+
+    ASSERT_EQ(3, x);
+  }
+
+  TEST_F(LongTest, operator_assignment_with_value)
+  {
+    ls_std::Long x{13};
+    x = (ls_std::long_type) 3;
+
     ASSERT_EQ(3, x);
   }
 
   // arithmetic operators
 
-  TEST_F(LongTest, operatorHyphen)
+  TEST_F(LongTest, operator_negative)
   {
     ls_std::Long x{13};
     ls_std::Long y{-13};
@@ -53,299 +58,373 @@ namespace
     ASSERT_EQ(13, -y);
   }
 
-  TEST_F(LongTest, operatorAddition)
+  TEST_F(LongTest, operator_add_with_reference)
   {
     ls_std::Long x{13};
     ls_std::Long y{7};
-    ls_std::Long z{-15};
 
     ASSERT_EQ(20, x + y);
-    ASSERT_EQ(8, 1 + y);
-    ASSERT_EQ(5, x + y + z);
-    ASSERT_EQ(15, x + (ls_std::long_type) 2);
-    ASSERT_EQ(0, x + (ls_std::long_type) 2 + z);
   }
 
-  TEST_F(LongTest, operatorMultiplication)
+  TEST_F(LongTest, operator_add_with_value)
+  {
+    ls_std::Long x{13};
+    ASSERT_EQ(20, x + (ls_std::long_type) 7);
+  }
+
+  TEST_F(LongTest, operator_mul_with_reference)
   {
     ls_std::Long x{3};
     ls_std::Long y{7};
-    ls_std::Long z{5};
 
     ASSERT_EQ(21, x * y);
-    ASSERT_EQ(105, x * y * z);
-    ASSERT_EQ(6, x * (ls_std::long_type) 2);
-    ASSERT_EQ(71, 1 + y * (ls_std::long_type) 2 * z);
   }
 
-  TEST_F(LongTest, operatorSubtraction)
+  TEST_F(LongTest, operator_mul_with_value)
+  {
+    ls_std::Long x{3};
+    ASSERT_EQ(21, x * (ls_std::long_type) 7);
+  }
+
+  TEST_F(LongTest, operator_sub_with_reference)
   {
     ls_std::Long x{51};
     ls_std::Long y{17};
 
     ASSERT_EQ(34, x - y);
-    ASSERT_EQ(30, x - (ls_std::long_type) 21);
-    ASSERT_EQ(13, x - (ls_std::long_type) 21 - y);
   }
 
-  TEST_F(LongTest, operatorDivision)
+  TEST_F(LongTest, operator_sub_with_value)
+  {
+    ls_std::Long x{51};
+    ASSERT_EQ(34, x - (ls_std::long_type) 17);
+  }
+
+  TEST_F(LongTest, operator_div_with_reference)
   {
     ls_std::Long x{81};
     ls_std::Long y{9};
 
     ASSERT_EQ(9, x / y);
-    ASSERT_EQ(3, 9 / 3);
-    ASSERT_EQ(3, x / y / 3);
-    ASSERT_EQ(1, 19 / 10);
   }
 
-  TEST_F(LongTest, operatorModulus)
+  TEST_F(LongTest, operator_div_with_value)
+  {
+    ls_std::Long x{81};
+    ASSERT_EQ(9, x / (ls_std::long_type) 9);
+  }
+
+  TEST_F(LongTest, operator_div_by_zero_with_reference)
+  {
+    EXPECT_THROW({
+                   try
+                   {
+                     ls_std::Long x{9};
+                     ls_std::Long y{0};
+
+                     x = x / y;
+                   } catch (const ls_std::IllegalArithmeticOperationException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::IllegalArithmeticOperationException);
+  }
+
+  TEST_F(LongTest, operator_div_by_zero_with_value)
+  {
+    EXPECT_THROW({
+                   try
+                   {
+                     ls_std::Long x{9};
+                     x = x / (ls_std::long_type) 0;
+                   } catch (const ls_std::IllegalArithmeticOperationException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::IllegalArithmeticOperationException);
+  }
+
+  TEST_F(LongTest, operator_mod_with_reference)
   {
     ls_std::Long x{85};
     ls_std::Long y{9};
 
     ASSERT_EQ(4, x % y);
-    ASSERT_EQ(1, x % (ls_std::long_type) 3);
+  }
+
+  TEST_F(LongTest, operator_mod_with_value)
+  {
+    ls_std::Long x{85};
+    ASSERT_EQ(4, x % (ls_std::long_type) 9);
   }
 
   // compound operators
 
-  TEST_F(LongTest, operatorAddEqual)
+  TEST_F(LongTest, operator_add_equals_with_reference)
   {
     ls_std::Long x{4};
     ls_std::Long y{2};
-    ASSERT_EQ(4, x);
-
     x += y;
+
     ASSERT_EQ(6, x);
+  }
 
-    x += 4;
-    ASSERT_EQ(10, x);
+  TEST_F(LongTest, operator_add_equals_with_value)
+  {
+    ls_std::Long x{4};
+    x += (ls_std::long_type) 2;
 
-    x += -5;
-    ASSERT_EQ(5, x);
+    ASSERT_EQ(6, x);
   }
 
-  TEST_F(LongTest, operatorSubEqual)
+  TEST_F(LongTest, operator_sub_equals_with_reference)
   {
     ls_std::Long x{14};
     ls_std::Long y{2};
-    ASSERT_EQ(14, x);
-
     x -= y;
+
     ASSERT_EQ(12, x);
+  }
 
-    x -= 6;
-    ASSERT_EQ(6, x);
+  TEST_F(LongTest, operator_sub_equals_with_value)
+  {
+    ls_std::Long x{14};
+    x -= (ls_std::long_type) 2;
 
-    x -= -3;
-    ASSERT_EQ(9, x);
+    ASSERT_EQ(12, x);
   }
 
-  TEST_F(LongTest, operatorMulEqual)
+  TEST_F(LongTest, operator_mul_equals_with_reference)
   {
     ls_std::Long x{6};
     ls_std::Long y{3};
-    ASSERT_EQ(6, x);
-
     x *= y;
+
     ASSERT_EQ(18, x);
+  }
 
-    x *= 2;
-    ASSERT_EQ(36, x);
+  TEST_F(LongTest, operator_mul_equals_with_value)
+  {
+    ls_std::Long x{6};
+    x *= (ls_std::long_type) 3;
+
+    ASSERT_EQ(18, x);
   }
 
-  TEST_F(LongTest, operatorDivEqual)
+  TEST_F(LongTest, operator_div_equals_with_reference)
   {
     ls_std::Long x{12};
     ls_std::Long y{3};
-    ASSERT_EQ(12, x);
-
     x /= y;
+
     ASSERT_EQ(4, x);
+  }
 
-    x /= 2;
-    ASSERT_EQ(2, x);
+  TEST_F(LongTest, operator_div_equals_with_value)
+  {
+    ls_std::Long x{12};
+    x /= (ls_std::long_type) 3;
+
+    ASSERT_EQ(4, x);
+  }
+
+  TEST_F(LongTest, operator_div_equals_by_zero_with_reference)
+  {
+    EXPECT_THROW({
+                   try
+                   {
+                     ls_std::Long x{9};
+                     ls_std::Long y{0};
+
+                     x = x /= y;
+                   } catch (const ls_std::IllegalArithmeticOperationException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::IllegalArithmeticOperationException);
+  }
+
+  TEST_F(LongTest, operator_div_equals_by_zero_with_value)
+  {
+    EXPECT_THROW({
+                   try
+                   {
+                     ls_std::Long x{9};
+                     x = x /= (ls_std::long_type) 0;
+                   } catch (const ls_std::IllegalArithmeticOperationException &_exception)
+                   {
+                     throw;
+                   }
+                 }, ls_std::IllegalArithmeticOperationException);
   }
 
   // comparison operators
 
-  TEST_F(LongTest, operatorEqual)
+  TEST_F(LongTest, operator_equals_with_reference)
   {
     ls_std::Long x{12};
-    ls_std::Long y{3};
-    ls_std::Long z{3};
+    ls_std::Long y{12};
+
+    ASSERT_TRUE(x == y);
+  }
 
+  TEST_F(LongTest, operator_equals_with_value)
+  {
+    ls_std::Long x{12};
     ASSERT_TRUE(x == (ls_std::long_type) 12);
-    ASSERT_TRUE(y == z);
-    ASSERT_TRUE(z == y);
   }
 
-  TEST_F(LongTest, operatorNotEqual)
+  TEST_F(LongTest, operator_not_equals_with_reference)
   {
     ls_std::Long x{12};
     ls_std::Long y{3};
-    ls_std::Long z{3};
 
-    ASSERT_TRUE(x != (ls_std::long_type) 14);
-    ASSERT_TRUE(y != x);
-    ASSERT_TRUE(z != x);
+    ASSERT_TRUE(x != y);
+  }
+
+  TEST_F(LongTest, operator_not_equals_with_value)
+  {
+    ls_std::Long x{12};
+    ASSERT_TRUE(x != (ls_std::long_type) 3);
   }
 
-  TEST_F(LongTest, operatorGreaterThan)
+  TEST_F(LongTest, operator_greater_than_with_reference)
   {
     ls_std::Long x{12};
     ls_std::Long y{3};
 
-    ASSERT_TRUE(x > (ls_std::long_type) 4);
     ASSERT_TRUE(x > y);
   }
 
-  TEST_F(LongTest, operatorGreaterThanNegative)
+  TEST_F(LongTest, operator_greater_than_with_value)
   {
     ls_std::Long x{12};
-    ls_std::Long y{3};
-
-    ASSERT_FALSE(x > (ls_std::long_type) 14);
-    ASSERT_FALSE(x > (y + (ls_std::long_type) 20));
+    ASSERT_TRUE(x > (ls_std::long_type) 3);
   }
 
-  TEST_F(LongTest, operatorGreaterThanEqual)
+  TEST_F(LongTest, operator_greater_than_equals_with_reference)
   {
     ls_std::Long x{12};
     ls_std::Long y{12};
 
-    ASSERT_TRUE(x >= (ls_std::long_type) 12);
     ASSERT_TRUE(x >= y);
   }
 
-  TEST_F(LongTest, operatorGreaterThanEqualNegative)
+  TEST_F(LongTest, operator_greater_than_equals_with_value)
   {
     ls_std::Long x{12};
-    ls_std::Long y{13};
-
-    ASSERT_FALSE(x >= (ls_std::long_type) 13);
-    ASSERT_FALSE(x >= y);
+    ASSERT_TRUE(x >= (ls_std::long_type) 12);
   }
 
-  TEST_F(LongTest, operatorLessThan)
+  TEST_F(LongTest, operator_less_than_with_reference)
   {
     ls_std::Long x{10};
     ls_std::Long y{12};
 
-    ASSERT_TRUE(x < (ls_std::long_type) 12);
     ASSERT_TRUE(x < y);
   }
 
-  TEST_F(LongTest, operatorLessThanNegative)
+  TEST_F(LongTest, operator_less_than_with_value)
   {
     ls_std::Long x{10};
-    ls_std::Long y{10};
+    ls_std::Long y{12};
 
-    ASSERT_FALSE(x < (ls_std::long_type) 10);
-    ASSERT_FALSE(x < y);
+    ASSERT_TRUE(x < (ls_std::long_type) 12);
   }
 
-  TEST_F(LongTest, operatorLessThanEqual)
+  TEST_F(LongTest, operator_less_than_equals_with_reference)
   {
     ls_std::Long x{10};
     ls_std::Long y{10};
 
-    ASSERT_TRUE(x <= (ls_std::long_type) 11);
     ASSERT_TRUE(x <= y);
   }
 
-  TEST_F(LongTest, operatorLessThanEqualNegative)
+  TEST_F(LongTest, operator_less_than_equals_with_value)
   {
-    ls_std::Long x{11};
-    ls_std::Long y{10};
-
-    ASSERT_FALSE(x <= (ls_std::long_type) 10);
-    ASSERT_FALSE(x <= y);
+    ls_std::Long x{10};
+    ASSERT_TRUE(x <= (ls_std::long_type) 10);
   }
 
   // logical operators
 
-  TEST_F(LongTest, operatorNot)
+  TEST_F(LongTest, operator_negation)
   {
     ls_std::Long x{};
     ASSERT_TRUE(!x);
   }
 
-  TEST_F(LongTest, operatorNotNegative)
+  TEST_F(LongTest, operator_and_with_reference)
   {
-    ls_std::Long x{10};
-    ASSERT_FALSE(!x);
+    ls_std::Long x{1};
+    ls_std::Long y{1};
+
+    ASSERT_TRUE(x && y);
   }
 
-  TEST_F(LongTest, operatorAnd)
+  TEST_F(LongTest, operator_and_with_value)
   {
     ls_std::Long x{1};
-    ls_std::Long y{1};
     ASSERT_TRUE(x && (ls_std::long_type) 1);
+  }
+
+  TEST_F(LongTest, operator_and_with_boolean)
+  {
+    ls_std::Long x{1};
     ASSERT_TRUE(x && true);
-    ASSERT_TRUE(x && y);
   }
 
-  TEST_F(LongTest, operatorAndNegative)
+  TEST_F(LongTest, operator_or_with_reference)
   {
     ls_std::Long x{};
     ls_std::Long y{1};
-    ASSERT_FALSE(x && (ls_std::long_type) 1);
-    ASSERT_FALSE(x && true);
-    ASSERT_FALSE(x && y);
+
+    ASSERT_TRUE(x || y);
   }
 
-  TEST_F(LongTest, operatorOr)
+  TEST_F(LongTest, operator_or_with_value)
   {
     ls_std::Long x{};
-    ls_std::Long y{1};
     ASSERT_TRUE(x || (ls_std::long_type) 1);
-    ASSERT_TRUE(x || true);
-    ASSERT_TRUE(x || y);
   }
 
-  TEST_F(LongTest, operatorOrNegative)
+  TEST_F(LongTest, operator_or_with_boolean)
   {
     ls_std::Long x{};
-    ls_std::Long y{};
-    ASSERT_FALSE(x || (ls_std::long_type) 0);
-    ASSERT_FALSE(x || false);
-    ASSERT_FALSE(x || y);
+    ASSERT_TRUE(x || true);
   }
 
   // increment / decrement operator
 
-  TEST_F(LongTest, operatorIncrement)
+  TEST_F(LongTest, operator_increment)
   {
     ls_std::Long x{};
-
     ++x;
-    ASSERT_EQ(1, x);
 
-    ++x;
-    ASSERT_EQ(2, x);
+    ASSERT_EQ(1, x);
   }
 
-  TEST_F(LongTest, operatorDecrement)
+  TEST_F(LongTest, operator_decrement)
   {
     ls_std::Long x{};
-
     --x;
-    ASSERT_EQ(-1, x);
 
-    --x;
-    ASSERT_EQ(-2, x);
+    ASSERT_EQ(-1, x);
   }
 
   // implementation
 
-  TEST_F(LongTest, parse)
+  TEST_F(LongTest, parse_with_positive_value)
   {
     ls_std::Long x{};
 
     x.parse("1989");
     ASSERT_EQ(1989, x);
+  }
+
+  TEST_F(LongTest, parse_with_negative_value)
+  {
+    ls_std::Long x{};
 
     x.parse("-17");
     ASSERT_EQ(-17, x);
@@ -372,6 +451,6 @@ namespace
     const ls_std::Long x{3};
     ASSERT_EQ(3, x);
 
-//     x = 4; // wouldn't work
+    // x = 4; // wouldn't work
   }
 }