提交 666f2899 编写于 作者: 邹晓航

完成二叉搜索树的测试

上级 96a1c5ec
#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();
}
}
}
\ No newline at end of file
#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
\ No newline at end of file
......@@ -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();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册