cyb70289 commented on a change in pull request #9310: URL: https://github.com/apache/arrow/pull/9310#discussion_r565120398
########## File path: cpp/src/arrow/util/tdigest_test.cc ########## @@ -0,0 +1,266 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// XXX: There's no rigid error bound available. The accuracy is to some degree +// *random*, which depends on input data and quantiles to be calculated. I also +// find small gaps among linux/windows/macos. +// In below tests, most quantiles are within 1% deviation from exact values, +// while the worst test case is about 10% drift. +// To make test result stable, I relaxed error bound to be *good enough*. + +#include <algorithm> +#include <cmath> +#include <vector> + +#include <gtest/gtest.h> + +#include "arrow/testing/random.h" +#include "arrow/testing/util.h" +#include "arrow/util/make_unique.h" +#include "arrow/util/tdigest.h" + +namespace arrow { +namespace internal { + +TEST(TDigestTest, SingleValue) { + const double value = 0.12345678; + + TDigest td; + td.Add(value); + EXPECT_TRUE(td.Verify()); + // all quantiles equal to same single vaue + for (double q = 0; q <= 1; q += 0.1) { + EXPECT_EQ(td.Quantile(q), value); + } +} + +TEST(TDigestTest, FewValues) { + // exact quantile at 0.1 intervanl, test sorted and unsorted input + std::vector<std::vector<double>> values_vector = { + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + {4, 1, 9, 0, 3, 2, 5, 6, 8, 7, 10}, + }; + + for (auto& values : values_vector) { + TDigest td; + for (double v : values) { + td.Add(v); + } + EXPECT_TRUE(td.Verify()); + + double q = 0; + for (size_t i = 0; i < values.size(); ++i) { + double expected = static_cast<double>(i); + EXPECT_EQ(td.Quantile(q), expected); + q += 0.1; + } + } +} + +// Calculate exact quantile as truth +std::vector<double> ExactQuantile(std::vector<double> values, + const std::vector<double> quantiles) { 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. For queries about this service, please contact Infrastructure at: [email protected]
