Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/tests/gtest/TestNsDeque.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
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/. */
6
7
#include "gtest/gtest.h"
8
#include "nsDeque.h"
9
#include "nsCRT.h"
10
#include "mozilla/TypeTraits.h"
11
#include <stdio.h>
12
#include <functional>
13
14
using mozilla::IsSame;
15
using mozilla::DeclVal;
16
17
/**************************************************************
18
  Now define the token deallocator class...
19
 **************************************************************/
20
namespace TestNsDeque {
21
22
  class _Dealloc: public nsDequeFunctor
23
  {
24
0
    virtual void operator()(void* aObject) {}
25
  };
26
27
  static bool VerifyContents(const nsDeque& aDeque, const int* aContents, size_t aLength)
28
0
  {
29
0
    for (size_t i=0; i<aLength; ++i) {
30
0
      if (*(int*)aDeque.ObjectAt(i) != aContents[i]) {
31
0
        return false;
32
0
      }
33
0
    }
34
0
    return true;
35
0
  }
36
37
  class Deallocator: public nsDequeFunctor
38
  {
39
    virtual void operator()(void* aObject)
40
0
    {
41
0
      if (aObject)
42
0
      {
43
0
        // Set value to -1, to use in test function.
44
0
        *((int*)aObject) = -1;
45
0
      }
46
0
    }
47
  };
48
49
  class ForEachAdder: public nsDequeFunctor
50
  {
51
    virtual void operator()(void* aObject)
52
0
    {
53
0
      if (aObject)
54
0
      {
55
0
        sum += *(int*)aObject;
56
0
      }
57
0
    }
58
59
    private:
60
     int sum = 0;
61
62
    public:
63
0
     int GetSum() { return sum; }
64
65
  };
66
}
67
68
using namespace TestNsDeque;
69
70
TEST(NsDeque, OriginalTest)
71
0
{
72
0
  const size_t size = 200;
73
0
  int ints[size];
74
0
  size_t i=0;
75
0
  int temp;
76
0
  nsDeque theDeque(new _Dealloc); //construct a simple one...
77
0
78
0
  // ints = [0...199]
79
0
  for (i=0;i<size;i++) { //initialize'em
80
0
    ints[i]=static_cast<int>(i);
81
0
  }
82
0
  // queue = [0...69]
83
0
  for (i=0;i<70;i++) {
84
0
    theDeque.Push(&ints[i]);
85
0
    temp=*(int*)theDeque.Peek();
86
0
    EXPECT_EQ(static_cast<int>(i), temp) << "Verify end after push #1";
87
0
    EXPECT_EQ(i + 1, theDeque.GetSize()) << "Verify size after push #1";
88
0
  }
89
0
90
0
  EXPECT_EQ(70u,theDeque.GetSize()) << "Verify overall size after pushes #1";
91
0
92
0
  // queue = [0...14]
93
0
  for (i=1;i<=55;i++) {
94
0
    temp=*(int*)theDeque.Pop();
95
0
    EXPECT_EQ(70-static_cast<int>(i),temp) << "Verify end after pop # 1";
96
0
    EXPECT_EQ(70u - i,theDeque.GetSize()) << "Verify size after pop # 1";
97
0
  }
98
0
  EXPECT_EQ(15u,theDeque.GetSize()) << "Verify overall size after pops";
99
0
100
0
  // queue = [0...14,0...54]
101
0
  for (i=0;i<55;i++) {
102
0
    theDeque.Push(&ints[i]);
103
0
    temp=*(int*)theDeque.Peek();
104
0
    EXPECT_EQ(static_cast<int>(i),temp) << "Verify end after push #2";
105
0
    EXPECT_EQ(i + 15u + 1,theDeque.GetSize()) << "Verify size after push # 2";
106
0
  }
107
0
  EXPECT_EQ(70u,theDeque.GetSize()) << "Verify size after end of all pushes #2";
108
0
109
0
  // queue = [0...14,0...19]
110
0
  for (i=1;i<=35;i++) {
111
0
    temp=*(int*)theDeque.Pop();
112
0
    EXPECT_EQ(55-static_cast<int>(i),temp ) << "Verify end after pop # 2";
113
0
    EXPECT_EQ(70u - i,theDeque.GetSize()) << "Verify size after pop #2";
114
0
  }
115
0
  EXPECT_EQ(35u,theDeque.GetSize()) << "Verify overall size after end of all pops #2";
116
0
117
0
  // queue = [0...14,0...19,0...34]
118
0
  for (i=0;i<35;i++) {
119
0
    theDeque.Push(&ints[i]);
120
0
    temp = *(int*)theDeque.Peek();
121
0
    EXPECT_EQ(static_cast<int>(i),temp) << "Verify end after push # 3";
122
0
    EXPECT_EQ(35u + 1u + i,theDeque.GetSize()) << "Verify size after push #3";
123
0
  }
124
0
125
0
  // queue = [0...14,0...19]
126
0
  for (i=0;i<35;i++) {
127
0
    temp=*(int*)theDeque.Pop();
128
0
    EXPECT_EQ(34 - static_cast<int>(i), temp) << "Verify end after pop # 3";
129
0
  }
130
0
131
0
  // queue = [0...14]
132
0
  for (i=0;i<20;i++) {
133
0
    temp=*(int*)theDeque.Pop();
134
0
    EXPECT_EQ(19 - static_cast<int>(i),temp) << "Verify end after pop # 4";
135
0
  }
136
0
137
0
  // queue = []
138
0
  for (i=0;i<15;i++) {
139
0
    temp=*(int*)theDeque.Pop();
140
0
    EXPECT_EQ(14 - static_cast<int>(i),temp) << "Verify end after pop # 5";
141
0
  }
142
0
143
0
  EXPECT_EQ(0u,theDeque.GetSize()) << "Deque should finish empty.";
144
0
}
145
146
TEST(NsDeque, OriginalFlaw)
147
0
{
148
0
  int ints[200];
149
0
  int i=0;
150
0
  int temp;
151
0
  nsDeque d(new _Dealloc);
152
0
  /**
153
0
   * Test 1. Origin near end, semi full, call Peek().
154
0
   * you start, mCapacity is 8
155
0
   */
156
0
  for (i=0; i<30; i++)
157
0
    ints[i]=i;
158
0
159
0
  for (i=0; i<6; i++) {
160
0
    d.Push(&ints[i]);
161
0
    temp = *(int*)d.Peek();
162
0
    EXPECT_EQ(i, temp) << "OriginalFlaw push #1";
163
0
  }
164
0
  EXPECT_EQ(6u, d.GetSize()) << "OriginalFlaw size check #1";
165
0
166
0
  for (i=0; i<4; i++) {
167
0
    temp=*(int*)d.PopFront();
168
0
    EXPECT_EQ(i, temp) << "PopFront test";
169
0
  }
170
0
  // d = [4,5]
171
0
  EXPECT_EQ(2u, d.GetSize()) << "OriginalFlaw size check #2";
172
0
173
0
  for (i=0; i<4; i++) {
174
0
    d.Push(&ints[6 + i]);
175
0
  }
176
0
177
0
  // d = [4...9]
178
0
  for (i=4; i<=9; i++) {
179
0
    temp=*(int*)d.PopFront();
180
0
    EXPECT_EQ(i, temp) << "OriginalFlaw empty check";
181
0
  }
182
0
}
183
184
TEST(NsDeque, TestObjectAt)
185
0
{
186
0
  nsDeque d;
187
0
  const int count = 10;
188
0
  int ints[count];
189
0
  for (int i=0; i<count; i++) {
190
0
    ints[i] = i;
191
0
  }
192
0
193
0
  for (int i=0; i<6; i++) {
194
0
    d.Push(&ints[i]);
195
0
  }
196
0
  // d = [0...5]
197
0
  d.PopFront();
198
0
  d.PopFront();
199
0
200
0
  // d = [2..5]
201
0
  for (size_t i=2; i<=5; i++) {
202
0
    int t = *(int*)d.ObjectAt(i-2);
203
0
    EXPECT_EQ(static_cast<int>(i),t) << "Verify ObjectAt()";
204
0
  }
205
0
}
206
207
TEST(NsDeque, TestPushFront)
208
0
{
209
0
  // PushFront has some interesting corner cases, primarily we're interested in whether:
210
0
  // - wrapping around works properly
211
0
  // - growing works properly
212
0
213
0
  nsDeque d;
214
0
215
0
  const int kPoolSize = 10;
216
0
  const size_t kMaxSizeBeforeGrowth = 8;
217
0
218
0
  int pool[kPoolSize];
219
0
  for (int i = 0; i < kPoolSize; i++) {
220
0
    pool[i] = i;
221
0
  }
222
0
223
0
  for (size_t i = 0; i < kMaxSizeBeforeGrowth; i++) {
224
0
    d.PushFront(pool + i);
225
0
  }
226
0
227
0
  EXPECT_EQ(kMaxSizeBeforeGrowth, d.GetSize()) << "verify size";
228
0
229
0
  static const int t1[] = {7,6,5,4,3,2,1,0};
230
0
  EXPECT_TRUE(VerifyContents(d, t1, kMaxSizeBeforeGrowth)) << "verify pushfront 1";
231
0
232
0
  // Now push one more so it grows
233
0
  d.PushFront(pool + kMaxSizeBeforeGrowth);
234
0
  EXPECT_EQ(kMaxSizeBeforeGrowth + 1, d.GetSize()) << "verify size";
235
0
236
0
  static const int t2[] = {8,7,6,5,4,3,2,1,0};
237
0
  EXPECT_TRUE(VerifyContents(d, t2, kMaxSizeBeforeGrowth + 1)) << "verify pushfront 2";
238
0
239
0
  // And one more so that it wraps again
240
0
  d.PushFront(pool + kMaxSizeBeforeGrowth + 1);
241
0
  EXPECT_EQ(kMaxSizeBeforeGrowth + 2, d.GetSize()) << "verify size";
242
0
243
0
  static const int t3[] = {9,8,7,6,5,4,3,2,1,0};
244
0
  EXPECT_TRUE(VerifyContents(d, t3, kMaxSizeBeforeGrowth + 2)) <<"verify pushfront 3";
245
0
}
246
247
void CheckIfQueueEmpty(nsDeque& d)
248
0
{
249
0
  EXPECT_EQ(0u, d.GetSize())         << "Size should be 0";
250
0
  EXPECT_EQ(nullptr, d.Pop())       <<  "Invalid operation should return nullptr";
251
0
  EXPECT_EQ(nullptr, d.PopFront())  <<  "Invalid operation should return nullptr";
252
0
  EXPECT_EQ(nullptr, d.Peek())      <<  "Invalid operation should return nullptr";
253
0
  EXPECT_EQ(nullptr, d.PeekFront()) <<  "Invalid operation should return nullptr";
254
0
  EXPECT_EQ(nullptr, d.ObjectAt(0u)) <<  "Invalid operation should return nullptr";
255
0
}
256
257
TEST(NsDeque,TestEmpty)
258
0
{
259
0
  // Make sure nsDeque gives sane results if it's empty.
260
0
  nsDeque d;
261
0
  size_t numberOfEntries = 8;
262
0
263
0
  CheckIfQueueEmpty(d);
264
0
265
0
  // Fill it up and drain it.
266
0
  for (size_t i = 0; i < numberOfEntries; i++) {
267
0
    d.Push((void*)0xAA);
268
0
  }
269
0
270
0
  EXPECT_EQ(numberOfEntries, d.GetSize());
271
0
272
0
  for (size_t i = 0; i < numberOfEntries; i++) {
273
0
    (void)d.Pop();
274
0
  }
275
0
276
0
  // Now check it again.
277
0
  CheckIfQueueEmpty(d);
278
0
}
279
280
TEST(NsDeque,TestEraseMethod)
281
0
{
282
0
  nsDeque d;
283
0
  const size_t numberOfEntries = 8;
284
0
285
0
  // Fill it up before calling Erase
286
0
  for (size_t i = 0; i < numberOfEntries; i++) {
287
0
    d.Push((void*)0xAA);
288
0
  }
289
0
290
0
  // Call Erase
291
0
  d.Erase();
292
0
293
0
  // Now check it again.
294
0
  CheckIfQueueEmpty(d);
295
0
}
296
297
TEST(NsDeque,TestEraseShouldCallDeallocator)
298
0
{
299
0
  nsDeque d(new Deallocator());
300
0
  const size_t NumTestValues = 8;
301
0
302
0
  int* testArray[NumTestValues];
303
0
  for (size_t i=0; i < NumTestValues; i++)
304
0
  {
305
0
    testArray[i] = new int();
306
0
    *(testArray[i]) = i;
307
0
    d.Push((void*)testArray[i]);
308
0
  }
309
0
310
0
  d.Erase();
311
0
312
0
  // Now check it again.
313
0
  CheckIfQueueEmpty(d);
314
0
315
0
  for (size_t i=0; i < NumTestValues; i++)
316
0
  {
317
0
    EXPECT_EQ(-1, *(testArray[i])) << "Erase should call deallocator: " << *(testArray[i]);
318
0
  }
319
0
}
320
321
TEST(NsDeque, TestForEach)
322
0
{
323
0
  nsDeque d(new Deallocator());
324
0
  const size_t NumTestValues = 8;
325
0
  int sum = 0;
326
0
327
0
  int* testArray[NumTestValues];
328
0
  for (size_t i=0; i < NumTestValues; i++)
329
0
  {
330
0
    testArray[i] = new int();
331
0
    *(testArray[i]) = i;
332
0
    sum += i;
333
0
    d.Push((void*)testArray[i]);
334
0
  }
335
0
336
0
  ForEachAdder adder;
337
0
  d.ForEach(adder);
338
0
  EXPECT_EQ(sum, adder.GetSum()) << "For each should iterate over values";
339
0
340
0
  d.Erase();
341
0
}
342
343
TEST(NsDeque, TestConstRangeFor)
344
0
{
345
0
  nsDeque d(new Deallocator());
346
0
347
0
  const size_t NumTestValues = 3;
348
0
  for (size_t i = 0; i < NumTestValues; ++i)
349
0
  {
350
0
    d.Push(new int(i + 1));
351
0
  }
352
0
353
0
  static_assert(IsSame<nsDeque::ConstDequeIterator,
354
0
                       decltype(DeclVal<const nsDeque&>().begin())>::value,
355
0
                "(const nsDeque).begin() should return ConstDequeIterator");
356
0
  static_assert(IsSame<nsDeque::ConstDequeIterator,
357
0
                       decltype(DeclVal<const nsDeque&>().end())>::value,
358
0
                "(const nsDeque).end() should return ConstDequeIterator");
359
0
360
0
  int sum = 0;
361
0
  for (void* ob : const_cast<const nsDeque&>(d)) {
362
0
    sum += *static_cast<int*>(ob);
363
0
  }
364
0
  EXPECT_EQ(1+2+3, sum)
365
0
    << "Const-range-for should iterate over values";
366
0
}
367
368
TEST(NsDeque, TestRangeFor)
369
0
{
370
0
  const size_t NumTestValues = 3;
371
0
  struct Test
372
0
  {
373
0
    size_t runAfterLoopCount;
374
0
    std::function<void(nsDeque&)> function;
375
0
    int expectedSum;
376
0
    const char* description;
377
0
  };
378
0
  // Note: All tests start with a deque containing 3 pointers to ints 1, 2, 3.
379
0
  Test tests[] = {
380
0
    { 0, [](nsDeque& d){}, 1+2+3, "no changes" },
381
0
382
0
    { 1, [](nsDeque& d) { d.Pop(); }, 1+2, "Pop after 1st loop" },
383
0
    { 2, [](nsDeque& d) { d.Pop(); }, 1+2, "Pop after 2nd loop" },
384
0
    { 3, [](nsDeque& d) { d.Pop(); }, 1+2+3, "Pop after 3rd loop" },
385
0
386
0
    { 1, [](nsDeque& d) { d.PopFront(); }, 1+3, "PopFront after 1st loop" },
387
0
    { 2, [](nsDeque& d) { d.PopFront(); }, 1+2, "PopFront after 2nd loop" },
388
0
    { 3, [](nsDeque& d) { d.PopFront(); }, 1+2+3, "PopFront after 3rd loop" },
389
0
390
0
    { 1, [](nsDeque& d) { d.Push(new int(4)); },
391
0
      1+2+3+4, "Push after 1st loop" },
392
0
    { 2, [](nsDeque& d) { d.Push(new int(4)); },
393
0
      1+2+3+4, "Push after 2nd loop" },
394
0
    { 3, [](nsDeque& d) { d.Push(new int(4)); },
395
0
      1+2+3+4, "Push after 3rd loop" },
396
0
    { 4, [](nsDeque& d) { d.Push(new int(4)); },
397
0
      1+2+3, "Push after would-be-4th loop" },
398
0
399
0
    { 1, [](nsDeque& d) { d.PushFront(new int(4)); },
400
0
      1+1+2+3, "PushFront after 1st loop" },
401
0
    { 2, [](nsDeque& d) { d.PushFront(new int(4)); },
402
0
      1+2+2+3, "PushFront after 2nd loop" },
403
0
    { 3, [](nsDeque& d) { d.PushFront(new int(4)); },
404
0
      1+2+3+3, "PushFront after 3rd loop" },
405
0
    { 4, [](nsDeque& d) { d.PushFront(new int(4)); },
406
0
      1+2+3, "PushFront after would-be-4th loop" },
407
0
408
0
    { 1, [](nsDeque& d) { d.Erase(); }, 1, "Erase after 1st loop" },
409
0
    { 2, [](nsDeque& d) { d.Erase(); }, 1+2, "Erase after 2nd loop" },
410
0
    { 3, [](nsDeque& d) { d.Erase(); }, 1+2+3, "Erase after 3rd loop" },
411
0
412
0
    { 1, [](nsDeque& d) { d.Erase(); d.Push(new int(4)); },
413
0
      1, "Erase after 1st loop, Push 4" },
414
0
    { 1, [](nsDeque& d) { d.Erase(); d.Push(new int(4)); d.Push(new int(5)); },
415
0
      1+5, "Erase after 1st loop, Push 4,5" },
416
0
    { 2, [](nsDeque& d) { d.Erase(); d.Push(new int(4)); },
417
0
      1+2, "Erase after 2nd loop, Push 4" },
418
0
    { 2, [](nsDeque& d) { d.Erase(); d.Push(new int(4)); d.Push(new int(5)); },
419
0
      1+2, "Erase after 2nd loop, Push 4,5" },
420
0
    { 2, [](nsDeque& d) { d.Erase(); d.Push(new int(4)); d.Push(new int(5)); d.Push(new int(6)); },
421
0
      1+2+6, "Erase after 2nd loop, Push 4,5,6" }
422
0
  };
423
0
424
0
  for (const Test& test : tests) {
425
0
    nsDeque d(new Deallocator());
426
0
427
0
    for (size_t i = 0; i < NumTestValues; ++i)
428
0
    {
429
0
      d.Push(new int(i + 1));
430
0
    }
431
0
432
0
    static_assert(IsSame<nsDeque::ConstIterator,
433
0
                        decltype(d.begin())>::value,
434
0
                  "(non-const nsDeque).begin() should return ConstIterator");
435
0
    static_assert(IsSame<nsDeque::ConstIterator,
436
0
                        decltype(d.end())>::value,
437
0
                  "(non-const nsDeque).end() should return ConstIterator");
438
0
439
0
    int sum = 0;
440
0
    size_t loopCount = 0;
441
0
    for (void* ob : d) {
442
0
      sum += *static_cast<int*>(ob);
443
0
      if (++loopCount == test.runAfterLoopCount) {
444
0
        test.function(d);
445
0
      }
446
0
    }
447
0
    EXPECT_EQ(test.expectedSum, sum)
448
0
      << "Range-for should iterate over values in test '"
449
0
      << test.description
450
0
      << "'";
451
0
  }
452
0
}