| /* | |
| * 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 <iterator> | |
| #include <vector> | |
| #include "count_min_v2_cases.hpp" | |
| namespace datasketches { | |
| // assume the binary sketches for this test have been generated by datasketches-java code | |
| // in the subdirectory called "serialization_test_data/java_generated_files" in the root directory of this project | |
| static std::string testBinaryInputPath = std::string(TEST_BINARY_INPUT_PATH) + "../../serialization_test_data/java_generated_files/"; | |
| // the checks of section 8.2 of the Count-Min format specification in datasketches-tck | |
| TEST_CASE("count min v2 java", "[serde_compat]") { | |
| for (const auto& c: count_min_v2_cases) { | |
| CAPTURE(c.name); | |
| std::ifstream is; | |
| is.exceptions(std::ios::failbit | std::ios::badbit); | |
| is.open(testBinaryInputPath + count_min_v2_file_name(c, "java"), std::ios::binary); | |
| const std::vector<uint8_t> bytes((std::istreambuf_iterator<char>(is)), std::istreambuf_iterator<char>()); | |
| const auto sketch = count_min_sketch<int64_t>::deserialize(bytes.data(), bytes.size(), c.seed); | |
| REQUIRE(sketch.get_serial_version() == 2); | |
| REQUIRE(sketch.get_num_hashes() == 3); | |
| REQUIRE(sketch.get_num_buckets() == 1000); | |
| REQUIRE(sketch.is_empty() == (c.n == 0)); | |
| REQUIRE(sketch.get_total_weight() == c.n * (c.n + 1) / 2 + c.n); | |
| for (int64_t i = 0; i < c.n; ++i) { | |
| REQUIRE(sketch.get_estimate(i) >= i + 1); | |
| REQUIRE(sketch.get_estimate(i) <= sketch.get_upper_bound(i)); | |
| REQUIRE(sketch.get_estimate(std::to_string(i)) >= 1); | |
| REQUIRE(sketch.get_estimate(std::to_string(i)) <= sketch.get_upper_bound(std::to_string(i))); | |
| } | |
| // version 2 is deterministic: the same case is byte-identical in every language | |
| const auto cpp_bytes = make_count_min_v2_case(c).serialize(); | |
| REQUIRE(std::vector<uint8_t>(cpp_bytes.begin(), cpp_bytes.end()) == bytes); | |
| } | |
| } | |
| } /* namespace datasketches */ |