| /* | |
| * 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 <functional> | |
| #include <initializer_list> | |
| #include <map> | |
| #include <vector> | |
| #include "array_of_strings_sketch.hpp" | |
| namespace datasketches { | |
| // assume the binary sketches for this test have been generated by datasketches-java code | |
| // in the subdirectory "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/"; | |
| static std::vector<uint8_t> read_binary_file(const std::string& path) { | |
| std::ifstream is; | |
| is.exceptions(std::ios::failbit | std::ios::badbit); | |
| is.open(path, std::ios::binary); | |
| is.seekg(0, std::ios::end); | |
| const auto size = static_cast<size_t>(is.tellg()); | |
| is.seekg(0, std::ios::beg); | |
| std::vector<uint8_t> bytes(size); | |
| if (size != 0) { | |
| is.read(reinterpret_cast<char*>(bytes.data()), size); | |
| } | |
| return bytes; | |
| } | |
| TEST_CASE("aos sketch one value", "[serde_compat]") { | |
| const unsigned n_arr[] = {0, 1, 10, 100, 1000, 10000, 100000, 1000000}; | |
| for (const unsigned n: n_arr) { | |
| const auto path = testBinaryInputPath + "aos_1_n" + std::to_string(n) + "_java.sk"; | |
| SECTION("stream") { | |
| std::ifstream is; | |
| is.exceptions(std::ios::failbit | std::ios::badbit); | |
| is.open(path, std::ios::binary); | |
| const auto sketch = compact_array_of_strings_tuple_sketch<>::deserialize( | |
| is, DEFAULT_SEED, default_array_of_strings_serde<>() | |
| ); | |
| 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.size() == 1); | |
| } | |
| } | |
| SECTION("bytes") { | |
| const auto bytes = read_binary_file(path); | |
| const auto sketch = compact_array_of_strings_tuple_sketch<>::deserialize( | |
| bytes.data(), bytes.size(), DEFAULT_SEED, default_array_of_strings_serde<>() | |
| ); | |
| 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.size() == 1); | |
| } | |
| } | |
| } | |
| } | |
| TEST_CASE("aos sketch three values", "[serde_compat]") { | |
| const unsigned n_arr[] = {0, 1, 10, 100, 1000, 10000, 100000, 1000000}; | |
| for (const unsigned n: n_arr) { | |
| const auto path = testBinaryInputPath + "aos_3_n" + std::to_string(n) + "_java.sk"; | |
| SECTION("stream") { | |
| std::ifstream is; | |
| is.exceptions(std::ios::failbit | std::ios::badbit); | |
| is.open(path, std::ios::binary); | |
| const auto sketch = compact_array_of_strings_tuple_sketch<>::deserialize( | |
| is, DEFAULT_SEED, default_array_of_strings_serde<>() | |
| ); | |
| 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.size() == 3); | |
| } | |
| } | |
| SECTION("bytes") { | |
| const auto bytes = read_binary_file(path); | |
| const auto sketch = compact_array_of_strings_tuple_sketch<>::deserialize( | |
| bytes.data(), bytes.size(), DEFAULT_SEED, default_array_of_strings_serde<>() | |
| ); | |
| 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.size() == 3); | |
| } | |
| } | |
| } | |
| } | |
| TEST_CASE("aos sketch non-empty no entries", "[serde_compat]") { | |
| const auto path = testBinaryInputPath + "aos_1_non_empty_no_entries_java.sk"; | |
| SECTION("stream") { | |
| std::ifstream is; | |
| is.exceptions(std::ios::failbit | std::ios::badbit); | |
| is.open(path, std::ios::binary); | |
| const auto sketch = compact_array_of_strings_tuple_sketch<>::deserialize( | |
| is, DEFAULT_SEED, default_array_of_strings_serde<>() | |
| ); | |
| REQUIRE_FALSE(sketch.is_empty()); | |
| REQUIRE(sketch.get_num_retained() == 0); | |
| } | |
| SECTION("bytes") { | |
| const auto bytes = read_binary_file(path); | |
| const auto sketch = compact_array_of_strings_tuple_sketch<>::deserialize( | |
| bytes.data(), bytes.size(), DEFAULT_SEED, default_array_of_strings_serde<>() | |
| ); | |
| REQUIRE_FALSE(sketch.is_empty()); | |
| REQUIRE(sketch.get_num_retained() == 0); | |
| } | |
| } | |
| TEST_CASE("aos sketch multi keys strings", "[serde_compat]") { | |
| const unsigned n_arr[] = {0, 1, 10, 100, 1000, 10000, 100000, 1000000}; | |
| for (const unsigned n: n_arr) { | |
| const auto path = testBinaryInputPath + "aos_multikey_n" + std::to_string(n) + "_java.sk"; | |
| SECTION("stream") { | |
| std::ifstream is; | |
| is.exceptions(std::ios::failbit | std::ios::badbit); | |
| is.open(path, std::ios::binary); | |
| const auto sketch = compact_array_of_strings_tuple_sketch<>::deserialize( | |
| is, DEFAULT_SEED, default_array_of_strings_serde<>() | |
| ); | |
| 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.size() == 1); | |
| } | |
| } | |
| SECTION("bytes") { | |
| const auto bytes = read_binary_file(path); | |
| const auto sketch = compact_array_of_strings_tuple_sketch<>::deserialize( | |
| bytes.data(), bytes.size(), DEFAULT_SEED, default_array_of_strings_serde<>() | |
| ); | |
| 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.size() == 1); | |
| } | |
| } | |
| } | |
| } | |
| TEST_CASE("aos sketch unicode strings", "[serde_compat]") { | |
| const auto path = testBinaryInputPath + "aos_unicode_java.sk"; | |
| auto check = [](const compact_array_of_strings_tuple_sketch<>& sketch) { | |
| REQUIRE_FALSE(sketch.is_empty()); | |
| REQUIRE_FALSE(sketch.is_estimation_mode()); | |
| REQUIRE(sketch.get_num_retained() == 3); | |
| const std::vector<std::vector<std::string>> expected_values = { | |
| {"밸류", "값"}, | |
| {"📦", "🎁"}, | |
| {"ценить1", "ценить2"} | |
| }; | |
| std::vector<bool> matched(expected_values.size(), false); | |
| for (const auto& entry: sketch) { | |
| REQUIRE(entry.first < sketch.get_theta64()); | |
| REQUIRE(entry.second.size() == 2); | |
| bool found = false; | |
| for (size_t i = 0; i < expected_values.size(); ++i) { | |
| if (matched[i]) continue; | |
| const auto& expected = expected_values[i]; | |
| if (entry.second.size() != expected.size()) continue; | |
| bool equal = true; | |
| for (size_t j = 0; j < expected.size(); ++j) { | |
| if (entry.second[j] != expected[j]) { | |
| equal = false; | |
| break; | |
| } | |
| } | |
| if (equal) { | |
| matched[i] = true; | |
| found = true; | |
| break; | |
| } | |
| } | |
| REQUIRE(found); | |
| } | |
| for (bool found: matched) REQUIRE(found); | |
| }; | |
| SECTION("stream") { | |
| std::ifstream is; | |
| is.exceptions(std::ios::failbit | std::ios::badbit); | |
| is.open(path, std::ios::binary); | |
| const auto sketch = compact_array_of_strings_tuple_sketch<>::deserialize( | |
| is, DEFAULT_SEED, default_array_of_strings_serde<>() | |
| ); | |
| check(sketch); | |
| } | |
| SECTION("bytes") { | |
| const auto bytes = read_binary_file(path); | |
| const auto sketch = compact_array_of_strings_tuple_sketch<>::deserialize( | |
| bytes.data(), bytes.size(), DEFAULT_SEED, default_array_of_strings_serde<>() | |
| ); | |
| check(sketch); | |
| } | |
| } | |
| TEST_CASE("aos sketch empty strings", "[serde_compat]") { | |
| const auto path = testBinaryInputPath + "aos_empty_strings_java.sk"; | |
| auto check = [](const compact_array_of_strings_tuple_sketch<>& sketch) { | |
| REQUIRE_FALSE(sketch.is_empty()); | |
| REQUIRE_FALSE(sketch.is_estimation_mode()); | |
| REQUIRE(sketch.get_num_retained() == 3); | |
| const std::vector<std::vector<std::string>> expected_values = { | |
| {"empty_key_value"}, | |
| {""}, | |
| {"", ""} | |
| }; | |
| std::vector<bool> matched(expected_values.size(), false); | |
| for (const auto& entry: sketch) { | |
| REQUIRE(entry.first < sketch.get_theta64()); | |
| bool found = false; | |
| for (size_t i = 0; i < expected_values.size(); ++i) { | |
| if (matched[i]) continue; | |
| const auto& expected = expected_values[i]; | |
| if (entry.second.size() != expected.size()) continue; | |
| bool equal = true; | |
| for (size_t j = 0; j < expected.size(); ++j) { | |
| if (entry.second[j] != expected[j]) { | |
| equal = false; | |
| break; | |
| } | |
| } | |
| if (equal) { | |
| matched[i] = true; | |
| found = true; | |
| break; | |
| } | |
| } | |
| REQUIRE(found); | |
| } | |
| for (bool found: matched) REQUIRE(found); | |
| }; | |
| SECTION("stream") { | |
| std::ifstream is; | |
| is.exceptions(std::ios::failbit | std::ios::badbit); | |
| is.open(path, std::ios::binary); | |
| const auto sketch = compact_array_of_strings_tuple_sketch<>::deserialize( | |
| is, DEFAULT_SEED, default_array_of_strings_serde<>() | |
| ); | |
| check(sketch); | |
| } | |
| SECTION("bytes") { | |
| const auto bytes = read_binary_file(path); | |
| const auto sketch = compact_array_of_strings_tuple_sketch<>::deserialize( | |
| bytes.data(), bytes.size(), DEFAULT_SEED, default_array_of_strings_serde<>() | |
| ); | |
| check(sketch); | |
| } | |
| } | |
| using aos_update = std::function<void(const array_of_strings& key, const array_of_strings& values)>; | |
| static array_of_strings make_array(std::initializer_list<std::string> items) { | |
| array_of_strings array(static_cast<uint8_t>(items.size()), ""); | |
| uint8_t i = 0; | |
| for (const auto& item: items) array[i++] = item; | |
| return array; | |
| } | |
| // Updates the sketch with the same keys and values as Java's AosSketchCrossLanguageTest and | |
| // requires it to retain exactly the hashes and summaries of the Java snapshot. | |
| static void check_java_hash_compat( | |
| const std::string& name, | |
| update_array_of_strings_tuple_sketch<> sketch, | |
| const std::function<void(const aos_update&)>& updates | |
| ) { | |
| INFO(name); | |
| updates([&sketch](const array_of_strings& key, const array_of_strings& values) { | |
| sketch.update(hash_array_of_strings_key(key), values); | |
| }); | |
| const auto cpp_sketch = compact_array_of_strings_sketch(sketch); | |
| const auto bytes = read_binary_file(testBinaryInputPath + name + "_java.sk"); | |
| const auto java_sketch = compact_array_of_strings_tuple_sketch<>::deserialize(bytes.data(), bytes.size()); | |
| REQUIRE(cpp_sketch.get_theta64() == java_sketch.get_theta64()); | |
| REQUIRE(cpp_sketch.get_num_retained() == java_sketch.get_num_retained()); | |
| std::map<uint64_t, array_of_strings> cpp_entries; | |
| for (const auto& entry: cpp_sketch) cpp_entries.emplace(entry.first, entry.second); | |
| for (const auto& entry: java_sketch) { | |
| const auto it = cpp_entries.find(entry.first); | |
| REQUIRE(it != cpp_entries.end()); | |
| REQUIRE(it->second == entry.second); | |
| } | |
| } | |
| TEST_CASE("aos sketch java hash compat", "[serde_compat]") { | |
| const unsigned n_arr[] = {0, 1, 10, 100, 1000, 10000, 100000, 1000000}; | |
| for (const unsigned n: n_arr) { | |
| const std::string sfx = "_n" + std::to_string(n); | |
| check_java_hash_compat("aos_1" + sfx, update_array_of_strings_tuple_sketch<>::builder().build(), | |
| [n](const aos_update& update) { | |
| for (unsigned i = 0; i < n; ++i) { | |
| update(make_array({std::to_string(i)}), make_array({"value" + std::to_string(i)})); | |
| } | |
| }); | |
| check_java_hash_compat("aos_3" + sfx, update_array_of_strings_tuple_sketch<>::builder().build(), | |
| [n](const aos_update& update) { | |
| for (unsigned i = 0; i < n; ++i) { | |
| const std::string s = std::to_string(i); | |
| update(make_array({s}), make_array({"a" + s, "b" + s, "c" + s})); | |
| } | |
| }); | |
| check_java_hash_compat("aos_multikey" + sfx, update_array_of_strings_tuple_sketch<>::builder().build(), | |
| [n](const aos_update& update) { | |
| for (unsigned i = 0; i < n; ++i) { | |
| update( | |
| make_array({"key" + std::to_string(i), "subkey" + std::to_string(i % 10)}), | |
| make_array({"value" + std::to_string(i)}) | |
| ); | |
| } | |
| }); | |
| } | |
| check_java_hash_compat("aos_1_non_empty_no_entries", | |
| update_array_of_strings_tuple_sketch<>::builder() | |
| .set_lg_k(12) | |
| .set_resize_factor(resize_factor::X8) | |
| .set_p(0.01f) | |
| .build(), | |
| [](const aos_update& update) { | |
| update(make_array({"key1"}), make_array({"value1"})); | |
| }); | |
| check_java_hash_compat("aos_unicode", update_array_of_strings_tuple_sketch<>::builder().build(), | |
| [](const aos_update& update) { | |
| update(make_array({"키", "열쇠"}), make_array({"밸류", "값"})); | |
| // "🗝️" is U+1F5DD followed by the variation selector U+FE0F, as in Java | |
| update(make_array({"🔑", "🗝️"}), make_array({"📦", "🎁"})); | |
| update(make_array({"ключ1", "ключ2"}), make_array({"ценить1", "ценить2"})); | |
| }); | |
| check_java_hash_compat("aos_empty_strings", update_array_of_strings_tuple_sketch<>::builder().build(), | |
| [](const aos_update& update) { | |
| update(make_array({""}), make_array({"empty_key_value"})); | |
| update(make_array({"empty_value_key"}), make_array({""})); | |
| update(make_array({"", ""}), make_array({"", ""})); | |
| }); | |
| } | |
| } |