Skip to content

Commit be3da7c

Browse files
More tests added
1 parent 8aec89c commit be3da7c

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

src/shared_ptr.hpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,19 @@ class shared_ptr: private shared_ptr_base
5656
public:
5757

5858
// FIXME: Should this be explicit ?
59-
shared_ptr() : m_pData(0), m_pRefCount(0)
59+
shared_ptr() : m_pointer(0), m_pRefCount(0)
6060
{
6161
m_pRefCount = new RefCount();
6262
m_pRefCount->AddRef();
6363
}
6464

65-
shared_ptr(T* ptr) : m_pData(ptr), m_pRefCount(0)
65+
shared_ptr(T* ptr) : m_pointer(ptr), m_pRefCount(0)
6666
{
6767
m_pRefCount = new RefCount();
6868
m_pRefCount->AddRef();
6969
}
7070

71-
shared_ptr(const shared_ptr<T>& ptr) : m_pData(ptr.m_pData), m_pRefCount(ptr.m_pRefCount)
71+
shared_ptr(const shared_ptr<T>& ptr) : m_pointer(ptr.m_pointer), m_pRefCount(ptr.m_pRefCount)
7272
{
7373
m_pRefCount->AddRef();
7474
}
@@ -77,29 +77,29 @@ class shared_ptr: private shared_ptr_base
7777
{
7878
if(m_pRefCount->Release() == 0)
7979
{
80-
if(m_pData)
81-
delete m_pData;
80+
if(m_pointer)
81+
delete m_pointer;
8282
delete m_pRefCount;
8383
}
8484
}
8585

86-
bool isNull()
86+
bool isNull() const
8787
{
88-
return m_pData == 0;
88+
return m_pointer == 0;
8989
}
9090

91-
bool operator!()
91+
bool operator!() const
9292
{
93-
return m_pData == 0;
93+
return m_pointer == 0;
9494
}
9595

9696
void swap(shared_ptr<T>& rhs)
9797
{
98-
T* pData = rhs.m_pData;
98+
T* pData = rhs.m_pointer;
9999
RefCount* pRefCount = rhs.m_pRefCount;
100-
rhs.m_pData = this->m_pData;
100+
rhs.m_pointer = this->m_pointer;
101101
rhs.m_pRefCount = this->m_pRefCount;
102-
this->m_pData = pData;
102+
this->m_pointer = pData;
103103
this->m_pRefCount = pRefCount;
104104
}
105105

@@ -118,18 +118,18 @@ class shared_ptr: private shared_ptr_base
118118

119119
T& operator*()
120120
{
121-
return *m_pData;
121+
return *m_pointer;
122122
}
123123

124124
T* operator->()
125125
{
126-
return m_pData;
126+
return m_pointer;
127127
}
128128

129129

130130
private:
131131

132-
T* m_pData;
132+
T* m_pointer;
133133
RefCount* m_pRefCount;
134134

135135
template<class X>
@@ -150,13 +150,13 @@ class shared_ptr: private shared_ptr_base
150150
template<class T>
151151
inline bool operator==(const shared_ptr<T>& p1, const shared_ptr<T>& p2)
152152
{
153-
return p1.m_pData == p2.m_pData;
153+
return p1.m_pointer == p2.m_pointer;
154154
}
155155

156156
template<class T>
157157
inline bool operator!=(const shared_ptr<T>& p1, const shared_ptr<T>& p2)
158158
{
159-
return p1.m_pData != p2.m_pData;
159+
return p1.m_pointer != p2.m_pointer;
160160
}
161161

162162

tests/shared_ptr_test.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,22 @@ void SharedPtrTest::Test()
2828

2929
CPPUNIT_ASSERT(sp3.isNull() == true);
3030
CPPUNIT_ASSERT(!sp3 == true);
31+
32+
// Comparison operators
33+
CPPUNIT_ASSERT(sp1 == sp2);
34+
CPPUNIT_ASSERT(!(sp1 != sp2));
35+
36+
// Derived classes
37+
shared_ptr<Dummy> derived1(new DummyDerived());
38+
39+
shared_ptr<Dummy> derived2(new Dummy());
40+
41+
CPPUNIT_ASSERT(derived1 != derived2);
42+
43+
derived1->m_data = 1;
44+
derived2 = derived1;
45+
46+
CPPUNIT_ASSERT(derived1->GetData() == 2);
47+
CPPUNIT_ASSERT(derived2->GetData() == 2);
3148
}
3249

tests/shared_ptr_test.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,28 @@ class Dummy
3535
{
3636
}
3737

38-
~Dummy()
38+
virtual ~Dummy()
3939
{
4040
if(m_pData)
4141
delete m_pData;
4242
}
4343

44+
virtual int GetData()
45+
{
46+
return m_data;
47+
}
48+
4449
int* m_pData;
4550
int m_data;
4651
};
4752

53+
class DummyDerived : public Dummy
54+
{
55+
virtual int GetData()
56+
{
57+
return m_data * 2;
58+
}
59+
};
60+
4861
#endif
4962

0 commit comments

Comments
 (0)