Skip to content

Commit

Permalink
tuple sketch serde compatibility test
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderSaydakov committed Aug 28, 2023
1 parent c1509a4 commit 216e5dc
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tuple/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ if (SERDE_COMPAT)
target_sources(tuple_test
PRIVATE
aod_sketch_deserialize_from_java_test.cpp
tuple_sketch_deserialize_from_java_test.cpp
)
endif()

if (GENERATE)
target_sources(tuple_test
PRIVATE
aod_sketch_serialize_for_java.cpp
tuple_sketch_serialize_for_java.cpp
)
endif()
47 changes: 47 additions & 0 deletions tuple/test/tuple_sketch_deserialize_from_java_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 <catch2/catch.hpp>
#include <fstream>
#include <tuple_sketch.hpp>

namespace datasketches {

// assume the binary sketches for this test have been generated by datasketches-java code
// in the subdirectory called "java" in the root directory of this project
static std::string testBinaryInputPath = std::string(TEST_BINARY_INPUT_PATH) + "../../java/";

TEST_CASE("tuple sketch int", "[serde_compat]") {
const unsigned n_arr[] = {0, 1, 10, 100, 1000, 10000, 100000, 1000000};
for (const unsigned n: n_arr) {
std::ifstream is;
is.exceptions(std::ios::failbit | std::ios::badbit);
is.open(testBinaryInputPath + "tuple_int_n" + std::to_string(n) + "_java.sk", std::ios::binary);
const auto sketch = compact_tuple_sketch<int>::deserialize(is);
REQUIRE(sketch.is_empty() == (n == 0));
REQUIRE(sketch.is_estimation_mode() == (n > 1000));
REQUIRE(sketch.get_estimate() == Approx(n).margin(n * 0.03));
for (const auto& entry: sketch) {
REQUIRE(entry.first < sketch.get_theta64());
REQUIRE(entry.second < static_cast<int>(n));
}
}
}

} /* namespace datasketches */
38 changes: 38 additions & 0 deletions tuple/test/tuple_sketch_serialize_for_java.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 <catch2/catch.hpp>
#include <fstream>
#include <tuple_sketch.hpp>

namespace datasketches {

TEST_CASE("tuple sketch int generate", "[serialize_for_java]") {
const unsigned n_arr[] = {0, 1, 10, 100, 1000, 10000, 100000, 1000000};
for (const unsigned n: n_arr) {
auto sketch = update_tuple_sketch<int>::builder().build();
for (unsigned i = 0; i < n; ++i) sketch.update(i, i);
REQUIRE(sketch.is_empty() == (n == 0));
REQUIRE(sketch.get_estimate() == Approx(n).margin(n * 0.03));
std::ofstream os("tuple_int_n" + std::to_string(n) + "_cpp.sk", std::ios::binary);
sketch.compact().serialize(os);
}
}

} /* namespace datasketches */

0 comments on commit 216e5dc

Please sign in to comment.