Skip to content

Commit

Permalink
Add base condition checks for division and modulo
Browse files Browse the repository at this point in the history
  • Loading branch information
faheel committed Jan 3, 2018
1 parent d707ff5 commit 364b0a3
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions test/operators/binary_arithmetic.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,32 +77,34 @@ TEST_CASE("Binary arithmetic operations on BigInts with integers, strings and Bi
}
}

TEST_CASE("Binary arithmetic operations on BigInts and zeroes",
TEST_CASE("Binary arithmetic operations with zeroes",
"[operators][binary-arithmetic][addition][subtraction][multiplication]"
"[division][modulo][integer][string][BigInt]") {
BigInt big_int = 1234567890;
"[division][modulo]") {
BigInt num;
num = "1234567890123456789012345678901234567890";

REQUIRE(num + 0 == num);
REQUIRE(num - 0 == num);
REQUIRE(num * 0 == 0);

REQUIRE(big_int + 0 == big_int);
REQUIRE(big_int - 0 == big_int);
REQUIRE(big_int * 0 == 0);
try {
BigInt this_is_undefined = big_int / 0;
BigInt undefined = num / 0;
}
catch (std::logic_error e) {
catch (std::logic_error &e) {
CHECK(e.what() == std::string("Attempted division by zero"));
}
try {
BigInt this_is_undefined = big_int % 0;
BigInt undefined = num % 0;
}
catch (std::logic_error e) {
catch (std::logic_error &e) {
CHECK(e.what() == std::string("Attempted division by zero"));
}

REQUIRE(0 + big_int == big_int);
REQUIRE(0 - big_int == -big_int);
REQUIRE(0 * big_int == 0);
REQUIRE(0 / big_int == 0);
REQUIRE(0 % big_int == 0);
REQUIRE(0 + num == num);
REQUIRE(0 - num == -num);
REQUIRE(0 * num == 0);
REQUIRE(0 / num == 0);
REQUIRE(0 % num == 0);
}

TEST_CASE("Chaining addition and subtraction",
Expand Down Expand Up @@ -278,6 +280,16 @@ TEST_CASE("Multiplication of big numbers",
REQUIRE(num1 * num2 == big_pow10(43326));
}

TEST_CASE("Base cases for division", "[binary-arithmetic][operators][division]") {
BigInt num;
num = "1234567890123456789012345678901234567890";

REQUIRE(num / 1 == num);
REQUIRE(num / -1 == -num);

REQUIRE((num - 1) / num == 0);
}

TEST_CASE("Division of big numbers",
"[binary-arithmetic][operators][division][big]") {
BigInt num1, num2;
Expand Down Expand Up @@ -322,6 +334,17 @@ TEST_CASE("Division of big numbers",
REQUIRE(num1 / num2 == big_pow10(3592));
}

TEST_CASE("Base cases for modulo", "[binary-arithmetic][operators][modulo]") {
BigInt num;
num = "1234567890123456789012345678901234567890";

REQUIRE(num % 1 == 0);
REQUIRE(num % -1 == 0);

REQUIRE(num % num == 0);
REQUIRE(num % -num == 0);
}

TEST_CASE("Modulo of big numbers",
"[binary-arithmetic][operators][modulo][big]") {
BigInt num1, num2;
Expand Down

0 comments on commit 364b0a3

Please sign in to comment.