Browse Source

Fixing test printers.

Sam Jaffe 7 năm trước cách đây
mục cha
commit
28db841237

+ 6 - 0
bigdecimal.xcodeproj/project.pbxproj

@@ -18,6 +18,7 @@
 		CD47698C20B03C5E009AA8BB /* biginteger_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD47697520AFA6B2009AA8BB /* biginteger_test.cpp */; };
 		CD5FB2911F06EFEF005A0D61 /* biginteger.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CD5FB2801F06EF7D005A0D61 /* biginteger.cpp */; };
 		CD5FB2921F06EFF2005A0D61 /* biginteger.h in Headers */ = {isa = PBXBuildFile; fileRef = CD5FB27F1F06EF70005A0D61 /* biginteger.h */; settings = {ATTRIBUTES = (Public, ); }; };
+		CDB7F52F20B19E0D0053645C /* bignumber_test_printers.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDB7F52E20B19E0D0053645C /* bignumber_test_printers.cpp */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -79,6 +80,8 @@
 		CD5FB27F1F06EF70005A0D61 /* biginteger.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = biginteger.h; sourceTree = "<group>"; };
 		CD5FB2801F06EF7D005A0D61 /* biginteger.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = biginteger.cpp; sourceTree = "<group>"; };
 		CD5FB2861F06EFEA005A0D61 /* libbigdecimal.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = libbigdecimal.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
+		CDB7F52D20B19D3F0053645C /* bignumber_test_printers.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = bignumber_test_printers.h; sourceTree = "<group>"; };
+		CDB7F52E20B19E0D0053645C /* bignumber_test_printers.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = bignumber_test_printers.cpp; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -165,6 +168,8 @@
 			isa = PBXGroup;
 			children = (
 				CD47697520AFA6B2009AA8BB /* biginteger_test.cpp */,
+				CDB7F52D20B19D3F0053645C /* bignumber_test_printers.h */,
+				CDB7F52E20B19E0D0053645C /* bignumber_test_printers.cpp */,
 				CD3B82A31F114E1C0081E9FC /* bignumber_integral_test.cpp */,
 				CD14CA711F0DA9FC0091A168 /* bigdecimal_test.cpp */,
 			);
@@ -320,6 +325,7 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CDB7F52F20B19E0D0053645C /* bignumber_test_printers.cpp in Sources */,
 				CD2EC1BF1F0AF3B800D49DF5 /* bigdecimal.cpp in Sources */,
 				CD5FB2911F06EFEF005A0D61 /* biginteger.cpp in Sources */,
 				CD2EC1C51F0BCCBF00D49DF5 /* bignum_helper.cpp in Sources */,

+ 9 - 20
test/bigdecimal_test.cpp

@@ -8,6 +8,7 @@
 #include <gmock/gmock.h>
 
 #include "bigdecimal.h"
+#include "bignumber_test_printers.h"
 
 TEST(BigDecimalTest, ConstructIntegerAsDecimal) {
   EXPECT_THAT(math::bigdecimal(1000, 2).to_string(), "1000.00");
@@ -87,23 +88,6 @@ TEST(BigDecimalTest, SubtractionNormalizeScaleToHighest) {
   EXPECT_THAT((b-a).scale(), a.scale());
 }
 
-struct ArithTuple { math::bigdecimal lhs, rhs; std::string expected; };
-struct BigDecPair { math::bigdecimal lhs, rhs; };
-void PrintTo(math::bigdecimal const & dec, std::ostream * out) {
-  (*out) << dec.to_string() << "(" << dec.scale() << ")";
-}
-std::ostream & operator<<(std::ostream & out, math::bigdecimal const & dec) {
-  PrintTo(dec, &out);
-  return out;
-}
-void PrintTo(BigDecPair const & tup, std::ostream * out) {
-  (*out) << "{ lhs = ";
-  PrintTo(tup.lhs, out);
-  (*out) << ", rhs = ";
-  PrintTo(tup.rhs, out);
-  (*out) << " }";
-}
-
 class MultiplicationScaleTest : public testing::TestWithParam<ArithTuple> {};
 
 TEST_P(MultiplicationScaleTest, ScaleIsSumOfOperAndMultipScales) {
@@ -162,7 +146,8 @@ INSTANTIATE_TEST_CASE_P(BigDecimal, DivisionScaleTest,
                                         ArithTuple{{1, 5}, {1,  5}, "1"},
                                         ArithTuple{{1, 5}, {1000000000, -9}, "0.00000000100000"},
                                         ArithTuple{{100, -2}, {1000000000, -9}, "0.0000001"},
-                                        ArithTuple{{10000, -4}, {100000, -5}, "0.1"}));
+                                        ArithTuple{{10000, -4}, {100000, -5}, "0.1"},
+                                        ArithTuple{{"11.0"}, {"1.10"}, "10"}));
 
 INSTANTIATE_TEST_CASE_P(BigDecimal, EqTest,
                         testing::Values(BigDecPair{{  0}, {  0,  4}},
@@ -171,8 +156,12 @@ INSTANTIATE_TEST_CASE_P(BigDecimal, EqTest,
                                         BigDecPair{{"0.1", 1}, {"0.10", 2}}));
 
 INSTANTIATE_TEST_CASE_P(BigDecimal, DecimalLtTest,
-                        testing::Values(BigDecPair{{ 0, 0}, { 1, 4}} ,
+                        testing::Values(BigDecPair{{ 0, 0}, { 1, 4}},
                                         BigDecPair{{ 0, 4}, { 1, 0}},
-                                        BigDecPair{{ 0, 4}, { 1, 4}}));
+                                        BigDecPair{{ 0, 4}, { 1, 4}},
+                                        BigDecPair{{-1, 4}, { 1, 4}},
+                                        BigDecPair{{-2, 0}, {-1, 4}},
+                                        BigDecPair{{-2, 4}, {-1, 0}},
+                                        BigDecPair{{-2, 4}, {-1, 4}}));
 
 #pragma clang diagnostic pop

+ 1 - 3
test/biginteger_test.cpp

@@ -5,6 +5,7 @@
 //  Created by Sam Jaffe on 5/18/18.
 //
 
+#include "bignumber_test_printers.h"
 #include <gmock/gmock.h>
 
 #include "biginteger.h"
@@ -41,9 +42,6 @@ TEST(BigIntegerTest, ModuloNegativesIsNegative) {
   EXPECT_THAT((bi%mod), -((-bi)%(-mod)));
 }
 
-
-using BigIntPair = std::tuple<math::biginteger, math::biginteger>;
-
 class BigIntModuloTest : public testing::TestWithParam<BigIntPair> {};
 
 TEST_P(BigIntModuloTest, IsZero) {

+ 1 - 0
test/bignumber_integral_test.cpp

@@ -9,6 +9,7 @@
 
 #include "biginteger.h"
 #include "bigdecimal.h"
+#include "bignumber_test_printers.h"
 
 template <typename T>
 class BigIntLikeTest : public testing::Test {

+ 42 - 0
test/bignumber_test_printers.cpp

@@ -0,0 +1,42 @@
+//
+//  bignumber_test_printers.cpp
+//  bigdecimal
+//
+//  Created by Sam Jaffe on 5/20/18.
+//
+
+#include "bignumber_test_printers.h"
+
+#include <iostream>
+
+void math::PrintTo(math::bigdecimal const & dec, std::ostream * out) {
+  (*out) << dec.to_string() << "(" << dec.scale() << ")";
+}
+
+void math::PrintTo(math::biginteger const & dec, std::ostream * out) {
+  (*out) << dec.to_string();
+}
+
+void PrintTo(BigDecPair const & tup, std::ostream * out) {
+  (*out) << "{ lhs = ";
+  math::PrintTo(tup.lhs, out);
+  (*out) << ", rhs = ";
+  math::PrintTo(tup.rhs, out);
+  (*out) << " }";
+}
+
+void std::PrintTo(BigIntPair const & tup, std::ostream * out) {
+  (*out) << "{ lhs = ";
+  math::PrintTo(std::get<0>(tup), out);
+  (*out) << ", rhs = ";
+  math::PrintTo(std::get<1>(tup), out);
+  (*out) << " }";
+}
+
+void PrintTo(ArithTuple const & tup, std::ostream * out) {
+  (*out) << "{ lhs = ";
+  math::PrintTo(tup.lhs, out);
+  (*out) << ", rhs = ";
+  math::PrintTo(tup.rhs, out);
+  (*out) << " } -> " << tup.expected;
+}

+ 29 - 0
test/bignumber_test_printers.h

@@ -0,0 +1,29 @@
+//
+//  bignumber_test_printers.h
+//  bigdecimal
+//
+//  Created by Sam Jaffe on 5/20/18.
+//
+
+#pragma once
+
+#include "biginteger.h"
+#include "bigdecimal.h"
+
+#include <string>
+#include <iosfwd>
+#include <tuple>
+
+struct ArithTuple { math::bigdecimal lhs, rhs; std::string expected; };
+struct BigDecPair { math::bigdecimal lhs, rhs; };
+using BigIntPair = std::tuple<math::biginteger, math::biginteger>;
+
+namespace math {
+  void PrintTo(math::bigdecimal const & dec, std::ostream * out);
+  void PrintTo(math::biginteger const & dec, std::ostream * out);
+}
+void PrintTo(BigDecPair const & tup, std::ostream * out);
+void PrintTo(ArithTuple const & tup, std::ostream * out);
+namespace std {
+  void PrintTo(BigIntPair const & tup, std::ostream * out);
+}