From 6b85b32575c2738e90491383c03e66932799b8c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Mon, 16 Mar 2015 15:14:36 +0800 Subject: [PATCH] add cow_ptr test --- TinySTL/Detail/COWPtr.impl.h | 32 ++++++++++++++++++++++++++++++++ TinySTL/Test/COWPtrTest.cpp | 24 ++++++++++++++++++++++++ TinySTL/Test/COWPtrTest.h | 16 ++++++++++++++++ TinySTL/TinySTL.vcxproj | 6 +++++- TinySTL/TinySTL.vcxproj.filters | 18 +++++++++++++++--- TinySTL/main.cpp | 2 ++ 6 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 TinySTL/Detail/COWPtr.impl.h create mode 100644 TinySTL/Test/COWPtrTest.cpp create mode 100644 TinySTL/Test/COWPtrTest.h diff --git a/TinySTL/Detail/COWPtr.impl.h b/TinySTL/Detail/COWPtr.impl.h new file mode 100644 index 0000000..69711f5 --- /dev/null +++ b/TinySTL/Detail/COWPtr.impl.h @@ -0,0 +1,32 @@ +#ifndef _COWPTR_IMPL_H_ +#define _COWPTR_IMPL_H_ + +namespace TinySTL{ + template + cow_ptr::cow_ptr(T *p = nullptr) :ptr_(p){} + template + template + cow_ptr::cow_ptr(T *p, D d) : ptr_(p, d){} + template + cow_ptr::cow_ptr(const cow_ptr& cp){ + ptr_ = cp.ptr_; + } + template + cow_ptr& cow_ptr::operator = (const cow_ptr& cp){ + if (this != &cp){ + ptr_.decrease_ref(); + ptr_ = cp.ptr_; + } + return *this; + } + template + typename cow_ptr::element_type cow_ptr::operator *()const{ + return *ptr_; + } + template + typename cow_ptr::element_type *cow_ptr::operator ->()const{ + return ptr_.operator->(); + } +} + +#endif \ No newline at end of file diff --git a/TinySTL/Test/COWPtrTest.cpp b/TinySTL/Test/COWPtrTest.cpp new file mode 100644 index 0000000..f5cf347 --- /dev/null +++ b/TinySTL/Test/COWPtrTest.cpp @@ -0,0 +1,24 @@ +#include "COWPtrTest.h" + +#include "../String.h" + +namespace TinySTL{ + namespace COWPtrTest{ + void testCase1(){ + cow_ptr cp1(new string("hello")); + assert(*cp1 == "hello"); + + cp1->append(" world"); + auto cp2 = cp1; + assert(*cp2 == "hello world"); + + cow_ptr cp3; + cp3 = cp1; + assert(*cp3 == "hello world"); + } + + void testAllCases(){ + testCase1(); + } + } +} \ No newline at end of file diff --git a/TinySTL/Test/COWPtrTest.h b/TinySTL/Test/COWPtrTest.h new file mode 100644 index 0000000..bb7499d --- /dev/null +++ b/TinySTL/Test/COWPtrTest.h @@ -0,0 +1,16 @@ +#ifndef _COWPTR_TEST_H_ +#define _COWPTR_TEST_H_ + +#include "../COWPtr.h" + +#include + +namespace TinySTL{ + namespace COWPtrTest{ + void testCase1(); + + void testAllCases(); + } +} + +#endif \ No newline at end of file diff --git a/TinySTL/TinySTL.vcxproj b/TinySTL/TinySTL.vcxproj index 222161f..8901679 100644 --- a/TinySTL/TinySTL.vcxproj +++ b/TinySTL/TinySTL.vcxproj @@ -82,7 +82,7 @@ - + @@ -90,6 +90,7 @@ + @@ -115,11 +116,13 @@ + + @@ -142,6 +145,7 @@ + diff --git a/TinySTL/TinySTL.vcxproj.filters b/TinySTL/TinySTL.vcxproj.filters index b806928..166fc97 100644 --- a/TinySTL/TinySTL.vcxproj.filters +++ b/TinySTL/TinySTL.vcxproj.filters @@ -90,9 +90,6 @@ Test - - Detail - Test @@ -105,6 +102,12 @@ Test + + Detail + + + Test + @@ -278,6 +281,15 @@ Test + + 头文件 + + + Detail + + + Test + diff --git a/TinySTL/main.cpp b/TinySTL/main.cpp index c6a1757..4c8bd60 100644 --- a/TinySTL/main.cpp +++ b/TinySTL/main.cpp @@ -8,6 +8,7 @@ #include "Test\BitmapTest.h" #include "Test\BinarySearchTreeTest.h" #include "Test\CircularBufferTest.h" +#include "Test\COWPtrTest.h" #include "Test\DequeTest.h" #include "Test\GraphTest.h" #include "Test\ListTest.h" @@ -32,6 +33,7 @@ int main(){ TinySTL::BitmapTest::testAllCases(); TinySTL::BinarySearchTreeTest::testAllCases(); TinySTL::CircularBufferTest::testAllCases(); + TinySTL::COWPtrTest::testAllCases(); TinySTL::DequeTest::testAllCases(); TinySTL::ListTest::testAllCases(); TinySTL::GraphTest::testAllCases();