Browse Source

Adding tests for bigdecimal using integer values

Sam Jaffe 7 years ago
parent
commit
fc1b655b75
2 changed files with 178 additions and 1 deletions
  1. 3 1
      bigdecimal.xcodeproj/project.pbxproj
  2. 175 0
      test/bigdecimal_integer.t.h

+ 3 - 1
bigdecimal.xcodeproj/project.pbxproj

@@ -40,6 +40,7 @@
 		CD2EC1BE1F0AF3B800D49DF5 /* bigdecimal.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = bigdecimal.cpp; sourceTree = "<group>"; };
 		CD2EC1C11F0BCCA700D49DF5 /* bignum_helper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = bignum_helper.h; sourceTree = "<group>"; };
 		CD2EC1C41F0BCCBF00D49DF5 /* bignum_helper.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = bignum_helper.cpp; sourceTree = "<group>"; };
+		CD3B82A31F114E1C0081E9FC /* bigdecimal_integer.t.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = bigdecimal_integer.t.h; sourceTree = "<group>"; };
 		CD5FB2711F06EEAF005A0D61 /* bigdecimal_tc */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = bigdecimal_tc; sourceTree = BUILT_PRODUCTS_DIR; };
 		CD5FB27E1F06EF64005A0D61 /* biginteger.t.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = biginteger.t.h; sourceTree = "<group>"; };
 		CD5FB27F1F06EF70005A0D61 /* biginteger.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = biginteger.h; sourceTree = "<group>"; };
@@ -119,6 +120,7 @@
 			isa = PBXGroup;
 			children = (
 				CD5FB27E1F06EF64005A0D61 /* biginteger.t.h */,
+				CD3B82A31F114E1C0081E9FC /* bigdecimal_integer.t.h */,
 				CD14CA711F0DA9FC0091A168 /* bigdecimal.t.h */,
 				CD5FB2A61F06F0D2005A0D61 /* bigdecimal_tc.cpp */,
 			);
@@ -256,7 +258,7 @@
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 			shellPath = /bin/sh;
-			shellScript = "cd test; cxxtestgen --error-printer -o bigdecimal_tc.cpp biginteger.t.h bigdecimal.t.h";
+			shellScript = "cd test; cxxtestgen --error-printer -o bigdecimal_tc.cpp biginteger.t.h bigdecimal.t.h bigdecimal_integer.t.h";
 		};
 /* End PBXShellScriptBuildPhase section */
 

+ 175 - 0
test/bigdecimal_integer.t.h

@@ -0,0 +1,175 @@
+//
+//  bigdecimal.t.h
+//  bigdecimal
+//
+//  Created by Sam Jaffe on 6/30/17.
+//
+
+#pragma once
+
+#include "bigdecimal.h"
+
+#include <cxxtest/TestSuite.h>
+
+class bigdecimal_integer_TestSuite : public CxxTest::TestSuite {
+public:
+  void testConstructFromStringIsSameValueAsFromInt() {
+    using bi = math::bigdecimal;
+    TS_ASSERT_EQUALS(bi("1000000"), bi(1000000));
+  }
+  
+  void testAddPastBounds() {
+    math::bigdecimal bi{999999999ULL};
+    TS_ASSERT_EQUALS((bi+1).to_string(), "1000000000");
+  }
+  
+  void testAddReciprocalIsZero() {
+    math::bigdecimal bi{1000};
+    TS_ASSERT_EQUALS((bi+(-bi)).to_string(), "0");
+  }
+
+  void testAddNegativeLargerGivesNegative() {
+    math::bigdecimal bi{1000};
+    TS_ASSERT_EQUALS((bi+(-1001)).to_string(), "-1");
+  }
+
+  void testAddNegativeSmallerGivesPositive() {
+    math::bigdecimal bi{1000};
+    TS_ASSERT_EQUALS((bi+(-999)).to_string(), "1");
+  }
+  
+  void testSubSelfIsZero() {
+    math::bigdecimal bi{1000};
+    TS_ASSERT_EQUALS((bi-bi).to_string(), "0");
+  }
+  
+  void testNegativeMinusNegativeIncreateAbs() {
+    math::bigdecimal bi{-1000};
+    TS_ASSERT_EQUALS((bi-100).to_string(), "-1100");
+  }
+  
+  void testSubLargerGivesNegative() {
+    math::bigdecimal bi{1000};
+    TS_ASSERT_EQUALS((bi-1001).to_string(), "-1");
+  }
+  
+  void testSubSmallerGivesPositive() {
+    math::bigdecimal bi{1000};
+    TS_ASSERT_EQUALS((bi-999).to_string(), "1");
+  }
+
+  void testSubUnderflowBorrows() {
+    math::bigdecimal bi{1000000000ULL};
+    TS_ASSERT_EQUALS((bi-1).to_string(), "999999999");
+  }
+
+  void testMultiplyZeroReturnsZero() {
+    auto &ZERO =  math::bigdecimal::ZERO;
+    math::bigdecimal bi{999999999ULL};
+    TS_ASSERT_EQUALS(bi*ZERO, ZERO);
+    TS_ASSERT_EQUALS(ZERO*bi, ZERO);
+  }
+  
+  void testMultiplyOneReturnsValue() {
+    auto &ONE =  math::bigdecimal::ONE;
+    math::bigdecimal bi{999999999ULL};
+    TS_ASSERT_EQUALS((bi*ONE).to_string(), "999999999");
+    TS_ASSERT_EQUALS((ONE*bi).to_string(), "999999999");
+  }
+
+  void testMultiplyNegativeOneReturnsInverse() {
+    auto &NEGATIVE_ONE =  math::bigdecimal::NEGATIVE_ONE;
+    math::bigdecimal bi{999999999ULL};
+    TS_ASSERT_EQUALS((bi*NEGATIVE_ONE).to_string(), "-999999999");
+    TS_ASSERT_EQUALS((NEGATIVE_ONE*bi).to_string(), "-999999999");
+  }
+  
+  void testMultiplyOverflowsIntoNextCell() {
+    math::bigdecimal bi{999999999ULL};
+    TS_ASSERT_EQUALS((bi*bi).to_string(), "999999998000000001");
+  }
+  
+  void testMultiplyCarryIntoNextCell() {
+    math::bigdecimal bi{999999999ULL};
+    math::bigdecimal big{bi*bi};
+    TS_ASSERT_EQUALS((big*big).to_string(),
+                     "999999996000000005999999996000000001");
+  }
+  
+  void testMultiplyNoOverflow() {
+    math::bigdecimal bi{1000};
+    TS_ASSERT_EQUALS((bi*bi).to_string(), "1000000");
+  }
+  
+  void testDivideByZeroThrows() {
+    auto &ZERO =  math::bigdecimal::ZERO;
+    math::bigdecimal bi{1000};
+    TS_ASSERT_THROWS(bi/ZERO, std::domain_error);
+  }
+  
+  void testDivideByOneReturnsValue() {
+    auto &ONE =  math::bigdecimal::ONE;
+    math::bigdecimal bi{1000};
+    TS_ASSERT_EQUALS(bi/ONE, bi);
+  }
+
+  void testDivideByNegativeOneReturnsInverse() {
+    auto &NEGATIVE_ONE =  math::bigdecimal::NEGATIVE_ONE;
+    math::bigdecimal bi{1000};
+    TS_ASSERT_EQUALS((bi/NEGATIVE_ONE).to_string(), "-1000");
+  }
+
+  void testDivisionWithMultipleMultSubSteps() {
+    math::bigdecimal bi{1112};
+    TS_ASSERT_EQUALS((bi/2).to_string(), "556");
+  }
+  
+  void testDivisionDroppingNumberOfCells() {
+    math::bigdecimal bi{1000000000ULL};
+    TS_ASSERT_EQUALS((bi/2).to_string(), "500000000");
+  }
+  
+  void testDivisionByBiggerNumberIsZero() {
+    math::bigdecimal bi{1000ULL};
+    TS_ASSERT_EQUALS((bi/1001).to_string(), "0");
+  }
+  
+  void testDivisionWithLargeNumbers() {
+    math::bigdecimal big{"999999998000000001"};
+    TS_ASSERT_EQUALS(big.to_string(), "999999998000000001");
+    TS_ASSERT_EQUALS((big/999999999ULL).to_string(),
+                     "999999999");
+  }
+//  
+//  void testModuloZeroThrows() {
+//    math::bigdecimal bi{1000};
+//    TS_ASSERT_THROWS(bi%0, std::domain_error);
+//  }
+//  
+//  void testModuloBiggerIsSameValue() {
+//    math::bigdecimal bi{1000};
+//    TS_ASSERT_EQUALS(bi%2000, bi);
+//  }
+//  
+//  void testModuloSameNumberIsZero() {
+//    math::bigdecimal bi{1000};
+//    TS_ASSERT_EQUALS(bi%1000, 0);
+//  }
+//
+//  void testModuloDivisorIsZero() {
+//    math::bigdecimal bi{1000};
+//    TS_ASSERT_EQUALS(bi%100, 0);
+//  }
+//
+//  void testModuloDiffSignIsInverseElement() {
+//    math::bigdecimal bi{1000};
+//    math::bigdecimal mod{13};
+//    TS_ASSERT_EQUALS((bi%mod)+((-bi)%mod), mod);
+//  }
+//
+//  void testModuloNegativesIsNegative() {
+//    math::bigdecimal bi{1000};
+//    math::bigdecimal mod{13};
+//    TS_ASSERT_EQUALS((bi%mod), -((-bi)%(-mod)));
+//  }
+};