quickstep-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hakanmemiso...@apache.org
Subject incubator-quickstep git commit: First part of the unit test.
Date Fri, 17 Jun 2016 21:06:40 GMT
Repository: incubator-quickstep
Updated Branches:
  refs/heads/decimal-type 18f526050 -> 5815103db


First part of the unit test.


Project: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/commit/5815103d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/tree/5815103d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/diff/5815103d

Branch: refs/heads/decimal-type
Commit: 5815103db00e978c156881d1f139244a28fa932d
Parents: 18f5260
Author: Hakan Memisoglu <hakanmemisoglu@apache.org>
Authored: Fri Jun 17 16:06:23 2016 -0500
Committer: Hakan Memisoglu <hakanmemisoglu@apache.org>
Committed: Fri Jun 17 16:06:23 2016 -0500

----------------------------------------------------------------------
 types/CMakeLists.txt                 | 11 ++++++
 types/DecimalType.cpp                |  4 +-
 types/tests/DecimalType_unittest.cpp | 64 +++++++++++++++++++++++++++++++
 3 files changed, 78 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/5815103d/types/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/types/CMakeLists.txt b/types/CMakeLists.txt
index 9522ed6..16a6f1e 100644
--- a/types/CMakeLists.txt
+++ b/types/CMakeLists.txt
@@ -311,6 +311,17 @@ target_link_libraries(DatetimeType_unittest
                       quickstep_utility_MemStream)
 add_test(DatetimeType_unittest DatetimeType_unittest)
 
+add_executable(DecimalType_unittest "${CMAKE_CURRENT_SOURCE_DIR}/tests/DecimalType_unittest.cpp")
+target_link_libraries(DecimalType_unittest
+                      gtest
+                      gtest_main
+                      quickstep_types_DecimalLit
+                      quickstep_types_DecimalType
+                      quickstep_types_Type
+                      quickstep_types_TypeFactory
+                      quickstep_types_TypedValue)
+add_test(DecimalType_unittest DecimalType_unittest)
+
 add_executable(DoubleType_unittest "${CMAKE_CURRENT_SOURCE_DIR}/tests/DoubleType_unittest.cpp")
 target_link_libraries(DoubleType_unittest
                       glog

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/5815103d/types/DecimalType.cpp
----------------------------------------------------------------------
diff --git a/types/DecimalType.cpp b/types/DecimalType.cpp
index 29c441a..31ca76d 100644
--- a/types/DecimalType.cpp
+++ b/types/DecimalType.cpp
@@ -19,6 +19,7 @@
 
 #include <cstdint>
 #include <cstdio>
+#include <iomanip>
 #include <string>
 #include <sstream>
 
@@ -74,7 +75,8 @@ std::string DecimalType::printValueToString(const TypedValue &value)
const {
   DecimalLit decimal = value.getLiteral<DecimalLit>();
   std::stringstream ss;
   ss << decimal.getIntegerPart() << "."
-     << decimal.getFractionalPart();
+     << std::setfill('0') << std::setw(DecimalLit::kPrecisionWidth)
+     <<  decimal.getFractionalPart();
   return ss.str();
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/5815103d/types/tests/DecimalType_unittest.cpp
----------------------------------------------------------------------
diff --git a/types/tests/DecimalType_unittest.cpp b/types/tests/DecimalType_unittest.cpp
new file mode 100644
index 0000000..052848b
--- /dev/null
+++ b/types/tests/DecimalType_unittest.cpp
@@ -0,0 +1,64 @@
+#include "types/DecimalLit.hpp"
+#include "types/DecimalType.hpp"
+#include "types/Type.hpp"
+#include "types/TypeFactory.hpp"
+#include "types/TypedValue.hpp"
+
+#include "gtest/gtest.h"
+
+namespace quickstep {
+
+TEST(DecimalTypeTest, PrintValueTest) {
+  const Type &decimal_type = TypeFactory::GetType(kDecimal);
+
+  // Try integer version.
+  DecimalLit integer_decimal = DecimalLit::fromInt(36509);
+  TypedValue value_for_integer_decimal(integer_decimal);
+  EXPECT_EQ(std::string("36509.00"),
+            decimal_type.printValueToString(value_for_integer_decimal));
+
+  // Try double version.
+  DecimalLit double_decimal = DecimalLit::fromDouble(36509.65);
+  TypedValue value_for_double_decimal(double_decimal);
+  EXPECT_EQ(std::string("36509.65"),
+            decimal_type.printValueToString(value_for_double_decimal));
+
+  // Try truncation of double version.
+  DecimalLit double_decimal_truncated = DecimalLit::fromDouble(36509.6526762);
+  TypedValue value_for_double_decimal_truncated(double_decimal_truncated);
+  EXPECT_EQ(std::string("36509.65"),
+            decimal_type.printValueToString(
+                value_for_double_decimal_truncated));
+
+  // Test that number is truncated, not rounded.
+  double_decimal_truncated = DecimalLit::fromDouble(36509.6599999);
+  TypedValue value_for_double_decimal_truncated_other(double_decimal_truncated);
+  EXPECT_EQ(std::string("36509.65"),
+            decimal_type.printValueToString(
+                value_for_double_decimal_truncated));
+}
+
+TEST(DecimalTypeTest, DecimalLitOperationsTest) {
+  const Type &decimal_type = TypeFactory::GetType(kDecimal);
+
+  DecimalLit decimal_a = DecimalLit::fromDouble(560.35);
+  DecimalLit decimal_b = DecimalLit::fromDouble(439.65);
+
+  EXPECT_EQ(std::string("1000.00"),
+            decimal_type.printValueToString(TypedValue(decimal_a + decimal_b)));
+
+  EXPECT_EQ(std::string("120.70"),
+            decimal_type.printValueToString(TypedValue(decimal_a - decimal_b)));
+
+  EXPECT_EQ(std::string("246357.87"),
+            decimal_type.printValueToString(TypedValue(decimal_a * decimal_b)));
+
+  EXPECT_EQ(std::string("1.27"),
+            decimal_type.printValueToString(TypedValue(decimal_a / decimal_b)));
+
+  EXPECT_EQ(std::string("120.70"),
+            decimal_type.printValueToString(TypedValue(decimal_a % decimal_b)));
+}
+
+
+}


Mime
View raw message