From 13abfa519a7c53543ed1e8e4e8f39db39398b30f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Mon, 5 Jan 2015 09:49:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90circular=5Fbuffer=E7=9A=84?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Test/CircularBufferTest.cpp | 85 +++++++++++++++++++++++++++++ TinySTL/Test/CircularBufferTest.h | 36 ++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 TinySTL/Test/CircularBufferTest.cpp create mode 100644 TinySTL/Test/CircularBufferTest.h diff --git a/TinySTL/Test/CircularBufferTest.cpp b/TinySTL/Test/CircularBufferTest.cpp new file mode 100644 index 0000000..c0b7abe --- /dev/null +++ b/TinySTL/Test/CircularBufferTest.cpp @@ -0,0 +1,85 @@ +#include "CircularBufferTest.h" + +namespace TinySTL{ + namespace CircularBufferTest{ + void testCase1(){ + tsCB cb1(10, 1); + for (auto i = 0; i != 10; ++i) + assert(cb1[i] == 1); + + int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + tsCB cb2(std::begin(arr), std::end(arr)); + for (auto i = 0; i != 10; ++i) + assert(cb2[i] == i); + + auto cb3(cb2); + assert(circular_buffer_equal(cb2, cb3)); + + auto cb4(std::move(cb2));//cb2 clear + assert(circular_buffer_equal(cb3, cb4)); + + auto cb5 = cb3; + assert(circular_buffer_equal(cb3, cb5)); + + auto cb6 = std::move(cb3);//cb3 clear + assert(circular_buffer_equal(cb5, cb6)); + } + void testCase2(){ + tsCB cb(1); + assert(cb.size() == 1); + cb.pop_front(); + assert(!cb.full()); + assert(cb.size() == 0); + assert(cb.empty()); + + cb.push_back(1), cb.push_back(2); + assert(cb.full()); + assert(!cb.empty()); + assert(cb.size() == 2); + } + void testCase3(){ + tsCB cb(3); + cb[0] = "one", cb[1] = "two", cb[2] = "three"; + + assert(*(cb.first()) == "one" && *(cb.last()) == "three"); + + assert(cb.front() == "one" && cb.back() == "three"); + } + void testCase4(){ + tsCB cb(1); + assert(cb.front() == std::string()); + + cb.push_back("zxh"); cb.push_back("jwl"); + assert(cb.back() == "jwl"); + cb.pop_front(); + assert(cb.front() == "zxh"); + } + void testCase5(){ + tsCB cb1(3), cb2(3); + assert(cb1 == cb2); + assert(!(cb1 != cb2)); + + cb1[0] = -1; + assert(!(cb1 == cb2)); + assert(cb1 != cb2); + } + void testCase6(){ + std::string arr[] = { "1", "2", "3" }; + tsCB cb(std::begin(arr), std::end(arr)); + std::cout << cb << std::endl; + } + } +} + +//using namespace TinySTL::CircularBufferTest; +//int main(){ +// testCase1(); +// testCase2(); +// testCase3(); +// testCase4(); +// testCase5(); +// testCase6(); +// +// system("pause"); +// return 0; +//} \ No newline at end of file diff --git a/TinySTL/Test/CircularBufferTest.h b/TinySTL/Test/CircularBufferTest.h new file mode 100644 index 0000000..f5a8c18 --- /dev/null +++ b/TinySTL/Test/CircularBufferTest.h @@ -0,0 +1,36 @@ +#ifndef _CIRCULAR_BUFFER_TEST_H_ +#define _CIRCULAR_BUFFER_TEST_H_ + +#include "TestUtil.h" + +#include "../CircularBuffer.h" + +#include +#include +#include + +namespace TinySTL{ + namespace CircularBufferTest{ + template + using tsCB = TinySTL::circular_buffer; + + template + bool circular_buffer_equal(tsCB& cb1, tsCB cb2){ + auto it1 = cb1.first(), it2 = cb2.first(); + for (; it1 != cb1.last() && it2 != cb1.last(); ++it1, ++it2){ + if (*it1 != *it2) + return false; + } + return (it1 == cb1.last() && it2 == cb2.last() && (*(cb1.last()) == *(cb2.last()))); + } + + void testCase1(); + void testCase2(); + void testCase3(); + void testCase4(); + void testCase5(); + void testCase6(); + } +} + +#endif \ No newline at end of file