From 32fefc463ec24bdaf7225003d29a8d5f3b0507e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Thu, 12 Mar 2015 16:31:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90unique=5Fptr=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 + TinySTL/Test/UniquePtrTest.cpp | 66 +++++++++++++++++++++++++++++++++ TinySTL/Test/UniquePtrTest.h | 18 +++++++++ TinySTL/TinySTL.vcxproj | 3 ++ TinySTL/TinySTL.vcxproj.filters | 9 +++++ TinySTL/main.cpp | 2 + 6 files changed, 100 insertions(+) create mode 100644 TinySTL/Test/UniquePtrTest.cpp create mode 100644 TinySTL/Test/UniquePtrTest.h diff --git a/README.md b/README.md index e7b216d..b85ecd4 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ TinySTL * pair:100% * list:100% * unordered_set:100% + * unique_ptr:100% * STL Algorithms: * fill:100% * fill_n:100% @@ -101,6 +102,7 @@ TinySTL * unordered_set:100% * directed_graph:100% * trie tree:100% + * unique_ptr:100% #TinySTL性能测试: ###测试环境:Windows 7 && VS2013 && release模式 diff --git a/TinySTL/Test/UniquePtrTest.cpp b/TinySTL/Test/UniquePtrTest.cpp new file mode 100644 index 0000000..4fac9a0 --- /dev/null +++ b/TinySTL/Test/UniquePtrTest.cpp @@ -0,0 +1,66 @@ +#include "UniquePtrTest.h" + +#include "../String.h" + +namespace { + template + struct Del{ + void operator()(T *p){ delete p; } + }; +} + +namespace TinySTL{ + namespace UniquePtrTest{ + void testCase1(){ + unique_ptr up1; + assert(up1 == nullptr); + + unique_ptr up2(new int(5)); + assert(up2 != nullptr); + assert(*up2 == 5); + + unique_ptr> up3(new string("zouxiaohang"), Del()); + assert(up3 != nullptr); + assert(*up3 == "zouxiaohang"); + + auto up4(std::move(up2)); + assert(*up4 == 5); + assert(up2 == nullptr); + + unique_ptr> up5; + up5 = std::move(up3); + assert(*up5 == "zouxiaohang"); + assert(up3 == nullptr); + + auto up6 = make_unique(6, 'z'); + assert(*up6 == "zzzzzz"); + } + void testCase2(){ + auto up = make_unique(10, '0'); + up->append("111"); + assert(*up == "0000000000111"); + + up.release(); + assert(up == nullptr); + + up.reset(new string("hello")); + assert(*up == "hello"); + } + void testCase3(){ + auto up1 = make_unique(2, '0'); + auto up2 = make_unique(2, '1'); + + up1.swap(up2); + assert(*up1 == "11" && *up2 == "00"); + + swap(up1, up2); + assert(*up1 == "00" && *up2 == "11"); + } + + void testAllCases(){ + testCase1(); + testCase2(); + testCase3(); + } + } +} \ No newline at end of file diff --git a/TinySTL/Test/UniquePtrTest.h b/TinySTL/Test/UniquePtrTest.h new file mode 100644 index 0000000..a0dc34f --- /dev/null +++ b/TinySTL/Test/UniquePtrTest.h @@ -0,0 +1,18 @@ +#ifndef _UNIQUEPTR_TEST_H_ +#define _UNIQUEPTR_TEST_H_ + +#include "TestUtil.h" + +#include "../Memory.h" + +#include + +namespace TinySTL{ + namespace UniquePtrTest{ + void testCase1(); + + void testAllCases(); + } +} + +#endif \ No newline at end of file diff --git a/TinySTL/TinySTL.vcxproj b/TinySTL/TinySTL.vcxproj index 0207942..5bebb18 100644 --- a/TinySTL/TinySTL.vcxproj +++ b/TinySTL/TinySTL.vcxproj @@ -100,6 +100,7 @@ + @@ -126,6 +127,7 @@ + @@ -148,6 +150,7 @@ + diff --git a/TinySTL/TinySTL.vcxproj.filters b/TinySTL/TinySTL.vcxproj.filters index 41a6b29..88f8c9e 100644 --- a/TinySTL/TinySTL.vcxproj.filters +++ b/TinySTL/TinySTL.vcxproj.filters @@ -96,6 +96,9 @@ Test + + Test + @@ -254,6 +257,12 @@ Test + + 头文件 + + + Test + diff --git a/TinySTL/main.cpp b/TinySTL/main.cpp index 9741d4e..26c4292 100644 --- a/TinySTL/main.cpp +++ b/TinySTL/main.cpp @@ -18,6 +18,7 @@ #include "Test\StringTest.h" #include "Test\SuffixArrayTest.h" #include "Test\TrieTreeTest.h" +#include "Test\UniquePtrTest.h" #include "Test\Unordered_setTest.h" #include "Test\VectorTest.h" @@ -39,6 +40,7 @@ int main(){ TinySTL::StringTest::testAllCases(); TinySTL::SuffixArrayTest::testAllCases(); TinySTL::TrieTreeTest::testAllCases(); + TinySTL::UniquePtrTest::testAllCases(); TinySTL::Unordered_setTest::testAllCases(); TinySTL::VectorTest::testAllCases();