xpcom/tests/TestArray.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/TestArray.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,211 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include <stdio.h>
    1.10 +#include <stdlib.h>
    1.11 +#include "nsISupportsArray.h"
    1.12 +
    1.13 +// {9e70a320-be02-11d1-8031-006008159b5a}
    1.14 +#define NS_IFOO_IID \
    1.15 +  {0x9e70a320, 0xbe02, 0x11d1,    \
    1.16 +    {0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a}}
    1.17 +
    1.18 +namespace TestArray {
    1.19 +
    1.20 +static const bool kExitOnError = true;
    1.21 +
    1.22 +class IFoo : public nsISupports {
    1.23 +public:
    1.24 +
    1.25 +  NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFOO_IID)
    1.26 +
    1.27 +  NS_IMETHOD_(nsrefcnt) RefCnt() = 0;
    1.28 +  NS_IMETHOD_(int32_t) ID() = 0;
    1.29 +};
    1.30 +
    1.31 +NS_DEFINE_STATIC_IID_ACCESSOR(IFoo, NS_IFOO_IID)
    1.32 +
    1.33 +class Foo : public IFoo {
    1.34 +public:
    1.35 +
    1.36 +  Foo(int32_t aID);
    1.37 +
    1.38 +  // nsISupports implementation
    1.39 +  NS_DECL_ISUPPORTS
    1.40 +
    1.41 +  // IFoo implementation
    1.42 +  NS_IMETHOD_(nsrefcnt) RefCnt() { return mRefCnt; }
    1.43 +  NS_IMETHOD_(int32_t) ID() { return mID; }
    1.44 +
    1.45 +  static int32_t gCount;
    1.46 +
    1.47 +  int32_t mID;
    1.48 +
    1.49 +private:
    1.50 +  ~Foo();
    1.51 +};
    1.52 +
    1.53 +int32_t Foo::gCount;
    1.54 +
    1.55 +Foo::Foo(int32_t aID)
    1.56 +{
    1.57 +  mID = aID;
    1.58 +  ++gCount;
    1.59 +  fprintf(stdout, "init: %d (%p), %d total)\n",
    1.60 +          mID, static_cast<void*>(this), gCount);
    1.61 +}
    1.62 +
    1.63 +Foo::~Foo()
    1.64 +{
    1.65 +  --gCount;
    1.66 +  fprintf(stdout, "destruct: %d (%p), %d remain)\n",
    1.67 +          mID, static_cast<void*>(this), gCount);
    1.68 +}
    1.69 +
    1.70 +NS_IMPL_ISUPPORTS(Foo, IFoo)
    1.71 +
    1.72 +const char* AssertEqual(int32_t aValue1, int32_t aValue2)
    1.73 +{
    1.74 +  if (aValue1 == aValue2) {
    1.75 +    return "OK";
    1.76 +  }
    1.77 +  if (kExitOnError) {
    1.78 +    exit(1);
    1.79 +  }
    1.80 +  return "ERROR";
    1.81 +}
    1.82 +
    1.83 +void DumpArray(nsISupportsArray* aArray, int32_t aExpectedCount, int32_t aElementIDs[], int32_t aExpectedTotal)
    1.84 +{
    1.85 +  uint32_t cnt = 0;
    1.86 +#ifdef DEBUG
    1.87 +  nsresult rv =
    1.88 +#endif
    1.89 +    aArray->Count(&cnt);
    1.90 +  NS_ASSERTION(NS_SUCCEEDED(rv), "Count failed");
    1.91 +  int32_t count = cnt;
    1.92 +  int32_t index;
    1.93 +
    1.94 +  fprintf(stdout, "object count %d = %d %s\n", Foo::gCount, aExpectedTotal, 
    1.95 +          AssertEqual(Foo::gCount, aExpectedTotal));
    1.96 +  fprintf(stdout, "array count %d = %d %s\n", count, aExpectedCount,
    1.97 +          AssertEqual(count, aExpectedCount));
    1.98 +  
    1.99 +  for (index = 0; (index < count) && (index < aExpectedCount); index++) {
   1.100 +    IFoo* foo = (IFoo*)(aArray->ElementAt(index));
   1.101 +    fprintf(stdout, "%2d: %d=%d (%p) c: %d %s\n", 
   1.102 +            index, aElementIDs[index], foo->ID(),
   1.103 +            static_cast<void*>(foo), foo->RefCnt() - 1,
   1.104 +            AssertEqual(foo->ID(), aElementIDs[index]));
   1.105 +    foo->Release();
   1.106 +  }
   1.107 +}
   1.108 +
   1.109 +void FillArray(nsISupportsArray* aArray, int32_t aCount)
   1.110 +{
   1.111 +  int32_t index;
   1.112 +  for (index = 0; index < aCount; index++) {
   1.113 +    nsCOMPtr<IFoo> foo = new Foo(index);
   1.114 +    aArray->AppendElement(foo);
   1.115 +  }
   1.116 +}
   1.117 +
   1.118 +}
   1.119 +
   1.120 +using namespace TestArray;
   1.121 +
   1.122 +int main(int argc, char *argv[])
   1.123 +{
   1.124 +  nsISupportsArray* array;
   1.125 +  nsresult  rv;
   1.126 +  
   1.127 +  if (NS_OK == (rv = NS_NewISupportsArray(&array))) {
   1.128 +    FillArray(array, 10);
   1.129 +    fprintf(stdout, "Array created:\n");
   1.130 +    int32_t   fillResult[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
   1.131 +    DumpArray(array, 10, fillResult, 10);
   1.132 +
   1.133 +    // test insert
   1.134 +    IFoo* foo = (IFoo*)array->ElementAt(3);
   1.135 +    foo->Release();  // pre-release to fix ref count for dumps
   1.136 +    array->InsertElementAt(foo, 5);
   1.137 +    fprintf(stdout, "insert 3 at 5:\n");
   1.138 +    int32_t   insertResult[11] = {0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9};
   1.139 +    DumpArray(array, 11, insertResult, 10);
   1.140 +    fprintf(stdout, "insert 3 at 0:\n");
   1.141 +    array->InsertElementAt(foo, 0);
   1.142 +    int32_t   insertResult2[12] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9};
   1.143 +    DumpArray(array, 12, insertResult2, 10);
   1.144 +    fprintf(stdout, "append 3:\n");
   1.145 +    array->AppendElement(foo);
   1.146 +    int32_t   appendResult[13] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9, 3};
   1.147 +    DumpArray(array, 13, appendResult, 10);
   1.148 +
   1.149 +
   1.150 +    // test IndexOf && LastIndexOf
   1.151 +    int32_t expectedIndex[5] = {0, 4, 6, 12, -1};
   1.152 +    int32_t count = 0;
   1.153 +    int32_t index = array->IndexOf(foo);
   1.154 +    fprintf(stdout, "IndexOf(foo): %d=%d %s\n", index, expectedIndex[count], 
   1.155 +            AssertEqual(index, expectedIndex[count]));
   1.156 +    while (-1 != index) {
   1.157 +      count++;
   1.158 +      index = array->IndexOfStartingAt(foo, index + 1);
   1.159 +      if (-1 != index)
   1.160 +        fprintf(stdout, "IndexOf(foo): %d=%d %s\n", index, expectedIndex[count], 
   1.161 +                AssertEqual(index, expectedIndex[count]));
   1.162 +    }
   1.163 +    index = array->LastIndexOf(foo);
   1.164 +    count--;
   1.165 +    fprintf(stdout, "LastIndexOf(foo): %d=%d %s\n", index, expectedIndex[count], 
   1.166 +            AssertEqual(index, expectedIndex[count]));
   1.167 +
   1.168 +    // test ReplaceElementAt
   1.169 +    fprintf(stdout, "ReplaceElementAt(8):\n");
   1.170 +    array->ReplaceElementAt(foo, 8);
   1.171 +    int32_t   replaceResult[13] = {3, 0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3};
   1.172 +    DumpArray(array, 13, replaceResult, 9);
   1.173 +
   1.174 +    // test RemoveElementAt, RemoveElement RemoveLastElement
   1.175 +    fprintf(stdout, "RemoveElementAt(0):\n");
   1.176 +    array->RemoveElementAt(0);
   1.177 +    int32_t   removeResult[12] = {0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3};
   1.178 +    DumpArray(array, 12, removeResult, 9);
   1.179 +    fprintf(stdout, "RemoveElementAt(7):\n");
   1.180 +    array->RemoveElementAt(7);
   1.181 +    int32_t   removeResult2[11] = {0, 1, 2, 3, 4, 3, 5, 7, 8, 9, 3};
   1.182 +    DumpArray(array, 11, removeResult2, 9);
   1.183 +    fprintf(stdout, "RemoveElement(foo):\n");
   1.184 +    array->RemoveElement(foo);
   1.185 +    int32_t   removeResult3[10] = {0, 1, 2, 4, 3, 5, 7, 8, 9, 3};
   1.186 +    DumpArray(array, 10, removeResult3, 9);
   1.187 +    fprintf(stdout, "RemoveLastElement(foo):\n");
   1.188 +    array->RemoveLastElement(foo);
   1.189 +    int32_t   removeResult4[9] = {0, 1, 2, 4, 3, 5, 7, 8, 9};
   1.190 +    DumpArray(array, 9, removeResult4, 9);
   1.191 +
   1.192 +    // test clear
   1.193 +    fprintf(stdout, "clear array:\n");
   1.194 +    array->Clear();
   1.195 +    DumpArray(array, 0, 0, 0);
   1.196 +    fprintf(stdout, "add 4 new:\n");
   1.197 +    FillArray(array, 4);
   1.198 +    DumpArray(array, 4, fillResult, 4);
   1.199 +
   1.200 +    // test compact
   1.201 +    fprintf(stdout, "compact array:\n");
   1.202 +    array->Compact();
   1.203 +    DumpArray(array, 4, fillResult, 4);
   1.204 +
   1.205 +    // test delete
   1.206 +    fprintf(stdout, "release array:\n");
   1.207 +    NS_RELEASE(array);
   1.208 +  }
   1.209 +  else {
   1.210 +    fprintf(stdout, "error can't create array: %x\n", rv);
   1.211 +  }
   1.212 +
   1.213 +  return 0;
   1.214 +}

mercurial