add cow_ptr test
This commit is contained in:
32
TinySTL/Detail/COWPtr.impl.h
Normal file
32
TinySTL/Detail/COWPtr.impl.h
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#ifndef _COWPTR_IMPL_H_
|
||||||
|
#define _COWPTR_IMPL_H_
|
||||||
|
|
||||||
|
namespace TinySTL{
|
||||||
|
template<class T>
|
||||||
|
cow_ptr<T>::cow_ptr(T *p = nullptr) :ptr_(p){}
|
||||||
|
template<class T>
|
||||||
|
template<class D>
|
||||||
|
cow_ptr<T>::cow_ptr(T *p, D d) : ptr_(p, d){}
|
||||||
|
template<class T>
|
||||||
|
cow_ptr<T>::cow_ptr(const cow_ptr& cp){
|
||||||
|
ptr_ = cp.ptr_;
|
||||||
|
}
|
||||||
|
template<class T>
|
||||||
|
cow_ptr<T>& cow_ptr<T>::operator = (const cow_ptr& cp){
|
||||||
|
if (this != &cp){
|
||||||
|
ptr_.decrease_ref();
|
||||||
|
ptr_ = cp.ptr_;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
template<class T>
|
||||||
|
typename cow_ptr<T>::element_type cow_ptr<T>::operator *()const{
|
||||||
|
return *ptr_;
|
||||||
|
}
|
||||||
|
template<class T>
|
||||||
|
typename cow_ptr<T>::element_type *cow_ptr<T>::operator ->()const{
|
||||||
|
return ptr_.operator->();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
24
TinySTL/Test/COWPtrTest.cpp
Normal file
24
TinySTL/Test/COWPtrTest.cpp
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#include "COWPtrTest.h"
|
||||||
|
|
||||||
|
#include "../String.h"
|
||||||
|
|
||||||
|
namespace TinySTL{
|
||||||
|
namespace COWPtrTest{
|
||||||
|
void testCase1(){
|
||||||
|
cow_ptr<string> cp1(new string("hello"));
|
||||||
|
assert(*cp1 == "hello");
|
||||||
|
|
||||||
|
cp1->append(" world");
|
||||||
|
auto cp2 = cp1;
|
||||||
|
assert(*cp2 == "hello world");
|
||||||
|
|
||||||
|
cow_ptr<string> cp3;
|
||||||
|
cp3 = cp1;
|
||||||
|
assert(*cp3 == "hello world");
|
||||||
|
}
|
||||||
|
|
||||||
|
void testAllCases(){
|
||||||
|
testCase1();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
16
TinySTL/Test/COWPtrTest.h
Normal file
16
TinySTL/Test/COWPtrTest.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#ifndef _COWPTR_TEST_H_
|
||||||
|
#define _COWPTR_TEST_H_
|
||||||
|
|
||||||
|
#include "../COWPtr.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
namespace TinySTL{
|
||||||
|
namespace COWPtrTest{
|
||||||
|
void testCase1();
|
||||||
|
|
||||||
|
void testAllCases();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -82,7 +82,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="Detail\Alloc.cpp" />
|
<ClCompile Include="Detail\Alloc.cpp" />
|
||||||
<ClCompile Include="Detail\String.cpp" />
|
<ClCompile Include="Detail\String.cpp" />
|
||||||
<ClCompile Include="Detail\TireTree.cpp" />
|
<ClCompile Include="Detail\TrieTree.cpp" />
|
||||||
<ClCompile Include="main.cpp" />
|
<ClCompile Include="main.cpp" />
|
||||||
<ClCompile Include="Profiler\Profiler.cpp" />
|
<ClCompile Include="Profiler\Profiler.cpp" />
|
||||||
<ClCompile Include="Test\AlgorithmTest.cpp" />
|
<ClCompile Include="Test\AlgorithmTest.cpp" />
|
||||||
@@ -90,6 +90,7 @@
|
|||||||
<ClCompile Include="Test\BinarySearchTreeTest.cpp" />
|
<ClCompile Include="Test\BinarySearchTreeTest.cpp" />
|
||||||
<ClCompile Include="Test\BitmapTest.cpp" />
|
<ClCompile Include="Test\BitmapTest.cpp" />
|
||||||
<ClCompile Include="Test\CircularBufferTest.cpp" />
|
<ClCompile Include="Test\CircularBufferTest.cpp" />
|
||||||
|
<ClCompile Include="Test\COWPtrTest.cpp" />
|
||||||
<ClCompile Include="Test\DequeTest.cpp" />
|
<ClCompile Include="Test\DequeTest.cpp" />
|
||||||
<ClCompile Include="Test\GraphTest.cpp" />
|
<ClCompile Include="Test\GraphTest.cpp" />
|
||||||
<ClCompile Include="Test\ListTest.cpp" />
|
<ClCompile Include="Test\ListTest.cpp" />
|
||||||
@@ -115,11 +116,13 @@
|
|||||||
<ClInclude Include="Bitmap.h" />
|
<ClInclude Include="Bitmap.h" />
|
||||||
<ClInclude Include="CircularBuffer.h" />
|
<ClInclude Include="CircularBuffer.h" />
|
||||||
<ClInclude Include="Construct.h" />
|
<ClInclude Include="Construct.h" />
|
||||||
|
<ClInclude Include="COWPtr.h" />
|
||||||
<ClInclude Include="Deque.h" />
|
<ClInclude Include="Deque.h" />
|
||||||
<ClInclude Include="Detail\AVLTree.impl.h" />
|
<ClInclude Include="Detail\AVLTree.impl.h" />
|
||||||
<ClInclude Include="Detail\BinarySearchTree.impl.h" />
|
<ClInclude Include="Detail\BinarySearchTree.impl.h" />
|
||||||
<ClInclude Include="Detail\Bitmap.impl.h" />
|
<ClInclude Include="Detail\Bitmap.impl.h" />
|
||||||
<ClInclude Include="Detail\CircularBuffer.impl.h" />
|
<ClInclude Include="Detail\CircularBuffer.impl.h" />
|
||||||
|
<ClInclude Include="Detail\COWPtr.impl.h" />
|
||||||
<ClInclude Include="Detail\Deque.impl.h" />
|
<ClInclude Include="Detail\Deque.impl.h" />
|
||||||
<ClInclude Include="Detail\Graph.impl.h" />
|
<ClInclude Include="Detail\Graph.impl.h" />
|
||||||
<ClInclude Include="Detail\List.impl.h" />
|
<ClInclude Include="Detail\List.impl.h" />
|
||||||
@@ -142,6 +145,7 @@
|
|||||||
<ClInclude Include="Test\BinarySearchTreeTest.h" />
|
<ClInclude Include="Test\BinarySearchTreeTest.h" />
|
||||||
<ClInclude Include="Test\BitmapTest.h" />
|
<ClInclude Include="Test\BitmapTest.h" />
|
||||||
<ClInclude Include="Test\CircularBufferTest.h" />
|
<ClInclude Include="Test\CircularBufferTest.h" />
|
||||||
|
<ClInclude Include="Test\COWPtrTest.h" />
|
||||||
<ClInclude Include="Test\DequeTest.h" />
|
<ClInclude Include="Test\DequeTest.h" />
|
||||||
<ClInclude Include="Test\GraphTest.h" />
|
<ClInclude Include="Test\GraphTest.h" />
|
||||||
<ClInclude Include="Test\ListTest.h" />
|
<ClInclude Include="Test\ListTest.h" />
|
||||||
|
|||||||
@@ -90,9 +90,6 @@
|
|||||||
<ClCompile Include="Test\GraphTest.cpp">
|
<ClCompile Include="Test\GraphTest.cpp">
|
||||||
<Filter>Test</Filter>
|
<Filter>Test</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="Detail\TireTree.cpp">
|
|
||||||
<Filter>Detail</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="Test\TrieTreeTest.cpp">
|
<ClCompile Include="Test\TrieTreeTest.cpp">
|
||||||
<Filter>Test</Filter>
|
<Filter>Test</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
@@ -105,6 +102,12 @@
|
|||||||
<ClCompile Include="Test\SharedPtrTest.cpp">
|
<ClCompile Include="Test\SharedPtrTest.cpp">
|
||||||
<Filter>Test</Filter>
|
<Filter>Test</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Detail\TrieTree.cpp">
|
||||||
|
<Filter>Detail</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="Test\COWPtrTest.cpp">
|
||||||
|
<Filter>Test</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="TypeTraits.h">
|
<ClInclude Include="TypeTraits.h">
|
||||||
@@ -278,6 +281,15 @@
|
|||||||
<ClInclude Include="Test\SharedPtrTest.h">
|
<ClInclude Include="Test\SharedPtrTest.h">
|
||||||
<Filter>Test</Filter>
|
<Filter>Test</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="COWPtr.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="Detail\COWPtr.impl.h">
|
||||||
|
<Filter>Detail</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="Test\COWPtrTest.h">
|
||||||
|
<Filter>Test</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="..\README.md" />
|
<None Include="..\README.md" />
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
#include "Test\BitmapTest.h"
|
#include "Test\BitmapTest.h"
|
||||||
#include "Test\BinarySearchTreeTest.h"
|
#include "Test\BinarySearchTreeTest.h"
|
||||||
#include "Test\CircularBufferTest.h"
|
#include "Test\CircularBufferTest.h"
|
||||||
|
#include "Test\COWPtrTest.h"
|
||||||
#include "Test\DequeTest.h"
|
#include "Test\DequeTest.h"
|
||||||
#include "Test\GraphTest.h"
|
#include "Test\GraphTest.h"
|
||||||
#include "Test\ListTest.h"
|
#include "Test\ListTest.h"
|
||||||
@@ -32,6 +33,7 @@ int main(){
|
|||||||
TinySTL::BitmapTest::testAllCases();
|
TinySTL::BitmapTest::testAllCases();
|
||||||
TinySTL::BinarySearchTreeTest::testAllCases();
|
TinySTL::BinarySearchTreeTest::testAllCases();
|
||||||
TinySTL::CircularBufferTest::testAllCases();
|
TinySTL::CircularBufferTest::testAllCases();
|
||||||
|
TinySTL::COWPtrTest::testAllCases();
|
||||||
TinySTL::DequeTest::testAllCases();
|
TinySTL::DequeTest::testAllCases();
|
||||||
TinySTL::ListTest::testAllCases();
|
TinySTL::ListTest::testAllCases();
|
||||||
TinySTL::GraphTest::testAllCases();
|
TinySTL::GraphTest::testAllCases();
|
||||||
|
|||||||
Reference in New Issue
Block a user