添加单元测试
This commit is contained in:
@@ -356,7 +356,7 @@ namespace TinySTL{
|
|||||||
if (*first1 != *first2)
|
if (*first1 != *first2)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return make_pair(first1, first2);
|
return TinySTL::make_pair(first1, first2);
|
||||||
}
|
}
|
||||||
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
|
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
|
||||||
pair<InputIterator1, InputIterator2>
|
pair<InputIterator1, InputIterator2>
|
||||||
|
|||||||
@@ -84,20 +84,112 @@ namespace TinySTL{
|
|||||||
|
|
||||||
assert(TinySTL::Test::container_equal(myvector, temp));
|
assert(TinySTL::Test::container_equal(myvector, temp));
|
||||||
}
|
}
|
||||||
|
void testFind(){
|
||||||
|
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(), 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<int> 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<char> 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<int> 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<int> 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<std::vector<int>::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::vector<int>v(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<int, 5> foo = { 1, 2, 3, 4, 5 };
|
||||||
|
std::array<int, 5> bar = { 3, 1, 4, 5, 2 };
|
||||||
|
|
||||||
|
assert(TinySTL::is_permutation(foo.begin(), foo.end(), bar.begin()));
|
||||||
|
}
|
||||||
|
void testSearch(){
|
||||||
|
std::vector<int> 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;
|
//using namespace TinySTL::AlgorithmTest;
|
||||||
int main(){
|
//int main(){
|
||||||
//testFill();
|
// //testFill();
|
||||||
//testFillN();
|
// //testFillN();
|
||||||
//testMinMax();
|
// //testMinMax();
|
||||||
//testHeapAlgorithm();
|
// //testHeapAlgorithm();
|
||||||
//testIsHeap();
|
// //testIsHeap();
|
||||||
//testAllOf();
|
// //testAllOf();
|
||||||
//testNoneOf();
|
// //testNoneOf();
|
||||||
//testAnyOf();
|
// //testAnyOf();
|
||||||
//testForEach();
|
// //testForEach();
|
||||||
system("pause");
|
// //testFind();
|
||||||
return 0;
|
// //testFindEnd();
|
||||||
}
|
// //testFindFirstOf();
|
||||||
|
// //testAdjacentFind();
|
||||||
|
// //testCount();
|
||||||
|
// //testMismatch();
|
||||||
|
// //testEqual();
|
||||||
|
// //testIsPermutation();
|
||||||
|
// //testSearch();
|
||||||
|
// system("pause");
|
||||||
|
// return 0;
|
||||||
|
//}
|
||||||
@@ -7,6 +7,8 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <cctype>
|
||||||
|
#include <cstring>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -21,6 +23,15 @@ namespace TinySTL{
|
|||||||
void testNoneOf();
|
void testNoneOf();
|
||||||
void testAnyOf();
|
void testAnyOf();
|
||||||
void testForEach();
|
void testForEach();
|
||||||
|
void testFind();
|
||||||
|
void testFindEnd();
|
||||||
|
void testFindFirstOf();
|
||||||
|
void testAdjacentFind();
|
||||||
|
void testCount();
|
||||||
|
void testMismatch();
|
||||||
|
void testEqual();
|
||||||
|
void testIsPermutation();
|
||||||
|
void testSearch();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user