From 60b1ff8ee127dc8c837836c6ed9e228d24b506f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Tue, 30 Dec 2014 10:15:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Test/PriorityQueueTest.cpp | 74 +++++++++++++++++++++++++++++- TinySTL/Test/PriorityQueueTest.h | 8 ++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/TinySTL/Test/PriorityQueueTest.cpp b/TinySTL/Test/PriorityQueueTest.cpp index ede02c1..4b8c5a3 100644 --- a/TinySTL/Test/PriorityQueueTest.cpp +++ b/TinySTL/Test/PriorityQueueTest.cpp @@ -2,6 +2,78 @@ namespace TinySTL{ namespace PriorityQueueTest{ + void testCase1(){ + int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, -1, -2, -3 }; + stdPQ pq1(std::begin(arr), std::end(arr)); + tsPQ pq2(std::begin(arr), std::end(arr)); + + while (!pq1.empty() && !pq2.empty()){ + assert(pq1.top() == pq2.top()); + pq1.pop(); pq2.pop(); + } + assert(pq1.empty() && pq2.empty()); + } + void testCase2(){ + tsPQ pq; + assert(pq.empty()); + pq.push("zxh"); + assert(!pq.empty()); + } + void testCase3(){ + tsPQ pq; + auto i = 1; + for (; i != 10; ++i){ + pq.push(i); + assert(pq.size() == i); + } + for (i = pq.size(); i != 0; --i){ + pq.pop(); + assert(pq.size() == (i - 1)); + } + } + void testCase4(){ + stdPQ pq1; + tsPQ pq2; + + pq1.push(30); + pq1.push(100); + pq1.push(25); + pq1.push(40); + + pq2.push(30); + pq2.push(100); + pq2.push(25); + pq2.push(40); + + while (!pq1.empty() && !pq2.empty()){ + assert(pq1.top() == pq2.top()); + pq1.pop(); + pq2.pop(); + } + } + void testCase5(){ + tsPQ foo, bar; + foo.push(15); foo.push(30); foo.push(10); + bar.push(101); bar.push(202); + + assert(foo.size() == 3 && bar.size() == 2); + foo.swap(bar); + assert(foo.size() == 2 && bar.size() == 3); + + TinySTL::swap(foo, bar); + assert(foo.size() == 3 && bar.size() == 2); + } } -} \ No newline at end of file +} + +//using namespace TinySTL::PriorityQueueTest; +//int main(){ +// testCase1(); +// testCase2(); +// testCase3(); +// testCase4(); +// testCase5(); +// system("pause"); +// return 0; +//} \ No newline at end of file diff --git a/TinySTL/Test/PriorityQueueTest.h b/TinySTL/Test/PriorityQueueTest.h index 9a3f25c..d61850e 100644 --- a/TinySTL/Test/PriorityQueueTest.h +++ b/TinySTL/Test/PriorityQueueTest.h @@ -6,7 +6,9 @@ #include "../Queue.h" #include +#include #include +#include namespace TinySTL{ namespace PriorityQueueTest{ @@ -14,6 +16,12 @@ namespace TinySTL{ using stdPQ = std::priority_queue < T > ; template using tsPQ = TinySTL::priority_queue < T > ; + + void testCase1(); + void testCase2(); + void testCase3(); + void testCase4(); + void testCase5(); } }