biginteger_test.cpp 2.3 KB

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