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

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

commit b16fce9f790f76f5331cb1a6aa3f781f5159cc06
Author: AlexanderSaydakov <[email protected]>
AuthorDate: Fri May 8 09:07:35 2020 -0700

    converted asserts to exceptions
---
 kll/include/kll_quantile_calculator_impl.hpp | 13 ++++++-------
 kll/include/kll_sketch_impl.hpp              | 10 +++++-----
 2 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/kll/include/kll_quantile_calculator_impl.hpp 
b/kll/include/kll_quantile_calculator_impl.hpp
index 2e3a422..d4f4c04 100644
--- a/kll/include/kll_quantile_calculator_impl.hpp
+++ b/kll/include/kll_quantile_calculator_impl.hpp
@@ -22,7 +22,6 @@
 
 #include <memory>
 #include <cmath>
-#include <assert.h>
 
 #include "kll_helper.hpp"
 
@@ -80,7 +79,7 @@ void kll_quantile_calculator<T, C, 
A>::populate_from_sketch(const T* items, uint
 
 template <typename T, typename C, typename A>
 T kll_quantile_calculator<T, C, 
A>::approximately_answer_positional_query(uint64_t pos) const {
-  assert (pos < n_);
+  if (pos >= n_) throw std::logic_error("position out of range");
   const uint32_t weights_size(levels_[num_levels_] + 1);
   const uint32_t index = chunk_containing_pos(weights_, weights_size, pos);
   return items_[index];
@@ -104,10 +103,10 @@ uint64_t kll_quantile_calculator<T, C, 
A>::pos_of_phi(double phi, uint64_t n) {
 
 template <typename T, typename C, typename A>
 uint32_t kll_quantile_calculator<T, C, A>::chunk_containing_pos(uint64_t* 
weights, uint32_t weights_size, uint64_t pos) {
-  assert (weights_size > 1); // remember, weights_ contains an "extra" position
+  if (weights_size <= 1) throw std::logic_error("weights array too short"); // 
weights_ contains an "extra" position
   const uint32_t nominal_length(weights_size - 1);
-  assert (weights[0] <= pos);
-  assert (pos < weights[nominal_length]);
+  if (pos < weights[0]) throw std::logic_error("position too small");
+  if (pos >= weights[nominal_length]) throw std::logic_error("position too 
large");
   return search_for_chunk_containing_pos(weights, pos, 0, nominal_length);
 }
 
@@ -145,8 +144,8 @@ void kll_quantile_calculator<T, C, 
A>::blocky_tandem_merge_sort_recursion(T* ite
   if (num_levels == 1) return;
   const uint8_t num_levels_1 = num_levels / 2;
   const uint8_t num_levels_2 = num_levels - num_levels_1;
-  assert (num_levels_1 >= 1);
-  assert (num_levels_2 >= num_levels_1);
+  if (num_levels_1 < 1) throw std::logic_error("level above 0 expected");
+  if (num_levels_2 < num_levels_1) throw std::logic_error("wrong order of 
levels");
   const uint8_t starting_level_1 = starting_level;
   const uint8_t starting_level_2 = starting_level + num_levels_1;
   // swap roles of src and dst
diff --git a/kll/include/kll_sketch_impl.hpp b/kll/include/kll_sketch_impl.hpp
index ff9ac0f..1f08e4b 100644
--- a/kll/include/kll_sketch_impl.hpp
+++ b/kll/include/kll_sketch_impl.hpp
@@ -685,7 +685,7 @@ void kll_sketch<T, C, S, A>::compress_while_updating(void) {
   }
 
   // verify that we freed up half_adj_pop array slots just below the current 
level
-  assert (levels_[level] == (raw_beg + half_adj_pop));
+  if (levels_[level] != (raw_beg + half_adj_pop)) throw 
std::logic_error("compaction error");
 
   // finally, we need to shift up the data in the levels below
   // so that the freed-up space can be used by level zero
@@ -701,7 +701,7 @@ template<typename T, typename C, typename S, typename A>
 uint8_t kll_sketch<T, C, S, A>::find_level_to_compact() const {
   uint8_t level = 0;
   while (true) {
-    assert (level < num_levels_);
+    if (level >= num_levels_) throw std::logic_error("capacity calculation 
error");
     const uint32_t pop = levels_[level + 1] - levels_[level];
     const uint32_t cap = kll_helper::level_capacity(k_, num_levels_, level, 
m_);
     if (pop >= cap) {
@@ -716,8 +716,8 @@ void kll_sketch<T, C, S, 
A>::add_empty_top_level_to_completely_full_sketch() {
   const uint32_t cur_total_cap = levels_[num_levels_];
 
   // make sure that we are following a certain growth scheme
-  assert (levels_[0] == 0);
-  assert (items_size_ == cur_total_cap);
+  if (levels_[0] != 0) throw std::logic_error("full sketch expected");
+  if (items_size_ != cur_total_cap) throw std::logic_error("current capacity 
mismatch");
 
   // note that merging MIGHT over-grow levels_, in which case we might not 
have to grow it here
   const uint8_t new_levels_size = num_levels_ + 2;
@@ -740,7 +740,7 @@ void kll_sketch<T, C, S, 
A>::add_empty_top_level_to_completely_full_sketch() {
     levels_[i] += delta_cap;
   }
 
-  assert (levels_[num_levels_] == new_total_cap);
+  if (levels_[num_levels_] != new_total_cap) throw std::logic_error("new 
capacity mismatch");
 
   num_levels_++;
   levels_[num_levels_] = new_total_cap; // initialize the new "extra" index at 
the top


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

Reply via email to