biginteger.t.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // biginteger.t.h
  3. // bigdecimal
  4. //
  5. // Created by Sam Jaffe on 6/30/17.
  6. //
  7. #pragma once
  8. #include "biginteger.h"
  9. #include <cxxtest/TestSuite.h>
  10. class biginteger_TestSuite : public CxxTest::TestSuite {
  11. public:
  12. void testAddPastBounds() {
  13. math::biginteger bi{999999999ULL};
  14. TS_ASSERT_EQUALS((bi+1).to_string(), "1000000000");
  15. }
  16. void testAddReciprocalIsZero() {
  17. math::biginteger bi{1000};
  18. TS_ASSERT_EQUALS((bi+(-bi)).to_string(), "0");
  19. }
  20. void testAddNegativeLargerGivesNegative() {
  21. math::biginteger bi{1000};
  22. TS_ASSERT_EQUALS((bi+(-1001)).to_string(), "-1");
  23. }
  24. void testAddNegativeSmallerGivesPositive() {
  25. math::biginteger bi{1000};
  26. TS_ASSERT_EQUALS((bi+(-999)).to_string(), "1");
  27. }
  28. void testSubSelfIsZero() {
  29. math::biginteger bi{1000};
  30. TS_ASSERT_EQUALS((bi-bi).to_string(), "0");
  31. }
  32. void testNegativeMinusNegativeIncreateAbs() {
  33. math::biginteger bi{-1000};
  34. TS_ASSERT_EQUALS((bi-100).to_string(), "-1100");
  35. }
  36. void testSubLargerGivesNegative() {
  37. math::biginteger bi{1000};
  38. TS_ASSERT_EQUALS((bi-1001).to_string(), "-1");
  39. }
  40. void testSubSmallerGivesPositive() {
  41. math::biginteger bi{1000};
  42. TS_ASSERT_EQUALS((bi-999).to_string(), "1");
  43. }
  44. void testSubUnderflowBorrows() {
  45. math::biginteger bi{1000000000ULL};
  46. TS_ASSERT_EQUALS((bi-1).to_string(), "999999999");
  47. }
  48. void testMultiplyZeroReturnsZero() {
  49. auto &ZERO = math::biginteger::ZERO;
  50. math::biginteger bi{999999999ULL};
  51. TS_ASSERT_EQUALS((bi*ZERO).to_string(), "0");
  52. TS_ASSERT_EQUALS((ZERO*bi).to_string(), "0");
  53. }
  54. void testMultiplyOneReturnsValue() {
  55. auto &ONE = math::biginteger::ONE;
  56. math::biginteger bi{999999999ULL};
  57. TS_ASSERT_EQUALS((bi*ONE).to_string(), "999999999");
  58. TS_ASSERT_EQUALS((ONE*bi).to_string(), "999999999");
  59. }
  60. void testMultiplyNegativeOneReturnsInverse() {
  61. auto &NEGATIVE_ONE = math::biginteger::NEGATIVE_ONE;
  62. math::biginteger bi{999999999ULL};
  63. TS_ASSERT_EQUALS((bi*NEGATIVE_ONE).to_string(), "-999999999");
  64. TS_ASSERT_EQUALS((NEGATIVE_ONE*bi).to_string(), "-999999999");
  65. }
  66. void testMultiplyOverflowsIntoNextCell() {
  67. math::biginteger bi{999999999ULL};
  68. TS_ASSERT_EQUALS((bi*bi).to_string(), "999999998000000001");
  69. }
  70. void testMultiplyCarryIntoNextCell() {
  71. math::biginteger bi{999999999ULL};
  72. math::biginteger big{bi*bi};
  73. TS_ASSERT_EQUALS((big*big).to_string(),
  74. "999999996000000005999999996000000001");
  75. }
  76. };