diff --git a/TinySTL/Test/AlgorithmTest.cpp b/TinySTL/Test/AlgorithmTest.cpp index 7a320ad..7090d31 100644 --- a/TinySTL/Test/AlgorithmTest.cpp +++ b/TinySTL/Test/AlgorithmTest.cpp @@ -1,195 +1,193 @@ -//#include "AlgorithmTest.h" -// -//namespace TinySTL{ -// namespace AlgorithmTest{ -// void testFill(){ -// std::vector v1(8), v2(8); -// std::fill(v1.begin(), v1.begin() + 4, 5); //5 5 5 5 0 0 0 0 -// std::fill(v1.begin() + 3, v1.end() - 2, 8); //5 5 5 8 8 8 0 0 -// TinySTL::fill(v2.begin(), v2.begin() + 4, 5); //5 5 5 5 0 0 0 0 -// TinySTL::fill(v2.begin() + 3, v2.end() - 2, 8); //5 5 5 8 8 8 0 0 -// -// assert(TinySTL::Test::container_equal(v1, v2)); -// } -// void testFillN(){ -// std::vector v1(8, 10), v2(8, 10); -// std::fill_n(v1.begin(), 4, 20); //20 20 20 20 10 10 10 10 -// std::fill_n(v1.begin() + 3, 3, 33); //20 20 20 33 33 33 10 10 -// TinySTL::fill_n(v2.begin(), 4, 20); //20 20 20 20 10 10 10 10 -// TinySTL::fill_n(v2.begin() + 3, 3, 33); //20 20 20 33 33 33 10 10 -// -// assert(TinySTL::Test::container_equal(v1, v2)); -// } -// void testMinMax(){ -// assert(TinySTL::min(1, 2) == 1); -// assert(TinySTL::min(2, 1) == 1); -// assert(TinySTL::min('a', 'z') == 'a'); -// assert(TinySTL::min(3.14, 2.72) == 2.72); -// -// assert(TinySTL::max(1, 2) == 2); -// assert(TinySTL::max(2, 1) == 2); -// assert(TinySTL::max('a', 'z') == 'z'); -// assert(TinySTL::max(3.14, 2.73) == 3.14); -// } -// void testHeapAlgorithm(){ -// int myints[] = { 10, 20, 30, 5, 15 }; -// std::vector v1(myints, myints + 5); -// std::vector v2(myints, myints + 5); -// -// std::make_heap(v1.begin(), v1.end()); -// TinySTL::make_heap(v2.begin(), v2.end()); -// assert(TinySTL::Test::container_equal(v1, v2)); -// -// std::pop_heap(v1.begin(), v1.end()); v1.pop_back(); -// TinySTL::pop_heap(v2.begin(), v2.end()); v2.pop_back(); -// assert(TinySTL::Test::container_equal(v1, v2)); -// -// v1.push_back(99); std::push_heap(v1.begin(), v1.end()); -// v2.push_back(99); TinySTL::push_heap(v2.begin(), v2.end()); -// assert(TinySTL::Test::container_equal(v1, v2)); -// -// std::sort_heap(v1.begin(), v1.end()); -// TinySTL::sort_heap(v2.begin(), v2.end()); -// assert(TinySTL::Test::container_equal(v1, v2)); -// } -// void testIsHeap(){ -// std::vector v1{ 9, 5, 2, 6, 4, 1, 3, 8, 7 }; -// std::vector v2{ 9, 5, 2, 6, 4, 1, 3, 8, 7 }; -// -// if (!std::is_heap(v1.begin(), v1.end())) -// std::make_heap(v1.begin(), v1.end()); -// if (!TinySTL::is_heap(v2.begin(), v2.end())) -// TinySTL::make_heap(v2.begin(), v2.end()); -// -// assert(TinySTL::Test::container_equal(v1, v2)); -// } -// void testAllOf(){ -// std::array foo = { 3, 5, 7, 11, 13, 17, 19, 23 }; -// assert(TinySTL::all_of(foo.begin(), foo.end(), [](int i){return i % 2; })); -// } -// void testNoneOf(){ -// std::array foo = { 1, 2, 4, 8, 16, 32, 64, 128 }; -// assert(TinySTL::none_of(foo.begin(), foo.end(), [](int i){return i < 0; })); -// } -// void testAnyOf(){ -// std::array foo = { 0, 1, -1, 3, -3, 5, -5 }; -// assert(std::any_of(foo.begin(), foo.end(), [](int i){return i < 0; })); -// } -// void testForEach(){ -// std::vector myvector{ 10, 20, 30 }; -// std::vector temp{ 11, 21, 31 }; -// TinySTL::for_each(myvector.begin(), myvector.end(), [&myvector](int& i){ -// ++i; -// }); -// -// 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(); -// //testFind(); -// //testFindEnd(); -// //testFindFirstOf(); -// //testAdjacentFind(); -// //testCount(); -// //testMismatch(); -// //testEqual(); -// //testIsPermutation(); -// //testSearch(); -// system("pause"); -// return 0; -//} \ No newline at end of file +#include "AlgorithmTest.h" + +namespace TinySTL{ + namespace AlgorithmTest{ + void testFill(){ + std::vector v1(8), v2(8); + std::fill(v1.begin(), v1.begin() + 4, 5); //5 5 5 5 0 0 0 0 + std::fill(v1.begin() + 3, v1.end() - 2, 8); //5 5 5 8 8 8 0 0 + TinySTL::fill(v2.begin(), v2.begin() + 4, 5); //5 5 5 5 0 0 0 0 + TinySTL::fill(v2.begin() + 3, v2.end() - 2, 8); //5 5 5 8 8 8 0 0 + + assert(TinySTL::Test::container_equal(v1, v2)); + } + void testFillN(){ + std::vector v1(8, 10), v2(8, 10); + std::fill_n(v1.begin(), 4, 20); //20 20 20 20 10 10 10 10 + std::fill_n(v1.begin() + 3, 3, 33); //20 20 20 33 33 33 10 10 + TinySTL::fill_n(v2.begin(), 4, 20); //20 20 20 20 10 10 10 10 + TinySTL::fill_n(v2.begin() + 3, 3, 33); //20 20 20 33 33 33 10 10 + + assert(TinySTL::Test::container_equal(v1, v2)); + } + void testMinMax(){ + assert(TinySTL::min(1, 2) == 1); + assert(TinySTL::min(2, 1) == 1); + assert(TinySTL::min('a', 'z') == 'a'); + assert(TinySTL::min(3.14, 2.72) == 2.72); + + assert(TinySTL::max(1, 2) == 2); + assert(TinySTL::max(2, 1) == 2); + assert(TinySTL::max('a', 'z') == 'z'); + assert(TinySTL::max(3.14, 2.73) == 3.14); + } + void testHeapAlgorithm(){ + int myints[] = { 10, 20, 30, 5, 15 }; + std::vector v1(myints, myints + 5); + std::vector v2(myints, myints + 5); + + std::make_heap(v1.begin(), v1.end()); + TinySTL::make_heap(v2.begin(), v2.end()); + assert(TinySTL::Test::container_equal(v1, v2)); + + std::pop_heap(v1.begin(), v1.end()); v1.pop_back(); + TinySTL::pop_heap(v2.begin(), v2.end()); v2.pop_back(); + assert(TinySTL::Test::container_equal(v1, v2)); + + v1.push_back(99); std::push_heap(v1.begin(), v1.end()); + v2.push_back(99); TinySTL::push_heap(v2.begin(), v2.end()); + assert(TinySTL::Test::container_equal(v1, v2)); + + std::sort_heap(v1.begin(), v1.end()); + TinySTL::sort_heap(v2.begin(), v2.end()); + assert(TinySTL::Test::container_equal(v1, v2)); + } + void testIsHeap(){ + std::vector v1{ 9, 5, 2, 6, 4, 1, 3, 8, 7 }; + std::vector v2{ 9, 5, 2, 6, 4, 1, 3, 8, 7 }; + + if (!std::is_heap(v1.begin(), v1.end())) + std::make_heap(v1.begin(), v1.end()); + if (!TinySTL::is_heap(v2.begin(), v2.end())) + TinySTL::make_heap(v2.begin(), v2.end()); + + assert(TinySTL::Test::container_equal(v1, v2)); + } + void testAllOf(){ + std::array foo = { 3, 5, 7, 11, 13, 17, 19, 23 }; + assert(TinySTL::all_of(foo.begin(), foo.end(), [](int i){return i % 2; })); + } + void testNoneOf(){ + std::array foo = { 1, 2, 4, 8, 16, 32, 64, 128 }; + assert(TinySTL::none_of(foo.begin(), foo.end(), [](int i){return i < 0; })); + } + void testAnyOf(){ + std::array foo = { 0, 1, -1, 3, -3, 5, -5 }; + assert(std::any_of(foo.begin(), foo.end(), [](int i){return i < 0; })); + } + void testForEach(){ + std::vector myvector{ 10, 20, 30 }; + std::vector temp{ 11, 21, 31 }; + TinySTL::for_each(myvector.begin(), myvector.end(), [&myvector](int& i){ + ++i; + }); + + 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()); + } + + + void testAllCases(){ + testFill(); + testFillN(); + testMinMax(); + testHeapAlgorithm(); + testIsHeap(); + testAllOf(); + testNoneOf(); + testAnyOf(); + testForEach(); + testFind(); + testFindEnd(); + testFindFirstOf(); + testAdjacentFind(); + testCount(); + testMismatch(); + testEqual(); + testIsPermutation(); + testSearch(); + } + } +} \ No newline at end of file diff --git a/TinySTL/Test/AlgorithmTest.h b/TinySTL/Test/AlgorithmTest.h index b416b3e..f25dd9b 100644 --- a/TinySTL/Test/AlgorithmTest.h +++ b/TinySTL/Test/AlgorithmTest.h @@ -32,6 +32,8 @@ namespace TinySTL{ void testEqual(); void testIsPermutation(); void testSearch(); + + void testAllCases(); } } diff --git a/TinySTL/Test/BitmapTest.cpp b/TinySTL/Test/BitmapTest.cpp index eb4637c..022d454 100644 --- a/TinySTL/Test/BitmapTest.cpp +++ b/TinySTL/Test/BitmapTest.cpp @@ -1,94 +1,91 @@ -//#include "BitmapTest.h" -// -//namespace TinySTL{ -// namespace BitmapTest{ -// void testCase1(){ -// bitmap<1> bt1; -// assert(bt1.size() == 8); -// -// bitmap<7> bt2; -// assert(bt2.size() == 8); -// -// bitmap<127> bt3; -// assert(bt3.size() == 128); -// } -// void testCase2(){ -// bitmap<8> bt1, bt2; -// bt1.set(); -// cout << bt1 << endl; -// bt1.reset(); -// cout << bt1 << endl; -// -// bt2.set(0); bt2.set(2); bt2.set(4); -// cout << bt2 << endl; -// bt2.reset(0); bt2.reset(2); bt2.reset(4); -// cout << bt2 << endl; -// } -// void testCase3(){ -// bitmap<8> bt; -// bt.flip(); -// cout << bt << endl; -// -// bt.flip(0); -// cout << bt << endl; -// } -// void testCase4(){ -// bitmap<8> bt; -// bt.set(); -// assert(bt.count() == 8); -// -// bt.flip(); -// assert(bt.count() == 0); -// -// bt.set(0); -// assert(bt.count() == 1); -// } -// void testCase5(){ -// bitmap<8> bt; -// assert(!bt.test(0)); -// -// bt.set(0); -// assert(bt.test(0)); -// } -// void testCase6(){ -// bitmap<8> bt; -// assert(!bt.any()); -// assert(bt.none()); -// assert(!bt.all()); -// -// bt.set(0); -// assert(bt.any()); -// assert(!bt.none()); -// assert(!bt.all()); -// -// bt.set(); -// assert(bt.any()); -// assert(!bt.none()); -// assert(bt.all()); -// -// bt.reset(); -// assert(!bt.any()); -// assert(bt.none()); -// assert(!bt.all()); -// } -// void testCase7(){ -// bitmap<8> bt; -// bt.set(0); bt.set(2); bt.set(4); bt.set(6); -// assert(bt.to_string() == TinySTL::string("10101010")); -// } -// } -//} -// -//using namespace TinySTL::BitmapTest; -//int main(){ -// //testCase1(); -// //testCase2(); -// //testCase3(); -// //testCase4(); -// //testCase5(); -// //testCase6(); -// //testCase7(); -// -// system("pause"); -// return 0; -//} \ No newline at end of file +#include "BitmapTest.h" + +namespace TinySTL{ + namespace BitmapTest{ + void testCase1(){ + bitmap<1> bt1; + assert(bt1.size() == 8); + + bitmap<7> bt2; + assert(bt2.size() == 8); + + bitmap<127> bt3; + assert(bt3.size() == 128); + } + void testCase2(){ + bitmap<8> bt1, bt2; + bt1.set(); + cout << bt1 << endl; + bt1.reset(); + cout << bt1 << endl; + + bt2.set(0); bt2.set(2); bt2.set(4); + cout << bt2 << endl; + bt2.reset(0); bt2.reset(2); bt2.reset(4); + cout << bt2 << endl; + } + void testCase3(){ + bitmap<8> bt; + bt.flip(); + cout << bt << endl; + + bt.flip(0); + cout << bt << endl; + } + void testCase4(){ + bitmap<8> bt; + bt.set(); + assert(bt.count() == 8); + + bt.flip(); + assert(bt.count() == 0); + + bt.set(0); + assert(bt.count() == 1); + } + void testCase5(){ + bitmap<8> bt; + assert(!bt.test(0)); + + bt.set(0); + assert(bt.test(0)); + } + void testCase6(){ + bitmap<8> bt; + assert(!bt.any()); + assert(bt.none()); + assert(!bt.all()); + + bt.set(0); + assert(bt.any()); + assert(!bt.none()); + assert(!bt.all()); + + bt.set(); + assert(bt.any()); + assert(!bt.none()); + assert(bt.all()); + + bt.reset(); + assert(!bt.any()); + assert(bt.none()); + assert(!bt.all()); + } + void testCase7(){ + bitmap<8> bt; + bt.set(0); bt.set(2); bt.set(4); bt.set(6); + assert(bt.to_string() == TinySTL::string("10101010")); + } + + + void testAllCases(){ + testCase1(); + testCase2(); + testCase3(); + testCase4(); + testCase5(); + testCase6(); + testCase7(); + } + } +} \ No newline at end of file diff --git a/TinySTL/Test/BitmapTest.h b/TinySTL/Test/BitmapTest.h index 43d4340..431b19c 100644 --- a/TinySTL/Test/BitmapTest.h +++ b/TinySTL/Test/BitmapTest.h @@ -20,6 +20,8 @@ namespace TinySTL{ void testCase5(); void testCase6(); void testCase7(); + + void testAllCases(); } } diff --git a/TinySTL/Test/CircularBufferTest.cpp b/TinySTL/Test/CircularBufferTest.cpp index 100f76a..f888ffb 100644 --- a/TinySTL/Test/CircularBufferTest.cpp +++ b/TinySTL/Test/CircularBufferTest.cpp @@ -1,85 +1,82 @@ -//#include "CircularBufferTest.h" -// -//namespace TinySTL{ -// namespace CircularBufferTest{ -// void testCase1(){ -// tsCB cb1(10, 1); -// for (auto i = 0; i != 10; ++i) -// assert(cb1[i] == 1); -// -// int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; -// tsCB cb2(std::begin(arr), std::end(arr)); -// for (auto i = 0; i != 10; ++i) -// assert(cb2[i] == i); -// -// auto cb3(cb2); -// assert(circular_buffer_equal(cb2, cb3)); -// -// auto cb4(std::move(cb2));//cb2 clear -// assert(circular_buffer_equal(cb3, cb4)); -// -// auto cb5 = cb3; -// assert(circular_buffer_equal(cb3, cb5)); -// -// auto cb6 = std::move(cb3);//cb3 clear -// assert(circular_buffer_equal(cb5, cb6)); -// } -// void testCase2(){ -// tsCB cb(1); -// assert(cb.size() == 1); -// cb.pop_front(); -// assert(!cb.full()); -// assert(cb.size() == 0); -// assert(cb.empty()); -// -// cb.push_back(1), cb.push_back(2); -// assert(cb.full()); -// assert(!cb.empty()); -// assert(cb.size() == 2); -// } -// void testCase3(){ -// tsCB cb(3); -// cb[0] = "one", cb[1] = "two", cb[2] = "three"; -// -// assert(*(cb.first()) == "one" && *(cb.last()) == "three"); -// -// assert(cb.front() == "one" && cb.back() == "three"); -// } -// void testCase4(){ -// tsCB cb(1); -// assert(cb.front() == std::string()); -// -// cb.push_back("zxh"); cb.push_back("jwl"); -// assert(cb.back() == "jwl"); -// cb.pop_front(); -// assert(cb.front() == "zxh"); -// } -// void testCase5(){ -// tsCB cb1(3), cb2(3); -// assert(cb1 == cb2); -// assert(!(cb1 != cb2)); -// -// cb1[0] = -1; -// assert(!(cb1 == cb2)); -// assert(cb1 != cb2); -// } -// void testCase6(){ -// std::string arr[] = { "1", "2", "3" }; -// tsCB cb(std::begin(arr), std::end(arr)); -// std::cout << cb << std::endl; -// } -// } -//} -// -////using namespace TinySTL::CircularBufferTest; -////int main(){ -//// testCase1(); -//// testCase2(); -//// testCase3(); -//// testCase4(); -//// testCase5(); -//// testCase6(); -//// -//// system("pause"); -//// return 0; -////} \ No newline at end of file +#include "CircularBufferTest.h" + +namespace TinySTL{ + namespace CircularBufferTest{ + void testCase1(){ + tsCB cb1(10, 1); + for (auto i = 0; i != 10; ++i) + assert(cb1[i] == 1); + + int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + tsCB cb2(std::begin(arr), std::end(arr)); + for (auto i = 0; i != 10; ++i) + assert(cb2[i] == i); + + auto cb3(cb2); + assert(circular_buffer_equal(cb2, cb3)); + + auto cb4(std::move(cb2));//cb2 clear + assert(circular_buffer_equal(cb3, cb4)); + + auto cb5 = cb3; + assert(circular_buffer_equal(cb3, cb5)); + + auto cb6 = std::move(cb3);//cb3 clear + assert(circular_buffer_equal(cb5, cb6)); + } + void testCase2(){ + tsCB cb(1); + assert(cb.size() == 1); + cb.pop_front(); + assert(!cb.full()); + assert(cb.size() == 0); + assert(cb.empty()); + + cb.push_back(1), cb.push_back(2); + assert(cb.full()); + assert(!cb.empty()); + assert(cb.size() == 2); + } + void testCase3(){ + tsCB cb(3); + cb[0] = "one", cb[1] = "two", cb[2] = "three"; + + assert(*(cb.first()) == "one" && *(cb.last()) == "three"); + + assert(cb.front() == "one" && cb.back() == "three"); + } + void testCase4(){ + tsCB cb(1); + assert(cb.front() == std::string()); + + cb.push_back("zxh"); cb.push_back("jwl"); + assert(cb.back() == "jwl"); + cb.pop_front(); + assert(cb.front() == "zxh"); + } + void testCase5(){ + tsCB cb1(3), cb2(3); + assert(cb1 == cb2); + assert(!(cb1 != cb2)); + + cb1[0] = -1; + assert(!(cb1 == cb2)); + assert(cb1 != cb2); + } + void testCase6(){ + std::string arr[] = { "1", "2", "3" }; + tsCB cb(std::begin(arr), std::end(arr)); + std::cout << cb << std::endl; + } + + + void testAllCases(){ + testCase1(); + testCase2(); + testCase3(); + testCase4(); + testCase5(); + testCase6(); + } + } +} \ No newline at end of file diff --git a/TinySTL/Test/CircularBufferTest.h b/TinySTL/Test/CircularBufferTest.h index f5a8c18..fde9b6d 100644 --- a/TinySTL/Test/CircularBufferTest.h +++ b/TinySTL/Test/CircularBufferTest.h @@ -30,6 +30,8 @@ namespace TinySTL{ void testCase4(); void testCase5(); void testCase6(); + + void testAllCases(); } } diff --git a/TinySTL/Test/PairTest.cpp b/TinySTL/Test/PairTest.cpp index 2653372..340c453 100644 --- a/TinySTL/Test/PairTest.cpp +++ b/TinySTL/Test/PairTest.cpp @@ -1,64 +1,63 @@ -//#include "PairTest.h" -// -//namespace TinySTL{ -// namespace PairTest{ -// template -// static inline bool container_equal(const Container1& pair1, const Container2& pair2){ -// return (pair1.first == pair2.first && pair1.second == pair2.second); -// } -// void testCase1(){ -// stdPair p1(5, 5); -// tsPair p2(5, 5); -// assert(container_equal(p1, p2)); -// } -// void testCase2(){ -// stdPair p1(stdPair(0, 0)); -// tsPair p2(tsPair(0, 0)); -// assert(container_equal(p1, p2)); -// } -// void testCase3(){ -// stdPair temp1 = std::make_pair(std::string("zxh"), std::string("zxh")); -// stdPair p1 = temp1; -// -// tsPair temp2 = TinySTL::make_pair(std::string("zxh"), std::string("zxh")); -// tsPair p2 = temp2; -// -// assert(container_equal(p1, p2)); -// } -// void testCase4(){ -// TinySTL::pair foo(10, 'z'); -// TinySTL::pair bar(90, 'a'); -// -// //foo and bar are not equal -// //foo is less than bar -// //foo is less than or equal to bar -// if (foo == bar) std::cout << "foo and bar are equal\n"; -// if (foo != bar) std::cout << "foo and bar are not equal\n"; -// if (foo< bar) std::cout << "foo is less than bar\n"; -// if (foo> bar) std::cout << "foo is greater than bar\n"; -// if (foo <= bar) std::cout << "foo is less than or equal to bar\n"; -// if (foo >= bar) std::cout << "foo is greater than or equal to bar\n"; -// } -// void testCase5(){ -// TinySTL::pair foo(10, 'z'); -// TinySTL::pair bar(90, 'a'); -// -// foo.swap(bar); -// -// std::cout << "foo : (" << foo.first << ", " << foo.second << ")" << std::endl; -// std::cout << "bar : (" << bar.first << ", " << bar.second << ")" << std::endl; -// //TinySTL::Test::print_container(foo); -// //TinySTL::Test::print_container(bar); -// } -// } -//} -////using namespace TinySTL::PairTest; -////int main(){ -//// testCase1(); -//// testCase2(); -//// testCase3(); -//// testCase4(); -//// testCase5(); -//// system("pause"); -//// return 0; -////} \ No newline at end of file +#include "PairTest.h" + +namespace TinySTL{ + namespace PairTest{ + template + static inline bool container_equal(const Container1& pair1, const Container2& pair2){ + return (pair1.first == pair2.first && pair1.second == pair2.second); + } + void testCase1(){ + stdPair p1(5, 5); + tsPair p2(5, 5); + assert(container_equal(p1, p2)); + } + void testCase2(){ + stdPair p1(stdPair(0, 0)); + tsPair p2(tsPair(0, 0)); + assert(container_equal(p1, p2)); + } + void testCase3(){ + stdPair temp1 = std::make_pair(std::string("zxh"), std::string("zxh")); + stdPair p1 = temp1; + + tsPair temp2 = TinySTL::make_pair(std::string("zxh"), std::string("zxh")); + tsPair p2 = temp2; + + assert(container_equal(p1, p2)); + } + void testCase4(){ + TinySTL::pair foo(10, 'z'); + TinySTL::pair bar(90, 'a'); + + //foo and bar are not equal + //foo is less than bar + //foo is less than or equal to bar + if (foo == bar) std::cout << "foo and bar are equal\n"; + if (foo != bar) std::cout << "foo and bar are not equal\n"; + if (foo< bar) std::cout << "foo is less than bar\n"; + if (foo> bar) std::cout << "foo is greater than bar\n"; + if (foo <= bar) std::cout << "foo is less than or equal to bar\n"; + if (foo >= bar) std::cout << "foo is greater than or equal to bar\n"; + } + void testCase5(){ + TinySTL::pair foo(10, 'z'); + TinySTL::pair bar(90, 'a'); + + foo.swap(bar); + + std::cout << "foo : (" << foo.first << ", " << foo.second << ")" << std::endl; + std::cout << "bar : (" << bar.first << ", " << bar.second << ")" << std::endl; + //TinySTL::Test::print_container(foo); + //TinySTL::Test::print_container(bar); + } + + + void testAllCases(){ + testCase1(); + testCase2(); + testCase3(); + testCase4(); + testCase5(); + } + } +} \ No newline at end of file diff --git a/TinySTL/Test/PairTest.h b/TinySTL/Test/PairTest.h index a150a35..893c98b 100644 --- a/TinySTL/Test/PairTest.h +++ b/TinySTL/Test/PairTest.h @@ -22,6 +22,8 @@ namespace TinySTL{ void testCase3(); void testCase4(); void testCase5(); + + void testAllCases(); } } #endif \ No newline at end of file diff --git a/TinySTL/Test/PriorityQueueTest.cpp b/TinySTL/Test/PriorityQueueTest.cpp index 111caed..0897bc0 100644 --- a/TinySTL/Test/PriorityQueueTest.cpp +++ b/TinySTL/Test/PriorityQueueTest.cpp @@ -1,79 +1,76 @@ -//#include "PriorityQueueTest.h" -// -//namespace TinySTL{ -// namespace PriorityQueueTest{ -// void testCase1(){ -// int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, -1, -2, -3 }; -// stdPQ pq1(std::begin(arr), std::end(arr)); -// tsPQ pq2(std::begin(arr), std::end(arr)); -// -// while (!pq1.empty() && !pq2.empty()){ -// assert(pq1.top() == pq2.top()); -// pq1.pop(); pq2.pop(); -// } -// assert(pq1.empty() && pq2.empty()); -// } -// void testCase2(){ -// tsPQ pq; -// assert(pq.empty()); -// -// pq.push("zxh"); -// assert(!pq.empty()); -// } -// void testCase3(){ -// tsPQ pq; -// auto i = 1; -// for (; i != 10; ++i){ -// pq.push(i); -// assert(pq.size() == i); -// } -// for (i = pq.size(); i != 0; --i){ -// pq.pop(); -// assert(pq.size() == (i - 1)); -// } -// } -// void testCase4(){ -// stdPQ pq1; -// tsPQ pq2; -// -// pq1.push(30); -// pq1.push(100); -// pq1.push(25); -// pq1.push(40); -// -// pq2.push(30); -// pq2.push(100); -// pq2.push(25); -// pq2.push(40); -// -// while (!pq1.empty() && !pq2.empty()){ -// assert(pq1.top() == pq2.top()); -// pq1.pop(); -// pq2.pop(); -// } -// } -// void testCase5(){ -// tsPQ foo, bar; -// foo.push(15); foo.push(30); foo.push(10); -// bar.push(101); bar.push(202); -// -// assert(foo.size() == 3 && bar.size() == 2); -// foo.swap(bar); -// assert(foo.size() == 2 && bar.size() == 3); -// -// TinySTL::swap(foo, bar); -// assert(foo.size() == 3 && bar.size() == 2); -// } -// } -//} -// -////using namespace TinySTL::PriorityQueueTest; -////int main(){ -//// testCase1(); -//// testCase2(); -//// testCase3(); -//// testCase4(); -//// testCase5(); -//// system("pause"); -//// return 0; -////} \ No newline at end of file +#include "PriorityQueueTest.h" + +namespace TinySTL{ + namespace PriorityQueueTest{ + void testCase1(){ + int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, -1, -2, -3 }; + stdPQ pq1(std::begin(arr), std::end(arr)); + tsPQ pq2(std::begin(arr), std::end(arr)); + + while (!pq1.empty() && !pq2.empty()){ + assert(pq1.top() == pq2.top()); + pq1.pop(); pq2.pop(); + } + assert(pq1.empty() && pq2.empty()); + } + void testCase2(){ + tsPQ pq; + assert(pq.empty()); + + pq.push("zxh"); + assert(!pq.empty()); + } + void testCase3(){ + tsPQ pq; + auto i = 1; + for (; i != 10; ++i){ + pq.push(i); + assert(pq.size() == i); + } + for (i = pq.size(); i != 0; --i){ + pq.pop(); + assert(pq.size() == (i - 1)); + } + } + void testCase4(){ + stdPQ pq1; + tsPQ pq2; + + pq1.push(30); + pq1.push(100); + pq1.push(25); + pq1.push(40); + + pq2.push(30); + pq2.push(100); + pq2.push(25); + pq2.push(40); + + while (!pq1.empty() && !pq2.empty()){ + assert(pq1.top() == pq2.top()); + pq1.pop(); + pq2.pop(); + } + } + void testCase5(){ + tsPQ foo, bar; + foo.push(15); foo.push(30); foo.push(10); + bar.push(101); bar.push(202); + + assert(foo.size() == 3 && bar.size() == 2); + foo.swap(bar); + assert(foo.size() == 2 && bar.size() == 3); + + TinySTL::swap(foo, bar); + assert(foo.size() == 3 && bar.size() == 2); + } + + void testAllCases(){ + testCase1(); + testCase2(); + testCase3(); + testCase4(); + testCase5(); + } + } +} \ No newline at end of file diff --git a/TinySTL/Test/PriorityQueueTest.h b/TinySTL/Test/PriorityQueueTest.h index d61850e..c2f25bf 100644 --- a/TinySTL/Test/PriorityQueueTest.h +++ b/TinySTL/Test/PriorityQueueTest.h @@ -22,6 +22,8 @@ namespace TinySTL{ void testCase3(); void testCase4(); void testCase5(); + + void testAllCases(); } } diff --git a/TinySTL/Test/QueueTest.cpp b/TinySTL/Test/QueueTest.cpp index d81f762..8abed20 100644 --- a/TinySTL/Test/QueueTest.cpp +++ b/TinySTL/Test/QueueTest.cpp @@ -1,69 +1,66 @@ -//#include "QueueTest.h" -// -//namespace TinySTL{ -// namespace QueueTest{ -// void testCase1(){ -// stdQ q1; -// tsQ q2; -// -// for (auto i = 0; i != 10; ++i){ -// q1.push(i); -// q2.push(i); -// } -// for (auto i = 0; i != 10; ++i){ -// assert(q1.front() == q2.front()); -// q1.pop(); -// q2.pop(); -// } -// } -// void testCase2(){ -// tsQ q1; -// for (auto i = 0; i != 10; ++i) -// q1.push(i); -// auto q2(q1); -// assert(q1 == q2); -// assert(!(q1 != q2)); -// } -// void testCase3(){ -// tsQ q; -// assert(q.empty()); -// assert(q.size() == 0); -// -// q.push(10); -// q.push(11); -// assert(!q.empty()); -// assert(q.size() == 2); -// } -// void testCase4(){ -// tsQ q; -// q.push("front"); -// q.push("back"); -// -// assert(q.front() == "front"); -// assert(q.back() == "back"); -// } -// void testCase5(){ -// tsQ q1, q2; -// -// q1.push(1); q1.push(2); q1.push(3); -// q2.push(1); q2.push(2); -// -// assert(q1.size() == 3 && q2.size() == 2); -// q1.swap(q2); -// assert(q1.size() == 2 && q2.size() == 3); -// TinySTL::swap(q1, q2); -// assert(q1.size() == 3 && q2.size() == 2); -// } -// } -//} -// -////using namespace TinySTL::QueueTest; -////int main(){ -//// testCase1(); -//// testCase2(); -//// testCase3(); -//// testCase4(); -//// testCase5(); -//// system("pause"); -//// return 0; -////} \ No newline at end of file +#include "QueueTest.h" + +namespace TinySTL{ + namespace QueueTest{ + void testCase1(){ + stdQ q1; + tsQ q2; + + for (auto i = 0; i != 10; ++i){ + q1.push(i); + q2.push(i); + } + for (auto i = 0; i != 10; ++i){ + assert(q1.front() == q2.front()); + q1.pop(); + q2.pop(); + } + } + void testCase2(){ + tsQ q1; + for (auto i = 0; i != 10; ++i) + q1.push(i); + auto q2(q1); + assert(q1 == q2); + assert(!(q1 != q2)); + } + void testCase3(){ + tsQ q; + assert(q.empty()); + assert(q.size() == 0); + + q.push(10); + q.push(11); + assert(!q.empty()); + assert(q.size() == 2); + } + void testCase4(){ + tsQ q; + q.push("front"); + q.push("back"); + + assert(q.front() == "front"); + assert(q.back() == "back"); + } + void testCase5(){ + tsQ q1, q2; + + q1.push(1); q1.push(2); q1.push(3); + q2.push(1); q2.push(2); + + assert(q1.size() == 3 && q2.size() == 2); + q1.swap(q2); + assert(q1.size() == 2 && q2.size() == 3); + TinySTL::swap(q1, q2); + assert(q1.size() == 3 && q2.size() == 2); + } + + void testAllCases(){ + testCase1(); + testCase2(); + testCase3(); + testCase4(); + testCase5(); + } + } +} \ No newline at end of file diff --git a/TinySTL/Test/QueueTest.h b/TinySTL/Test/QueueTest.h index 80d3e5f..03376c4 100644 --- a/TinySTL/Test/QueueTest.h +++ b/TinySTL/Test/QueueTest.h @@ -21,6 +21,8 @@ namespace TinySTL{ void testCase3(); void testCase4(); void testCase5(); + + void testAllCases(); } } diff --git a/TinySTL/Test/StackTest.cpp b/TinySTL/Test/StackTest.cpp index f1370ef..68d7f84 100644 --- a/TinySTL/Test/StackTest.cpp +++ b/TinySTL/Test/StackTest.cpp @@ -1,59 +1,55 @@ -//#include "StackTest.h" -// -//namespace TinySTL{ -// namespace StackTest{ -// void testCase1(){ -// stdSt st1; -// tsSt st2; -// -// for (auto i = 0; i != 10; ++i){ -// st1.push(i); -// st2.push(i); -// } -// for (auto i = 0; i != 10; ++i){ -// assert(st1.top() == st2.top()); -// st1.pop(); -// st2.pop(); -// } -// } -// void testCase2(){ -// tsSt st; -// assert(st.empty()); -// assert(st.size() == 0); -// -// st.push("one"); -// st.push("two"); -// assert(!st.empty()); -// assert(st.size() == 2); -// } -// void testCase3(){ -// tsSt st1; -// for (auto i = 0; i != 5; ++i) -// st1.push(i); -// auto st2(st1); -// assert(st1 == st2); -// assert(!(st1 != st2)); -// } -// void testCase4(){ -// tsSt st1, st2; -// st1.push(1); st1.push(2); st1.push(3); -// st2.push(1); st2.push(2); -// assert(st1.size() == 3 && st2.size() == 2); -// st1.swap(st2); -// assert(st1.size() == 2 && st2.size() == 3); -// TinySTL::swap(st1, st2); -// assert(st1.size() == 3 && st2.size() == 2); -// } -// } -//} -// -////using namespace TinySTL::StackTest; -////int main(){ -//// testCase1(); -//// testCase2(); -//// testCase3(); -//// testCase4(); -//// -//// system("pause"); -//// return 0; -////} \ No newline at end of file +#include "StackTest.h" + +namespace TinySTL{ + namespace StackTest{ + void testCase1(){ + stdSt st1; + tsSt st2; + + for (auto i = 0; i != 10; ++i){ + st1.push(i); + st2.push(i); + } + for (auto i = 0; i != 10; ++i){ + assert(st1.top() == st2.top()); + st1.pop(); + st2.pop(); + } + } + void testCase2(){ + tsSt st; + assert(st.empty()); + assert(st.size() == 0); + + st.push("one"); + st.push("two"); + assert(!st.empty()); + assert(st.size() == 2); + } + void testCase3(){ + tsSt st1; + for (auto i = 0; i != 5; ++i) + st1.push(i); + auto st2(st1); + assert(st1 == st2); + assert(!(st1 != st2)); + } + void testCase4(){ + tsSt st1, st2; + st1.push(1); st1.push(2); st1.push(3); + st2.push(1); st2.push(2); + assert(st1.size() == 3 && st2.size() == 2); + st1.swap(st2); + assert(st1.size() == 2 && st2.size() == 3); + TinySTL::swap(st1, st2); + assert(st1.size() == 3 && st2.size() == 2); + } + + void testAllCases(){ + testCase1(); + testCase2(); + testCase3(); + testCase4(); + } + } +} \ No newline at end of file diff --git a/TinySTL/Test/StackTest.h b/TinySTL/Test/StackTest.h index ad45679..8f74230 100644 --- a/TinySTL/Test/StackTest.h +++ b/TinySTL/Test/StackTest.h @@ -20,6 +20,8 @@ namespace TinySTL{ void testCase2(); void testCase3(); void testCase4(); + + void testAllCases(); } } diff --git a/TinySTL/Test/StringTest.cpp b/TinySTL/Test/StringTest.cpp index 2bf5e86..47689dd 100644 --- a/TinySTL/Test/StringTest.cpp +++ b/TinySTL/Test/StringTest.cpp @@ -1,484 +1,481 @@ -//#include "StringTest.h" -// -//namespace TinySTL{ -// namespace StringTest{ -// -// void testCase1(){ -// const char *ptr = "hello world"; -// -// stdStr s1(ptr); -// tsStr s2(ptr); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// stdStr s3(ptr, 5); -// tsStr s4(ptr, 5); -// assert(TinySTL::Test::container_equal(s3, s4)); -// -// stdStr s5(10, 'z'); -// tsStr s6(10, 'z'); -// assert(TinySTL::Test::container_equal(s5, s6)); -// -// char arr[] = "zouxiaohang love cpp"; -// stdStr s7(std::begin(arr), std::end(arr)); -// tsStr s8(std::begin(arr), std::end(arr)); -// assert(TinySTL::Test::container_equal(s7, s8)); -// } -// void testCase2(){ -// stdStr temp1("hello, world"); -// tsStr temp2("hello, world"); -// -// stdStr s1(temp1); -// tsStr s2(temp2); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// stdStr s3(std::move(s1)); -// tsStr s4(std::move(s2)); -// assert(TinySTL::Test::container_equal(s3, s4)); -// -// stdStr s5(temp1, 1); -// tsStr s6(temp2, 1); -// assert(TinySTL::Test::container_equal(s5, s6)); -// -// stdStr s7(temp1, 0, 5); -// tsStr s8(temp2, 0, 5); -// assert(TinySTL::Test::container_equal(s7, s8)); -// } -// void testCase3(){ -// stdStr t1("hello, world"); -// tsStr t2("hello, world"); -// -// stdStr s1; s1 = 'a'; -// tsStr s2; s2 = 'a'; -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// stdStr s3; s3 = "hello"; -// tsStr s4; s4 = "hello"; -// assert(TinySTL::Test::container_equal(s3, s4)); -// -// stdStr s5; s5 = t1; -// tsStr s6; s6 = t2; -// assert(TinySTL::Test::container_equal(s5, s6)); -// -// stdStr s7; s7 = std::move(t1); -// tsStr s8; s8 = std::move(t2); -// assert(TinySTL::Test::container_equal(s7, s8)); -// } -// void testCase4(){ -// tsStr str("Test string"); -// for (tsStr::iterator it = str.begin(); it != str.end(); ++it) -// std::cout << *it; -// std::cout << '\n'; -// for (tsStr::reverse_iterator it = str.rbegin(); it != str.rend(); ++it) -// std::cout << *it; -// std::cout << '\n'; -// } -// void testCase5(){ -// tsStr s; -// assert(s.size() == 0); -// assert(s.length() == 0); -// -// s = "hello, world"; -// assert(s.size() == 12); -// assert(s.size() == 12); -// } -// void testCase6(){ -// stdStr s1("hello, world"); -// tsStr s2("hello, world"); -// -// s1.resize(5); -// s2.resize(5); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.resize(20, 'z'); -// s2.resize(20, 'z'); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.resize(6, 'a'); -// s2.resize(6, 'a'); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.resize(100); -// s2.resize(100); -// assert(TinySTL::Test::container_equal(s1, s2)); -// } -// void testCase7(){ -// tsStr s; -// s.reserve(10); -// assert(s.capacity() == 10); -// } -// void testCase8(){ -// tsStr s; -// assert(s.empty()); -// -// s = "hello, world"; -// assert(!s.empty()); -// -// s.clear(); -// assert(s.empty()); -// } -// void testCase9(){ -// tsStr s; -// s.resize(10); -// for (auto i = 0; i != s.size(); ++i) -// s[i] = 'a' + i; -// TinySTL::Test::print_container(s); -// -// s.back() = 'Z'; -// s.front() = 'A'; -// TinySTL::Test::print_container(s); -// } -// void testCase10(){ -// stdStr s1; -// tsStr s2; -// for (auto i = 0; i != 10; ++i){ -// s1.push_back('a' + i); -// s2.push_back('a' + i); -// } -// assert(TinySTL::Test::container_equal(s1, s2)); -// } -// void testCase11(){ -// stdStr s1; -// tsStr s2; -// -// s1.insert(s1.begin(), 'A'); -// s2.insert(s2.begin(), 'A'); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.insert(s1.end(), 2, 'Z'); -// s2.insert(s2.end(), 2, 'Z'); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// size_t n = 2; -// s1.insert(2, 10, '@'); -// s2.insert(2, 10, '@'); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.insert(0, "hello, world"); -// s2.insert(0, "hello, world"); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.insert(s1.size() - 1, "zouxiaohang", 3); -// s2.insert(s2.size() - 1, "zouxiaohang", 3); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// stdStr s3; -// tsStr s4; -// -// s3.insert(s3.begin(), s1.begin(), s1.end()); -// s4.insert(s4.begin(), s2.begin(), s2.end()); -// assert(TinySTL::Test::container_equal(s3, s4)); -// -// s3.insert(1, s1); -// s4.insert(1, s2); -// assert(TinySTL::Test::container_equal(s3, s4)); -// -// stdStr t1("zouxiaoHANG"); -// tsStr t2("zouxiaoHANG"); -// s3.insert(s3.size(), t1, 7, t1.size() - 7); -// s4.insert(s4.size(), t2, 7, t2.size() - 7); -// assert(TinySTL::Test::container_equal(s3, s4)); -// } -// void testCase12(){ -// stdStr s1; -// tsStr s2; -// -// s1.append(stdStr("abc")); -// s2.append(tsStr("abc")); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.append(stdStr("123456789"), 1, 3); -// s2.append(tsStr("123456789"), 1, 3); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.append("hello"); -// s2.append("hello"); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.append("world", 0, 5); -// s2.append("world", 0, 5); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.append(10, 'A'); -// s2.append(10, 'A'); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// stdStr s3; s3.append(s1.begin(), s1.end()); -// tsStr s4; s4.append(s2.begin(), s2.end()); -// assert(TinySTL::Test::container_equal(s3, s4)); -// } -// void testCase13(){ -// stdStr s1; -// tsStr s2; -// -// s1 += 'A'; -// s2 += 'A'; -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1 += "hello"; -// s2 += "hello"; -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1 += stdStr("world"); -// s2 += tsStr("world"); -// assert(TinySTL::Test::container_equal(s1, s2)); -// } -// void testCase14(){ -// stdStr s1("hello world"); -// tsStr s2("hello world"); -// -// s1.pop_back(); -// s2.pop_back(); -// assert(TinySTL::Test::container_equal(s1, s2)); -// } -// void testCase15(){ -// stdStr s1("hello world"); -// tsStr s2("hello world"); -// -// s1.erase(s1.begin() + 1); -// s2.erase(s2.begin() + 1); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.erase(2, s1.size() - 4); -// s2.erase(2, s2.size() - 4); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.erase(s1.begin(), s1.end()); -// s2.erase(s2.begin(), s2.end()); -// assert(TinySTL::Test::container_equal(s1, s2)); -// } -// void testCase16(){ -// stdStr s1("zouxiaohang"), t1("I Love C++"); -// tsStr s2("zouxiaohang"), t2("I Love C++"); -// -// s1.replace(0, 3, t1); -// s2.replace(0, 3, t2); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.replace(s1.begin(), s1.begin() + s1.size() / 2, t1); -// s2.replace(s2.begin(), s2.begin() + s2.size() / 2, t2); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.replace(0, s1.size(), t1, 0, t1.size()); -// s2.replace(0, s2.size(), t2, 0, t2.size()); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.replace(0, s1.size(), "123456789"); -// s2.replace(0, s2.size(), "123456789"); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.replace(s1.begin(), s1.end(), stdStr("hubei")); -// s2.replace(s2.begin(), s2.end(), tsStr("hubei")); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.replace(0, s1.size(), "wuhan", 5); -// s2.replace(0, s2.size(), "wuhan", 5); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.replace(s1.begin(), s1.end(), "hongshanqu", 10); -// s2.replace(s2.begin(), s2.end(), "hongshanqu", 10); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.replace(0, s1.size(), 10, 'Z'); -// s2.replace(0, s2.size(), 10, 'Z'); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.replace(s1.begin(), s1.end(), 10, 'A'); -// s2.replace(s2.begin(), s2.end(), 10, 'A'); -// assert(TinySTL::Test::container_equal(s1, s2)); -// -// s1.replace(s1.begin(), s1.end(), t1.begin(), t1.end()); -// s2.replace(s2.begin(), s2.end(), t2.begin(), t2.end()); -// assert(TinySTL::Test::container_equal(s1, s2)); -// } -// void testCase17(){ -// tsStr buyer("money"); -// tsStr seller("goods"); -// -// seller.swap(buyer); -// TinySTL::Test::print_container(buyer, "buyer"); -// TinySTL::Test::print_container(seller, "seller"); -// } -// void testCase18(){ -// char buffer[20]; -// tsStr str("Test string..."); -// std::size_t length = str.copy(buffer, 6, 5); -// buffer[length] = '\0'; -// std::cout << "buffer contains: " << buffer << '\n'; -// } -// void testCase19(){ -// tsStr str("There are two needles in this haystack with needles."); -// tsStr str2("needle"); -// -// auto found = str.find(str2); -// assert(found == 14); -// -// found = str.find("needles are small", found + 1, 6); -// assert(found == 44); -// -// found = str.find(tsStr("wuhan")); -// assert(found == tsStr::npos); -// -// found = str.find("haystack"); -// assert(found == 30); -// -// found = str.find('.'); -// assert(found == 51); -// -// str.replace(str.find(str2), str2.length(), "preposition"); -// assert(TinySTL::Test::container_equal(str, -// tsStr("There are two prepositions in this haystack with needles."))); -// } -// void testCase20(){ -// tsStr str("The sixth sick sheik's sixth sheep's sick."); -// tsStr key("sixth"); -// -// auto found = str.rfind(key); -// assert(found == 23); -// -// found = str.rfind(key, 24); -// assert(found == 23); -// -// found = str.rfind('.'); -// assert(found == str.size() - 1); -// -// found = str.rfind("The"); -// assert(found == 0); -// -// found = str.rfind("sick111", 10, 4); -// assert(found == 10); -// } -// void testCase21(){ -// tsStr str("Please, replace the vowels in this sentence by asterisks."); -// tsStr key("aeiou"); -// const char *arr = "aeiou"; -// -// auto found = str.find_first_of(arr); -// assert(found == 2); -// -// found = str.find_first_of(arr, found + 1); -// assert(found == 3); -// -// found = str.find_first_of(arr, found + 1, 1); -// assert(found == 12); -// -// found = str.find_first_of(key, found + 1); -// assert(found == 14); -// -// found = str.find_first_of('v', found + 1); -// assert(found == 20); -// } -// void testCase22(){ -// tsStr str("1234567890098765432112345678900"); -// -// auto found = str.find_last_of('6'); -// assert(found == 25); -// -// found = str.find_last_of('6', found - 1); -// assert(found == 14); -// -// found = str.find_last_of("01", 11, 2); -// assert(found == 10); -// -// found = str.find_last_of(tsStr("#1"), 19); -// assert(found == 19); -// } -// void testCase23(){ -// tsStr str("look for non-alphabetic characters..."); -// -// auto found = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz "); -// assert(found == 12); -// } -// void testCase24(){ -// tsStr str("12345678900987654321"); -// -// auto found = str.find_last_not_of("023456789", str.size() - 2); -// assert(found == 0); -// } -// void testCase25(){ -// tsStr str = "We think in generalities, but we live in details."; -// -// auto s = str.substr(3, 5); -// assert(TinySTL::Test::container_equal(s, tsStr("think"))); -// } -// void testCase26(){ -// tsStr str1("green apple"); -// tsStr str2("red apple"); -// -// assert(str1.compare(str2) != 0); -// assert(str1.compare(6, 5, "apple") == 0); -// assert(str2.compare(str2.size() - 5, 5, "apple") == 0); -// assert(str1.compare(6, 5, str2, 4, 5) == 0); -// } -// void testCase27(){ -// tsStr firstlevel("com"); -// tsStr secondlevel("cplusplus"); -// tsStr scheme("http://"); -// -// auto hostname = "www." + secondlevel + '.' + firstlevel; -// auto url = scheme + hostname; -// -// assert(TinySTL::Test::container_equal(url, tsStr("http://www.cplusplus.com"))); -// } -// void testCase28(){ -// tsStr foo = "alpha"; -// tsStr bar = "beta"; -// -// assert(!(foo == bar)); -// assert(foo != bar); -// assert(foo < bar); -// assert(!(foo > bar)); -// assert(foo <= bar); -// assert(!(foo >= bar)); -// } -// void testCase29(){ -// tsStr name; -// -// std::cout << "Please, enter your name: "; -// std::cin >> name; -// std::cout << "Hello, " << name << "!\n"; -// } -// void testCase30(){ -// tsStr name; -// -// std::cout << "Please, enter your full name: "; -// TinySTL::getline(std::cin, name); -// std::cout << "Hello, " << name << "!\n"; -// } -// } -//} -// -////using namespace TinySTL::StringTest; -////int main(){ -//// testCase1(); -//// testCase2(); -//// testCase3(); -//// testCase4(); -//// testCase5(); -//// testCase6(); -//// testCase7(); -//// testCase8(); -//// testCase9(); -//// testCase10(); -//// testCase11(); -//// testCase12(); -//// testCase13(); -//// testCase14(); -//// testCase15(); -//// testCase16(); -//// testCase17(); -//// testCase18(); -//// testCase19(); -//// testCase20(); -//// testCase21(); -//// testCase22(); -//// testCase23(); -//// testCase24(); -//// testCase25(); -//// testCase26(); -//// testCase27(); -//// testCase28(); -//// testCase29(); -//// testCase30(); -//// system("pause"); -//// return 0; -////} \ No newline at end of file +#include "StringTest.h" + +namespace TinySTL{ + namespace StringTest{ + + void testCase1(){ + const char *ptr = "hello world"; + + stdStr s1(ptr); + tsStr s2(ptr); + assert(TinySTL::Test::container_equal(s1, s2)); + + stdStr s3(ptr, 5); + tsStr s4(ptr, 5); + assert(TinySTL::Test::container_equal(s3, s4)); + + stdStr s5(10, 'z'); + tsStr s6(10, 'z'); + assert(TinySTL::Test::container_equal(s5, s6)); + + char arr[] = "zouxiaohang love cpp"; + stdStr s7(std::begin(arr), std::end(arr)); + tsStr s8(std::begin(arr), std::end(arr)); + assert(TinySTL::Test::container_equal(s7, s8)); + } + void testCase2(){ + stdStr temp1("hello, world"); + tsStr temp2("hello, world"); + + stdStr s1(temp1); + tsStr s2(temp2); + assert(TinySTL::Test::container_equal(s1, s2)); + + stdStr s3(std::move(s1)); + tsStr s4(std::move(s2)); + assert(TinySTL::Test::container_equal(s3, s4)); + + stdStr s5(temp1, 1); + tsStr s6(temp2, 1); + assert(TinySTL::Test::container_equal(s5, s6)); + + stdStr s7(temp1, 0, 5); + tsStr s8(temp2, 0, 5); + assert(TinySTL::Test::container_equal(s7, s8)); + } + void testCase3(){ + stdStr t1("hello, world"); + tsStr t2("hello, world"); + + stdStr s1; s1 = 'a'; + tsStr s2; s2 = 'a'; + assert(TinySTL::Test::container_equal(s1, s2)); + + stdStr s3; s3 = "hello"; + tsStr s4; s4 = "hello"; + assert(TinySTL::Test::container_equal(s3, s4)); + + stdStr s5; s5 = t1; + tsStr s6; s6 = t2; + assert(TinySTL::Test::container_equal(s5, s6)); + + stdStr s7; s7 = std::move(t1); + tsStr s8; s8 = std::move(t2); + assert(TinySTL::Test::container_equal(s7, s8)); + } + void testCase4(){ + tsStr str("Test string"); + for (tsStr::iterator it = str.begin(); it != str.end(); ++it) + std::cout << *it; + std::cout << '\n'; + for (tsStr::reverse_iterator it = str.rbegin(); it != str.rend(); ++it) + std::cout << *it; + std::cout << '\n'; + } + void testCase5(){ + tsStr s; + assert(s.size() == 0); + assert(s.length() == 0); + + s = "hello, world"; + assert(s.size() == 12); + assert(s.size() == 12); + } + void testCase6(){ + stdStr s1("hello, world"); + tsStr s2("hello, world"); + + s1.resize(5); + s2.resize(5); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.resize(20, 'z'); + s2.resize(20, 'z'); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.resize(6, 'a'); + s2.resize(6, 'a'); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.resize(100); + s2.resize(100); + assert(TinySTL::Test::container_equal(s1, s2)); + } + void testCase7(){ + tsStr s; + s.reserve(10); + assert(s.capacity() == 10); + } + void testCase8(){ + tsStr s; + assert(s.empty()); + + s = "hello, world"; + assert(!s.empty()); + + s.clear(); + assert(s.empty()); + } + void testCase9(){ + tsStr s; + s.resize(10); + for (auto i = 0; i != s.size(); ++i) + s[i] = 'a' + i; + TinySTL::Test::print_container(s); + + s.back() = 'Z'; + s.front() = 'A'; + TinySTL::Test::print_container(s); + } + void testCase10(){ + stdStr s1; + tsStr s2; + for (auto i = 0; i != 10; ++i){ + s1.push_back('a' + i); + s2.push_back('a' + i); + } + assert(TinySTL::Test::container_equal(s1, s2)); + } + void testCase11(){ + stdStr s1; + tsStr s2; + + s1.insert(s1.begin(), 'A'); + s2.insert(s2.begin(), 'A'); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.insert(s1.end(), 2, 'Z'); + s2.insert(s2.end(), 2, 'Z'); + assert(TinySTL::Test::container_equal(s1, s2)); + + size_t n = 2; + s1.insert(2, 10, '@'); + s2.insert(2, 10, '@'); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.insert(0, "hello, world"); + s2.insert(0, "hello, world"); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.insert(s1.size() - 1, "zouxiaohang", 3); + s2.insert(s2.size() - 1, "zouxiaohang", 3); + assert(TinySTL::Test::container_equal(s1, s2)); + + stdStr s3; + tsStr s4; + + s3.insert(s3.begin(), s1.begin(), s1.end()); + s4.insert(s4.begin(), s2.begin(), s2.end()); + assert(TinySTL::Test::container_equal(s3, s4)); + + s3.insert(1, s1); + s4.insert(1, s2); + assert(TinySTL::Test::container_equal(s3, s4)); + + stdStr t1("zouxiaoHANG"); + tsStr t2("zouxiaoHANG"); + s3.insert(s3.size(), t1, 7, t1.size() - 7); + s4.insert(s4.size(), t2, 7, t2.size() - 7); + assert(TinySTL::Test::container_equal(s3, s4)); + } + void testCase12(){ + stdStr s1; + tsStr s2; + + s1.append(stdStr("abc")); + s2.append(tsStr("abc")); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.append(stdStr("123456789"), 1, 3); + s2.append(tsStr("123456789"), 1, 3); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.append("hello"); + s2.append("hello"); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.append("world", 0, 5); + s2.append("world", 0, 5); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.append(10, 'A'); + s2.append(10, 'A'); + assert(TinySTL::Test::container_equal(s1, s2)); + + stdStr s3; s3.append(s1.begin(), s1.end()); + tsStr s4; s4.append(s2.begin(), s2.end()); + assert(TinySTL::Test::container_equal(s3, s4)); + } + void testCase13(){ + stdStr s1; + tsStr s2; + + s1 += 'A'; + s2 += 'A'; + assert(TinySTL::Test::container_equal(s1, s2)); + + s1 += "hello"; + s2 += "hello"; + assert(TinySTL::Test::container_equal(s1, s2)); + + s1 += stdStr("world"); + s2 += tsStr("world"); + assert(TinySTL::Test::container_equal(s1, s2)); + } + void testCase14(){ + stdStr s1("hello world"); + tsStr s2("hello world"); + + s1.pop_back(); + s2.pop_back(); + assert(TinySTL::Test::container_equal(s1, s2)); + } + void testCase15(){ + stdStr s1("hello world"); + tsStr s2("hello world"); + + s1.erase(s1.begin() + 1); + s2.erase(s2.begin() + 1); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.erase(2, s1.size() - 4); + s2.erase(2, s2.size() - 4); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.erase(s1.begin(), s1.end()); + s2.erase(s2.begin(), s2.end()); + assert(TinySTL::Test::container_equal(s1, s2)); + } + void testCase16(){ + stdStr s1("zouxiaohang"), t1("I Love C++"); + tsStr s2("zouxiaohang"), t2("I Love C++"); + + s1.replace(0, 3, t1); + s2.replace(0, 3, t2); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.replace(s1.begin(), s1.begin() + s1.size() / 2, t1); + s2.replace(s2.begin(), s2.begin() + s2.size() / 2, t2); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.replace(0, s1.size(), t1, 0, t1.size()); + s2.replace(0, s2.size(), t2, 0, t2.size()); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.replace(0, s1.size(), "123456789"); + s2.replace(0, s2.size(), "123456789"); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.replace(s1.begin(), s1.end(), stdStr("hubei")); + s2.replace(s2.begin(), s2.end(), tsStr("hubei")); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.replace(0, s1.size(), "wuhan", 5); + s2.replace(0, s2.size(), "wuhan", 5); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.replace(s1.begin(), s1.end(), "hongshanqu", 10); + s2.replace(s2.begin(), s2.end(), "hongshanqu", 10); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.replace(0, s1.size(), 10, 'Z'); + s2.replace(0, s2.size(), 10, 'Z'); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.replace(s1.begin(), s1.end(), 10, 'A'); + s2.replace(s2.begin(), s2.end(), 10, 'A'); + assert(TinySTL::Test::container_equal(s1, s2)); + + s1.replace(s1.begin(), s1.end(), t1.begin(), t1.end()); + s2.replace(s2.begin(), s2.end(), t2.begin(), t2.end()); + assert(TinySTL::Test::container_equal(s1, s2)); + } + void testCase17(){ + tsStr buyer("money"); + tsStr seller("goods"); + + seller.swap(buyer); + TinySTL::Test::print_container(buyer, "buyer"); + TinySTL::Test::print_container(seller, "seller"); + } + void testCase18(){ + char buffer[20]; + tsStr str("Test string..."); + std::size_t length = str.copy(buffer, 6, 5); + buffer[length] = '\0'; + std::cout << "buffer contains: " << buffer << '\n'; + } + void testCase19(){ + tsStr str("There are two needles in this haystack with needles."); + tsStr str2("needle"); + + auto found = str.find(str2); + assert(found == 14); + + found = str.find("needles are small", found + 1, 6); + assert(found == 44); + + found = str.find(tsStr("wuhan")); + assert(found == tsStr::npos); + + found = str.find("haystack"); + assert(found == 30); + + found = str.find('.'); + assert(found == 51); + + str.replace(str.find(str2), str2.length(), "preposition"); + assert(TinySTL::Test::container_equal(str, + tsStr("There are two prepositions in this haystack with needles."))); + } + void testCase20(){ + tsStr str("The sixth sick sheik's sixth sheep's sick."); + tsStr key("sixth"); + + auto found = str.rfind(key); + assert(found == 23); + + found = str.rfind(key, 24); + assert(found == 23); + + found = str.rfind('.'); + assert(found == str.size() - 1); + + found = str.rfind("The"); + assert(found == 0); + + found = str.rfind("sick111", 10, 4); + assert(found == 10); + } + void testCase21(){ + tsStr str("Please, replace the vowels in this sentence by asterisks."); + tsStr key("aeiou"); + const char *arr = "aeiou"; + + auto found = str.find_first_of(arr); + assert(found == 2); + + found = str.find_first_of(arr, found + 1); + assert(found == 3); + + found = str.find_first_of(arr, found + 1, 1); + assert(found == 12); + + found = str.find_first_of(key, found + 1); + assert(found == 14); + + found = str.find_first_of('v', found + 1); + assert(found == 20); + } + void testCase22(){ + tsStr str("1234567890098765432112345678900"); + + auto found = str.find_last_of('6'); + assert(found == 25); + + found = str.find_last_of('6', found - 1); + assert(found == 14); + + found = str.find_last_of("01", 11, 2); + assert(found == 10); + + found = str.find_last_of(tsStr("#1"), 19); + assert(found == 19); + } + void testCase23(){ + tsStr str("look for non-alphabetic characters..."); + + auto found = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz "); + assert(found == 12); + } + void testCase24(){ + tsStr str("12345678900987654321"); + + auto found = str.find_last_not_of("023456789", str.size() - 2); + assert(found == 0); + } + void testCase25(){ + tsStr str = "We think in generalities, but we live in details."; + + auto s = str.substr(3, 5); + assert(TinySTL::Test::container_equal(s, tsStr("think"))); + } + void testCase26(){ + tsStr str1("green apple"); + tsStr str2("red apple"); + + assert(str1.compare(str2) != 0); + assert(str1.compare(6, 5, "apple") == 0); + assert(str2.compare(str2.size() - 5, 5, "apple") == 0); + assert(str1.compare(6, 5, str2, 4, 5) == 0); + } + void testCase27(){ + tsStr firstlevel("com"); + tsStr secondlevel("cplusplus"); + tsStr scheme("http://"); + + auto hostname = "www." + secondlevel + '.' + firstlevel; + auto url = scheme + hostname; + + assert(TinySTL::Test::container_equal(url, tsStr("http://www.cplusplus.com"))); + } + void testCase28(){ + tsStr foo = "alpha"; + tsStr bar = "beta"; + + assert(!(foo == bar)); + assert(foo != bar); + assert(foo < bar); + assert(!(foo > bar)); + assert(foo <= bar); + assert(!(foo >= bar)); + } + void testCase29(){ + tsStr name; + + std::cout << "Please, enter your name: "; + std::cin >> name; + std::cout << "Hello, " << name << "!\n"; + } + void testCase30(){ + tsStr name; + + std::cout << "Please, enter your full name: "; + TinySTL::getline(std::cin, name); + std::cout << "Hello, " << name << "!\n"; + } + + void testAllCases(){ + testCase1(); + testCase2(); + testCase3(); + testCase4(); + testCase5(); + testCase6(); + testCase7(); + testCase8(); + testCase9(); + testCase10(); + testCase11(); + testCase12(); + testCase13(); + testCase14(); + testCase15(); + testCase16(); + testCase17(); + testCase18(); + testCase19(); + testCase20(); + testCase21(); + testCase22(); + testCase23(); + testCase24(); + testCase25(); + testCase26(); + testCase27(); + testCase28(); + testCase29(); + testCase30(); + } + } +} \ No newline at end of file diff --git a/TinySTL/Test/StringTest.h b/TinySTL/Test/StringTest.h index 781fdd4..ddc5977 100644 --- a/TinySTL/Test/StringTest.h +++ b/TinySTL/Test/StringTest.h @@ -44,6 +44,8 @@ namespace TinySTL{ void testCase28(); void testCase29(); void testCase30(); + + void testAllCases(); } } diff --git a/TinySTL/Test/SuffixArrayTest.cpp b/TinySTL/Test/SuffixArrayTest.cpp index 3aa486c..523dedf 100644 --- a/TinySTL/Test/SuffixArrayTest.cpp +++ b/TinySTL/Test/SuffixArrayTest.cpp @@ -1,32 +1,29 @@ -//#include "SuffixArrayTest.h" -// -//namespace TinySTL{ -// namespace SuffixArrayTest{ -// void testCase(){ -// //char arr[] = { 'a', 'a', 'b', 'a', 'a', 'a', 'a', 'b' }; -// std::string str("aabaaaab"); -// -// //TinySTL::suffix_array sa(arr, 8); -// TinySTL::suffix_array sa(str.data(), str.size()); -// auto sa1 = sa.suffixArray(); -// auto sa2 = TinySTL::suffix_array::array_type{3, 4, 5, 0, 6, 1, 7, 2}; -// assert(TinySTL::Test::container_equal(sa1, sa2)); -// -// auto ra1 = sa.rankArray(); -// auto ra2 = TinySTL::suffix_array::array_type{ 3, 5, 7, 0, 1, 2, 4, 6 }; -// assert(TinySTL::Test::container_equal(ra1, ra2)); -// -// auto ha1 = sa.heightArray(); -// auto ha2 = TinySTL::suffix_array::array_type{ 3, 2, 3, 1, 2, 0, 1 }; -// assert(TinySTL::Test::container_equal(ha1, ha2)); -// } -// } -//} -// -////using namespace TinySTL::SuffixArrayTest; -////int main(){ -//// testCase(); -//// -//// system("pause"); -//// return 0; -////} \ No newline at end of file +#include "SuffixArrayTest.h" + +namespace TinySTL{ + namespace SuffixArrayTest{ + void testCase(){ + //char arr[] = { 'a', 'a', 'b', 'a', 'a', 'a', 'a', 'b' }; + std::string str("aabaaaab"); + + //TinySTL::suffix_array sa(arr, 8); + TinySTL::suffix_array sa(str.data(), str.size()); + auto sa1 = sa.suffixArray(); + auto sa2 = TinySTL::suffix_array::array_type{3, 4, 5, 0, 6, 1, 7, 2}; + assert(TinySTL::Test::container_equal(sa1, sa2)); + + auto ra1 = sa.rankArray(); + auto ra2 = TinySTL::suffix_array::array_type{ 3, 5, 7, 0, 1, 2, 4, 6 }; + assert(TinySTL::Test::container_equal(ra1, ra2)); + + auto ha1 = sa.heightArray(); + auto ha2 = TinySTL::suffix_array::array_type{ 3, 2, 3, 1, 2, 0, 1 }; + assert(TinySTL::Test::container_equal(ha1, ha2)); + } + + + void testAllCases(){ + testCase(); + } + } +} \ No newline at end of file diff --git a/TinySTL/Test/SuffixArrayTest.h b/TinySTL/Test/SuffixArrayTest.h index a0c801b..b8eded6 100644 --- a/TinySTL/Test/SuffixArrayTest.h +++ b/TinySTL/Test/SuffixArrayTest.h @@ -11,6 +11,8 @@ namespace TinySTL{ namespace SuffixArrayTest{ void testCase(); + + void testAllCases(); } } diff --git a/TinySTL/Test/VectorTest.cpp b/TinySTL/Test/VectorTest.cpp index 7e89c1e..b6d1bbd 100644 --- a/TinySTL/Test/VectorTest.cpp +++ b/TinySTL/Test/VectorTest.cpp @@ -1,208 +1,206 @@ -//#include "VectorTest.h" -// -//namespace TinySTL{ -// namespace VectorTest{ -// -// void testCase1(){ -// stdVec v1(10, "zxh"); -// tsVec v2(10, "zxh"); -// assert(TinySTL::Test::container_equal(v1, v2)); -// -// stdVec v3(10); -// tsVec v4(10); -// assert(TinySTL::Test::container_equal(v3, v4)); -// -// std::array arr = { "abc", "def", "ghi" }; -// stdVec v5(std::begin(arr), std::end(arr)); -// tsVec v6(std::begin(arr), std::end(arr)); -// assert(TinySTL::Test::container_equal(v5, v6)); -// } -// void testCase2(){ -// stdVec temp1(10, 0); -// tsVec temp2(10, 0); -// -// auto v1(temp1); -// auto v2(temp2); -// assert(TinySTL::Test::container_equal(v1, v2)); -// -// auto v3(std::move(temp1)); -// auto v4(std::move(temp2)); -// assert(TinySTL::Test::container_equal(v3, v4)); -// -// auto v5 = v1; -// auto v6 = v2; -// assert(TinySTL::Test::container_equal(v5, v6)); -// -// auto v7 = std::move(v3); -// auto v8 = std::move(v4); -// assert(TinySTL::Test::container_equal(v7, v8)); -// } -// void testCase3(){ -// tsVec v1, v2; -// for (int i = 0; i != 100; ++i){ -// v1.push_back(i); -// v2.push_back(i); -// } -// -// assert(v1 == v2); -// assert(!(v1 != v2)); -// } -// void testCase4(){ -// tsVec myvector; -// for (int i = 1; i <= 5; i++) myvector.push_back(i); -// -// std::cout << "myvector contains:"; -// for (tsVec::iterator it = myvector.begin(); it != myvector.end(); ++it) -// std::cout << ' ' << *it; -// std::cout << '\n'; -// -// std::cout << "myvector contains:"; -// for (tsVec::const_iterator it = myvector.cbegin(); it != myvector.cend(); ++it) -// std::cout << ' ' << *it; -// std::cout << '\n'; -// } -// void testCase5(){ -// tsVec myvector(5); // 5 default-constructed ints -// int i = 0; -// tsVec::reverse_iterator rit = myvector.rbegin(); -// for (; rit != myvector.rend(); ++rit) -// *rit = ++i; -// -// std::cout << "myvector contains:"; -// for (tsVec::iterator it = myvector.begin(); it != myvector.end(); ++it) -// std::cout << ' ' << *it; -// std::cout << '\n'; -// std::cout << "myvector contains(reverse order):"; -// for (tsVec::reverse_iterator it = myvector.rbegin(); it != myvector.rend(); ++it) -// std::cout << ' ' << *it; -// std::cout << '\n'; -// } -// void testCase6(){ -// tsVec v(11, 0); -// assert(v.size() == 11); -// -// v.resize(5); -// assert(v.size() == 5); -// -// v.resize(20); -// assert(v.size() == 20); -// } -// void testCase7(){ -// tsVec v; -// v.reserve(20); -// assert(v.capacity() == 20); -// } -// void testCase8(){ -// stdVec v1(10); -// tsVec v2(10); -// for (unsigned i = 0; i < 10; i++){ -// v1[i] = i; -// v2[i] = i; -// } -// assert(TinySTL::Test::container_equal(v1, v2)); -// -// v1.front() = 99; -// v2.front() = 99; -// v1.back() = 100; -// v2.back() = 100; -// -// assert(TinySTL::Test::container_equal(v1, v2)); -// } -// void testCase9(){ -// stdVec v1(5); -// tsVec v2(5); -// -// auto p1 = v1.data(); -// auto p2 = v2.data(); -// *p1 = 10; ++p1; *p1 = 20; p1[2] = 100; -// *p2 = 10; ++p2; *p2 = 20; p2[2] = 100; -// -// assert(TinySTL::Test::container_equal(v1, v2)); -// } -// void testCase10(){ -// tsVec foo(3, 100); // three ints with a value of 100 -// tsVec bar(5, 200); // five ints with a value of 200 -// -// TinySTL::Test::print_container(foo, "foo"); -// TinySTL::Test::print_container(bar, "bar"); -// foo.swap(bar); -// TinySTL::Test::print_container(foo, "foo"); -// TinySTL::Test::print_container(bar, "bar"); -// } -// void testCase11(){ -// stdVec v1; -// tsVec v2; -// -// v1.push_back("hello "); v1.push_back("world"); -// v2.push_back("hello "); v2.push_back("world"); -// assert(TinySTL::Test::container_equal(v1, v2)); -// -// v1.pop_back(); -// v2.pop_back(); -// assert(TinySTL::Test::container_equal(v1, v2)); -// } -// void testCase12(){ -// stdVec v1; -// tsVec v2; -// -// v1.insert(v1.begin(), 0); -// v2.insert(v2.begin(), 0); -// assert(TinySTL::Test::container_equal(v1, v2)); -// -// v1.insert(v1.end(), 1); -// v2.insert(v2.end(), 1); -// assert(TinySTL::Test::container_equal(v1, v2)); -// -// v1.insert(v1.begin() + v1.size() / 2, 10, 0); -// v2.insert(v2.begin() + v2.size() / 2, 10, 0); -// assert(TinySTL::Test::container_equal(v1, v2)); -// -// int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; -// v1.insert(v1.end(), std::begin(arr), std::end(arr)); -// v2.insert(v2.end(), std::begin(arr), std::end(arr)); -// assert(TinySTL::Test::container_equal(v1, v2)); -// } -// void testCase13(){ -// stdVec v1; -// tsVec v2; -// for (int i = 1; i <= 10; i++) { -// v1.push_back(i); -// v2.push_back(i); -// } -// v1.erase(v1.begin() + 5); -// v2.erase(v2.begin() + 5); -// assert(TinySTL::Test::container_equal(v1, v2)); -// -// v1.erase(v1.begin(), v1.begin() + 3); -// v2.erase(v2.begin(), v2.begin() + 3); -// assert(TinySTL::Test::container_equal(v1, v2)); -// } -// void testCase14(){ -// tsVec foo(3, 100); -// tsVec bar(2, 200); -// -// assert(!(foo == bar)); -// assert(foo != bar); -// } -// } -//} -// -////using namespace TinySTL::VectorTest; -////int main(){ -//// //testCase1(); -//// //testCase2(); -//// //testCase3(); -//// //testCase4(); -//// //testCase5(); -//// //testCase6(); -//// //testCase7(); -//// //testCase8(); -//// //testCase9(); -//// //testCase10(); -//// //testCase11(); -//// //testCase12(); -//// //testCase13(); -//// //testCase14(); -//// system("pause"); -//// return 0; -////} \ No newline at end of file +#include "VectorTest.h" + +namespace TinySTL{ + namespace VectorTest{ + + void testCase1(){ + stdVec v1(10, "zxh"); + tsVec v2(10, "zxh"); + assert(TinySTL::Test::container_equal(v1, v2)); + + stdVec v3(10); + tsVec v4(10); + assert(TinySTL::Test::container_equal(v3, v4)); + + std::array arr = { "abc", "def", "ghi" }; + stdVec v5(std::begin(arr), std::end(arr)); + tsVec v6(std::begin(arr), std::end(arr)); + assert(TinySTL::Test::container_equal(v5, v6)); + } + void testCase2(){ + stdVec temp1(10, 0); + tsVec temp2(10, 0); + + auto v1(temp1); + auto v2(temp2); + assert(TinySTL::Test::container_equal(v1, v2)); + + auto v3(std::move(temp1)); + auto v4(std::move(temp2)); + assert(TinySTL::Test::container_equal(v3, v4)); + + auto v5 = v1; + auto v6 = v2; + assert(TinySTL::Test::container_equal(v5, v6)); + + auto v7 = std::move(v3); + auto v8 = std::move(v4); + assert(TinySTL::Test::container_equal(v7, v8)); + } + void testCase3(){ + tsVec v1, v2; + for (int i = 0; i != 100; ++i){ + v1.push_back(i); + v2.push_back(i); + } + + assert(v1 == v2); + assert(!(v1 != v2)); + } + void testCase4(){ + tsVec myvector; + for (int i = 1; i <= 5; i++) myvector.push_back(i); + + std::cout << "myvector contains:"; + for (tsVec::iterator it = myvector.begin(); it != myvector.end(); ++it) + std::cout << ' ' << *it; + std::cout << '\n'; + + std::cout << "myvector contains:"; + for (tsVec::const_iterator it = myvector.cbegin(); it != myvector.cend(); ++it) + std::cout << ' ' << *it; + std::cout << '\n'; + } + void testCase5(){ + tsVec myvector(5); // 5 default-constructed ints + int i = 0; + tsVec::reverse_iterator rit = myvector.rbegin(); + for (; rit != myvector.rend(); ++rit) + *rit = ++i; + + std::cout << "myvector contains:"; + for (tsVec::iterator it = myvector.begin(); it != myvector.end(); ++it) + std::cout << ' ' << *it; + std::cout << '\n'; + std::cout << "myvector contains(reverse order):"; + for (tsVec::reverse_iterator it = myvector.rbegin(); it != myvector.rend(); ++it) + std::cout << ' ' << *it; + std::cout << '\n'; + } + void testCase6(){ + tsVec v(11, 0); + assert(v.size() == 11); + + v.resize(5); + assert(v.size() == 5); + + v.resize(20); + assert(v.size() == 20); + } + void testCase7(){ + tsVec v; + v.reserve(20); + assert(v.capacity() == 20); + } + void testCase8(){ + stdVec v1(10); + tsVec v2(10); + for (unsigned i = 0; i < 10; i++){ + v1[i] = i; + v2[i] = i; + } + assert(TinySTL::Test::container_equal(v1, v2)); + + v1.front() = 99; + v2.front() = 99; + v1.back() = 100; + v2.back() = 100; + + assert(TinySTL::Test::container_equal(v1, v2)); + } + void testCase9(){ + stdVec v1(5); + tsVec v2(5); + + auto p1 = v1.data(); + auto p2 = v2.data(); + *p1 = 10; ++p1; *p1 = 20; p1[2] = 100; + *p2 = 10; ++p2; *p2 = 20; p2[2] = 100; + + assert(TinySTL::Test::container_equal(v1, v2)); + } + void testCase10(){ + tsVec foo(3, 100); // three ints with a value of 100 + tsVec bar(5, 200); // five ints with a value of 200 + + TinySTL::Test::print_container(foo, "foo"); + TinySTL::Test::print_container(bar, "bar"); + foo.swap(bar); + TinySTL::Test::print_container(foo, "foo"); + TinySTL::Test::print_container(bar, "bar"); + } + void testCase11(){ + stdVec v1; + tsVec v2; + + v1.push_back("hello "); v1.push_back("world"); + v2.push_back("hello "); v2.push_back("world"); + assert(TinySTL::Test::container_equal(v1, v2)); + + v1.pop_back(); + v2.pop_back(); + assert(TinySTL::Test::container_equal(v1, v2)); + } + void testCase12(){ + stdVec v1; + tsVec v2; + + v1.insert(v1.begin(), 0); + v2.insert(v2.begin(), 0); + assert(TinySTL::Test::container_equal(v1, v2)); + + v1.insert(v1.end(), 1); + v2.insert(v2.end(), 1); + assert(TinySTL::Test::container_equal(v1, v2)); + + v1.insert(v1.begin() + v1.size() / 2, 10, 0); + v2.insert(v2.begin() + v2.size() / 2, 10, 0); + assert(TinySTL::Test::container_equal(v1, v2)); + + int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + v1.insert(v1.end(), std::begin(arr), std::end(arr)); + v2.insert(v2.end(), std::begin(arr), std::end(arr)); + assert(TinySTL::Test::container_equal(v1, v2)); + } + void testCase13(){ + stdVec v1; + tsVec v2; + for (int i = 1; i <= 10; i++) { + v1.push_back(i); + v2.push_back(i); + } + v1.erase(v1.begin() + 5); + v2.erase(v2.begin() + 5); + assert(TinySTL::Test::container_equal(v1, v2)); + + v1.erase(v1.begin(), v1.begin() + 3); + v2.erase(v2.begin(), v2.begin() + 3); + assert(TinySTL::Test::container_equal(v1, v2)); + } + void testCase14(){ + tsVec foo(3, 100); + tsVec bar(2, 200); + + assert(!(foo == bar)); + assert(foo != bar); + } + + + void testAllCases(){ + testCase1(); + testCase2(); + testCase3(); + testCase4(); + testCase5(); + testCase6(); + testCase7(); + testCase8(); + testCase9(); + testCase10(); + testCase11(); + testCase12(); + testCase13(); + testCase14(); + } + } +} \ No newline at end of file diff --git a/TinySTL/Test/VectorTest.h b/TinySTL/Test/VectorTest.h index ad52a6a..bb36075 100644 --- a/TinySTL/Test/VectorTest.h +++ b/TinySTL/Test/VectorTest.h @@ -34,6 +34,8 @@ namespace TinySTL{ void testCase12(); void testCase13(); void testCase14(); + + void testAllCases(); } } diff --git a/TinySTL/main.cpp b/TinySTL/main.cpp index 7794944..f504ee1 100644 --- a/TinySTL/main.cpp +++ b/TinySTL/main.cpp @@ -1,24 +1,33 @@ #include #include "Algorithm.h" -#include "String.h" -#include "Vector.h" #include "Profiler\Profiler.h" -#include "AVLTree.h" -#include "BinarySearchTree.h" -#include "CircularBuffer.h" -#include "Deque.h" -#include "List.h" -#include "Queue.h" -#include "Stack.h" +#include "Test\AlgorithmTest.h" +#include "Test\BitmapTest.h" +#include "Test\CircularBufferTest.h" +#include "Test\PairTest.h" +#include "Test\PriorityQueueTest.h" +#include "Test\QueueTest.h" +#include "Test\StackTest.h" +#include "Test\StringTest.h" +#include "Test\SuffixArrayTest.h" +#include "Test\VectorTest.h" using namespace std; using namespace TinySTL::Profiler; int main(){ - TinySTL::string str1("Hello World\n"), str2("This is TinySTL"); - std::cout << (str1 + str2) << std::endl; + TinySTL::AlgorithmTest::testAllCases(); + TinySTL::BitmapTest::testAllCases(); + TinySTL::CircularBufferTest::testAllCases(); + TinySTL::PairTest::testAllCases(); + TinySTL::PriorityQueueTest::testAllCases(); + TinySTL::QueueTest::testAllCases(); + TinySTL::StackTest::testAllCases(); + TinySTL::StringTest::testAllCases(); + TinySTL::SuffixArrayTest::testAllCases(); + TinySTL::VectorTest::testAllCases(); system("pause"); return 0;