biginteger_test.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // biginteger_test.cpp
  3. // bigdecimal
  4. //
  5. // Created by Sam Jaffe on 5/18/18.
  6. //
  7. #include "bignumber_test_printers.h"
  8. #include <gmock/gmock.h>
  9. #include "biginteger.h"
  10. TEST(BigIntegerTest, ModuloZeroThrows) {
  11. math::biginteger bi{1000};
  12. EXPECT_THROW(bi%0, std::domain_error);
  13. }
  14. TEST(BigIntegerTest, ModuloBiggerIsSameValue) {
  15. math::biginteger bi{1000};
  16. EXPECT_THAT(bi%2000, bi);
  17. }
  18. TEST(BigIntegerTest, ModuloSameNumberIsZero) {
  19. math::biginteger bi{1000};
  20. EXPECT_THAT(bi%1000, 0);
  21. }
  22. TEST(BigIntegerTest, ModuloDivisorIsZero) {
  23. math::biginteger bi{1000};
  24. EXPECT_THAT(bi%100, 0);
  25. }
  26. TEST(BigIntegerTest, ModuloDiffSignIsInverseElement) {
  27. math::biginteger bi{1000};
  28. math::biginteger mod{13};
  29. EXPECT_THAT((bi%mod)+((-bi)%mod), mod);
  30. }
  31. TEST(BigIntegerTest, ModuloNegativesIsNegative) {
  32. math::biginteger bi{1000};
  33. math::biginteger mod{13};
  34. EXPECT_THAT((bi%mod), -((-bi)%(-mod)));
  35. }
  36. class BigIntModuloTest : public testing::TestWithParam<BigIntPair> {};
  37. TEST_P(BigIntModuloTest, IsZero) {
  38. auto & ZERO = math::biginteger::ZERO;
  39. auto pair = GetParam();
  40. EXPECT_THAT(std::get<0>(pair)%std::get<1>(pair), ZERO);
  41. }
  42. class LtTest : public testing::TestWithParam<BigIntPair> {};
  43. TEST_P(LtTest, IsLessThan) {
  44. auto pair = GetParam();
  45. EXPECT_THAT(std::get<0>(pair), testing::Lt(std::get<1>(pair)));
  46. }
  47. #pragma clang diagnostic push
  48. #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
  49. INSTANTIATE_TEST_CASE_P(BigInteger, LtTest,
  50. testing::Values(BigIntPair{-1, 1},
  51. BigIntPair{ 0, 1},
  52. BigIntPair{-2, -1},
  53. BigIntPair{ 1000000000, 1000000001},
  54. BigIntPair{-1000000001, -1000000000}));
  55. INSTANTIATE_TEST_CASE_P(ZeroModAny, BigIntModuloTest,
  56. testing::Combine(testing::Values(0),
  57. testing::Values(1, 2, 10, 100, -4, 1000000000, -1000000000)));
  58. INSTANTIATE_TEST_CASE_P(AnyModOne, BigIntModuloTest,
  59. testing::Combine(testing::Values(1, 2, 10, 100, -4, 1000000000, -1000000000),
  60. testing::Values(1)));
  61. #pragma clang diagnostic pop