/src/mozilla-central/mfbt/tests/gtest/TestLinkedList.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 | | |
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 | | { |
18 | | public: |
19 | | bool* mResult; |
20 | | |
21 | | explicit PtrClass(bool* result) |
22 | | : mResult(result) |
23 | 0 | { |
24 | 0 | EXPECT_TRUE(!*mResult); |
25 | 0 | } |
26 | | |
27 | 0 | virtual ~PtrClass() { |
28 | 0 | *mResult = true; |
29 | 0 | } |
30 | | }; |
31 | | |
32 | | class InheritedPtrClass : public PtrClass { |
33 | | public: |
34 | | bool* mInheritedResult; |
35 | | |
36 | | InheritedPtrClass(bool* result, bool* inheritedResult) |
37 | | : PtrClass(result) |
38 | | , mInheritedResult(inheritedResult) |
39 | 0 | { |
40 | 0 | EXPECT_TRUE(!*mInheritedResult); |
41 | 0 | } |
42 | | |
43 | 0 | virtual ~InheritedPtrClass() { |
44 | 0 | *mInheritedResult = true; |
45 | 0 | } |
46 | | }; |
47 | | |
48 | | TEST(LinkedList, AutoCleanLinkedList) |
49 | 0 | { |
50 | 0 | bool rv1 = false; |
51 | 0 | bool rv2 = false; |
52 | 0 | bool rv3 = false; |
53 | 0 | { |
54 | 0 | AutoCleanLinkedList<PtrClass> list; |
55 | 0 | list.insertBack(new PtrClass(&rv1)); |
56 | 0 | list.insertBack(new InheritedPtrClass(&rv2, &rv3)); |
57 | 0 | } |
58 | 0 |
|
59 | 0 | EXPECT_TRUE(rv1); |
60 | 0 | EXPECT_TRUE(rv2); |
61 | 0 | EXPECT_TRUE(rv3); |
62 | 0 | } |
63 | | |
64 | | class CountedClass final : public LinkedListElement<RefPtr<CountedClass>> |
65 | | { |
66 | | public: |
67 | | int mCount; |
68 | 0 | void AddRef() { mCount++; } |
69 | 0 | void Release() { mCount--; } |
70 | | |
71 | | CountedClass() |
72 | | : mCount(0) |
73 | 0 | { |
74 | 0 | } |
75 | 0 | ~CountedClass() { EXPECT_TRUE(mCount == 0); } |
76 | | }; |
77 | | |
78 | | TEST(LinkedList, AutoCleanLinkedListRefPtr) |
79 | 0 | { |
80 | 0 | RefPtr<CountedClass> elt1 = new CountedClass; |
81 | 0 | CountedClass* elt2 = new CountedClass; |
82 | 0 | { |
83 | 0 | AutoCleanLinkedList<RefPtr<CountedClass>> list; |
84 | 0 | list.insertBack(elt1); |
85 | 0 | list.insertBack(elt2); |
86 | 0 |
|
87 | 0 | EXPECT_TRUE(elt1->mCount == 2); |
88 | 0 | EXPECT_TRUE(elt2->mCount == 1); |
89 | 0 | } |
90 | 0 |
|
91 | 0 | EXPECT_TRUE(elt1->mCount == 1); |
92 | 0 | EXPECT_TRUE(elt2->mCount == 0); |
93 | 0 | } |