diff --git a/TinySTL/Test/SharedPtrTest.cpp b/TinySTL/Test/SharedPtrTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..219be6d1d36705f999c7d223c5d24cd33b2e0e76 --- /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 0000000000000000000000000000000000000000..16922da3651d7ce0be77de04cc520f00f82a470f --- /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 c4c559aa69c383cef5af6423696e5553edf85297..222161f3ff328d6a73bb70325ad5920b92e6cb93 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 5d06a90e48ea28554c8a7f577665b2378a7aec0b..b8069281aaf2901fe091e549902a4f6f20ac2c6a 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 f58bc0dd7975c7656bcf655e18c1dea08e83cb44..c6a1757a1963a20cf3992a69bbbd718020bb9fb5 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();