From 666f2899230e03565bf117fafa77a75378760d61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Wed, 14 Jan 2015 14:51:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E7=9A=84=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Test/BinarySearchTreeTest.cpp | 70 +++++++++++++++++++++++++++ TinySTL/Test/BinarySearchTreeTest.h | 24 +++++++++ TinySTL/main.cpp | 2 + 3 files changed, 96 insertions(+) create mode 100644 TinySTL/Test/BinarySearchTreeTest.cpp create mode 100644 TinySTL/Test/BinarySearchTreeTest.h diff --git a/TinySTL/Test/BinarySearchTreeTest.cpp b/TinySTL/Test/BinarySearchTreeTest.cpp new file mode 100644 index 0000000..50e0cf1 --- /dev/null +++ b/TinySTL/Test/BinarySearchTreeTest.cpp @@ -0,0 +1,70 @@ +#include "BinarySearchTreeTest.h" + +namespace TinySTL{ + namespace BinarySearchTreeTest{ + 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(){ + tsBst bst; + + assert(bst.empty()); + assert(bst.size() == 0); + + bst.insert("1"); + assert(!bst.empty()); + assert(bst.size() == 1); + } + void testCase2(){ + tsBst bst; + + for (auto i = 0; i != 100; ++i) + bst.insert(i); + assert(bst.height() == 100); + } + void testCase3(){ + tsBst bst; + std::vector 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 bst1; + bst1.insert(v.begin(), v.end()); + assert(container_equal(bst1, v)); + } + void testCase4(){ + tsBst 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 diff --git a/TinySTL/Test/BinarySearchTreeTest.h b/TinySTL/Test/BinarySearchTreeTest.h new file mode 100644 index 0000000..0cd4023 --- /dev/null +++ b/TinySTL/Test/BinarySearchTreeTest.h @@ -0,0 +1,24 @@ +#ifndef _BINARY_SEARCH_TREE_TEST_H_ +#define _BINARY_SEARCH_TREE_TEST_H_ + +#include "TestUtil.h" + +#include "../BinarySearchTree.h" + +#include +#include +#include +#include +#include + +namespace TinySTL{ + namespace BinarySearchTreeTest{ + template + using tsBst = TinySTL::binary_search_tree < T > ; + + + void testAllCases(); + } +} + +#endif \ No newline at end of file diff --git a/TinySTL/main.cpp b/TinySTL/main.cpp index 688c139..c52258f 100644 --- a/TinySTL/main.cpp +++ b/TinySTL/main.cpp @@ -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();