biginteger_test.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // biginteger_test.cpp
  3. // bigdecimal
  4. //
  5. // Created by Sam Jaffe on 5/18/18.
  6. //
  7. #include <gmock/gmock.h>
  8. #include "biginteger.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. using BigIntPair = std::tuple<math::biginteger, math::biginteger>;
  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 BigIntegerLtTest : public testing::TestWithParam<BigIntPair> {};
  43. TEST_P(BigIntegerLtTest, 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(SmallNum, BigIntegerLtTest,
  50. testing::Values(BigIntPair{-1, 1},
  51. BigIntPair{ 0, 1},
  52. BigIntPair{-2, -1}));
  53. INSTANTIATE_TEST_CASE_P(LargeNum, BigIntegerLtTest,
  54. testing::Values(BigIntPair{ 1000000000, 1000000001},
  55. BigIntPair{-1000000001, -1000000000}));
  56. INSTANTIATE_TEST_CASE_P(ZeroModAny, BigIntModuloTest,
  57. testing::Combine(testing::Values(0),
  58. testing::Values(1, 2, 10, 100, -4, 1000000000, -1000000000)));
  59. INSTANTIATE_TEST_CASE_P(AnyModOne, BigIntModuloTest,
  60. testing::Combine(testing::Values(1, 2, 10, 100, -4, 1000000000, -1000000000),
  61. testing::Values(1)));
  62. #pragma clang diagnostic pop