zeroshade commented on code in PR #43957:
URL: https://github.com/apache/arrow/pull/43957#discussion_r1767127900


##########
cpp/src/arrow/util/decimal_test.cc:
##########
@@ -1389,6 +1435,838 @@ std::vector<CType> GetRandomNumbers(int32_t size) {
   return ret;
 }
 
+TEST(Decimal32Test, Multiply) {
+  ASSERT_EQ(Decimal32(60501), Decimal32(301) * Decimal32(201));
+
+  ASSERT_EQ(Decimal32(-60501), Decimal32(-301) * Decimal32(201));
+
+  ASSERT_EQ(Decimal32(-60501), Decimal32(301) * Decimal32(-201));
+
+  ASSERT_EQ(Decimal32(60501), Decimal32(-301) * Decimal32(-201));
+
+  // Test some random numbers.
+  for (auto x : GetRandomNumbers<Int32Type>(16)) {
+    for (auto y : GetRandomNumbers<Int32Type>(16)) {
+      Decimal32 result = Decimal32(x) * Decimal32(y);
+      ASSERT_EQ(Decimal32(static_cast<int64_t>(x) * y), result)
+          << " x: " << x << " y: " << y;
+    }
+  }
+}
+
+TEST(Decimal32Test, Divide) {
+  ASSERT_EQ(Decimal32(66), Decimal32(20100) / Decimal32(301));
+
+  ASSERT_EQ(Decimal32(-66), Decimal32(-20100) / Decimal32(301));
+
+  ASSERT_EQ(Decimal32(-66), Decimal32(20100) / Decimal32(-301));
+
+  ASSERT_EQ(Decimal32(66), Decimal32(-20100) / Decimal32(-301));
+
+  // Test some random numbers.
+  for (auto x : GetRandomNumbers<Int32Type>(16)) {
+    for (auto y : GetRandomNumbers<Int32Type>(16)) {
+      if (y == 0) {
+        continue;
+      }
+
+      Decimal32 result = Decimal32(x) / Decimal32(y);
+      ASSERT_EQ(Decimal32(static_cast<int64_t>(x) / y), result)
+          << " x: " << x << " y: " << y;
+    }
+  }
+}
+
+TEST(Decimal32Test, Rescale) {
+  ASSERT_OK_AND_EQ(Decimal32(11100), Decimal32(111).Rescale(0, 2));
+  ASSERT_OK_AND_EQ(Decimal32(111), Decimal32(11100).Rescale(2, 0));
+  ASSERT_OK_AND_EQ(Decimal32(5), Decimal32(500000).Rescale(6, 1));
+  ASSERT_OK_AND_EQ(Decimal32(500000), Decimal32(5).Rescale(1, 6));
+  ASSERT_RAISES(Invalid, Decimal32(555555).Rescale(6, 1));
+
+  // Test some random numbers.
+  for (auto original_scale : GetRandomNumbers<Int8Type>(16)) {
+    for (auto value : GetRandomNumbers<Int16Type>(16)) {
+      Decimal32 unscaled_value = Decimal32(value);
+      Decimal32 scaled_value = unscaled_value;
+      for (int32_t new_scale = original_scale; new_scale < original_scale + 4;
+           new_scale++, scaled_value *= Decimal32(10)) {
+        ASSERT_OK_AND_EQ(scaled_value, unscaled_value.Rescale(original_scale, 
new_scale));
+        ASSERT_OK_AND_EQ(unscaled_value, scaled_value.Rescale(new_scale, 
original_scale));
+      }
+    }
+  }
+
+  for (auto original_scale : GetRandomNumbers<Int16Type>(16)) {
+    Decimal32 value(1);
+    for (int32_t new_scale = original_scale; new_scale < original_scale + 8;
+         new_scale++, value *= Decimal32(10)) {
+      Decimal32 negative_value = value * -1;
+      ASSERT_OK_AND_EQ(value, Decimal32(1).Rescale(original_scale, new_scale));
+      ASSERT_OK_AND_EQ(negative_value, Decimal32(-1).Rescale(original_scale, 
new_scale));
+      ASSERT_OK_AND_EQ(Decimal32(1), value.Rescale(new_scale, original_scale));
+      ASSERT_OK_AND_EQ(Decimal32(-1), negative_value.Rescale(new_scale, 
original_scale));
+    }
+  }
+}
+
+TEST(Decimal32Test, Mod) {
+  ASSERT_EQ(Decimal32(234), Decimal32(20100) % Decimal32(301));
+
+  ASSERT_EQ(Decimal32(-234), Decimal32(-20100) % Decimal32(301));
+
+  ASSERT_EQ(Decimal32(234), Decimal32(20100) % Decimal32(-301));
+
+  ASSERT_EQ(Decimal32(-234), Decimal32(-20100) % Decimal32(-301));
+
+  // Test some random numbers.
+  for (auto x : GetRandomNumbers<Int32Type>(16)) {
+    for (auto y : GetRandomNumbers<Int32Type>(16)) {
+      if (y == 0) {
+        continue;
+      }
+
+      Decimal32 result = Decimal32(x) % Decimal32(y);
+      ASSERT_EQ(Decimal32(static_cast<int64_t>(x) % y), result)
+          << " x: " << x << " y: " << y;
+    }
+  }
+}
+
+TEST(Decimal32Test, Sign) {
+  ASSERT_EQ(1, Decimal32(999999).Sign());
+  ASSERT_EQ(-1, Decimal32(-999999).Sign());
+  ASSERT_EQ(1, Decimal32(0).Sign());
+}
+
+TEST(Decimal32Test, GetWholeAndFraction) {
+  Decimal32 value("123456");
+  Decimal32 whole;
+  Decimal32 fraction;
+  int32_t out;
+
+  value.GetWholeAndFraction(0, &whole, &fraction);
+  ASSERT_OK(whole.ToInteger(&out));
+  ASSERT_EQ(123456, out);
+  ASSERT_OK(fraction.ToInteger(&out));
+  ASSERT_EQ(0, out);

Review Comment:
   added a helper via lambda like in other tests



##########
cpp/src/arrow/util/decimal_test.cc:
##########
@@ -1389,6 +1435,838 @@ std::vector<CType> GetRandomNumbers(int32_t size) {
   return ret;
 }
 
+TEST(Decimal32Test, Multiply) {
+  ASSERT_EQ(Decimal32(60501), Decimal32(301) * Decimal32(201));
+
+  ASSERT_EQ(Decimal32(-60501), Decimal32(-301) * Decimal32(201));
+
+  ASSERT_EQ(Decimal32(-60501), Decimal32(301) * Decimal32(-201));
+
+  ASSERT_EQ(Decimal32(60501), Decimal32(-301) * Decimal32(-201));
+
+  // Test some random numbers.
+  for (auto x : GetRandomNumbers<Int32Type>(16)) {
+    for (auto y : GetRandomNumbers<Int32Type>(16)) {
+      Decimal32 result = Decimal32(x) * Decimal32(y);
+      ASSERT_EQ(Decimal32(static_cast<int64_t>(x) * y), result)
+          << " x: " << x << " y: " << y;
+    }
+  }
+}
+
+TEST(Decimal32Test, Divide) {
+  ASSERT_EQ(Decimal32(66), Decimal32(20100) / Decimal32(301));
+
+  ASSERT_EQ(Decimal32(-66), Decimal32(-20100) / Decimal32(301));
+
+  ASSERT_EQ(Decimal32(-66), Decimal32(20100) / Decimal32(-301));
+
+  ASSERT_EQ(Decimal32(66), Decimal32(-20100) / Decimal32(-301));
+
+  // Test some random numbers.
+  for (auto x : GetRandomNumbers<Int32Type>(16)) {
+    for (auto y : GetRandomNumbers<Int32Type>(16)) {
+      if (y == 0) {
+        continue;
+      }
+
+      Decimal32 result = Decimal32(x) / Decimal32(y);
+      ASSERT_EQ(Decimal32(static_cast<int64_t>(x) / y), result)
+          << " x: " << x << " y: " << y;
+    }
+  }
+}
+
+TEST(Decimal32Test, Rescale) {
+  ASSERT_OK_AND_EQ(Decimal32(11100), Decimal32(111).Rescale(0, 2));
+  ASSERT_OK_AND_EQ(Decimal32(111), Decimal32(11100).Rescale(2, 0));
+  ASSERT_OK_AND_EQ(Decimal32(5), Decimal32(500000).Rescale(6, 1));
+  ASSERT_OK_AND_EQ(Decimal32(500000), Decimal32(5).Rescale(1, 6));
+  ASSERT_RAISES(Invalid, Decimal32(555555).Rescale(6, 1));
+
+  // Test some random numbers.
+  for (auto original_scale : GetRandomNumbers<Int8Type>(16)) {
+    for (auto value : GetRandomNumbers<Int16Type>(16)) {
+      Decimal32 unscaled_value = Decimal32(value);
+      Decimal32 scaled_value = unscaled_value;
+      for (int32_t new_scale = original_scale; new_scale < original_scale + 4;
+           new_scale++, scaled_value *= Decimal32(10)) {
+        ASSERT_OK_AND_EQ(scaled_value, unscaled_value.Rescale(original_scale, 
new_scale));
+        ASSERT_OK_AND_EQ(unscaled_value, scaled_value.Rescale(new_scale, 
original_scale));
+      }
+    }
+  }
+
+  for (auto original_scale : GetRandomNumbers<Int16Type>(16)) {
+    Decimal32 value(1);
+    for (int32_t new_scale = original_scale; new_scale < original_scale + 8;
+         new_scale++, value *= Decimal32(10)) {
+      Decimal32 negative_value = value * -1;
+      ASSERT_OK_AND_EQ(value, Decimal32(1).Rescale(original_scale, new_scale));
+      ASSERT_OK_AND_EQ(negative_value, Decimal32(-1).Rescale(original_scale, 
new_scale));
+      ASSERT_OK_AND_EQ(Decimal32(1), value.Rescale(new_scale, original_scale));
+      ASSERT_OK_AND_EQ(Decimal32(-1), negative_value.Rescale(new_scale, 
original_scale));
+    }
+  }
+}
+
+TEST(Decimal32Test, Mod) {
+  ASSERT_EQ(Decimal32(234), Decimal32(20100) % Decimal32(301));
+
+  ASSERT_EQ(Decimal32(-234), Decimal32(-20100) % Decimal32(301));
+
+  ASSERT_EQ(Decimal32(234), Decimal32(20100) % Decimal32(-301));
+
+  ASSERT_EQ(Decimal32(-234), Decimal32(-20100) % Decimal32(-301));
+
+  // Test some random numbers.
+  for (auto x : GetRandomNumbers<Int32Type>(16)) {
+    for (auto y : GetRandomNumbers<Int32Type>(16)) {
+      if (y == 0) {
+        continue;
+      }
+
+      Decimal32 result = Decimal32(x) % Decimal32(y);
+      ASSERT_EQ(Decimal32(static_cast<int64_t>(x) % y), result)
+          << " x: " << x << " y: " << y;
+    }
+  }
+}
+
+TEST(Decimal32Test, Sign) {
+  ASSERT_EQ(1, Decimal32(999999).Sign());
+  ASSERT_EQ(-1, Decimal32(-999999).Sign());
+  ASSERT_EQ(1, Decimal32(0).Sign());
+}
+
+TEST(Decimal32Test, GetWholeAndFraction) {
+  Decimal32 value("123456");
+  Decimal32 whole;
+  Decimal32 fraction;
+  int32_t out;
+
+  value.GetWholeAndFraction(0, &whole, &fraction);
+  ASSERT_OK(whole.ToInteger(&out));
+  ASSERT_EQ(123456, out);
+  ASSERT_OK(fraction.ToInteger(&out));
+  ASSERT_EQ(0, out);
+
+  value.GetWholeAndFraction(1, &whole, &fraction);
+  ASSERT_OK(whole.ToInteger(&out));
+  ASSERT_EQ(12345, out);
+  ASSERT_OK(fraction.ToInteger(&out));
+  ASSERT_EQ(6, out);
+
+  value.GetWholeAndFraction(5, &whole, &fraction);
+  ASSERT_OK(whole.ToInteger(&out));
+  ASSERT_EQ(1, out);
+  ASSERT_OK(fraction.ToInteger(&out));
+  ASSERT_EQ(23456, out);
+
+  value.GetWholeAndFraction(7, &whole, &fraction);
+  ASSERT_OK(whole.ToInteger(&out));
+  ASSERT_EQ(0, out);
+  ASSERT_OK(fraction.ToInteger(&out));
+  ASSERT_EQ(123456, out);
+}
+
+TEST(Decimal32Test, GetWholeAndFractionNegative) {
+  Decimal32 value("-123456");
+  Decimal32 whole;
+  Decimal32 fraction;
+  int32_t out;
+
+  value.GetWholeAndFraction(0, &whole, &fraction);
+  ASSERT_OK(whole.ToInteger(&out));
+  ASSERT_EQ(-123456, out);
+  ASSERT_OK(fraction.ToInteger(&out));
+  ASSERT_EQ(0, out);
+
+  value.GetWholeAndFraction(1, &whole, &fraction);
+  ASSERT_OK(whole.ToInteger(&out));
+  ASSERT_EQ(-12345, out);
+  ASSERT_OK(fraction.ToInteger(&out));
+  ASSERT_EQ(-6, out);
+
+  value.GetWholeAndFraction(5, &whole, &fraction);
+  ASSERT_OK(whole.ToInteger(&out));
+  ASSERT_EQ(-1, out);
+  ASSERT_OK(fraction.ToInteger(&out));
+  ASSERT_EQ(-23456, out);
+
+  value.GetWholeAndFraction(7, &whole, &fraction);
+  ASSERT_OK(whole.ToInteger(&out));
+  ASSERT_EQ(0, out);
+  ASSERT_OK(fraction.ToInteger(&out));
+  ASSERT_EQ(-123456, out);
+}
+
+TEST(Decimal32Test, IncreaseScale) {
+  Decimal32 result;
+  int32_t out;
+
+  result = Decimal32("1234").IncreaseScaleBy(0);

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to