Skip to content

Commit

Permalink
Tests fixed
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Chigarev <dmitry.chigarev@intel.com>
  • Loading branch information
dchigarev committed Feb 8, 2021
1 parent ba94f9c commit 09f7b1c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cpp/src/arrow/array/array_decimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ class ARROW_EXPORT BaseDecimalArray : public FixedSizeBinaryArray {
};

// Backward compatibility
using DecimalArray = Decimal128Array;
using DecimalArray = BaseDecimalArray<128>;

} // namespace arrow
7 changes: 5 additions & 2 deletions cpp/src/arrow/util/basic_decimal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1186,8 +1186,11 @@ DecimalStatus BasicDecimalAnyWidth<width>::Divide(const BasicDecimalAnyWidth& di
bool dividen_was_negative = Sign() == -1;
bool divisor_was_negative = divisor.Sign() == -1;

*result = value / divisor.value;
*remainder = value % divisor.value;
BasicDecimalAnyWidth<width> dividen_abs = BasicDecimalAnyWidth<width>::Abs(*this);
BasicDecimalAnyWidth<width> divisor_abs = BasicDecimalAnyWidth<width>::Abs(divisor);

*result = dividen_abs.value / divisor_abs.value;
*remainder = dividen_abs.value % divisor_abs.value;

FixDivisionSigns(result, remainder, dividen_was_negative, divisor_was_negative);
return DecimalStatus::kSuccess;
Expand Down
20 changes: 16 additions & 4 deletions cpp/src/arrow/util/basic_decimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ ARROW_EXPORT BasicDecimal256 operator/(const BasicDecimal256& left,
template <uint32_t width>
class ARROW_EXPORT BasicDecimalAnyWidth {
public:
static constexpr int bit_width = width;
using ValueType = typename IntTypes<width>::signed_type;
/// \brief Empty constructor creates a BasicDecimal with a value of 0.
constexpr BasicDecimalAnyWidth() noexcept : value(0) {}
Expand All @@ -354,16 +355,16 @@ class ARROW_EXPORT BasicDecimalAnyWidth {
constexpr BasicDecimalAnyWidth(T value) noexcept
: value(static_cast<ValueType>(value)) {}

/// \brief Create a BasicDecimal from an array of bytes. Bytes are assumed to be in
/// native-endian byte order.
explicit BasicDecimalAnyWidth(const uint8_t* bytes);

/// \brief Upcast BasicDecimal with less widths
template <uint32_t _width,
typename = typename std::enable_if<_width <= width, void>::type>
constexpr BasicDecimalAnyWidth(const BasicDecimalAnyWidth<_width>& other) noexcept
: value(static_cast<ValueType>(other.Value())) {}

/// \brief Create a BasicDecimal from an array of bytes. Bytes are assumed to be in
/// native-endian byte order.
explicit BasicDecimalAnyWidth(const uint8_t* bytes);

/// \brief Negate the current value (in-place)
BasicDecimalAnyWidth& Negate();

Expand All @@ -373,6 +374,17 @@ class ARROW_EXPORT BasicDecimalAnyWidth {
/// \brief Absolute value
static BasicDecimalAnyWidth Abs(const BasicDecimalAnyWidth& left);

/// Divide this number by right and return the result.
///
/// This operation is not destructive.
/// The answer rounds to zero. Signs work like:
/// 21 / 5 -> 4, 1
/// -21 / 5 -> -4, -1
/// 21 / -5 -> -4, 1
/// -21 / -5 -> 4, -1
/// \param[in] divisor the number to divide by
/// \param[out] result the quotient
/// \param[out] remainder the remainder after the division
DecimalStatus Divide(const BasicDecimalAnyWidth& divisor, BasicDecimalAnyWidth* result,
BasicDecimalAnyWidth* remainder) const;

Expand Down
14 changes: 7 additions & 7 deletions cpp/src/arrow/util/decimal_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1729,20 +1729,20 @@ const std::vector<std::pair<std::string, std::function<T(T, T)>>>
TYPED_TEST(DecimalAnyWidthTest, BinaryOperations) {
using ValueType = typename arrow::DecimalAnyWidthTest_BinaryOperations_Test<
gtest_TypeParam_>::TypeParam::ValueType;
using ArrowValueType = typename arrow::CTypeTraits<ValueType>::ArrowType;

auto DecimalFns = DecimalAnyWidthBinaryParams<TypeParam>::value;
auto NumericFns = DecimalAnyWidthBinaryParams<int64_t>::value;

for (size_t i = 0; i < DecimalFns.size(); i++) {
for (auto x : GetRandomNumbers<ArrowValueType>(8)) {
for (auto y : GetRandomNumbers<ArrowValueType>(8)) {
for (ValueType x : GetRandomNumbers<Int16Type>(8)) {
for (ValueType y : GetRandomNumbers<Int16Type>(8)) {
TypeParam d1(x), d2(y);
auto result = DecimalFns[i].second(d1, d2);
auto reference = static_cast<ValueType>(NumericFns[i].second(x, y));
TypeParam result = DecimalFns[i].second(d1, d2);
ValueType reference = static_cast<ValueType>(NumericFns[i].second(x, y));
ASSERT_EQ(reference, result)
<< d1 << " " << DecimalFns[i].first << " " << d2 << " "
<< " != " << result;
<< "(" << x << " " << DecimalFns[i].first << " " << y << " = " << reference
<< ") != (" << d1 << " " << DecimalFns[i].first << " " << d2 << " = "
<< result << ")";
}
}
}
Expand Down

0 comments on commit 09f7b1c

Please sign in to comment.