Basic Push and Pop operations on total_num_values integers.
| 31 | |
| 32 | // Basic Push and Pop operations on total_num_values integers. |
| 33 | void TestInt(int total_num_values) { |
| 34 | |
| 35 | IntComparator int_comparator; |
| 36 | PriorityQueue<int, IntComparator> queue(int_comparator); |
| 37 | int values[total_num_values]; |
| 38 | |
| 39 | // Setup a total of total_num_values distinct integers in arrayvalues[]. |
| 40 | for (int i = 0; i < total_num_values; i++) { |
| 41 | values[i] = i; |
| 42 | } |
| 43 | // Randomize all elements in values[]. |
| 44 | srand(19); |
| 45 | for (int i = 0; i < total_num_values - 1; i++) { |
| 46 | // find an element to swap with the current element at i. |
| 47 | int index = i + rand() % (total_num_values - i - 1) + 1; |
| 48 | // |
| 49 | ASSERT_GE(index, i + 1); |
| 50 | ASSERT_LE(index, total_num_values - 1); |
| 51 | // swap elements at i and index |
| 52 | int t = values[i]; |
| 53 | values[i] = values[index]; |
| 54 | values[index] = t; |
| 55 | } |
| 56 | // Push all elements in values[] into the heap. |
| 57 | for (int i = 0; i < total_num_values; i++) { |
| 58 | queue.Push(values[i]); |
| 59 | } |
| 60 | // Checkout the heap size and allocation capacity. |
| 61 | ASSERT_EQ(queue.Size(), total_num_values); |
| 62 | // Pop each element out. Since this is a max-heap, we expect to see |
| 63 | // elements poped in descending order. |
| 64 | int v = -1; |
| 65 | for (int i = 0; i < total_num_values; i++) { |
| 66 | v = queue.Pop(); |
| 67 | ASSERT_EQ(v, total_num_values - i - 1); |
| 68 | ASSERT_EQ(queue.Size(), total_num_values - i - 1); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | TEST(PriorityQueueTest, TestBasic) { |
| 73 | TestInt(100); |