/Users/deen/code/yugabyte-db/src/yb/util/priority_queue-test.cc
Line | Count | Source |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include <queue> |
15 | | |
16 | | #include <gtest/gtest.h> |
17 | | |
18 | | #include "yb/util/priority_queue.h" |
19 | | #include "yb/util/random_util.h" |
20 | | |
21 | | namespace yb { |
22 | | |
23 | 1 | TEST(PriorityQueueTest, Simple) { |
24 | 1 | PriorityQueue<int> pq; |
25 | 1 | ASSERT_TRUE(pq.empty()); |
26 | 1 | pq.Push(3); |
27 | 1 | ASSERT_FALSE(pq.empty()); |
28 | 1 | pq.Push(5); |
29 | 1 | pq.Push(2); |
30 | 1 | pq.Push(1); |
31 | 1 | pq.Push(4); |
32 | | |
33 | 1 | ASSERT_EQ(pq.Pop(), 5); |
34 | 1 | ASSERT_EQ(pq.Pop(), 4); |
35 | 1 | ASSERT_EQ(pq.Pop(), 3); |
36 | 1 | ASSERT_EQ(pq.Pop(), 2); |
37 | 1 | ASSERT_EQ(pq.Pop(), 1); |
38 | 1 | ASSERT_TRUE(pq.empty()); |
39 | 1 | } |
40 | | |
41 | 1 | TEST(PriorityQueueTest, Swap) { |
42 | 1 | PriorityQueue<int> pq; |
43 | 1 | pq.Push(3); |
44 | 1 | pq.Push(2); |
45 | 1 | pq.Push(1); |
46 | | |
47 | 1 | std::vector<int> vector = {6, 5, 4}; |
48 | | |
49 | 1 | pq.Swap(&vector); |
50 | 1 | ASSERT_EQ(vector.size(), 3); |
51 | 1 | ASSERT_EQ(vector[0], 3); |
52 | | |
53 | 1 | ASSERT_EQ(pq.Pop(), 6); |
54 | 1 | ASSERT_EQ(pq.Pop(), 5); |
55 | 1 | ASSERT_EQ(pq.Pop(), 4); |
56 | 1 | } |
57 | | |
58 | 1 | TEST(PriorityQueueTest, RemoveIf) { |
59 | 1 | PriorityQueue<int> pq; |
60 | 11 | for (int i = 1; i <= 10; ++i) { |
61 | 10 | pq.Push(i); |
62 | 10 | } |
63 | 10 | pq.RemoveIf([](int* v) { return (*v & 1) == 0; }); |
64 | | |
65 | 1 | ASSERT_EQ(pq.Pop(), 9); |
66 | 1 | ASSERT_EQ(pq.Pop(), 7); |
67 | 1 | ASSERT_EQ(pq.Pop(), 5); |
68 | 1 | ASSERT_EQ(pq.Pop(), 3); |
69 | 1 | ASSERT_EQ(pq.Pop(), 1); |
70 | 1 | } |
71 | | |
72 | 1 | TEST(PriorityQueueTest, RemoveNone) { |
73 | 1 | PriorityQueue<int> pq; |
74 | 6 | for (int i = 1; i <= 5; ++i) { |
75 | 5 | pq.Push(i); |
76 | 5 | } |
77 | 5 | pq.RemoveIf([](int* v) { return false; }); |
78 | | |
79 | 1 | ASSERT_EQ(pq.Pop(), 5); |
80 | 1 | ASSERT_EQ(pq.Pop(), 4); |
81 | 1 | ASSERT_EQ(pq.Pop(), 3); |
82 | 1 | ASSERT_EQ(pq.Pop(), 2); |
83 | 1 | ASSERT_EQ(pq.Pop(), 1); |
84 | 1 | } |
85 | | |
86 | 1 | TEST(PriorityQueueTest, RemoveAll) { |
87 | 1 | PriorityQueue<int> pq; |
88 | 6 | for (int i = 1; i <= 5; ++i) { |
89 | 5 | pq.Push(i); |
90 | 5 | } |
91 | 5 | pq.RemoveIf([](int* v) { return true; }); |
92 | | |
93 | 1 | ASSERT_TRUE(pq.empty()); |
94 | 1 | } |
95 | | |
96 | 1 | TEST(PriorityQueueTest, Random) { |
97 | 1 | PriorityQueue<int> pq; |
98 | 1 | std::priority_queue<int> std_pq; |
99 | | |
100 | 1.00k | for (int i = 0; i != 1000; ++i) { |
101 | 1.00k | ASSERT_EQ(pq.empty(), std_pq.empty()); |
102 | 1.00k | if (pq.empty() || RandomUniformInt(0, 1) == 0) { |
103 | 505 | int value = RandomUniformInt<int>(-1000, 1000); |
104 | 505 | pq.Push(value); |
105 | 505 | std_pq.push(value); |
106 | 495 | } else { |
107 | 495 | ASSERT_EQ(pq.Pop(), std_pq.top()); |
108 | 495 | std_pq.pop(); |
109 | 495 | } |
110 | 1.00k | } |
111 | 1 | } |
112 | | |
113 | | } // namespace yb |