xpcom/tests/TestPriorityQueue.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial