update
This commit is contained in:
@@ -1,175 +1,175 @@
|
|||||||
#include "AlgorithmTest.h"
|
//#include "AlgorithmTest.h"
|
||||||
|
//
|
||||||
namespace TinySTL{
|
//namespace TinySTL{
|
||||||
namespace AlgorithmTest{
|
// namespace AlgorithmTest{
|
||||||
void testFill(){
|
// void testFill(){
|
||||||
std::vector<int> v1(8), v2(8);
|
// std::vector<int> v1(8), v2(8);
|
||||||
std::fill(v1.begin(), v1.begin() + 4, 5); //5 5 5 5 0 0 0 0
|
// 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
|
// 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(), 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
|
// TinySTL::fill(v2.begin() + 3, v2.end() - 2, 8); //5 5 5 8 8 8 0 0
|
||||||
|
//
|
||||||
assert(TinySTL::Test::container_equal(v1, v2));
|
// assert(TinySTL::Test::container_equal(v1, v2));
|
||||||
}
|
// }
|
||||||
void testFillN(){
|
// void testFillN(){
|
||||||
std::vector<int> v1(8, 10), v2(8, 10);
|
// std::vector<int> 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(), 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
|
// 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(), 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
|
// TinySTL::fill_n(v2.begin() + 3, 3, 33); //20 20 20 33 33 33 10 10
|
||||||
|
//
|
||||||
assert(TinySTL::Test::container_equal(v1, v2));
|
// assert(TinySTL::Test::container_equal(v1, v2));
|
||||||
}
|
// }
|
||||||
void testMinMax(){
|
// void testMinMax(){
|
||||||
assert(TinySTL::min(1, 2) == 1);
|
// assert(TinySTL::min(1, 2) == 1);
|
||||||
assert(TinySTL::min(2, 1) == 1);
|
// assert(TinySTL::min(2, 1) == 1);
|
||||||
assert(TinySTL::min('a', 'z') == 'a');
|
// assert(TinySTL::min('a', 'z') == 'a');
|
||||||
assert(TinySTL::min(3.14, 2.72) == 2.72);
|
// assert(TinySTL::min(3.14, 2.72) == 2.72);
|
||||||
|
//
|
||||||
assert(TinySTL::max(1, 2) == 2);
|
// assert(TinySTL::max(1, 2) == 2);
|
||||||
assert(TinySTL::max(2, 1) == 2);
|
// assert(TinySTL::max(2, 1) == 2);
|
||||||
assert(TinySTL::max('a', 'z') == 'z');
|
// assert(TinySTL::max('a', 'z') == 'z');
|
||||||
assert(TinySTL::max(3.14, 2.73) == 3.14);
|
// assert(TinySTL::max(3.14, 2.73) == 3.14);
|
||||||
}
|
// }
|
||||||
void testHeapAlgorithm(){
|
// void testHeapAlgorithm(){
|
||||||
int myints[] = { 10, 20, 30, 5, 15 };
|
// int myints[] = { 10, 20, 30, 5, 15 };
|
||||||
std::vector<int> v1(myints, myints + 5);
|
// std::vector<int> v1(myints, myints + 5);
|
||||||
std::vector<int> v2(myints, myints + 5);
|
// std::vector<int> v2(myints, myints + 5);
|
||||||
|
//
|
||||||
std::make_heap(v1.begin(), v1.end());
|
// std::make_heap(v1.begin(), v1.end());
|
||||||
TinySTL::make_heap(v2.begin(), v2.end());
|
// TinySTL::make_heap(v2.begin(), v2.end());
|
||||||
assert(TinySTL::Test::container_equal(v1, v2));
|
// assert(TinySTL::Test::container_equal(v1, v2));
|
||||||
|
//
|
||||||
std::pop_heap(v1.begin(), v1.end()); v1.pop_back();
|
// std::pop_heap(v1.begin(), v1.end()); v1.pop_back();
|
||||||
TinySTL::pop_heap(v2.begin(), v2.end()); v2.pop_back();
|
// TinySTL::pop_heap(v2.begin(), v2.end()); v2.pop_back();
|
||||||
assert(TinySTL::Test::container_equal(v1, v2));
|
// assert(TinySTL::Test::container_equal(v1, v2));
|
||||||
|
//
|
||||||
v1.push_back(99); std::push_heap(v1.begin(), v1.end());
|
// v1.push_back(99); std::push_heap(v1.begin(), v1.end());
|
||||||
v2.push_back(99); TinySTL::push_heap(v2.begin(), v2.end());
|
// v2.push_back(99); TinySTL::push_heap(v2.begin(), v2.end());
|
||||||
assert(TinySTL::Test::container_equal(v1, v2));
|
// assert(TinySTL::Test::container_equal(v1, v2));
|
||||||
|
//
|
||||||
std::sort_heap(v1.begin(), v1.end());
|
// std::sort_heap(v1.begin(), v1.end());
|
||||||
TinySTL::sort_heap(v2.begin(), v2.end());
|
// TinySTL::sort_heap(v2.begin(), v2.end());
|
||||||
assert(TinySTL::Test::container_equal(v1, v2));
|
// assert(TinySTL::Test::container_equal(v1, v2));
|
||||||
}
|
// }
|
||||||
void testIsHeap(){
|
// void testIsHeap(){
|
||||||
std::vector<int> v1{ 9, 5, 2, 6, 4, 1, 3, 8, 7 };
|
// std::vector<int> v1{ 9, 5, 2, 6, 4, 1, 3, 8, 7 };
|
||||||
std::vector<int> v2{ 9, 5, 2, 6, 4, 1, 3, 8, 7 };
|
// std::vector<int> v2{ 9, 5, 2, 6, 4, 1, 3, 8, 7 };
|
||||||
|
//
|
||||||
if (!std::is_heap(v1.begin(), v1.end()))
|
// if (!std::is_heap(v1.begin(), v1.end()))
|
||||||
std::make_heap(v1.begin(), v1.end());
|
// std::make_heap(v1.begin(), v1.end());
|
||||||
if (!TinySTL::is_heap(v2.begin(), v2.end()))
|
// if (!TinySTL::is_heap(v2.begin(), v2.end()))
|
||||||
TinySTL::make_heap(v2.begin(), v2.end());
|
// TinySTL::make_heap(v2.begin(), v2.end());
|
||||||
|
//
|
||||||
assert(TinySTL::Test::container_equal(v1, v2));
|
// assert(TinySTL::Test::container_equal(v1, v2));
|
||||||
}
|
// }
|
||||||
void testAllOf(){
|
// void testAllOf(){
|
||||||
std::array<int, 8> foo = { 3, 5, 7, 11, 13, 17, 19, 23 };
|
// std::array<int, 8> foo = { 3, 5, 7, 11, 13, 17, 19, 23 };
|
||||||
assert(TinySTL::all_of(foo.begin(), foo.end(), [](int i){return i % 2; }));
|
// assert(TinySTL::all_of(foo.begin(), foo.end(), [](int i){return i % 2; }));
|
||||||
}
|
// }
|
||||||
void testNoneOf(){
|
// void testNoneOf(){
|
||||||
std::array<int, 8> foo = { 1, 2, 4, 8, 16, 32, 64, 128 };
|
// std::array<int, 8> foo = { 1, 2, 4, 8, 16, 32, 64, 128 };
|
||||||
assert(TinySTL::none_of(foo.begin(), foo.end(), [](int i){return i < 0; }));
|
// assert(TinySTL::none_of(foo.begin(), foo.end(), [](int i){return i < 0; }));
|
||||||
}
|
// }
|
||||||
void testAnyOf(){
|
// void testAnyOf(){
|
||||||
std::array<int, 7> foo = { 0, 1, -1, 3, -3, 5, -5 };
|
// std::array<int, 7> foo = { 0, 1, -1, 3, -3, 5, -5 };
|
||||||
assert(std::any_of(foo.begin(), foo.end(), [](int i){return i < 0; }));
|
// assert(std::any_of(foo.begin(), foo.end(), [](int i){return i < 0; }));
|
||||||
}
|
// }
|
||||||
void testForEach(){
|
// void testForEach(){
|
||||||
std::vector<int> myvector{ 10, 20, 30 };
|
// std::vector<int> myvector{ 10, 20, 30 };
|
||||||
std::vector<int> temp{ 11, 21, 31 };
|
// std::vector<int> temp{ 11, 21, 31 };
|
||||||
TinySTL::for_each(myvector.begin(), myvector.end(), [&myvector](int& i){
|
// TinySTL::for_each(myvector.begin(), myvector.end(), [&myvector](int& i){
|
||||||
++i;
|
// ++i;
|
||||||
});
|
// });
|
||||||
|
//
|
||||||
assert(TinySTL::Test::container_equal(myvector, temp));
|
// assert(TinySTL::Test::container_equal(myvector, temp));
|
||||||
}
|
// }
|
||||||
void testFind(){
|
// void testFind(){
|
||||||
std::vector<int> v{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
// std::vector<int> 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(), 5) != v.end());
|
||||||
assert(TinySTL::find(v.begin(), v.end(), 10) == 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(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());
|
// assert(TinySTL::find_if_not(v.begin(), v.end(), [](int i){return i < 0; }) != v.end());
|
||||||
}
|
// }
|
||||||
void testFindEnd(){
|
// void testFindEnd(){
|
||||||
int myints[] = { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 };
|
// int myints[] = { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 };
|
||||||
std::vector<int> v(myints, myints + 10);
|
// std::vector<int> v(myints, myints + 10);
|
||||||
int needle1[] = { 1, 2, 3 };
|
// int needle1[] = { 1, 2, 3 };
|
||||||
auto it = TinySTL::find_end(v.begin(), v.end(), needle1, needle1 + 3);
|
// auto it = TinySTL::find_end(v.begin(), v.end(), needle1, needle1 + 3);
|
||||||
assert(it == v.begin() + 5);
|
// assert(it == v.begin() + 5);
|
||||||
|
//
|
||||||
int needle2[] = { 4, 5, 1 };
|
// int needle2[] = { 4, 5, 1 };
|
||||||
it = TinySTL::find_end(v.begin(), v.end(), needle2, needle2 + 3, [](int i, int j){return i == j; });
|
// it = TinySTL::find_end(v.begin(), v.end(), needle2, needle2 + 3, [](int i, int j){return i == j; });
|
||||||
assert(it == v.begin() + 3);
|
// assert(it == v.begin() + 3);
|
||||||
}
|
// }
|
||||||
void testFindFirstOf(){
|
// void testFindFirstOf(){
|
||||||
int mychars[] = { 'a', 'b', 'c', 'A', 'B', 'C' };
|
// int mychars[] = { 'a', 'b', 'c', 'A', 'B', 'C' };
|
||||||
std::vector<char> v(mychars, mychars + 6);
|
// std::vector<char> v(mychars, mychars + 6);
|
||||||
int needle[] = { 'A', 'B', 'C' };
|
// int needle[] = { 'A', 'B', 'C' };
|
||||||
auto it = TinySTL::find_first_of(v.begin(), v.end(), needle, needle + 3);
|
// auto it = TinySTL::find_first_of(v.begin(), v.end(), needle, needle + 3);
|
||||||
assert(*it == 'A');
|
// assert(*it == 'A');
|
||||||
|
//
|
||||||
it = TinySTL::find_first_of(v.begin(), v.end(), needle, needle + 3,
|
// it = TinySTL::find_first_of(v.begin(), v.end(), needle, needle + 3,
|
||||||
[](char ch1, char ch2){return std::tolower(ch1) == std::tolower(ch2); });
|
// [](char ch1, char ch2){return std::tolower(ch1) == std::tolower(ch2); });
|
||||||
assert(*it == 'a');
|
// assert(*it == 'a');
|
||||||
}
|
// }
|
||||||
void testAdjacentFind(){
|
// void testAdjacentFind(){
|
||||||
int myints[] = { 5, 20, 5, 30, 30, 20, 10, 10, 20 };
|
// int myints[] = { 5, 20, 5, 30, 30, 20, 10, 10, 20 };
|
||||||
std::vector<int> v(myints, myints + 8);
|
// std::vector<int> v(myints, myints + 8);
|
||||||
auto it = TinySTL::adjacent_find(v.begin(), v.end());
|
// auto it = TinySTL::adjacent_find(v.begin(), v.end());
|
||||||
assert(*it == 30);
|
// assert(*it == 30);
|
||||||
|
//
|
||||||
it = TinySTL::adjacent_find(++it, v.end(), [](int i, int j){return i == j; });
|
// it = TinySTL::adjacent_find(++it, v.end(), [](int i, int j){return i == j; });
|
||||||
assert(*it == 10);
|
// assert(*it == 10);
|
||||||
}
|
// }
|
||||||
void testCount(){
|
// void testCount(){
|
||||||
int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 }; // 8 elements
|
// int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 }; // 8 elements
|
||||||
int mycount = TinySTL::count(myints, myints + 8, 10);
|
// int mycount = TinySTL::count(myints, myints + 8, 10);
|
||||||
assert(mycount == 3);
|
// assert(mycount == 3);
|
||||||
|
//
|
||||||
mycount = TinySTL::count_if(myints, myints + 8, [](int i){return i % 2 == 0; });
|
// mycount = TinySTL::count_if(myints, myints + 8, [](int i){return i % 2 == 0; });
|
||||||
assert(mycount == 8);
|
// assert(mycount == 8);
|
||||||
}
|
// }
|
||||||
void testMismatch(){
|
// void testMismatch(){
|
||||||
std::vector<int> v;
|
// std::vector<int> v;
|
||||||
for (int i = 1; i<6; i++) v.push_back(i * 10); //10 20 30 40 50
|
// for (int i = 1; i<6; i++) v.push_back(i * 10); //10 20 30 40 50
|
||||||
int myints[] = { 10, 20, 80, 320, 1024 };
|
// int myints[] = { 10, 20, 80, 320, 1024 };
|
||||||
TinySTL::pair<std::vector<int>::iterator, int*> mypair;
|
// TinySTL::pair<std::vector<int>::iterator, int*> mypair;
|
||||||
mypair = TinySTL::mismatch(v.begin(), v.end(), myints);
|
// mypair = TinySTL::mismatch(v.begin(), v.end(), myints);
|
||||||
assert(*mypair.first == 30 && *mypair.second == 80);
|
// assert(*mypair.first == 30 && *mypair.second == 80);
|
||||||
|
//
|
||||||
++mypair.first; ++mypair.second;
|
// ++mypair.first; ++mypair.second;
|
||||||
mypair = TinySTL::mismatch(mypair.first, v.end(), mypair.second, [](int i, int j){return i == j; });
|
// mypair = TinySTL::mismatch(mypair.first, v.end(), mypair.second, [](int i, int j){return i == j; });
|
||||||
}
|
// }
|
||||||
void testEqual(){
|
// void testEqual(){
|
||||||
int myints[] = { 20, 40, 60, 80, 100 };
|
// int myints[] = { 20, 40, 60, 80, 100 };
|
||||||
std::vector<int>v(myints, myints + 5); //20 40 60 80 100
|
// std::vector<int>v(myints, myints + 5); //20 40 60 80 100
|
||||||
assert(TinySTL::equal(v.begin(), v.end(), myints));
|
// assert(TinySTL::equal(v.begin(), v.end(), myints));
|
||||||
|
//
|
||||||
v[3] = 81;
|
// v[3] = 81;
|
||||||
assert(!TinySTL::equal(v.begin(), v.end(), myints, [](int i, int j){return i == j; }));
|
// assert(!TinySTL::equal(v.begin(), v.end(), myints, [](int i, int j){return i == j; }));
|
||||||
}
|
// }
|
||||||
void testIsPermutation(){
|
// void testIsPermutation(){
|
||||||
std::array<int, 5> foo = { 1, 2, 3, 4, 5 };
|
// std::array<int, 5> foo = { 1, 2, 3, 4, 5 };
|
||||||
std::array<int, 5> bar = { 3, 1, 4, 5, 2 };
|
// std::array<int, 5> bar = { 3, 1, 4, 5, 2 };
|
||||||
|
//
|
||||||
assert(TinySTL::is_permutation(foo.begin(), foo.end(), bar.begin()));
|
// assert(TinySTL::is_permutation(foo.begin(), foo.end(), bar.begin()));
|
||||||
}
|
// }
|
||||||
void testSearch(){
|
// void testSearch(){
|
||||||
std::vector<int> v;
|
// std::vector<int> v;
|
||||||
for (int i = 1; i<10; i++) v.push_back(i * 10);
|
// for (int i = 1; i<10; i++) v.push_back(i * 10);
|
||||||
int needle1[] = { 40, 50, 60, 70 };
|
// int needle1[] = { 40, 50, 60, 70 };
|
||||||
auto it = TinySTL::search(v.begin(), v.end(), needle1, needle1 + 4);
|
// auto it = TinySTL::search(v.begin(), v.end(), needle1, needle1 + 4);
|
||||||
assert(it == v.begin() + 3);
|
// assert(it == v.begin() + 3);
|
||||||
|
//
|
||||||
int needle2[] = { 20, 30, 50 };
|
// int needle2[] = { 20, 30, 50 };
|
||||||
it = std::search(v.begin(), v.end(), needle2, needle2 + 3, [](int i, int j){return i == j; });
|
// it = std::search(v.begin(), v.end(), needle2, needle2 + 3, [](int i, int j){return i == j; });
|
||||||
assert(it == v.end());
|
// assert(it == v.end());
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
//
|
||||||
//using namespace TinySTL::AlgorithmTest;
|
//using namespace TinySTL::AlgorithmTest;
|
||||||
//int main(){
|
//int main(){
|
||||||
// //testFill();
|
// //testFill();
|
||||||
|
|||||||
Reference in New Issue
Block a user