Skip to content

Commit d42b3dc

Browse files
committed
test: cover req sketch exact mode from serialized bytes
1 parent 4014233 commit d42b3dc

3 files changed

Lines changed: 134 additions & 0 deletions

File tree

req/test/req_sketch_deserialize_from_java_test.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,50 @@ TEST_CASE("req float", "[serde_compat]") {
5252
}
5353
}
5454

55+
TEST_CASE("req float negative", "[serde_compat]") {
56+
const unsigned n_arr[] = {1, 10};
57+
for (const unsigned n: n_arr) {
58+
std::ifstream is;
59+
is.exceptions(std::ios::failbit | std::ios::badbit);
60+
is.open(testBinaryInputPath + "req_float_negative_n" + std::to_string(n) + "_java.sk", std::ios::binary);
61+
const auto sketch = req_sketch<float>::deserialize(is);
62+
REQUIRE(sketch.is_HRA());
63+
REQUIRE_FALSE(sketch.is_empty());
64+
REQUIRE_FALSE(sketch.is_estimation_mode());
65+
REQUIRE(sketch.get_n() == n);
66+
REQUIRE(sketch.get_min_item() == -static_cast<float>(n));
67+
REQUIRE(sketch.get_max_item() == -1.0f);
68+
uint64_t weight = 0;
69+
for (const auto pair: sketch) {
70+
REQUIRE(pair.first >= sketch.get_min_item());
71+
REQUIRE(pair.first <= sketch.get_max_item());
72+
weight += pair.second;
73+
}
74+
REQUIRE(weight == sketch.get_n());
75+
}
76+
}
77+
78+
TEST_CASE("req float mixed", "[serde_compat]") {
79+
const unsigned n_arr[] = {1, 10};
80+
for (const unsigned n: n_arr) {
81+
std::ifstream is;
82+
is.exceptions(std::ios::failbit | std::ios::badbit);
83+
is.open(testBinaryInputPath + "req_float_mixed_n" + std::to_string(n) + "_java.sk", std::ios::binary);
84+
const auto sketch = req_sketch<float>::deserialize(is);
85+
REQUIRE(sketch.is_HRA());
86+
REQUIRE_FALSE(sketch.is_empty());
87+
REQUIRE_FALSE(sketch.is_estimation_mode());
88+
REQUIRE(sketch.get_n() == 2 * n + 1);
89+
REQUIRE(sketch.get_min_item() == -static_cast<float>(n));
90+
REQUIRE(sketch.get_max_item() == static_cast<float>(n));
91+
uint64_t weight = 0;
92+
for (const auto pair: sketch) {
93+
REQUIRE(pair.first >= sketch.get_min_item());
94+
REQUIRE(pair.first <= sketch.get_max_item());
95+
weight += pair.second;
96+
}
97+
REQUIRE(weight == sketch.get_n());
98+
}
99+
}
100+
55101
} /* namespace datasketches */

req/test/req_sketch_serialize_for_java.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,24 @@ TEST_CASE("req sketch float generate", "[serialize_for_java]") {
3333
}
3434
}
3535

36+
TEST_CASE("req sketch float negative generate", "[serialize_for_java]") {
37+
const unsigned n_arr[] = {1, 10};
38+
for (const unsigned n: n_arr) {
39+
req_sketch<float> sketch(12);
40+
for (unsigned i = 1; i <= n; ++i) sketch.update(-static_cast<float>(i));
41+
std::ofstream os("req_float_negative_n" + std::to_string(n) + "_cpp.sk", std::ios::binary);
42+
sketch.serialize(os);
43+
}
44+
}
45+
46+
TEST_CASE("req sketch float mixed generate", "[serialize_for_java]") {
47+
const unsigned n_arr[] = {1, 10};
48+
for (const unsigned n: n_arr) {
49+
req_sketch<float> sketch(12);
50+
for (int i = -static_cast<int>(n); i <= static_cast<int>(n); ++i) sketch.update(static_cast<float>(i));
51+
std::ofstream os("req_float_mixed_n" + std::to_string(n) + "_cpp.sk", std::ios::binary);
52+
sketch.serialize(os);
53+
}
54+
}
55+
3656
} /* namespace datasketches */

req/test/req_sketch_test.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,74 @@ TEST_CASE("req sketch: byte serialize-deserialize estimation mode", "[req_sketch
316316
REQUIRE(sketch2.get_max_item() == sketch.get_max_item());
317317
}
318318

319+
TEST_CASE("req sketch: stream serialize-deserialize negative values", "[req_sketch]") {
320+
req_sketch<float> sketch(12);
321+
const size_t n = 50;
322+
for (size_t i = 1; i <= n; ++i) sketch.update(-static_cast<float>(i));
323+
324+
std::stringstream s(std::ios::in | std::ios::out | std::ios::binary);
325+
sketch.serialize(s);
326+
auto sketch2 = req_sketch<float>::deserialize(s);
327+
REQUIRE(s.tellg() == s.tellp());
328+
REQUIRE(sketch2.is_empty() == sketch.is_empty());
329+
REQUIRE(sketch2.is_estimation_mode() == sketch.is_estimation_mode());
330+
REQUIRE(sketch2.get_num_retained() == sketch.get_num_retained());
331+
REQUIRE(sketch2.get_n() == sketch.get_n());
332+
REQUIRE(sketch2.get_min_item() == -static_cast<float>(n));
333+
REQUIRE(sketch2.get_max_item() == -1.0f);
334+
}
335+
336+
TEST_CASE("req sketch: byte serialize-deserialize negative values", "[req_sketch]") {
337+
req_sketch<float> sketch(12);
338+
const size_t n = 50;
339+
for (size_t i = 1; i <= n; ++i) sketch.update(-static_cast<float>(i));
340+
341+
auto bytes = sketch.serialize();
342+
REQUIRE(bytes.size() == sketch.get_serialized_size_bytes());
343+
auto sketch2 = req_sketch<float>::deserialize(bytes.data(), bytes.size());
344+
REQUIRE(bytes.size() == sketch2.get_serialized_size_bytes());
345+
REQUIRE(sketch2.is_empty() == sketch.is_empty());
346+
REQUIRE(sketch2.is_estimation_mode() == sketch.is_estimation_mode());
347+
REQUIRE(sketch2.get_num_retained() == sketch.get_num_retained());
348+
REQUIRE(sketch2.get_n() == sketch.get_n());
349+
REQUIRE(sketch2.get_min_item() == -static_cast<float>(n));
350+
REQUIRE(sketch2.get_max_item() == -1.0f);
351+
}
352+
353+
TEST_CASE("req sketch: stream serialize-deserialize mixed values", "[req_sketch]") {
354+
req_sketch<float> sketch(12);
355+
const int half = 25;
356+
for (int i = -half; i < half; ++i) sketch.update(static_cast<float>(i));
357+
358+
std::stringstream s(std::ios::in | std::ios::out | std::ios::binary);
359+
sketch.serialize(s);
360+
auto sketch2 = req_sketch<float>::deserialize(s);
361+
REQUIRE(s.tellg() == s.tellp());
362+
REQUIRE(sketch2.is_empty() == sketch.is_empty());
363+
REQUIRE(sketch2.is_estimation_mode() == sketch.is_estimation_mode());
364+
REQUIRE(sketch2.get_num_retained() == sketch.get_num_retained());
365+
REQUIRE(sketch2.get_n() == sketch.get_n());
366+
REQUIRE(sketch2.get_min_item() == static_cast<float>(-half));
367+
REQUIRE(sketch2.get_max_item() == static_cast<float>(half - 1));
368+
}
369+
370+
TEST_CASE("req sketch: byte serialize-deserialize mixed values", "[req_sketch]") {
371+
req_sketch<float> sketch(12);
372+
const int half = 25;
373+
for (int i = -half; i < half; ++i) sketch.update(static_cast<float>(i));
374+
375+
auto bytes = sketch.serialize();
376+
REQUIRE(bytes.size() == sketch.get_serialized_size_bytes());
377+
auto sketch2 = req_sketch<float>::deserialize(bytes.data(), bytes.size());
378+
REQUIRE(bytes.size() == sketch2.get_serialized_size_bytes());
379+
REQUIRE(sketch2.is_empty() == sketch.is_empty());
380+
REQUIRE(sketch2.is_estimation_mode() == sketch.is_estimation_mode());
381+
REQUIRE(sketch2.get_num_retained() == sketch.get_num_retained());
382+
REQUIRE(sketch2.get_n() == sketch.get_n());
383+
REQUIRE(sketch2.get_min_item() == static_cast<float>(-half));
384+
REQUIRE(sketch2.get_max_item() == static_cast<float>(half - 1));
385+
}
386+
319387
TEST_CASE("req sketch: serialize deserialize stream and bytes equivalence", "[req_sketch]") {
320388
req_sketch<float> sketch(12);
321389
const size_t n = 100000;

0 commit comments

Comments
 (0)