From 41cfb930e7b1e68e37e9b138ced39f4d3bd50585 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Sat, 20 Dec 2014 13:23:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Algorithm.h | 2 +- TinySTL/Test/AlgorithmTest.cpp | 120 +++++++++++++++++++++++++++++---- TinySTL/Test/AlgorithmTest.h | 11 +++ 3 files changed, 118 insertions(+), 15 deletions(-) diff --git a/TinySTL/Algorithm.h b/TinySTL/Algorithm.h index 7554dc4..20e01f3 100644 --- a/TinySTL/Algorithm.h +++ b/TinySTL/Algorithm.h @@ -356,7 +356,7 @@ namespace TinySTL{ if (*first1 != *first2) break; } - return make_pair(first1, first2); + return TinySTL::make_pair(first1, first2); } template pair diff --git a/TinySTL/Test/AlgorithmTest.cpp b/TinySTL/Test/AlgorithmTest.cpp index 9650244..1513f76 100644 --- a/TinySTL/Test/AlgorithmTest.cpp +++ b/TinySTL/Test/AlgorithmTest.cpp @@ -84,20 +84,112 @@ namespace TinySTL{ assert(TinySTL::Test::container_equal(myvector, temp)); } + void testFind(){ + std::vector v{ 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + assert(TinySTL::find(v.begin(), v.end(), 5) != v.end()); + assert(TinySTL::find(v.begin(), v.end(), 10) == v.end()); + + assert(TinySTL::find_if(v.begin(), v.end(), [](int i){return i < 0; }) == v.end()); + assert(TinySTL::find_if_not(v.begin(), v.end(), [](int i){return i < 0; }) != v.end()); + } + void testFindEnd(){ + int myints[] = { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 }; + std::vector v(myints, myints + 10); + int needle1[] = { 1, 2, 3 }; + auto it = TinySTL::find_end(v.begin(), v.end(), needle1, needle1 + 3); + assert(it == v.begin() + 5); + + int needle2[] = { 4, 5, 1 }; + it = TinySTL::find_end(v.begin(), v.end(), needle2, needle2 + 3, [](int i, int j){return i == j; }); + assert(it == v.begin() + 3); + } + void testFindFirstOf(){ + int mychars[] = { 'a', 'b', 'c', 'A', 'B', 'C' }; + std::vector v(mychars, mychars + 6); + int needle[] = { 'A', 'B', 'C' }; + auto it = TinySTL::find_first_of(v.begin(), v.end(), needle, needle + 3); + assert(*it == 'A'); + + it = TinySTL::find_first_of(v.begin(), v.end(), needle, needle + 3, + [](char ch1, char ch2){return std::tolower(ch1) == std::tolower(ch2); }); + assert(*it == 'a'); + } + void testAdjacentFind(){ + int myints[] = { 5, 20, 5, 30, 30, 20, 10, 10, 20 }; + std::vector v(myints, myints + 8); + auto it = TinySTL::adjacent_find(v.begin(), v.end()); + assert(*it == 30); + + it = TinySTL::adjacent_find(++it, v.end(), [](int i, int j){return i == j; }); + assert(*it == 10); + } + void testCount(){ + int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 }; // 8 elements + int mycount = TinySTL::count(myints, myints + 8, 10); + assert(mycount == 3); + + mycount = TinySTL::count_if(myints, myints + 8, [](int i){return i % 2 == 0; }); + assert(mycount == 8); + } + void testMismatch(){ + std::vector v; + for (int i = 1; i<6; i++) v.push_back(i * 10); //10 20 30 40 50 + int myints[] = { 10, 20, 80, 320, 1024 }; + TinySTL::pair::iterator, int*> mypair; + mypair = TinySTL::mismatch(v.begin(), v.end(), myints); + assert(*mypair.first == 30 && *mypair.second == 80); + + ++mypair.first; ++mypair.second; + mypair = TinySTL::mismatch(mypair.first, v.end(), mypair.second, [](int i, int j){return i == j; }); + } + void testEqual(){ + int myints[] = { 20, 40, 60, 80, 100 }; + std::vectorv(myints, myints + 5); //20 40 60 80 100 + assert(TinySTL::equal(v.begin(), v.end(), myints)); + + v[3] = 81; + assert(!TinySTL::equal(v.begin(), v.end(), myints, [](int i, int j){return i == j; })); + } + void testIsPermutation(){ + std::array foo = { 1, 2, 3, 4, 5 }; + std::array bar = { 3, 1, 4, 5, 2 }; + + assert(TinySTL::is_permutation(foo.begin(), foo.end(), bar.begin())); + } + void testSearch(){ + std::vector v; + for (int i = 1; i<10; i++) v.push_back(i * 10); + int needle1[] = { 40, 50, 60, 70 }; + auto it = TinySTL::search(v.begin(), v.end(), needle1, needle1 + 4); + assert(it == v.begin() + 3); + + int needle2[] = { 20, 30, 50 }; + it = std::search(v.begin(), v.end(), needle2, needle2 + 3, [](int i, int j){return i == j; }); + assert(it == v.end()); + } } } -using namespace TinySTL::AlgorithmTest; -int main(){ - //testFill(); - //testFillN(); - //testMinMax(); - //testHeapAlgorithm(); - //testIsHeap(); - //testAllOf(); - //testNoneOf(); - //testAnyOf(); - //testForEach(); - system("pause"); - return 0; -} \ No newline at end of file +//using namespace TinySTL::AlgorithmTest; +//int main(){ +// //testFill(); +// //testFillN(); +// //testMinMax(); +// //testHeapAlgorithm(); +// //testIsHeap(); +// //testAllOf(); +// //testNoneOf(); +// //testAnyOf(); +// //testForEach(); +// //testFind(); +// //testFindEnd(); +// //testFindFirstOf(); +// //testAdjacentFind(); +// //testCount(); +// //testMismatch(); +// //testEqual(); +// //testIsPermutation(); +// //testSearch(); +// system("pause"); +// return 0; +//} \ No newline at end of file diff --git a/TinySTL/Test/AlgorithmTest.h b/TinySTL/Test/AlgorithmTest.h index 1f17fc0..b416b3e 100644 --- a/TinySTL/Test/AlgorithmTest.h +++ b/TinySTL/Test/AlgorithmTest.h @@ -7,6 +7,8 @@ #include #include +#include +#include #include #include @@ -21,6 +23,15 @@ namespace TinySTL{ void testNoneOf(); void testAnyOf(); void testForEach(); + void testFind(); + void testFindEnd(); + void testFindFirstOf(); + void testAdjacentFind(); + void testCount(); + void testMismatch(); + void testEqual(); + void testIsPermutation(); + void testSearch(); } }