You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by al...@apache.org on 2020/06/23 00:40:09 UTC

[incubator-datasketches-cpp] 02/02: test allocations

This is an automated email from the ASF dual-hosted git repository.

alsay pushed a commit to branch tuple_sketch
in repository https://gitbox.apache.org/repos/asf/incubator-datasketches-cpp.git

commit ee3d2e20af647f169544e6e84c87773594ebd918
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Mon Jun 22 17:39:54 2020 -0700

    test allocations
---
 common/test/test_allocator.cpp              |  4 ++
 common/test/test_allocator.hpp              |  3 ++
 tuple/test/CMakeLists.txt                   |  1 +
 tuple/test/tuple_sketch_allocation_test.cpp | 61 +++++++++++++++++++++++++++++
 4 files changed, 69 insertions(+)

diff --git a/common/test/test_allocator.cpp b/common/test/test_allocator.cpp
index 02c5654..47389ff 100644
--- a/common/test/test_allocator.cpp
+++ b/common/test/test_allocator.cpp
@@ -24,4 +24,8 @@ namespace datasketches {
 // global variable to keep track of allocated size
 long long test_allocator_total_bytes = 0;
 
+// global variable to keep track of net allocations
+// (number of allocations minus number of deallocations)
+long long test_allocator_net_allocations = 0;
+
 } /* namespace datasketches */
diff --git a/common/test/test_allocator.hpp b/common/test/test_allocator.hpp
index 41dacfd..8b5481b 100644
--- a/common/test/test_allocator.hpp
+++ b/common/test/test_allocator.hpp
@@ -28,6 +28,7 @@
 namespace datasketches {
 
 extern long long test_allocator_total_bytes;
+extern long long test_allocator_net_allocations;
 
 template <class T> class test_allocator {
 public:
@@ -57,12 +58,14 @@ public:
     void* p = new char[n * sizeof(value_type)];
     if (!p) throw std::bad_alloc();
     test_allocator_total_bytes += n * sizeof(value_type);
+    ++test_allocator_net_allocations;
     return static_cast<pointer>(p);
   }
 
   void deallocate(pointer p, size_type n) {
     if (p) delete[] (char*) p;
     test_allocator_total_bytes -= n * sizeof(value_type);
+    --test_allocator_net_allocations;
   }
 
   size_type max_size() const {
diff --git a/tuple/test/CMakeLists.txt b/tuple/test/CMakeLists.txt
index 29f9c75..9c6a3a3 100644
--- a/tuple/test/CMakeLists.txt
+++ b/tuple/test/CMakeLists.txt
@@ -39,6 +39,7 @@ add_test(
 target_sources(tuple_test
   PRIVATE
     tuple_sketch_test.cpp
+    tuple_sketch_allocation_test.cpp
     tuple_union_test.cpp
     theta_sketch_experimental_test.cpp
     theta_union_experimental_test.cpp
diff --git a/tuple/test/tuple_sketch_allocation_test.cpp b/tuple/test/tuple_sketch_allocation_test.cpp
new file mode 100644
index 0000000..b9f969d
--- /dev/null
+++ b/tuple/test/tuple_sketch_allocation_test.cpp
@@ -0,0 +1,61 @@
+/*
+ * 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 <iostream>
+
+#include <catch.hpp>
+#include <tuple_sketch.hpp>
+#include <test_allocator.hpp>
+
+namespace datasketches {
+
+using update_tuple_sketch_int_alloc =
+    update_tuple_sketch<int, int, default_update_policy<int, int>, serde<int>, test_allocator<int>>;
+
+TEST_CASE("tuple sketch with test allocator: exact mode", "[tuple_sketch]") {
+  test_allocator_total_bytes = 0;
+  test_allocator_net_allocations = 0;
+  {
+    auto update_sketch = update_tuple_sketch_int_alloc::builder().build();
+    for (int i = 0; i < 10000; ++i) update_sketch.update(i, 1);
+    for (int i = 0; i < 10000; ++i) update_sketch.update(i, 1);
+    REQUIRE(!update_sketch.is_empty());
+    REQUIRE(update_sketch.is_estimation_mode());
+    unsigned count = 0;
+    for (const auto& entry: update_sketch) {
+      REQUIRE(entry.second == 2);
+      ++count;
+    }
+    REQUIRE(count == update_sketch.get_num_retained());
+
+    auto compact_sketch = update_sketch.compact();
+    REQUIRE(!compact_sketch.is_empty());
+    REQUIRE(compact_sketch.is_estimation_mode());
+    count = 0;
+    for (const auto& entry: compact_sketch) {
+      REQUIRE(entry.second == 2);
+      ++count;
+    }
+    REQUIRE(count == update_sketch.get_num_retained());
+  }
+  REQUIRE(test_allocator_total_bytes == 0);
+  REQUIRE(test_allocator_net_allocations == 0);
+}
+
+} /* namespace datasketches */


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org