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

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


The following commit(s) were added to refs/heads/kll_no_default_construction by 
this push:
     new 90ff7d5  common custom type for testing
90ff7d5 is described below

commit 90ff7d5e9d2ae33347ae7b57421de4cf69d336d7
Author: AlexanderSaydakov <[email protected]>
AuthorDate: Thu Jul 25 12:50:02 2019 -0700

    common custom type for testing
---
 common/test/A.hpp                                  | 103 ++++++++++++++++++
 fi/fi.mk                                           |   2 +-
 fi/test/CMakeLists.txt                             |   5 +
 fi/test/frequent_items_sketch_custom_type_test.cpp |  64 +----------
 kll/kll.mk                                         |   2 +-
 kll/test/CMakeLists.txt                            |   6 ++
 kll/test/kll_sketch_custom_type_test.cpp           | 117 +++++++++++++++++++++
 7 files changed, 234 insertions(+), 65 deletions(-)

diff --git a/common/test/A.hpp b/common/test/A.hpp
new file mode 100644
index 0000000..5c806c0
--- /dev/null
+++ b/common/test/A.hpp
@@ -0,0 +1,103 @@
+/*
+ * 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.
+ */
+
+#ifndef CLASS_A_HPP_
+#define CLASS_A_HPP_
+
+#include <iostream>
+
+namespace datasketches {
+
+class A {
+  static const bool DEBUG = false;
+public:
+  // no default constructor should be required
+  A(int value): value(value) {
+    if (DEBUG) std::cerr << "A constructor" << std::endl;
+  }
+  ~A() {
+    if (DEBUG) std::cerr << "A destructor" << std::endl;
+  }
+  A(const A& other): value(other.value) {
+    if (DEBUG) std::cerr << "A copy constructor" << std::endl;
+  }
+  // noexcept is important here so that, for instance, std::vector could move 
this type
+  A(A&& other) noexcept : value(other.value) {
+    if (DEBUG) std::cerr << "A move constructor" << std::endl;
+  }
+  A& operator=(const A& other) {
+    if (DEBUG) std::cerr << "A copy assignment" << std::endl;
+    value = other.value;
+    return *this;
+  }
+  A& operator=(A&& other) {
+    if (DEBUG) std::cerr << "A move assignment" << std::endl;
+    value = other.value;
+    return *this;
+  }
+  int get_value() const { return value; }
+private:
+  int value;
+};
+
+struct hashA {
+  std::size_t operator()(const A& a) const {
+    return std::hash<int>()(a.get_value());
+  }
+};
+
+struct equalA {
+  bool operator()(const A& a1, const A& a2) const {
+    return a1.get_value() == a2.get_value();
+  }
+};
+
+struct lessA {
+  bool operator()(const A& a1, const A& a2) const {
+    return a1.get_value() < a2.get_value();
+  }
+};
+
+struct serdeA {
+  void serialize(std::ostream& os, const A* items, unsigned num) {
+    for (unsigned i = 0; i < num; i++) {
+      const int value = items[i].get_value();
+      os.write((char*)&value, sizeof(value));
+    }
+  }
+  void deserialize(std::istream& is, A* items, unsigned num) {
+    for (unsigned i = 0; i < num; i++) {
+      int value;
+      is.read((char*)&value, sizeof(value));
+      new (&items[i]) A(value);
+    }
+  }
+  size_t size_of_item(const A& item) {
+    return sizeof(int);
+  }
+};
+
+std::ostream& operator<<(std::ostream& os, const A& a) {
+  os << a.get_value();
+  return os;
+}
+
+} /* namespace datasketches */
+
+#endif
diff --git a/fi/fi.mk b/fi/fi.mk
index 7a990e6..f2dc446 100644
--- a/fi/fi.mk
+++ b/fi/fi.mk
@@ -10,7 +10,7 @@ FI_TARGET := fi/$(FI_TEST_BIN)
 FI_TSTSOURCES := $(shell find $(FI_TSTDIR) -type f -name "*.cpp")
 FI_TSTOBJS := $(patsubst 
$(FI_TSTDIR)/%,$(FI_TSTBUILD)/%,$(FI_TSTSOURCES:.cpp=.o))
 
-FI_INCLIST := $(COM_INCLIST) -I $(FI_INCDIR)
+FI_INCLIST := $(COM_INCLIST) -I $(FI_INCDIR) -I common/test
 
 $(FI_TSTBUILD)/%.o: $(FI_TSTDIR)/%.cpp
        @mkdir -p $(FI_TSTBUILD)
diff --git a/fi/test/CMakeLists.txt b/fi/test/CMakeLists.txt
index 4feba3a..126ae75 100644
--- a/fi/test/CMakeLists.txt
+++ b/fi/test/CMakeLists.txt
@@ -24,3 +24,8 @@ target_sources(fi_test
     frequent_items_sketch_test.cpp
     frequent_items_sketch_custom_type_test.cpp
 )
+
+target_include_directories(fi_test
+  PRIVATE
+    ../../common/test
+)
diff --git a/fi/test/frequent_items_sketch_custom_type_test.cpp 
b/fi/test/frequent_items_sketch_custom_type_test.cpp
index 9d715d9..4e7a794 100644
--- a/fi/test/frequent_items_sketch_custom_type_test.cpp
+++ b/fi/test/frequent_items_sketch_custom_type_test.cpp
@@ -21,72 +21,10 @@
 #include <cppunit/extensions/HelperMacros.h>
 
 #include <frequent_items_sketch.hpp>
+#include <A.hpp>
 
 namespace datasketches {
 
-class A {
-public:
-  A(int value): value(value) {
-    std::cerr << "A constructor" << std::endl;
-  }
-  ~A() {
-    std::cerr << "A destructor" << std::endl;
-  }
-  A(const A& other): value(other.value) {
-    std::cerr << "A copy constructor" << std::endl;
-  }
-  // noexcept is important here so that std::vector can move this type
-  A(A&& other) noexcept : value(other.value) {
-    std::cerr << "A move constructor" << std::endl;
-  }
-  A& operator=(const A& other) {
-    std::cerr << "A copy assignment" << std::endl;
-    value = other.value;
-    return *this;
-  }
-  A& operator=(A&& other) {
-    std::cerr << "A move assignment" << std::endl;
-    value = other.value;
-    return *this;
-  }
-  int get_value() const { return value; }
-private:
-  int value;
-};
-
-struct hashA {
-  std::size_t operator()(const A& a) const {
-    return std::hash<int>()(a.get_value());
-  }
-};
-
-struct equalA {
-  bool operator()(const A& a1, const A& a2) const {
-    return a1.get_value() == a2.get_value();
-  }
-};
-
-struct serdeA {
-  void serialize(std::ostream& os, const A* items, unsigned num) {
-    for (unsigned i = 0; i < num; i++) {
-      const int value = items[i].get_value();
-      os.write((char*)&value, sizeof(value));
-    }
-  }
-  void deserialize(std::istream& is, A* items, unsigned num) {
-    for (unsigned i = 0; i < num; i++) {
-      int value;
-      is.read((char*)&value, sizeof(value));
-      new (&items[i]) A(value);
-    }
-  }
-};
-
-std::ostream& operator<<(std::ostream& os, const A& a) {
-  os << a.get_value();
-  return os;
-}
-
 typedef frequent_items_sketch<A, hashA, equalA, serdeA> frequent_A_sketch;
 
 class frequent_items_sketch_custom_type_test: public CppUnit::TestFixture {
diff --git a/kll/kll.mk b/kll/kll.mk
index b574589..eae0ad2 100644
--- a/kll/kll.mk
+++ b/kll/kll.mk
@@ -10,7 +10,7 @@ KLL_TARGET := kll/$(KLL_TEST_BIN)
 KLL_TSTSOURCES := $(shell find $(KLL_TSTDIR) -type f -name "*.cpp")
 KLL_TSTOBJS := $(patsubst 
$(KLL_TSTDIR)/%,$(KLL_TSTBUILD)/%,$(KLL_TSTSOURCES:.cpp=.o))
 
-KLL_INCLIST := $(COM_INCLIST) -I $(KLL_INCDIR)
+KLL_INCLIST := $(COM_INCLIST) -I $(KLL_INCDIR) -I common/test
 
 $(KLL_TSTBUILD)/%.o: $(KLL_TSTDIR)/%.cpp
        @mkdir -p $(KLL_TSTBUILD)
diff --git a/kll/test/CMakeLists.txt b/kll/test/CMakeLists.txt
index 9cf540f..486f222 100644
--- a/kll/test/CMakeLists.txt
+++ b/kll/test/CMakeLists.txt
@@ -21,5 +21,11 @@ add_test(
 target_sources(kll_test
   PRIVATE
     kll_sketch_test.cpp
+    kll_sketch_custom_type_test.cpp
     kll_sketch_validation.cpp
 )
+
+target_include_directories(kll_test
+  PRIVATE
+    ../../common/test
+)
diff --git a/kll/test/kll_sketch_custom_type_test.cpp 
b/kll/test/kll_sketch_custom_type_test.cpp
new file mode 100644
index 0000000..b781714
--- /dev/null
+++ b/kll/test/kll_sketch_custom_type_test.cpp
@@ -0,0 +1,117 @@
+/*
+ * 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 <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <kll_sketch.hpp>
+#include <A.hpp>
+
+namespace datasketches {
+
+typedef kll_sketch<A, lessA, serdeA> kll_sketch_a;
+
+class kll_sketch_custom_type_test: public CppUnit::TestFixture {
+
+  CPPUNIT_TEST_SUITE(kll_sketch_custom_type_test);
+  CPPUNIT_TEST(compact_level_zero);
+  CPPUNIT_TEST(merge_small);
+  CPPUNIT_TEST(merge_higher_levels);
+  CPPUNIT_TEST_SUITE_END();
+
+  void compact_level_zero() {
+    kll_sketch_a sketch(8);
+    CPPUNIT_ASSERT_THROW(sketch.get_quantile(0), std::runtime_error);
+    CPPUNIT_ASSERT_THROW(sketch.get_min_value(), std::runtime_error);
+    CPPUNIT_ASSERT_THROW(sketch.get_max_value(), std::runtime_error);
+    CPPUNIT_ASSERT_EQUAL(8u, sketch.get_serialized_size_bytes());
+
+    sketch.update(1);
+    sketch.update(2);
+    sketch.update(3);
+    sketch.update(4);
+    sketch.update(5);
+    sketch.update(6);
+    sketch.update(7);
+    sketch.update(8);
+    sketch.update(9);
+
+    //sketch.to_stream(std::cout);
+
+    CPPUNIT_ASSERT(sketch.is_estimation_mode());
+    CPPUNIT_ASSERT(sketch.get_n() > sketch.get_num_retained());
+    CPPUNIT_ASSERT_EQUAL(1, sketch.get_min_value().get_value());
+    CPPUNIT_ASSERT_EQUAL(9, sketch.get_max_value().get_value());
+  }
+
+  void merge_small() {
+    kll_sketch_a sketch1(8);
+    sketch1.update(1);
+
+    kll_sketch_a sketch2(8);
+    sketch2.update(2);
+
+    sketch2.merge(sketch1);
+
+    //sketch2.to_stream(std::cout);
+
+    CPPUNIT_ASSERT(!sketch2.is_estimation_mode());
+    CPPUNIT_ASSERT_EQUAL((int) sketch2.get_n(), (int) 
sketch2.get_num_retained());
+    CPPUNIT_ASSERT_EQUAL(1, sketch2.get_min_value().get_value());
+    CPPUNIT_ASSERT_EQUAL(2, sketch2.get_max_value().get_value());
+  }
+
+  void merge_higher_levels() {
+    kll_sketch_a sketch1(8);
+    sketch1.update(1);
+    sketch1.update(2);
+    sketch1.update(3);
+    sketch1.update(4);
+    sketch1.update(5);
+    sketch1.update(6);
+    sketch1.update(7);
+    sketch1.update(8);
+    sketch1.update(9);
+
+    kll_sketch_a sketch2(8);
+    sketch2.update(10);
+    sketch2.update(11);
+    sketch2.update(12);
+    sketch2.update(13);
+    sketch2.update(14);
+    sketch2.update(15);
+    sketch2.update(16);
+    sketch2.update(17);
+    sketch2.update(18);
+
+    sketch2.merge(sketch1);
+
+    //sketch2.to_stream(std::cout);
+
+    CPPUNIT_ASSERT(sketch2.is_estimation_mode());
+    CPPUNIT_ASSERT(sketch2.get_n() > sketch2.get_num_retained());
+    CPPUNIT_ASSERT_EQUAL(1, sketch2.get_min_value().get_value());
+    CPPUNIT_ASSERT_EQUAL(18, sketch2.get_max_value().get_value());
+  }
+
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(kll_sketch_custom_type_test);
+
+} /* namespace datasketches */


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to