]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/: fix tests for prioritized_queue and wpq 9241/head
authorSamuel Just <sjust@redhat.com>
Fri, 20 May 2016 22:22:56 +0000 (15:22 -0700)
committerSamuel Just <sjust@redhat.com>
Fri, 20 May 2016 22:22:56 +0000 (15:22 -0700)
Introduced: 27b6ec40b93c05fbaa228783e11e2fac8f1a1ee1
Signed-off-by: Samuel Just <sjust@redhat.com>
src/test/common/test_prioritized_queue.cc
src/test/common/test_weighted_priority_queue.cc

index 84ccf9ea613cb70c764e0d90a7ea997bc5403df8..7b29413ac57ba3187cde9496d276e30b28dfce56 100644 (file)
@@ -164,10 +164,17 @@ TEST_F(PrioritizedQueueTest, fairness_by_class) {
 template <typename T>
 struct Greater {
   const T rhs;
-  explicit Greater(const T& v) : rhs(v)
+  std::list<T> *removed;
+  explicit Greater(const T& v, std::list<T> *removed) : rhs(v), removed(removed)
   {}
-  bool operator()(const T& lhs) const {
-    return lhs > rhs;
+  bool operator()(const T& lhs) {
+    if (lhs > rhs) {
+      if (removed)
+       removed->push_back(lhs);
+      return true;
+    } else {
+      return false;
+    }
   }
 };
 
@@ -176,7 +183,7 @@ TEST_F(PrioritizedQueueTest, remove_by_filter) {
   const unsigned max_tokens_per_subqueue = 50;
   PQ pq(max_tokens_per_subqueue, min_cost);
 
-  const Greater<Item> pred(item_size/2);
+  Greater<Item> pred(item_size/2, nullptr);
   unsigned num_to_remove = 0;
   for (unsigned i = 0; i < item_size; i++) {
     const Item& item = items[i];
@@ -186,7 +193,8 @@ TEST_F(PrioritizedQueueTest, remove_by_filter) {
     }
   }
   std::list<Item> removed;
-  pq.remove_by_filter(pred, &removed);
+  Greater<Item> pred2(item_size/2, &removed);
+  pq.remove_by_filter(pred2);
 
   // see if the removed items are expected ones.
   for (std::list<Item>::iterator it = removed.begin();
index f94af71e97edd85b19d07e2723ce5a6790efb6d7..558b80c6e4fa2cdb48341318f7d7f10a071f35cb 100644 (file)
@@ -200,9 +200,16 @@ TEST_F(WeightedPriorityQueueTest, wpq_test_random) {
 template <typename T>
 struct Greater {
   const T rhs;
-  Greater(const T &v) : rhs(v) {}
-  bool operator()(const T &lhs) const {
-    return std::get<2>(lhs) > std::get<2>(rhs);
+  std::list<T> *removed;
+  Greater(const T &v, std::list<T> *removed) : rhs(v), removed(removed) {}
+  bool operator()(const T &lhs) {
+    if (std::get<2>(lhs) > std::get<2>(rhs)) {
+      if (removed)
+       removed->push_back(lhs);
+      return true;
+    } else {
+      return false;
+    }
   }
 };
 
@@ -212,9 +219,9 @@ TEST_F(WeightedPriorityQueueTest, wpq_test_remove_by_filter_null) {
   unsigned num_items = 100;
   fill_queue(wq, strictq, normq, num_items);
   // Pick a value that we didn't enqueue
-  const Greater<Item> pred(std::make_tuple(0, 0, 1 << 17));
   Removed wq_removed;
-  wq.remove_by_filter(pred, &wq_removed);
+  Greater<Item> pred(std::make_tuple(0, 0, 1 << 17), &wq_removed);
+  wq.remove_by_filter(pred);
   EXPECT_EQ(0u, wq_removed.size());
 }
 
@@ -223,7 +230,7 @@ TEST_F(WeightedPriorityQueueTest, wpq_test_remove_by_filter) {
   LQ strictq, normq;
   unsigned num_items = 1000;
   fill_queue(wq, strictq, normq, num_items);
-  const Greater<Item> pred(std::make_tuple(0, 0, (1 << 16) - (1 << 16)/10));
+  Greater<Item> pred2(std::make_tuple(0, 0, (1 << 16) - (1 << 16)/10), nullptr);
   Removed r_strictq, r_normq;
   unsigned num_to_remove = 0;
   // Figure out from what has been queued what we
@@ -234,7 +241,7 @@ TEST_F(WeightedPriorityQueueTest, wpq_test_remove_by_filter) {
         ki != pi->second.end(); ++ki) {
       for (ItemList::iterator li = ki->second.begin();
           li != ki->second.end(); ++li) {
-       if (pred(li->second)) {
+       if (pred2(li->second)) {
          ++num_to_remove;
        }
       }
@@ -246,18 +253,21 @@ TEST_F(WeightedPriorityQueueTest, wpq_test_remove_by_filter) {
         ki != pi->second.end(); ++ki) {
       for (ItemList::iterator li = ki->second.begin();
           li != ki->second.end(); ++li) {
-       if (pred(li->second)) {
+       if (pred2(li->second)) {
          ++num_to_remove;
        }
       }
     }
   }
   Removed wq_removed;
-  wq.remove_by_filter(pred, &wq_removed);
+  Greater<Item> pred(
+    std::make_tuple(0, 0, (1 << 16) - (1 << 16)/10),
+    &wq_removed);
+  wq.remove_by_filter(pred);
   // Check that what was removed was correct
   for (Removed::iterator it = wq_removed.begin();
        it != wq_removed.end(); ++it) {
-    EXPECT_TRUE(pred(*it));
+    EXPECT_TRUE(pred2(*it));
   }
   EXPECT_EQ(num_to_remove, wq_removed.size());
   EXPECT_EQ(num_items - num_to_remove, wq.length());