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