|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
|
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 |
|
11 template<class T, class Compare> |
|
12 void |
|
13 CheckPopSequence(const nsTPriorityQueue<T, Compare>& aQueue, |
|
14 const T* aExpectedSequence, const uint32_t aSequenceLength) |
|
15 { |
|
16 nsTPriorityQueue<T, Compare> copy(aQueue); |
|
17 |
|
18 for (uint32_t i = 0; i < aSequenceLength; i++) { |
|
19 if (copy.IsEmpty()) { |
|
20 printf("Number of elements in the queue is too short by %d.\n", |
|
21 aSequenceLength - i); |
|
22 exit(-1); |
|
23 } |
|
24 |
|
25 T pop = copy.Pop(); |
|
26 if (pop != aExpectedSequence[i]) { |
|
27 printf("Unexpected value in pop sequence at position %d\n", i); |
|
28 printf(" Sequence:"); |
|
29 for (size_t j = 0; j < aSequenceLength; j++) { |
|
30 printf(" %d", aExpectedSequence[j]); |
|
31 if (j == i) { |
|
32 printf("**"); |
|
33 } |
|
34 } |
|
35 printf("\n ** Got %d instead\n", pop); |
|
36 exit(-1); |
|
37 } |
|
38 } |
|
39 |
|
40 if (!copy.IsEmpty()) { |
|
41 printf("Number of elements in the queue is too long by %d.\n", |
|
42 copy.Length()); |
|
43 exit(-1); |
|
44 } |
|
45 } |
|
46 |
|
47 template<class A> |
|
48 class MaxCompare { |
|
49 public: |
|
50 bool LessThan(const A& a, const A& b) { |
|
51 return a > b; |
|
52 } |
|
53 }; |
|
54 |
|
55 int main() |
|
56 { |
|
57 nsTPriorityQueue<int> queue; |
|
58 |
|
59 NS_ABORT_IF_FALSE(queue.IsEmpty(), "Queue not initially empty"); |
|
60 |
|
61 queue.Push(8); |
|
62 queue.Push(6); |
|
63 queue.Push(4); |
|
64 queue.Push(2); |
|
65 queue.Push(10); |
|
66 queue.Push(6); |
|
67 NS_ABORT_IF_FALSE(queue.Top() == 2, "Unexpected queue top"); |
|
68 NS_ABORT_IF_FALSE(queue.Length() == 6, "Unexpected queue length"); |
|
69 NS_ABORT_IF_FALSE(!queue.IsEmpty(), "Queue empty when populated"); |
|
70 int expected[] = { 2, 4, 6, 6, 8, 10 }; |
|
71 CheckPopSequence(queue, expected, sizeof(expected) / sizeof(expected[0])); |
|
72 |
|
73 // copy ctor is tested by using CheckPopSequence, but check default assignment |
|
74 // operator |
|
75 nsTPriorityQueue<int> queue2; |
|
76 queue2 = queue; |
|
77 CheckPopSequence(queue2, expected, sizeof(expected) / sizeof(expected[0])); |
|
78 |
|
79 queue.Clear(); |
|
80 NS_ABORT_IF_FALSE(queue.IsEmpty(), "Queue not emptied by Clear"); |
|
81 |
|
82 // try same sequence with a max heap |
|
83 nsTPriorityQueue<int, MaxCompare<int> > max_queue; |
|
84 max_queue.Push(8); |
|
85 max_queue.Push(6); |
|
86 max_queue.Push(4); |
|
87 max_queue.Push(2); |
|
88 max_queue.Push(10); |
|
89 max_queue.Push(6); |
|
90 NS_ABORT_IF_FALSE(max_queue.Top() == 10, "Unexpected queue top for max heap"); |
|
91 int expected_max[] = { 10, 8, 6, 6, 4, 2 }; |
|
92 CheckPopSequence(max_queue, expected_max, |
|
93 sizeof(expected_max) / sizeof(expected_max[0])); |
|
94 |
|
95 return 0; |
|
96 } |