michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsTPriorityQueue.h" michael@0: #include michael@0: #include michael@0: michael@0: template michael@0: void michael@0: CheckPopSequence(const nsTPriorityQueue& aQueue, michael@0: const T* aExpectedSequence, const uint32_t aSequenceLength) michael@0: { michael@0: nsTPriorityQueue copy(aQueue); michael@0: michael@0: for (uint32_t i = 0; i < aSequenceLength; i++) { michael@0: if (copy.IsEmpty()) { michael@0: printf("Number of elements in the queue is too short by %d.\n", michael@0: aSequenceLength - i); michael@0: exit(-1); michael@0: } michael@0: michael@0: T pop = copy.Pop(); michael@0: if (pop != aExpectedSequence[i]) { michael@0: printf("Unexpected value in pop sequence at position %d\n", i); michael@0: printf(" Sequence:"); michael@0: for (size_t j = 0; j < aSequenceLength; j++) { michael@0: printf(" %d", aExpectedSequence[j]); michael@0: if (j == i) { michael@0: printf("**"); michael@0: } michael@0: } michael@0: printf("\n ** Got %d instead\n", pop); michael@0: exit(-1); michael@0: } michael@0: } michael@0: michael@0: if (!copy.IsEmpty()) { michael@0: printf("Number of elements in the queue is too long by %d.\n", michael@0: copy.Length()); michael@0: exit(-1); michael@0: } michael@0: } michael@0: michael@0: template michael@0: class MaxCompare { michael@0: public: michael@0: bool LessThan(const A& a, const A& b) { michael@0: return a > b; michael@0: } michael@0: }; michael@0: michael@0: int main() michael@0: { michael@0: nsTPriorityQueue queue; michael@0: michael@0: NS_ABORT_IF_FALSE(queue.IsEmpty(), "Queue not initially empty"); michael@0: michael@0: queue.Push(8); michael@0: queue.Push(6); michael@0: queue.Push(4); michael@0: queue.Push(2); michael@0: queue.Push(10); michael@0: queue.Push(6); michael@0: NS_ABORT_IF_FALSE(queue.Top() == 2, "Unexpected queue top"); michael@0: NS_ABORT_IF_FALSE(queue.Length() == 6, "Unexpected queue length"); michael@0: NS_ABORT_IF_FALSE(!queue.IsEmpty(), "Queue empty when populated"); michael@0: int expected[] = { 2, 4, 6, 6, 8, 10 }; michael@0: CheckPopSequence(queue, expected, sizeof(expected) / sizeof(expected[0])); michael@0: michael@0: // copy ctor is tested by using CheckPopSequence, but check default assignment michael@0: // operator michael@0: nsTPriorityQueue queue2; michael@0: queue2 = queue; michael@0: CheckPopSequence(queue2, expected, sizeof(expected) / sizeof(expected[0])); michael@0: michael@0: queue.Clear(); michael@0: NS_ABORT_IF_FALSE(queue.IsEmpty(), "Queue not emptied by Clear"); michael@0: michael@0: // try same sequence with a max heap michael@0: nsTPriorityQueue > max_queue; michael@0: max_queue.Push(8); michael@0: max_queue.Push(6); michael@0: max_queue.Push(4); michael@0: max_queue.Push(2); michael@0: max_queue.Push(10); michael@0: max_queue.Push(6); michael@0: NS_ABORT_IF_FALSE(max_queue.Top() == 10, "Unexpected queue top for max heap"); michael@0: int expected_max[] = { 10, 8, 6, 6, 4, 2 }; michael@0: CheckPopSequence(max_queue, expected_max, michael@0: sizeof(expected_max) / sizeof(expected_max[0])); michael@0: michael@0: return 0; michael@0: }