xpcom/tests/TestObserverArray.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.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 // vim:cindent:ts=4:et:sw=4:
     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/. */
     7 #include "TestHarness.h"
     8 #include "nsTObserverArray.h"
    10 using namespace mozilla;
    12 typedef nsTObserverArray<int> IntArray;
    14 #define DO_TEST(_type, _exp, _code)                                   \
    15   do {                                                                \
    16     ++testNum;                                                        \
    17     count = 0;                                                        \
    18     IntArray::_type iter(arr);                                        \
    19     while (iter.HasMore() && count != ArrayLength(_exp)) {            \
    20       _code                                                           \
    21       int next = iter.GetNext();                                      \
    22       int expected = _exp[count++];                                   \
    23       if (next != expected) {                                         \
    24         fail("During test %d at position %d got %d expected %d\n",    \
    25              testNum, count-1, next, expected);                       \
    26         rv = 1;                                                       \
    27       }                                                               \
    28     }                                                                 \
    29     if (iter.HasMore()) {                                             \
    30       fail("During test %d, iterator ran over", testNum);             \
    31       rv = 1;                                                         \
    32     }                                                                 \
    33     if (count != ArrayLength(_exp)) {                             \
    34       fail("During test %d, iterator finished too early", testNum);   \
    35       rv = 1;                                                         \
    36     }                                                                 \
    37   } while (0)
    39 int main(int argc, char **argv)
    40 {
    41   ScopedXPCOM xpcom("nsTObserverArrayTests");
    42   if (xpcom.failed()) {
    43     return 1;
    44   }
    46   int rv = 0;
    48   IntArray arr;
    49   arr.AppendElement(3);
    50   arr.AppendElement(4);
    52   size_t count;
    53   int testNum = 0;
    55   // Basic sanity
    56   static int test1Expected[] = { 3, 4 };
    57   DO_TEST(ForwardIterator, test1Expected, { /* nothing */ });
    59   // Appends
    60   static int test2Expected[] = { 3, 4, 2 };
    61   DO_TEST(ForwardIterator, test2Expected,
    62           if (count == 1) arr.AppendElement(2);
    63           );
    64   DO_TEST(ForwardIterator, test2Expected, { /* nothing */ });
    66   DO_TEST(EndLimitedIterator, test2Expected,
    67           if (count == 1) arr.AppendElement(5);
    68           );
    70   static int test5Expected[] = { 3, 4, 2, 5 };
    71   DO_TEST(ForwardIterator, test5Expected, { /* nothing */ });
    73   // Removals
    74   DO_TEST(ForwardIterator, test5Expected,
    75           if (count == 1) arr.RemoveElementAt(0);
    76           );
    78   static int test7Expected[] = { 4, 2, 5 };
    79   DO_TEST(ForwardIterator, test7Expected, { /* nothing */ });
    81   static int test8Expected[] = { 4, 5 };
    82   DO_TEST(ForwardIterator, test8Expected,
    83           if (count == 1) arr.RemoveElementAt(1);
    84           );
    85   DO_TEST(ForwardIterator, test8Expected, { /* nothing */ });
    87   arr.AppendElement(2);
    88   arr.AppendElementUnlessExists(6);
    89   static int test10Expected[] = { 4, 5, 2, 6 };
    90   DO_TEST(ForwardIterator, test10Expected, { /* nothing */ });
    92   arr.AppendElementUnlessExists(5);
    93   DO_TEST(ForwardIterator, test10Expected, { /* nothing */ });
    95   static int test12Expected[] = { 4, 5, 6 };
    96   DO_TEST(ForwardIterator, test12Expected,
    97           if (count == 1) arr.RemoveElementAt(2);
    98           );
    99   DO_TEST(ForwardIterator, test12Expected, { /* nothing */ });
   101   // Removals + Appends
   102   static int test14Expected[] = { 4, 6, 7 };
   103   DO_TEST(ForwardIterator, test14Expected,
   104           if (count == 1) {
   105             arr.RemoveElementAt(1);
   106             arr.AppendElement(7);
   107           }
   108           );
   109   DO_TEST(ForwardIterator, test14Expected, { /* nothing */ });
   111   arr.AppendElement(2);
   112   static int test16Expected[] = { 4, 6, 7, 2 };
   113   DO_TEST(ForwardIterator, test16Expected, { /* nothing */ });
   115   static int test17Expected[] = { 4, 7, 2 };
   116   DO_TEST(EndLimitedIterator, test17Expected,
   117           if (count == 1) {
   118             arr.RemoveElementAt(1);
   119             arr.AppendElement(8);
   120           }
   121           );
   123   static int test18Expected[] = { 4, 7, 2, 8 };
   124   DO_TEST(ForwardIterator, test18Expected, { /* nothing */ });
   126   // Prepends
   127   arr.PrependElementUnlessExists(3);
   128   static int test19Expected[] = { 3, 4, 7, 2, 8 };
   129   DO_TEST(ForwardIterator, test19Expected, { /* nothing */ });
   131   arr.PrependElementUnlessExists(7);
   132   DO_TEST(ForwardIterator, test19Expected, { /* nothing */ });
   134   // Commented out because it fails; bug 474369 will fix
   135   /*  DO_TEST(ForwardIterator, test19Expected,
   136           if (count == 1) {
   137             arr.PrependElementUnlessExists(9);
   138           }
   139           );
   141   static int test22Expected[] = { 9, 3, 4, 7, 2, 8 };
   142   DO_TEST(ForwardIterator, test22Expected, { });
   143   */
   145   return rv;
   146 }

mercurial