From 2febc598c5ef214dd9605b1086fb4048eb962c67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Sun, 15 Mar 2015 19:37:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0shared=5Fptr=E7=9A=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Test/SharedPtrTest.cpp | 40 +++++++++++++++++++++++++++++++++ TinySTL/Test/SharedPtrTest.h | 16 +++++++++++++ TinySTL/TinySTL.vcxproj | 2 ++ TinySTL/TinySTL.vcxproj.filters | 6 +++++ TinySTL/main.cpp | 2 ++ 5 files changed, 66 insertions(+) create mode 100644 TinySTL/Test/SharedPtrTest.cpp create mode 100644 TinySTL/Test/SharedPtrTest.h diff --git a/TinySTL/Test/SharedPtrTest.cpp b/TinySTL/Test/SharedPtrTest.cpp new file mode 100644 index 0000000..219be6d --- /dev/null +++ b/TinySTL/Test/SharedPtrTest.cpp @@ -0,0 +1,40 @@ +#include "SharedPtrTest.h" + +#include "../String.h" + +namespace TinySTL{ + namespace SharedPtrTest{ + void testCase1(){ + shared_ptr sp1(new int(10)); + assert(*(sp1.get()) == 10); + + shared_ptr sp2(new int(1), default_delete()); + assert(sp2.use_count() == 1); + + auto sp3(sp2); + assert(sp3.use_count() == 2); + + auto sp4 = sp2; + assert(sp4.use_count() == 3); + + assert(sp2.get() == sp3.get() && sp2.get() == sp4.get()); + assert(sp2 == sp3 && !(sp2 != sp4)); + + shared_ptr sp5(new string("hello")); + assert(*sp5 == "hello"); + sp5->append(" world"); + assert(*sp5 == "hello world"); + + auto sp6 = make_shared(10, '0'); + assert(*sp6 == "0000000000"); + + shared_ptr spp; + assert(spp == nullptr); + assert(!(spp != nullptr)); + } + + void testAllCases(){ + testCase1(); + } + } +} \ No newline at end of file diff --git a/TinySTL/Test/SharedPtrTest.h b/TinySTL/Test/SharedPtrTest.h new file mode 100644 index 0000000..16922da --- /dev/null +++ b/TinySTL/Test/SharedPtrTest.h @@ -0,0 +1,16 @@ +#ifndef _SHARED_PTR_TEST_H_ +#define _SHARED_PTR_TEST_H_ + +#include "../Memory.h" + +#include + +namespace TinySTL{ + namespace SharedPtrTest{ + void testCase1(); + + void testAllCases(); + } +} + +#endif \ No newline at end of file diff --git a/TinySTL/TinySTL.vcxproj b/TinySTL/TinySTL.vcxproj index c4c559a..222161f 100644 --- a/TinySTL/TinySTL.vcxproj +++ b/TinySTL/TinySTL.vcxproj @@ -97,6 +97,7 @@ + @@ -148,6 +149,7 @@ + diff --git a/TinySTL/TinySTL.vcxproj.filters b/TinySTL/TinySTL.vcxproj.filters index 5d06a90..b806928 100644 --- a/TinySTL/TinySTL.vcxproj.filters +++ b/TinySTL/TinySTL.vcxproj.filters @@ -102,6 +102,9 @@ Test + + Test + @@ -272,6 +275,9 @@ Test + + Test + diff --git a/TinySTL/main.cpp b/TinySTL/main.cpp index f58bc0d..c6a1757 100644 --- a/TinySTL/main.cpp +++ b/TinySTL/main.cpp @@ -15,6 +15,7 @@ #include "Test\PriorityQueueTest.h" #include "Test\QueueTest.h" #include "Test\RefTest.h" +#include "Test\SharedPtrTest.h" #include "Test\StackTest.h" #include "Test\StringTest.h" #include "Test\SuffixArrayTest.h" @@ -38,6 +39,7 @@ int main(){ TinySTL::PriorityQueueTest::testAllCases(); TinySTL::QueueTest::testAllCases(); TinySTL::RefTest::testAllCases(); + TinySTL::SharedPtrTest::testAllCases(); TinySTL::StackTest::testAllCases(); TinySTL::StringTest::testAllCases(); TinySTL::SuffixArrayTest::testAllCases(); -- GitLab