Debian Codegraph @

mfbt/tests/gtest/TestLinkedList.cpp

raw
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
9 #include "mozilla/LinkedList.h"
10 #include "mozilla/RefPtr.h"
11
12 using mozilla::AutoCleanLinkedList;
13 using mozilla::LinkedList;
14 using mozilla::LinkedListElement;
15
16 class PtrClass : public LinkedListElement<PtrClass> {
17 public:
18 bool* mResult;
19
20 explicit PtrClass(bool* result) : mResult(result) { EXPECT_TRUE(!*mResult); }
21
22 virtual ~PtrClass() { *mResult = true; }
23 };
24
25 class InheritedPtrClass : public PtrClass {
26 public:
27 bool* mInheritedResult;
28
29 InheritedPtrClass(bool* result, bool* inheritedResult)
30 : PtrClass(result), mInheritedResult(inheritedResult) {
31 EXPECT_TRUE(!*mInheritedResult);
32 }
33
34 virtual ~InheritedPtrClass() { *mInheritedResult = true; }
35 };
36
37 TEST(LinkedList, AutoCleanLinkedList)
38 {
39 bool rv1 = false;
40 bool rv2 = false;
41 bool rv3 = false;
42 {
43 AutoCleanLinkedList<PtrClass> list;
44 list.insertBack(new PtrClass(&rv1));
45 list.insertBack(new InheritedPtrClass(&rv2, &rv3));
46 }
47
48 EXPECT_TRUE(rv1);
49 EXPECT_TRUE(rv2);
50 EXPECT_TRUE(rv3);
51 }
52
53 class CountedClass final : public LinkedListElement<RefPtr<CountedClass>> {
54 public:
55 int mCount;
56 void AddRef() { mCount++; }
57 void Release() { mCount--; }
58
59 CountedClass() : mCount(0) {}
60 ~CountedClass() { EXPECT_TRUE(mCount == 0); }
61 };
62
63 TEST(LinkedList, AutoCleanLinkedListRefPtr)
64 {
65 RefPtr<CountedClass> elt1 = new CountedClass;
66 CountedClass* elt2 = new CountedClass;
67 {
68 AutoCleanLinkedList<RefPtr<CountedClass>> list;
69 list.insertBack(elt1);
70 list.insertBack(elt2);
71
72 EXPECT_TRUE(elt1->mCount == 2);
73 EXPECT_TRUE(elt2->mCount == 1);
74 }
75
76 EXPECT_TRUE(elt1->mCount == 1);
77 EXPECT_TRUE(elt2->mCount == 0);
78 }