Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ if (COVERAGE)
find_program(GENHTML_PATH NAMES "genhtml")
if (NOT LCOV_PATH-NOTFOUND AND NOT GENHTML_PATH-NOTFOUND)
add_custom_target(coverage_report
COMMAND ${LCOV_PATH} --capture --exclude '*/test/*' --exclude '/Library/*' --exclude '/usr/include/*' --directory . --output-file lcov.info
COMMAND ${LCOV_PATH} --capture --exclude '*/test/*' --exclude '*/_deps/*' --exclude '/Library/*' --exclude '/usr/include/*' --directory . --output-file lcov.info
COMMAND ${GENHTML_PATH} --legend lcov.info --output-directory coverage --demangle-cpp)
endif()
endif()
Expand Down
2 changes: 2 additions & 0 deletions common/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ target_sources(common_test
quantiles_sorted_view_test.cpp
optional_test.cpp
binomial_bounds_test.cpp
hash_test.cpp
serde_test.cpp
)

# now the integration test part
Expand Down
125 changes: 125 additions & 0 deletions common/test/hash_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* 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 <cstdint>
#include <vector>

#include "MurmurHash3.h"
#include "xxhash64.h"

namespace datasketches {

// The expected values below were computed with the DataSketches Java MurmurHash3 and XxHash64,
// on the bytes (i * 7 + 1) for i = 0 .. length - 1, with seed 9001.
// The lengths cover every tail length of both hashes, and inputs below and above their block sizes
// (16 bytes for MurmurHash3, 32 bytes for XXH64).

static std::vector<uint8_t> test_bytes(size_t length) {
std::vector<uint8_t> bytes(length);
for (size_t i = 0; i < length; ++i) bytes[i] = static_cast<uint8_t>(i * 7 + 1);
return bytes;
}

struct murmur_vector { size_t length; uint64_t h1; uint64_t h2; };

static const murmur_vector murmur_vectors[] = {
{1, 0x07D0803C66013EC8ULL, 0x927863C2EEFD6912ULL},
{7, 0x020B51C078D587CBULL, 0x316B3141E9FFD773ULL},
{8, 0xE9F71E57E963903AULL, 0xA957E0843C44F3AAULL},
{9, 0x6B6CB32940E705E3ULL, 0x3E7CD4000704ACDAULL},
{15, 0x736A252A1C5E172DULL, 0x517E714AC2248BA4ULL},
{16, 0xD8B586BF256F1DDCULL, 0xB5C448377D77B514ULL},
{17, 0xC0A352AC625BAFFAULL, 0x0913B04F53B223CCULL},
{31, 0x4B9F83F8F651658AULL, 0x1205B2CA952D9C19ULL},
{32, 0x5B55CBEEB43D7D0BULL, 0x4DB30635C11EED0CULL},
{33, 0x3867BF4C60D1A19AULL, 0xFF095481A58FDC70ULL},
{47, 0x3EE5809A1A0C112AULL, 0x7DACD3C87CC9F091ULL},
{48, 0x9E66BF0565E4F423ULL, 0x395DD92B981FEE6DULL},
{63, 0x0FC165544C602A43ULL, 0x5BFB95F694356CC3ULL},
};

TEST_CASE("MurmurHash3_x64_128 matches Java", "[common]") {
for (const auto& v: murmur_vectors) {
CAPTURE(v.length);
const auto bytes = test_bytes(v.length);
HashState hashes;
MurmurHash3_x64_128(bytes.data(), bytes.size(), 9001, hashes);
REQUIRE(hashes.h1 == v.h1);
REQUIRE(hashes.h2 == v.h2);
}
}

struct xxhash64_vector { size_t length; uint64_t hash; };

static const xxhash64_vector xxhash64_vectors[] = {
{0, 0x9C981E42BD7E64E7ULL},
{1, 0xA0AB08F4B1C01485ULL},
{3, 0xE65244A36F7F595DULL},
{4, 0xF12CB5FF2035CE10ULL},
{7, 0xBCEB60AC33C41D23ULL},
{8, 0x84F082F72149F5FFULL},
{31, 0x42B387B64FCB9962ULL},
{32, 0x6FCB36F5CEDF1CD4ULL},
{33, 0x98B48780014AF78CULL},
{63, 0x0C72E0BAFADA60F5ULL},
{64, 0xD80BE3B3B7780235ULL},
{65, 0xCD0333A1820D1C01ULL},
{100, 0xF2F5A9088E94AFF8ULL},
};

TEST_CASE("XXHash64 matches Java", "[common]") {
for (const auto& v: xxhash64_vectors) {
CAPTURE(v.length);
const auto bytes = test_bytes(v.length);
REQUIRE(XXHash64::hash(bytes.data(), bytes.size(), 9001) == v.hash);
}
}

// test vectors from the xxHash specification, as listed in the Count-Min format specification in datasketches-tck
TEST_CASE("XXHash64 specification vectors", "[common]") {
REQUIRE(XXHash64::hash("", 0, 0) == 0xEF46DB3751D8E999ULL);
REQUIRE(XXHash64::hash("a", 1, 0) == 0xD24EC4F1A98C6E5BULL);
REQUIRE(XXHash64::hash("abc", 3, 0) == 0x44BC2CF5AD770999ULL);
}

TEST_CASE("XXHash64 streaming gives the same hash as one call", "[common]") {
const auto bytes = test_bytes(100);
const uint64_t expected = XXHash64::hash(bytes.data(), bytes.size(), 9001);
// split points before, inside and across the 32-byte blocks
for (size_t first: {1, 5, 31, 32, 33, 50, 64, 99}) {
CAPTURE(first);
XXHash64 hasher(9001);
REQUIRE(hasher.add(bytes.data(), first));
REQUIRE(hasher.add(bytes.data() + first, bytes.size() - first));
REQUIRE(hasher.hash() == expected);
}
// many small pieces
XXHash64 hasher(9001);
for (size_t i = 0; i < bytes.size(); i += 3) {
REQUIRE(hasher.add(bytes.data() + i, std::min<size_t>(3, bytes.size() - i)));
}
REQUIRE(hasher.hash() == expected);
// no data is rejected and changes nothing
REQUIRE_FALSE(hasher.add(nullptr, 5));
REQUIRE_FALSE(hasher.add(bytes.data(), 0));
REQUIRE(hasher.hash() == expected);
}

} /* namespace datasketches */
92 changes: 92 additions & 0 deletions common/test/serde_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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 <cstdint>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>

#include "serde.hpp"

namespace datasketches {

// a stream buffer that accepts no output
struct failing_buffer: std::streambuf {
protected:
int_type overflow(int_type) override { return traits_type::eof(); }
std::streamsize xsputn(const char*, std::streamsize) override { return 0; }
};

// serde reports a stream error as std::runtime_error, whether or not the stream throws exceptions
TEST_CASE("serde: arithmetic write failure", "[common]") {
const int64_t items[] = {1, 2, 3};
for (bool exceptions: {false, true}) {
CAPTURE(exceptions);
failing_buffer buffer;
std::ostream os(&buffer);
if (exceptions) os.exceptions(std::ios::failbit | std::ios::badbit);
REQUIRE_THROWS_AS(serde<int64_t>().serialize(os, items, 3), std::runtime_error);
}
}

TEST_CASE("serde: arithmetic read failure", "[common]") {
for (bool exceptions: {false, true}) {
CAPTURE(exceptions);
std::istringstream is(std::string(10, 'x')); // 10 bytes, 24 needed
if (exceptions) is.exceptions(std::ios::failbit | std::ios::badbit);
int64_t items[3];
REQUIRE_THROWS_AS(serde<int64_t>().deserialize(is, items, 3), std::runtime_error);
}
}

TEST_CASE("serde: string write failure", "[common]") {
const std::string items[] = {"apple", "pear"};
for (bool exceptions: {false, true}) {
CAPTURE(exceptions);
failing_buffer buffer;
std::ostream os(&buffer);
if (exceptions) os.exceptions(std::ios::failbit | std::ios::badbit);
REQUIRE_THROWS_AS(serde<std::string>().serialize(os, items, 2), std::runtime_error);
}
}

TEST_CASE("serde: string read failure", "[common]") {
// the first string is complete, the second is cut short
const std::string items[] = {"apple", "a longer string than the short string optimization holds"};
std::stringstream full(std::ios::in | std::ios::out | std::ios::binary);
serde<std::string>().serialize(full, items, 2);
const std::string bytes = full.str();
for (size_t cut: {bytes.size() - 1, size_t(4 + 5 + 2), size_t(4 + 2)}) {
for (bool exceptions: {false, true}) {
CAPTURE(cut, exceptions);
std::istringstream is(bytes.substr(0, cut));
if (exceptions) is.exceptions(std::ios::failbit | std::ios::badbit);
// items are constructed by deserialize and destroyed by it on failure
std::allocator<std::string> alloc;
std::string* read = alloc.allocate(2);
REQUIRE_THROWS_AS(serde<std::string>().deserialize(is, read, 2), std::runtime_error);
alloc.deallocate(read, 2);
}
}
}

} /* namespace datasketches */
40 changes: 40 additions & 0 deletions cpc/test/cpc_union_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,44 @@ TEST_CASE("cpc union: moving update", "[cpc_union]") {



// A sketch that comes out of a union has no HIP estimate, so its bounds come from the ICON estimate.
// The expected values were computed with the DataSketches Java CpcUnion on the same input.
TEST_CASE("cpc union: bounds of the result", "[cpc_union]") {
struct expected_bounds { uint8_t lg_k; double estimate; double lb[3]; double ub[3]; };
const expected_bounds cases[] = {
// lg_k up to 14 uses the measured error table, above it a constant
{10, 15159.228912348806, {14831.317746546260, 14513.557043381377, 14202.553767187180}, {15488, 15828, 16174}},
{16, 15005.337536986240, {14964.818701476400, 14924.518102269025, 14884.433980953483}, {15047, 15088, 15129}}
};
for (const auto& c: cases) {
const int lg_k = c.lg_k; // printed as a number, not a character
CAPTURE(lg_k);
cpc_sketch s1(c.lg_k), s2(c.lg_k);
for (uint64_t i = 0; i < 5000; ++i) s1.update(i);
for (uint64_t i = 5000; i < 15000; ++i) s2.update(i);
cpc_union u(c.lg_k);
u.update(s1);
u.update(s2);
const auto result = u.get_result();
// Floating-point results differ slightly between compilers and math libraries
// (about 4e-12 relative with gcc in the sanitizer builds), so compare within 1e-9.
// The upper bound is rounded up to a whole number, so it may differ by 1.
REQUIRE(result.get_estimate() == Approx(c.estimate).epsilon(1e-9));
for (unsigned kappa = 1; kappa <= 3; ++kappa) {
CAPTURE(kappa);
REQUIRE(result.get_lower_bound(kappa) == Approx(c.lb[kappa - 1]).epsilon(1e-9));
REQUIRE(result.get_upper_bound(kappa) == Approx(c.ub[kappa - 1]).margin(1));
}
REQUIRE_THROWS_AS(result.get_lower_bound(0), std::invalid_argument);
REQUIRE_THROWS_AS(result.get_upper_bound(4), std::invalid_argument);
}

// the result of a union of empty sketches has bounds of 0
cpc_union u(10);
u.update(cpc_sketch(10));
const auto empty = u.get_result();
REQUIRE(empty.get_lower_bound(1) == 0.0);
REQUIRE(empty.get_upper_bound(1) == 0.0);
}

} /* namespace datasketches */
Loading