From 6d4ddb0cd164f213774d98f3d54dbe48cdee1c8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Thu, 15 Jan 2015 09:08:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90avl=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Test/AVLTreeTest.cpp | 69 ++++++++++++++++++++++++++++++++++++ TinySTL/Test/AVLTreeTest.h | 29 +++++++++++++++ TinySTL/main.cpp | 2 ++ 3 files changed, 100 insertions(+) create mode 100644 TinySTL/Test/AVLTreeTest.cpp create mode 100644 TinySTL/Test/AVLTreeTest.h diff --git a/TinySTL/Test/AVLTreeTest.cpp b/TinySTL/Test/AVLTreeTest.cpp new file mode 100644 index 0000000..9f140fb --- /dev/null +++ b/TinySTL/Test/AVLTreeTest.cpp @@ -0,0 +1,69 @@ +#include "AVLTreeTest.h" + +namespace TinySTL{ + namespace AVLTreeTest{ + template + 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 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 avl; + for (auto i = 0; i != 10000; ++i){ + avl.insert(i); + } + assert(avl.height() == 14); + } + void testCase3(){ + tsAVL 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 avl; + std::vector 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 avl1; + avl1.insert(v.begin(), v.end()); + assert(container_equal(avl1, v)); + } + + void testAllCases(){ + testCase1(); + testCase2(); + testCase3(); + testCase4(); + } + } +} \ No newline at end of file diff --git a/TinySTL/Test/AVLTreeTest.h b/TinySTL/Test/AVLTreeTest.h new file mode 100644 index 0000000..470287f --- /dev/null +++ b/TinySTL/Test/AVLTreeTest.h @@ -0,0 +1,29 @@ +#ifndef _AVL_TREE_TEST_H_ +#define _AVL_TREE_TEST_H_ + +#include "TestUtil.h" + +#include "../AVLTree.h" + +#include +#include +#include +#include +#include + +namespace TinySTL{ + namespace AVLTreeTest{ + template + using tsAVL = TinySTL::avl_tree < T > ; + + void testCase1(); + void testCase2(); + void testCase3(); + void testCase4(); + + + void testAllCases(); + } +} + +#endif \ No newline at end of file diff --git a/TinySTL/main.cpp b/TinySTL/main.cpp index c52258f..6f0b90e 100644 --- a/TinySTL/main.cpp +++ b/TinySTL/main.cpp @@ -4,6 +4,7 @@ #include "Profiler\Profiler.h" #include "Test\AlgorithmTest.h" +#include "Test\AVLTreeTest.h" #include "Test\BitmapTest.h" #include "Test\BinarySearchTreeTest.h" #include "Test\CircularBufferTest.h" @@ -22,6 +23,7 @@ using namespace TinySTL::Profiler; int main(){ TinySTL::AlgorithmTest::testAllCases(); + TinySTL::AVLTreeTest::testAllCases(); TinySTL::BitmapTest::testAllCases(); TinySTL::BinarySearchTreeTest::testAllCases(); TinySTL::CircularBufferTest::testAllCases();