Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/tests/gtest/TestAutoPtr.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
// vim:cindent:ts=4:et:sw=4:
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 "nsAutoPtr.h"
8
#include "gtest/gtest.h"
9
10
class TestObjectBaseA {
11
  public:
12
    // Virtual dtor for deleting through base class pointer
13
0
    virtual ~TestObjectBaseA() { }
14
    void MemberFunction(int, int*, int&)
15
0
    {
16
0
    }
17
0
    virtual void VirtualMemberFunction(int, int*, int&) { };
18
0
    virtual void VirtualConstMemberFunction(int, int*, int&) const { };
19
    int fooA;
20
};
21
22
class TestObjectBaseB {
23
  public:
24
    // Virtual dtor for deleting through base class pointer
25
0
    virtual ~TestObjectBaseB() { }
26
    int fooB;
27
};
28
29
class TestObject : public TestObjectBaseA, public TestObjectBaseB {
30
  public:
31
    TestObject()
32
0
    {
33
0
    }
34
35
    // Virtual dtor for deleting through base class pointer
36
    virtual ~TestObject()
37
0
    {
38
0
      destructed++;
39
0
    }
40
41
    virtual void VirtualMemberFunction(int, int*, int&) override
42
0
    {
43
0
    }
44
    virtual void VirtualConstMemberFunction(int, int*, int&) const override
45
0
    {
46
0
    }
47
48
    static int destructed;
49
    static const void* last_ptr;
50
};
51
52
int TestObject::destructed = 0;
53
const void* TestObject::last_ptr = nullptr;
54
55
static void CreateTestObject(TestObject **aResult)
56
0
{
57
0
  *aResult = new TestObject();
58
0
}
59
60
static void DoSomethingWithTestObject(TestObject *aIn)
61
0
{
62
0
  TestObject::last_ptr = static_cast<void*>(aIn);
63
0
}
64
65
static void DoSomethingWithConstTestObject(const TestObject *aIn)
66
0
{
67
0
  TestObject::last_ptr = static_cast<const void*>(aIn);
68
0
}
69
70
static void DoSomethingWithTestObjectBaseB(TestObjectBaseB *aIn)
71
0
{
72
0
  TestObject::last_ptr = static_cast<void*>(aIn);
73
0
}
74
75
static void DoSomethingWithConstTestObjectBaseB(const TestObjectBaseB *aIn)
76
0
{
77
0
  TestObject::last_ptr = static_cast<const void*>(aIn);
78
0
}
79
80
TEST(AutoPtr, Assignment)
81
0
{
82
0
  TestObject::destructed = 0;
83
0
84
0
  {
85
0
    nsAutoPtr<TestObject> pobj( new TestObject() );
86
0
  }
87
0
88
0
  ASSERT_EQ(1, TestObject::destructed);
89
0
90
0
  {
91
0
    nsAutoPtr<TestObject> pobj( new TestObject() );
92
0
    pobj = new TestObject();
93
0
    ASSERT_EQ(2, TestObject::destructed);
94
0
  }
95
0
96
0
  ASSERT_EQ(3, TestObject::destructed);
97
0
}
98
99
TEST(AutoPtr, getter_Transfers)
100
0
{
101
0
  TestObject::destructed = 0;
102
0
103
0
  {
104
0
    nsAutoPtr<TestObject> ptr;
105
0
    CreateTestObject(getter_Transfers(ptr));
106
0
  }
107
0
108
0
  ASSERT_EQ(1, TestObject::destructed);
109
0
}
110
111
TEST(AutoPtr, Casting)
112
0
{
113
0
  // This comparison is always false, as it should be. The extra parens
114
0
  // suppress a -Wunreachable-code warning about printf being unreachable.
115
0
  ASSERT_NE(((void*)(TestObject*)0x1000),
116
0
             ((void*)(TestObjectBaseB*)(TestObject*)0x1000));
117
0
118
0
  {
119
0
    nsAutoPtr<TestObject> p1(new TestObject());
120
0
    TestObjectBaseB *p2 = p1;
121
0
    ASSERT_NE(static_cast<void*>(p1),
122
0
               static_cast<void*>(p2));
123
0
    ASSERT_EQ(p1, p2);
124
0
    ASSERT_FALSE(p1 != p2);
125
0
    ASSERT_EQ(p2, p1);
126
0
    ASSERT_FALSE(p2 != p1);
127
0
  }
128
0
129
0
  {
130
0
    TestObject *p1 = new TestObject();
131
0
    nsAutoPtr<TestObjectBaseB> p2(p1);
132
0
    ASSERT_EQ(p1, p2);
133
0
    ASSERT_FALSE(p1 != p2);
134
0
    ASSERT_EQ(p2, p1);
135
0
    ASSERT_FALSE(p2 != p1);
136
0
  }
137
0
}
138
139
TEST(AutoPtr, Forget)
140
0
{
141
0
  TestObject::destructed = 0;
142
0
143
0
  {
144
0
    nsAutoPtr<TestObject> pobj( new TestObject() );
145
0
    nsAutoPtr<TestObject> pobj2( pobj.forget() );
146
0
    ASSERT_EQ(0, TestObject::destructed);
147
0
  }
148
0
149
0
  ASSERT_EQ(1, TestObject::destructed);
150
0
}
151
152
TEST(AutoPtr, Construction)
153
0
{
154
0
  TestObject::destructed = 0;
155
0
156
0
  {
157
0
    nsAutoPtr<TestObject> pobj(new TestObject());
158
0
  }
159
0
160
0
  ASSERT_EQ(1, TestObject::destructed);
161
0
}
162
163
TEST(AutoPtr, ImplicitConversion)
164
0
{
165
0
  // This test is basically successful if it builds. We add a few assertions
166
0
  // to make gtest happy.
167
0
  TestObject::destructed = 0;
168
0
169
0
  {
170
0
    nsAutoPtr<TestObject> pobj(new TestObject());
171
0
    DoSomethingWithTestObject(pobj);
172
0
    DoSomethingWithConstTestObject(pobj);
173
0
  }
174
0
175
0
  ASSERT_EQ(1, TestObject::destructed);
176
0
177
0
  {
178
0
    nsAutoPtr<TestObject> pobj(new TestObject());
179
0
    DoSomethingWithTestObjectBaseB(pobj);
180
0
    DoSomethingWithConstTestObjectBaseB(pobj);
181
0
  }
182
0
183
0
  ASSERT_EQ(2, TestObject::destructed);
184
0
185
0
  {
186
0
    const nsAutoPtr<TestObject> pobj(new TestObject());
187
0
    DoSomethingWithTestObject(pobj);
188
0
    DoSomethingWithConstTestObject(pobj);
189
0
  }
190
0
191
0
  ASSERT_EQ(3, TestObject::destructed);
192
0
193
0
  {
194
0
    const nsAutoPtr<TestObject> pobj(new TestObject());
195
0
    DoSomethingWithTestObjectBaseB(pobj);
196
0
    DoSomethingWithConstTestObjectBaseB(pobj);
197
0
  }
198
0
199
0
  ASSERT_EQ(4, TestObject::destructed);
200
0
}
201
202
TEST(AutoPtr, ArrowOperator)
203
0
{
204
0
  // This test is basically successful if it builds. We add a few assertions
205
0
  // to make gtest happy.
206
0
  TestObject::destructed = 0;
207
0
208
0
  {
209
0
    int test = 1;
210
0
    void (TestObjectBaseA::*fPtr)( int, int*, int& ) = &TestObjectBaseA::MemberFunction;
211
0
    void (TestObjectBaseA::*fVPtr)( int, int*, int& ) = &TestObjectBaseA::VirtualMemberFunction;
212
0
    void (TestObjectBaseA::*fVCPtr)( int, int*, int& ) const = &TestObjectBaseA::VirtualConstMemberFunction;
213
0
    nsAutoPtr<TestObjectBaseA> pobj(new TestObject());
214
0
    (pobj->*fPtr)(test, &test, test);
215
0
    (pobj->*fVPtr)(test, &test, test);
216
0
    (pobj->*fVCPtr)(test, &test, test);
217
0
  }
218
0
219
0
  ASSERT_EQ(1, TestObject::destructed);
220
0
}