完成二叉搜索树的测试
This commit is contained in:
70
TinySTL/Test/BinarySearchTreeTest.cpp
Normal file
70
TinySTL/Test/BinarySearchTreeTest.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
#include "BinarySearchTreeTest.h"
|
||||
|
||||
namespace TinySTL{
|
||||
namespace BinarySearchTreeTest{
|
||||
template<class Container1, class Container2>
|
||||
bool container_equal(const Container1& con1, const Container2& con2){
|
||||
auto first1 = con1.cbegin(), last1 = con1.cend();
|
||||
auto first2 = con2.cbegin(), last2 = con2.cend();
|
||||
for (; first1 != last1 && first2 != last2; ++first1, ++first2){
|
||||
if (*first1 != *first2)
|
||||
return false;
|
||||
}
|
||||
return (first1 == last1 && first2 == last2);
|
||||
}
|
||||
|
||||
|
||||
void testCase1(){
|
||||
tsBst<std::string> bst;
|
||||
|
||||
assert(bst.empty());
|
||||
assert(bst.size() == 0);
|
||||
|
||||
bst.insert("1");
|
||||
assert(!bst.empty());
|
||||
assert(bst.size() == 1);
|
||||
}
|
||||
void testCase2(){
|
||||
tsBst<int> bst;
|
||||
|
||||
for (auto i = 0; i != 100; ++i)
|
||||
bst.insert(i);
|
||||
assert(bst.height() == 100);
|
||||
}
|
||||
void testCase3(){
|
||||
tsBst<int> bst;
|
||||
std::vector<int> v;
|
||||
std::random_device rd;
|
||||
|
||||
for (auto i = 0; i != 10; ++i){
|
||||
auto r = rd() % 65536;
|
||||
bst.insert(r);
|
||||
v.push_back(r);
|
||||
}
|
||||
std::sort(v.begin(), v.end());
|
||||
v.erase(std::unique(v.begin(), v.end()), v.end());
|
||||
assert(container_equal(bst, v));
|
||||
|
||||
tsBst<int> bst1;
|
||||
bst1.insert(v.begin(), v.end());
|
||||
assert(container_equal(bst1, v));
|
||||
}
|
||||
void testCase4(){
|
||||
tsBst<int> bst;
|
||||
for (auto i = 0; i != 10; ++i)
|
||||
bst.insert(i);
|
||||
|
||||
assert(*bst.find(5) == 5);
|
||||
assert(*bst.find_min() == 0);
|
||||
assert(*bst.find_max() == 9);
|
||||
}
|
||||
|
||||
|
||||
void testAllCases(){
|
||||
testCase1();
|
||||
testCase2();
|
||||
testCase3();
|
||||
testCase4();
|
||||
}
|
||||
}
|
||||
}
|
||||
24
TinySTL/Test/BinarySearchTreeTest.h
Normal file
24
TinySTL/Test/BinarySearchTreeTest.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef _BINARY_SEARCH_TREE_TEST_H_
|
||||
#define _BINARY_SEARCH_TREE_TEST_H_
|
||||
|
||||
#include "TestUtil.h"
|
||||
|
||||
#include "../BinarySearchTree.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace TinySTL{
|
||||
namespace BinarySearchTreeTest{
|
||||
template<class T>
|
||||
using tsBst = TinySTL::binary_search_tree < T > ;
|
||||
|
||||
|
||||
void testAllCases();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "Test\AlgorithmTest.h"
|
||||
#include "Test\BitmapTest.h"
|
||||
#include "Test\BinarySearchTreeTest.h"
|
||||
#include "Test\CircularBufferTest.h"
|
||||
#include "Test\DequeTest.h"
|
||||
#include "Test\ListTest.h"
|
||||
@@ -22,6 +23,7 @@ using namespace TinySTL::Profiler;
|
||||
int main(){
|
||||
TinySTL::AlgorithmTest::testAllCases();
|
||||
TinySTL::BitmapTest::testAllCases();
|
||||
TinySTL::BinarySearchTreeTest::testAllCases();
|
||||
TinySTL::CircularBufferTest::testAllCases();
|
||||
TinySTL::DequeTest::testAllCases();
|
||||
TinySTL::ListTest::testAllCases();
|
||||
|
||||
Reference in New Issue
Block a user