diff --git a/README.md b/README.md index 6dbb8e827f395061e92a4a379e6941d86c43ae94..cb1cad63945c9e71f46dc0fadcbcc8aee13bc9d7 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ TinySTL * unordered_set:100% * unique_ptr:100% * shared_ptr:100% + * cow_ptr:100% * STL Algorithms: * fill:100% * fill_n:100% @@ -563,6 +564,23 @@ TinySTL up.reset(new string("hello")); assert(*up == "hello"); + + + +####(19):cow_ptr + + cow_ptr cp1(new string("zouxiaohang")); + auto cp2 = cp1, cp3 = cp1; + assert(cp1 == cp2 && cp2 == cp3); + assert(*cp1 == *cp2 && *cp2 == *cp3 && *cp3 == "zouxiaohang"); + + *cp2;//read + assert(cp1 == cp2 && cp2 == cp3); + assert(*cp1 == *cp2 && *cp2 == *cp3 && *cp3 == "zouxiaohang"); + + cp2->append(" C++");//write + assert(*cp1 == *cp3 && *cp3 == "zouxiaohang"); + assert(*cp2 == "zouxiaohang C++"); diff --git a/TinySTL/Test/COWPtrTest.cpp b/TinySTL/Test/COWPtrTest.cpp index 139c1f44cdf21160106fb24547132fe671fe31d6..08667aa2d472e2a903baafe9304a62f573ea8e1a 100644 --- a/TinySTL/Test/COWPtrTest.cpp +++ b/TinySTL/Test/COWPtrTest.cpp @@ -20,12 +20,31 @@ namespace TinySTL{ assert(cp1 == cp2 && !(cp2 != cp3)); + *cp1 = "zouxiaohang"; + assert(*cp1 == "zouxiaohang"); + assert(*cp2 == "hello" && *cp3 == "hello"); + cow_ptr cp4; assert(cp4 == nullptr); } + void testCase2(){ + cow_ptr cp1(new string("zouxiaohang")); + auto cp2 = cp1, cp3 = cp1; + assert(cp1 == cp2 && cp2 == cp3); + assert(*cp1 == *cp2 && *cp2 == *cp3 && *cp3 == "zouxiaohang"); + + *cp2;//read + assert(cp1 == cp2 && cp2 == cp3); + assert(*cp1 == *cp2 && *cp2 == *cp3 && *cp3 == "zouxiaohang"); + + cp2->append(" C++");//write + assert(*cp1 == *cp3 && *cp3 == "zouxiaohang"); + assert(*cp2 == "zouxiaohang C++"); + } void testAllCases(){ testCase1(); + testCase2(); } } } \ No newline at end of file diff --git a/TinySTL/main.cpp b/TinySTL/main.cpp index 4c8bd6045237ef05c2b9ea16f33388ee273b8868..ebb5f91b9ad8c09c89a569775284ac2ca853d49a 100644 --- a/TinySTL/main.cpp +++ b/TinySTL/main.cpp @@ -26,6 +26,8 @@ #include "Test\VectorTest.h" using namespace TinySTL::Profiler; +#include +#include int main(){ TinySTL::AlgorithmTest::testAllCases(); @@ -50,6 +52,10 @@ int main(){ TinySTL::Unordered_setTest::testAllCases(); TinySTL::VectorTest::testAllCases(); + std::string s; + if (std::is_const::value){ + std::cout << "const" << std::endl; + } system("pause"); return 0; } \ No newline at end of file