/src/mozilla-central/xpcom/tests/gtest/TestPriorityQueue.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "nsTPriorityQueue.h" |
8 | | #include <stdio.h> |
9 | | #include <stdlib.h> |
10 | | #include "gtest/gtest.h" |
11 | | |
12 | | template<class T, class Compare> |
13 | | void |
14 | | CheckPopSequence(const nsTPriorityQueue<T, Compare>& aQueue, |
15 | | const T* aExpectedSequence, const uint32_t aSequenceLength) |
16 | 0 | { |
17 | 0 | nsTPriorityQueue<T, Compare> copy(aQueue); |
18 | 0 |
|
19 | 0 | for (uint32_t i = 0; i < aSequenceLength; i++) { |
20 | 0 | EXPECT_FALSE(copy.IsEmpty()); |
21 | 0 |
|
22 | 0 | T pop = copy.Pop(); |
23 | 0 | EXPECT_EQ(pop, aExpectedSequence[i]); |
24 | 0 | } |
25 | 0 |
|
26 | 0 | EXPECT_TRUE(copy.IsEmpty()); |
27 | 0 | } Unexecuted instantiation: void CheckPopSequence<int, nsDefaultComparator<int, int> >(nsTPriorityQueue<int, nsDefaultComparator<int, int> > const&, int const*, unsigned int) Unexecuted instantiation: void CheckPopSequence<int, MaxCompare<int> >(nsTPriorityQueue<int, MaxCompare<int> > const&, int const*, unsigned int) |
28 | | |
29 | | template<class A> |
30 | | class MaxCompare { |
31 | | public: |
32 | 0 | bool LessThan(const A& a, const A& b) { |
33 | 0 | return a > b; |
34 | 0 | } |
35 | | }; |
36 | | |
37 | | TEST(PriorityQueue, Main) |
38 | 0 | { |
39 | 0 | nsTPriorityQueue<int> queue; |
40 | 0 |
|
41 | 0 | EXPECT_TRUE(queue.IsEmpty()); |
42 | 0 |
|
43 | 0 | queue.Push(8); |
44 | 0 | queue.Push(6); |
45 | 0 | queue.Push(4); |
46 | 0 | queue.Push(2); |
47 | 0 | queue.Push(10); |
48 | 0 | queue.Push(6); |
49 | 0 | EXPECT_EQ(queue.Top(), 2); |
50 | 0 | EXPECT_EQ(queue.Length(), 6u); |
51 | 0 | EXPECT_FALSE(queue.IsEmpty()); |
52 | 0 | int expected[] = { 2, 4, 6, 6, 8, 10 }; |
53 | 0 | CheckPopSequence(queue, expected, sizeof(expected) / sizeof(expected[0])); |
54 | 0 |
|
55 | 0 | // copy ctor is tested by using CheckPopSequence, but check default assignment |
56 | 0 | // operator |
57 | 0 | nsTPriorityQueue<int> queue2; |
58 | 0 | queue2 = queue; |
59 | 0 | CheckPopSequence(queue2, expected, sizeof(expected) / sizeof(expected[0])); |
60 | 0 |
|
61 | 0 | queue.Clear(); |
62 | 0 | EXPECT_TRUE(queue.IsEmpty()); |
63 | 0 |
|
64 | 0 | // try same sequence with a max heap |
65 | 0 | nsTPriorityQueue<int, MaxCompare<int> > max_queue; |
66 | 0 | max_queue.Push(8); |
67 | 0 | max_queue.Push(6); |
68 | 0 | max_queue.Push(4); |
69 | 0 | max_queue.Push(2); |
70 | 0 | max_queue.Push(10); |
71 | 0 | max_queue.Push(6); |
72 | 0 | EXPECT_EQ(max_queue.Top(), 10); |
73 | 0 | int expected_max[] = { 10, 8, 6, 6, 4, 2 }; |
74 | 0 | CheckPopSequence(max_queue, expected_max, |
75 | 0 | sizeof(expected_max) / sizeof(expected_max[0])); |
76 | 0 | } |