pitrou commented on a change in pull request #8269:
URL: https://github.com/apache/arrow/pull/8269#discussion_r496686115



##########
File path: cpp/src/arrow/compute/kernels/aggregate_test.cc
##########
@@ -914,5 +914,127 @@ TEST_F(TestInt32ModeKernel, LargeValueRange) {
   CheckModeWithRange<ArrowType>(-10000000, 10000000);
 }
 
+//
+// Var/Std
+//
+
+template <typename ArrowType>
+class TestPrimitiveVarStdKernel : public ::testing::Test {
+ public:
+  using Traits = TypeTraits<ArrowType>;
+  using ScalarType = typename TypeTraits<DoubleType>::ScalarType;
+
+  void AssertVarStdIs(const Datum& array, const VarStdOptions& options,
+                      double expected_var, double diff = 0) {
+    ASSERT_OK_AND_ASSIGN(Datum out_var, Var(array, options));
+    ASSERT_OK_AND_ASSIGN(Datum out_std, Std(array, options));
+    auto var = checked_cast<const ScalarType*>(out_var.scalar().get());
+    auto std = checked_cast<const ScalarType*>(out_std.scalar().get());
+    ASSERT_TRUE(var->is_valid && std->is_valid);
+    ASSERT_DOUBLE_EQ(std->value * std->value, var->value);
+    if (diff == 0) {
+      ASSERT_DOUBLE_EQ(var->value, expected_var);  // < 4ULP
+    } else {
+      ASSERT_NEAR(var->value, expected_var, diff);
+    }
+  }
+
+  void AssertVarStdIs(const std::string& json, const VarStdOptions& options,
+                      double expected_var) {
+    auto array = ArrayFromJSON(type_singleton(), json);
+    AssertVarStdIs(array, options, expected_var);
+  }
+
+  void AssertVarStdIs(const std::vector<std::string>& json, const 
VarStdOptions& options,
+                      double expected_var) {
+    auto chunked = ChunkedArrayFromJSON(type_singleton(), json);
+    AssertVarStdIs(chunked, options, expected_var);
+  }
+
+  void AssertVarStdIsInvalid(const Datum& array, const VarStdOptions& options) 
{
+    ASSERT_OK_AND_ASSIGN(Datum out_var, Var(array, options));
+    ASSERT_OK_AND_ASSIGN(Datum out_std, Std(array, options));
+    auto var = checked_cast<const ScalarType*>(out_var.scalar().get());
+    auto std = checked_cast<const ScalarType*>(out_std.scalar().get());
+    ASSERT_FALSE(var->is_valid || std->is_valid);
+  }
+
+  void AssertVarStdIsInvalid(const std::string& json, const VarStdOptions& 
options) {
+    auto array = ArrayFromJSON(type_singleton(), json);
+    AssertVarStdIsInvalid(array, options);
+  }
+
+  std::shared_ptr<DataType> type_singleton() { return 
Traits::type_singleton(); }
+};
+
+template <typename ArrowType>
+class TestNumericVarStdKernel : public TestPrimitiveVarStdKernel<ArrowType> {};
+
+// Reference value from numpy.var
+TYPED_TEST_SUITE(TestNumericVarStdKernel, NumericArrowTypes);
+TYPED_TEST(TestNumericVarStdKernel, Basics) {
+  VarStdOptions options;  // ddof = 0, population var/std
+
+  this->AssertVarStdIs("[100]", options, 0);
+  this->AssertVarStdIs("[1, 2, 3]", options, 0.6666666666666666);
+  this->AssertVarStdIs("[null, 1, 2, null, 3]", options, 0.6666666666666666);
+
+  this->AssertVarStdIs({"[]", "[1]", "[2]", "[null]", "[3]"}, options,
+                       0.6666666666666666);
+  this->AssertVarStdIs({"[1, 2, 3]", "[4, 5, 6]", "[7, 8]"}, options, 5.25);

Review comment:
       Can we also test with chunks of very different sizes? For example `[1, 
2, 3, 4, 5, 6, 7]` and `[8]`.




----------------------------------------------------------------
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.

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


Reply via email to