#include <catch2/catch.hpp>
#include <cmath>
#include <sstream>
#include <fstream>
#include <quantiles_sketch.hpp>
#include <serde.hpp>
#include <test_allocator.hpp>
namespace datasketches {
#ifdef TEST_BINARY_INPUT_PATH
static std::string testBinaryInputPath = TEST_BINARY_INPUT_PATH;
#else
static std::string testBinaryInputPath = "test/";
#endif
using quantiles_double_sketch = quantiles_sketch<double, std::less<double>, test_allocator<double>>;
static void quantiles_decode_and_check(uint16_t k, uint64_t n, const std::string& version,
double expected_median) {
const double median_rank = 0.5;
std::ostringstream filestr;
filestr << "Qk" << k << "_n" << n << "_v" << version << ".sk";
std::ifstream is;
is.exceptions(std::ios::failbit | std::ios::badbit);
std::string filename = testBinaryInputPath + filestr.str();
is.open(filename, std::ios::binary);
auto sketch_stream = quantiles_double_sketch::deserialize(is, serde<double>(), std::less<double>(), 0);
is.close();
REQUIRE(sketch_stream.get_quantile(median_rank) == expected_median);
std::ifstream infile(filename, std::ios::binary);
std::vector<char> bytes(
(std::istreambuf_iterator<char>(infile)),
(std::istreambuf_iterator<char>()));
infile.close();
auto sketch_bytes = quantiles_double_sketch::deserialize(bytes.data(), bytes.size(), serde<double>(),
std::less<double>(), 0);
REQUIRE(sketch_bytes.get_quantile(median_rank) == expected_median);
}
TEST_CASE("quantiles compatibility", "[quantiles_compatibility]") {
test_allocator_total_bytes = 0;
SECTION("Qk128_n50_v0.3.0.sk") {
quantiles_decode_and_check(128, 50, "0.3.0", 25);
}
SECTION("Qk128_n1000_v0.3.0.sk") {
quantiles_decode_and_check(128, 1000, "0.3.0", 497);
}
SECTION("Qk128_n50_v0.6.0.sk") {
quantiles_decode_and_check(128, 50, "0.6.0", 25);
}
SECTION("Qk128_n1000_v0.6.0.sk") {
quantiles_decode_and_check(128, 1000, "0.6.0", 497);
}
SECTION("Qk128_n50_v0.8.0.sk") {
quantiles_decode_and_check(128, 50, "0.8.0", 25);
}
SECTION("Qk128_n1000_v0.8.0.sk") {
quantiles_decode_and_check(128, 1000, "0.8.0", 497);
}
SECTION("Qk128_n50_v0.8.3.sk") {
quantiles_decode_and_check(128, 50, "0.8.3", 25);
}
SECTION("Qk128_n1000_v0.8.3.sk") {
quantiles_decode_and_check(128, 1000, "0.8.3", 497);
}
if (test_allocator_total_bytes != 0) {
REQUIRE(test_allocator_total_bytes == 0);
}
}
}