summaryrefslogtreecommitdiffstats
path: root/xpcom/tests/gtest/TestCOMArray.cpp
blob: 9e29e152301499aa945fdae084e0e65f83b4fb17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
// vim:cindent:ts=4:et:sw=4:
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsCOMArray.h"
#include "nsCOMPtr.h"
#include "nsISupports.h"
#include "gtest/gtest.h"

// {9e70a320-be02-11d1-8031-006008159b5a}
#define NS_IFOO_IID                                  \
  {                                                  \
    0x9e70a320, 0xbe02, 0x11d1, {                    \
      0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a \
    }                                                \
  }

class IFoo : public nsISupports {
 public:
  NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFOO_IID)

  NS_IMETHOD_(MozExternalRefCountType) RefCnt() = 0;
  NS_IMETHOD_(int32_t) ID() = 0;
};

NS_DEFINE_STATIC_IID_ACCESSOR(IFoo, NS_IFOO_IID)

class Foo final : public IFoo {
  ~Foo();

 public:
  explicit Foo(int32_t aID);

  // nsISupports implementation
  NS_DECL_ISUPPORTS

  // IFoo implementation
  NS_IMETHOD_(MozExternalRefCountType) RefCnt() override { return mRefCnt; }
  NS_IMETHOD_(int32_t) ID() override { return mID; }

  static int32_t gCount;

  int32_t mID;
};

int32_t Foo::gCount = 0;

Foo::Foo(int32_t aID) {
  mID = aID;
  ++gCount;
}

Foo::~Foo() { --gCount; }

NS_IMPL_ISUPPORTS(Foo, IFoo)

// {0e70a320-be02-11d1-8031-006008159b5a}
#define NS_IBAR_IID                                  \
  {                                                  \
    0x0e70a320, 0xbe02, 0x11d1, {                    \
      0x80, 0x31, 0x00, 0x60, 0x08, 0x15, 0x9b, 0x5a \
    }                                                \
  }

class IBar : public nsISupports {
 public:
  NS_DECLARE_STATIC_IID_ACCESSOR(NS_IBAR_IID)
};

NS_DEFINE_STATIC_IID_ACCESSOR(IBar, NS_IBAR_IID)

class Bar final : public IBar {
 public:
  explicit Bar(nsCOMArray<IBar>& aArray);

  // nsISupports implementation
  NS_DECL_ISUPPORTS

  static int32_t sReleaseCalled;

 private:
  ~Bar();

  nsCOMArray<IBar>& mArray;
};

int32_t Bar::sReleaseCalled = 0;

typedef nsCOMArray<IBar> Array2;

Bar::Bar(Array2& aArray) : mArray(aArray) {}

Bar::~Bar() { EXPECT_FALSE(mArray.RemoveObject(this)); }

NS_IMPL_ADDREF(Bar)
NS_IMPL_QUERY_INTERFACE(Bar, IBar)

NS_IMETHODIMP_(MozExternalRefCountType)
Bar::Release(void) {
  ++Bar::sReleaseCalled;
  EXPECT_GT(int(mRefCnt), 0);
  NS_ASSERT_OWNINGTHREAD(_class);
  --mRefCnt;
  NS_LOG_RELEASE(this, mRefCnt, "Bar");
  if (mRefCnt == 0) {
    mRefCnt = 1; /* stabilize */
    delete this;
    return 0;
  }
  return mRefCnt;
}

TEST(COMArray, Sizing)
{
  nsCOMArray<IFoo> arr;

  for (int32_t i = 0; i < 20; ++i) {
    nsCOMPtr<IFoo> foo = new Foo(i);
    arr.AppendObject(foo);
  }

  ASSERT_EQ(arr.Count(), int32_t(20));
  ASSERT_EQ(Foo::gCount, int32_t(20));

  arr.TruncateLength(10);

  ASSERT_EQ(arr.Count(), int32_t(10));
  ASSERT_EQ(Foo::gCount, int32_t(10));

  arr.SetCount(30);

  ASSERT_EQ(arr.Count(), int32_t(30));
  ASSERT_EQ(Foo::gCount, int32_t(10));

  for (int32_t i = 0; i < 10; ++i) {
    ASSERT_NE(arr[i], nullptr);
  }

  for (int32_t i = 10; i < 30; ++i) {
    ASSERT_EQ(arr[i], nullptr);
  }
}

TEST(COMArray, ObjectFunctions)
{
  int32_t base;
  {
    nsCOMArray<IBar> arr2;

    IBar *thirdObject = nullptr, *fourthObject = nullptr,
         *fifthObject = nullptr, *ninthObject = nullptr;
    for (int32_t i = 0; i < 20; ++i) {
      nsCOMPtr<IBar> bar = new Bar(arr2);
      switch (i) {
        case 2:
          thirdObject = bar;
          break;
        case 3:
          fourthObject = bar;
          break;
        case 4:
          fifthObject = bar;
          break;
        case 8:
          ninthObject = bar;
          break;
      }
      arr2.AppendObject(bar);
    }

    base = Bar::sReleaseCalled;

    arr2.SetCount(10);
    ASSERT_EQ(Bar::sReleaseCalled, base + 10);
    ASSERT_EQ(arr2.Count(), int32_t(10));

    arr2.RemoveObjectAt(9);
    ASSERT_EQ(Bar::sReleaseCalled, base + 11);
    ASSERT_EQ(arr2.Count(), int32_t(9));

    arr2.RemoveObject(ninthObject);
    ASSERT_EQ(Bar::sReleaseCalled, base + 12);
    ASSERT_EQ(arr2.Count(), int32_t(8));

    arr2.RemoveObjectsAt(2, 3);
    ASSERT_EQ(Bar::sReleaseCalled, base + 15);
    ASSERT_EQ(arr2.Count(), int32_t(5));
    for (int32_t j = 0; j < arr2.Count(); ++j) {
      ASSERT_NE(arr2.ObjectAt(j), thirdObject);
      ASSERT_NE(arr2.ObjectAt(j), fourthObject);
      ASSERT_NE(arr2.ObjectAt(j), fifthObject);
    }

    arr2.RemoveObjectsAt(4, 1);
    ASSERT_EQ(Bar::sReleaseCalled, base + 16);
    ASSERT_EQ(arr2.Count(), int32_t(4));

    arr2.Clear();
    ASSERT_EQ(Bar::sReleaseCalled, base + 20);
  }
}

TEST(COMArray, ElementFunctions)
{
  int32_t base;
  {
    nsCOMArray<IBar> arr2;

    IBar *thirdElement = nullptr, *fourthElement = nullptr,
         *fifthElement = nullptr, *ninthElement = nullptr;
    for (int32_t i = 0; i < 20; ++i) {
      nsCOMPtr<IBar> bar = new Bar(arr2);
      switch (i) {
        case 2:
          thirdElement = bar;
          break;
        case 3:
          fourthElement = bar;
          break;
        case 4:
          fifthElement = bar;
          break;
        case 8:
          ninthElement = bar;
          break;
      }
      arr2.AppendElement(bar);
    }

    base = Bar::sReleaseCalled;

    arr2.TruncateLength(10);
    ASSERT_EQ(Bar::sReleaseCalled, base + 10);
    ASSERT_EQ(arr2.Length(), uint32_t(10));

    arr2.RemoveElementAt(9);
    ASSERT_EQ(Bar::sReleaseCalled, base + 11);
    ASSERT_EQ(arr2.Length(), uint32_t(9));

    arr2.RemoveElement(ninthElement);
    ASSERT_EQ(Bar::sReleaseCalled, base + 12);
    ASSERT_EQ(arr2.Length(), uint32_t(8));

    arr2.RemoveElementsAt(2, 3);
    ASSERT_EQ(Bar::sReleaseCalled, base + 15);
    ASSERT_EQ(arr2.Length(), uint32_t(5));
    for (uint32_t j = 0; j < arr2.Length(); ++j) {
      ASSERT_NE(arr2.ElementAt(j), thirdElement);
      ASSERT_NE(arr2.ElementAt(j), fourthElement);
      ASSERT_NE(arr2.ElementAt(j), fifthElement);
    }

    arr2.RemoveElementsAt(4, 1);
    ASSERT_EQ(Bar::sReleaseCalled, base + 16);
    ASSERT_EQ(arr2.Length(), uint32_t(4));

    arr2.Clear();
    ASSERT_EQ(Bar::sReleaseCalled, base + 20);
  }
}

TEST(COMArray, Destructor)
{
  int32_t base;
  Bar::sReleaseCalled = 0;

  {
    nsCOMArray<IBar> arr2;

    for (int32_t i = 0; i < 20; ++i) {
      nsCOMPtr<IBar> bar = new Bar(arr2);
      arr2.AppendObject(bar);
    }

    base = Bar::sReleaseCalled;

    // Let arr2 be destroyed
  }
  ASSERT_EQ(Bar::sReleaseCalled, base + 20);
}

TEST(COMArray, ConvertIteratorToConstIterator)
{
  nsCOMArray<IFoo> array;

  for (int32_t i = 0; i < 20; ++i) {
    nsCOMPtr<IFoo> foo = new Foo(i);
    array.AppendObject(foo);
  }

  nsCOMArray<IFoo>::const_iterator it = array.begin();
  ASSERT_EQ(array.cbegin(), it);
}