From e938ab5b85facd0f0193e596d2431d14f8dd07b0 Mon Sep 17 00:00:00 2001 From: Lee Rhodes Date: Thu, 19 Nov 2026 12:08:24 -0800 Subject: [PATCH] density_sketch: generate serialization snapshots under -DGENERATE=true Adds density_sketch_serialize_for_java.cpp, built only with GENERATE like the other sketches' generators. It writes density_float_n*_cpp.sk and density_double_n*_cpp.sk for n = 0, 1, 10, 100, 1000 and 10000, from the same inputs as the datasketches-rust generator (k = 10, dim = 3, the point {x, sqrt(x), -x} for x = 0 .. n-1), so each C++ snapshot has a Rust counterpart with the same name. Closes #584 Co-Authored-By: Claude Opus 5.5 --- density/test/CMakeLists.txt | 7 +++ .../density_sketch_serialize_for_java.cpp | 59 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 density/test/density_sketch_serialize_for_java.cpp diff --git a/density/test/CMakeLists.txt b/density/test/CMakeLists.txt index a47cd020b..bc2edb7f8 100644 --- a/density/test/CMakeLists.txt +++ b/density/test/CMakeLists.txt @@ -32,3 +32,10 @@ target_sources(density_test PRIVATE density_sketch_test.cpp ) + +if (GENERATE) +target_sources(density_test + PRIVATE + density_sketch_serialize_for_java.cpp +) +endif() diff --git a/density/test/density_sketch_serialize_for_java.cpp b/density/test/density_sketch_serialize_for_java.cpp new file mode 100644 index 000000000..22eec94fb --- /dev/null +++ b/density/test/density_sketch_serialize_for_java.cpp @@ -0,0 +1,59 @@ +/* + * 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. + */ + +#include +#include +#include +#include +#include + +#include "density_sketch.hpp" + +namespace datasketches { + +// The same inputs as the datasketches-rust generator (datagen/src/density.rs), +// so that each C++ snapshot has a Rust counterpart with the same name + +TEST_CASE("density sketch float generate", "[serialize_for_java]") { + const unsigned n_arr[] = {0, 1, 10, 100, 1000, 10000}; + for (const unsigned n: n_arr) { + density_sketch sketch(10, 3); + for (unsigned i = 0; i < n; ++i) { + const float x = static_cast(i); + sketch.update(std::vector({x, std::sqrt(x), -x})); + } + std::ofstream os("density_float_n" + std::to_string(n) + "_cpp.sk", std::ios::binary); + sketch.serialize(os); + } +} + +TEST_CASE("density sketch double generate", "[serialize_for_java]") { + const unsigned n_arr[] = {0, 1, 10, 100, 1000, 10000}; + for (const unsigned n: n_arr) { + density_sketch sketch(10, 3); + for (unsigned i = 0; i < n; ++i) { + const double x = static_cast(i); + sketch.update(std::vector({x, std::sqrt(x), -x})); + } + std::ofstream os("density_double_n" + std::to_string(n) + "_cpp.sk", std::ios::binary); + sketch.serialize(os); + } +} + +} /* namespace datasketches */