完成avl树的测试
This commit is contained in:
69
TinySTL/Test/AVLTreeTest.cpp
Normal file
69
TinySTL/Test/AVLTreeTest.cpp
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
#include "AVLTreeTest.h"
|
||||||
|
|
||||||
|
namespace TinySTL{
|
||||||
|
namespace AVLTreeTest{
|
||||||
|
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(){
|
||||||
|
tsAVL<std::string> avl;
|
||||||
|
assert(avl.empty());
|
||||||
|
assert(avl.size() == 0);
|
||||||
|
|
||||||
|
avl.insert("0");
|
||||||
|
assert(!avl.empty());
|
||||||
|
assert(avl.size() == 1);
|
||||||
|
assert(*avl.root() == "0");
|
||||||
|
}
|
||||||
|
void testCase2(){
|
||||||
|
tsAVL<int> avl;
|
||||||
|
for (auto i = 0; i != 10000; ++i){
|
||||||
|
avl.insert(i);
|
||||||
|
}
|
||||||
|
assert(avl.height() == 14);
|
||||||
|
}
|
||||||
|
void testCase3(){
|
||||||
|
tsAVL<int> avl;
|
||||||
|
for (auto i = 0; i != 10; ++i){
|
||||||
|
avl.insert(i);
|
||||||
|
}
|
||||||
|
assert(*avl.find(5) == 5);
|
||||||
|
assert(*avl.find_min() == 0);
|
||||||
|
assert(*avl.find_max() == 9);
|
||||||
|
}
|
||||||
|
void testCase4(){
|
||||||
|
tsAVL<int> avl;
|
||||||
|
std::vector<int> v;
|
||||||
|
std::random_device rd;
|
||||||
|
|
||||||
|
for (auto i = 0; i != 100; ++i){
|
||||||
|
auto r = rd() % 65536;
|
||||||
|
avl.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(avl, v));
|
||||||
|
|
||||||
|
tsAVL<int> avl1;
|
||||||
|
avl1.insert(v.begin(), v.end());
|
||||||
|
assert(container_equal(avl1, v));
|
||||||
|
}
|
||||||
|
|
||||||
|
void testAllCases(){
|
||||||
|
testCase1();
|
||||||
|
testCase2();
|
||||||
|
testCase3();
|
||||||
|
testCase4();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
29
TinySTL/Test/AVLTreeTest.h
Normal file
29
TinySTL/Test/AVLTreeTest.h
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#ifndef _AVL_TREE_TEST_H_
|
||||||
|
#define _AVL_TREE_TEST_H_
|
||||||
|
|
||||||
|
#include "TestUtil.h"
|
||||||
|
|
||||||
|
#include "../AVLTree.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cassert>
|
||||||
|
#include <random>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace TinySTL{
|
||||||
|
namespace AVLTreeTest{
|
||||||
|
template<class T>
|
||||||
|
using tsAVL = TinySTL::avl_tree < T > ;
|
||||||
|
|
||||||
|
void testCase1();
|
||||||
|
void testCase2();
|
||||||
|
void testCase3();
|
||||||
|
void testCase4();
|
||||||
|
|
||||||
|
|
||||||
|
void testAllCases();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "Profiler\Profiler.h"
|
#include "Profiler\Profiler.h"
|
||||||
|
|
||||||
#include "Test\AlgorithmTest.h"
|
#include "Test\AlgorithmTest.h"
|
||||||
|
#include "Test\AVLTreeTest.h"
|
||||||
#include "Test\BitmapTest.h"
|
#include "Test\BitmapTest.h"
|
||||||
#include "Test\BinarySearchTreeTest.h"
|
#include "Test\BinarySearchTreeTest.h"
|
||||||
#include "Test\CircularBufferTest.h"
|
#include "Test\CircularBufferTest.h"
|
||||||
@@ -22,6 +23,7 @@ using namespace TinySTL::Profiler;
|
|||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
TinySTL::AlgorithmTest::testAllCases();
|
TinySTL::AlgorithmTest::testAllCases();
|
||||||
|
TinySTL::AVLTreeTest::testAllCases();
|
||||||
TinySTL::BitmapTest::testAllCases();
|
TinySTL::BitmapTest::testAllCases();
|
||||||
TinySTL::BinarySearchTreeTest::testAllCases();
|
TinySTL::BinarySearchTreeTest::testAllCases();
|
||||||
TinySTL::CircularBufferTest::testAllCases();
|
TinySTL::CircularBufferTest::testAllCases();
|
||||||
|
|||||||
Reference in New Issue
Block a user