abacus/tests/unit/bignum.cc

200 lines
4.9 KiB
C++
Raw Normal View History

2021-08-20 18:09:28 +02:00
#include <sstream>
2021-08-20 17:22:21 +02:00
#include <gtest/gtest.h>
#include "bignum.hh"
using namespace abacus::bignum;
2021-08-20 18:09:28 +02:00
TEST(BigNum, dump) {
auto const zero = BigNum(0);
auto const one = BigNum(1);
auto const minus_one = BigNum(-1);
auto const forty_two = BigNum(42);
auto const to_str = [](auto num) {
std::stringstream str;
str << num;
return str.str();
};
EXPECT_EQ(to_str(zero), "0");
EXPECT_EQ(to_str(one), "1");
EXPECT_EQ(to_str(minus_one), "-1");
EXPECT_EQ(to_str(forty_two), "42");
2021-08-20 18:09:28 +02:00
}
2021-08-20 19:00:53 +02:00
TEST(BigNum, read) {
auto const from_str = [](auto num) -> BigNum {
std::stringstream str(num);
BigNum res;
EXPECT_TRUE(str >> res);
return res;
};
EXPECT_EQ(from_str("0"), BigNum(0));
EXPECT_EQ(from_str("1"), BigNum(1));
EXPECT_EQ(from_str("-1"), BigNum(-1));
EXPECT_EQ(from_str("42"), BigNum(42));
}
2021-08-20 17:22:21 +02:00
TEST(BigNum, equality) {
auto const zero = BigNum(0);
auto const one = BigNum(1);
EXPECT_EQ(zero, zero);
EXPECT_EQ(one, one);
EXPECT_NE(zero, one);
2021-08-20 17:22:21 +02:00
}
TEST(BigNum, comparisons) {
auto const zero = BigNum(0);
auto const one = BigNum(1);
EXPECT_LT(zero, one);
EXPECT_LE(zero, one);
EXPECT_LE(zero, zero);
2021-08-20 17:22:21 +02:00
EXPECT_GT(one, zero);
EXPECT_GE(one, zero);
EXPECT_GE(one, one);
2021-08-20 17:22:21 +02:00
}
2021-08-20 17:27:56 +02:00
TEST(BigNum, unary) {
auto const zero = BigNum(0);
auto const one = BigNum(1);
EXPECT_EQ(zero, -zero);
EXPECT_NE(one, -one);
2021-08-20 17:27:56 +02:00
auto const minus_one = BigNum(-1);
EXPECT_EQ(minus_one, -one);
2021-08-20 17:27:56 +02:00
}
TEST(BigNum, addition_positive) {
auto const zero = BigNum(0);
auto const one = BigNum(1);
auto const two = BigNum(2);
EXPECT_EQ(zero + zero, zero);
EXPECT_EQ(zero + one, one);
EXPECT_EQ(one + zero, one);
EXPECT_EQ(one + one, two);
}
TEST(BigNum, addition_negative) {
auto const zero = BigNum(0);
auto const minus_one = BigNum(-1);
auto const minus_two = BigNum(-2);
EXPECT_EQ(zero + zero, zero);
EXPECT_EQ(zero + minus_one, minus_one);
EXPECT_EQ(minus_one + zero, minus_one);
EXPECT_EQ(minus_one + minus_one, minus_two);
}
TEST(BigNum, addition_mixed_signs) {
auto const zero = BigNum(0);
auto const one = BigNum(1);
auto const minus_one = BigNum(-1);
EXPECT_EQ(one + minus_one, zero);
EXPECT_EQ(minus_one + one, zero);
}
TEST(BigNum, addition_flips_sign) {
auto const one = BigNum(1);
auto const two = BigNum(2);
auto const minus_one = BigNum(-1);
auto const minus_two = BigNum(-2);
EXPECT_EQ(one + minus_two, minus_one);
EXPECT_EQ(minus_two + one, minus_one);
EXPECT_EQ(minus_one + two, one);
EXPECT_EQ(two + minus_one, one);
}
TEST(BigNum, substraction_positive) {
auto const zero = BigNum(0);
auto const one = BigNum(1);
auto const two = BigNum(2);
EXPECT_EQ(zero - zero, zero);
EXPECT_EQ(two - zero, two);
EXPECT_EQ(two - one, one);
EXPECT_EQ(one - one, zero);
}
TEST(BigNum, substraction_negative) {
auto const zero = BigNum(0);
auto const minus_one = BigNum(-1);
auto const one = BigNum(1);
EXPECT_EQ(zero - zero, zero);
EXPECT_EQ(zero - minus_one, one);
EXPECT_EQ(zero - one, minus_one);
}
TEST(BigNum, substraction_mixed_signs) {
auto const zero = BigNum(0);
auto const one = BigNum(1);
auto const two = BigNum(2);
auto const minus_one = BigNum(-1);
auto const minus_two = BigNum(-2);
EXPECT_EQ(one - minus_one, two);
EXPECT_EQ(minus_one - one, minus_two);
}
TEST(BigNum, substraction_flips_sign) {
auto const one = BigNum(1);
auto const two = BigNum(2);
auto const minus_one = BigNum(-1);
auto const minus_two = BigNum(-2);
EXPECT_EQ(one - two, minus_one);
EXPECT_EQ(minus_one - minus_two, one);
}
2021-08-20 22:19:20 +02:00
TEST(BigNum, multiplication_zero) {
auto const zero = BigNum(0);
auto const one = BigNum(1);
auto const minus_one = BigNum(-1);
EXPECT_EQ(zero * zero, zero);
EXPECT_EQ(zero * one, zero);
EXPECT_EQ(one * zero, zero);
EXPECT_EQ(zero * minus_one, zero);
EXPECT_EQ(minus_one * zero, zero);
}
TEST(BigNum, multiplication_positive) {
auto const one = BigNum(1);
auto const two = BigNum(2);
auto const four = BigNum(4);
EXPECT_EQ(one * one, one);
EXPECT_EQ(two * one, two);
EXPECT_EQ(one * two, two);
EXPECT_EQ(two * two, four);
}
TEST(BigNum, multiplication_mixed_signs) {
auto const minus_one = BigNum(-1);
auto const one = BigNum(1);
auto const minus_two = BigNum(-2);
auto const two = BigNum(2);
auto const minus_four = BigNum(-4);
auto const four = BigNum(4);
EXPECT_EQ(one * minus_one, minus_one);
EXPECT_EQ(minus_one * one, minus_one);
EXPECT_EQ(minus_one * minus_one, one);
EXPECT_EQ(two * minus_one, minus_two);
EXPECT_EQ(minus_one * two, minus_two);
EXPECT_EQ(two * minus_two, minus_four);
EXPECT_EQ(minus_two * two, minus_four);
EXPECT_EQ(minus_two * minus_two, four);
}