From bff1793592e54afc448b1f9634d8fe3ae4a8ec16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Sat, 14 Mar 2015 19:15:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E5=8E=BB=E8=87=AA=E5=8A=A8=E5=8C=96?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=9A=84=E5=B1=8F=E5=B9=95=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Test/BitmapTest.cpp | 12 ++--- TinySTL/Test/CircularBufferTest.cpp | 7 ++- TinySTL/Test/GraphTest.cpp | 74 +++++++++++++++++++++++++---- TinySTL/Test/PairTest.cpp | 21 ++++---- TinySTL/Test/StringTest.cpp | 45 +++++++++--------- TinySTL/Test/StringTest.h | 1 - TinySTL/Test/VectorTest.cpp | 46 +++++++++--------- TinySTL/TestData/string_test.txt | 1 + TinySTL/TinySTL.vcxproj | 1 + TinySTL/TinySTL.vcxproj.filters | 3 ++ 10 files changed, 136 insertions(+), 75 deletions(-) create mode 100644 TinySTL/TestData/string_test.txt diff --git a/TinySTL/Test/BitmapTest.cpp b/TinySTL/Test/BitmapTest.cpp index 022d454..eba1367 100644 --- a/TinySTL/Test/BitmapTest.cpp +++ b/TinySTL/Test/BitmapTest.cpp @@ -15,22 +15,22 @@ namespace TinySTL{ void testCase2(){ bitmap<8> bt1, bt2; bt1.set(); - cout << bt1 << endl; + assert(bt1.to_string() == "11111111"); bt1.reset(); - cout << bt1 << endl; + assert(bt1.to_string() == "00000000"); bt2.set(0); bt2.set(2); bt2.set(4); - cout << bt2 << endl; + assert(bt2.to_string() == "10101000"); bt2.reset(0); bt2.reset(2); bt2.reset(4); - cout << bt2 << endl; + assert(bt2.to_string() == "00000000"); } void testCase3(){ bitmap<8> bt; bt.flip(); - cout << bt << endl; + assert(bt.to_string() == "11111111"); bt.flip(0); - cout << bt << endl; + assert(bt.to_string() == "01111111"); } void testCase4(){ bitmap<8> bt; diff --git a/TinySTL/Test/CircularBufferTest.cpp b/TinySTL/Test/CircularBufferTest.cpp index f888ffb..e104964 100644 --- a/TinySTL/Test/CircularBufferTest.cpp +++ b/TinySTL/Test/CircularBufferTest.cpp @@ -1,5 +1,7 @@ #include "CircularBufferTest.h" +#include + namespace TinySTL{ namespace CircularBufferTest{ void testCase1(){ @@ -66,7 +68,10 @@ namespace TinySTL{ void testCase6(){ std::string arr[] = { "1", "2", "3" }; tsCB cb(std::begin(arr), std::end(arr)); - std::cout << cb << std::endl; + + std::ostringstream os; + os << cb; + assert(os.str() == "(1, 2, 3)"); } diff --git a/TinySTL/Test/GraphTest.cpp b/TinySTL/Test/GraphTest.cpp index ed26904..a6bdf45 100644 --- a/TinySTL/Test/GraphTest.cpp +++ b/TinySTL/Test/GraphTest.cpp @@ -1,5 +1,8 @@ #include "GraphTest.h" +#include +#include + namespace TinySTL{ namespace GraphTest{ void testCase1(){ @@ -36,20 +39,71 @@ namespace TinySTL{ g.make_edge(12, 2); g.make_edge(12, 3); g.make_edge(12, 0); - std::cout << "graph after add nodes:" << std::endl; - std::cout << g.to_string(); + std::ostringstream os; + os << g.to_string(); + std::string res(R"([14,1414]:[nil] + | +[13,1313]:[nil] + | +[12,1212]:[0,0]->[3,33]->[2,22]->[nil] + | +[7,77]:[14,1414]->[13,1313]->[12,1212]->[nil] + | +[6,66]:[nil] + | +[5,55]:[nil] + | +[3,33]:[nil] + | +[2,22]:[nil] + | +[1,11]:[7,77]->[6,66]->[5,55]->[nil] + | +[0,0]:[3,33]->[2,22]->[1,11]->[nil] + | +[nil] +)"); + assert(os.str() == res); - auto func = [](const dGraph::node_type& node){ - std::cout << "[" << node.first << "," << node.second << "]" << std::endl; + std::ostringstream os1, os2; + auto func1 = [&os1](const dGraph::node_type& node){ + os1 << "[" << node.first << "," << node.second << "]"; + }; + auto func2 = [&os2](const dGraph::node_type& node){ + os2 << "[" << node.first << "," << node.second << "]"; }; - std::cout << "graph DFS from node(1, 11):" << std::endl; - g.DFS(1, func); - std::cout << "graph BFS from node(1, 11):" << std::endl; - g.BFS(1, func); + res = R"([1,11][7,77][14,1414][13,1313][12,1212][0,0][3,33][2,22][6,66][5,55])"; + g.DFS(1, func1); + assert(os1.str() == res); + + res = R"([1,11][7,77][6,66][5,55][14,1414][13,1313][12,1212][0,0][3,33][2,22])"; + g.BFS(1, func2); + assert(os2.str() == res); - std::cout << "graph after delete node(7, 77):" << std::endl; + std::ostringstream os3; g.delete_node(dGraph::node_type(7, 77)); - std::cout << g.to_string(); + os3 << g.to_string(); + res = R"([14,1414]:[nil] + | +[13,1313]:[nil] + | +[12,1212]:[0,0]->[3,33]->[2,22]->[nil] + | +[6,66]:[nil] + | +[5,55]:[nil] + | +[3,33]:[nil] + | +[2,22]:[nil] + | +[1,11]:[6,66]->[5,55]->[nil] + | +[0,0]:[3,33]->[2,22]->[1,11]->[nil] + | +[nil] +)"; + assert(os3.str() == res); } void testCase3(){ dGraph g; diff --git a/TinySTL/Test/PairTest.cpp b/TinySTL/Test/PairTest.cpp index 340c453..05dc492 100644 --- a/TinySTL/Test/PairTest.cpp +++ b/TinySTL/Test/PairTest.cpp @@ -29,15 +29,12 @@ namespace TinySTL{ TinySTL::pair foo(10, 'z'); TinySTL::pair bar(90, 'a'); - //foo and bar are not equal - //foo is less than bar - //foo is less than or equal to bar - if (foo == bar) std::cout << "foo and bar are equal\n"; - if (foo != bar) std::cout << "foo and bar are not equal\n"; - if (foo< bar) std::cout << "foo is less than bar\n"; - if (foo> bar) std::cout << "foo is greater than bar\n"; - if (foo <= bar) std::cout << "foo is less than or equal to bar\n"; - if (foo >= bar) std::cout << "foo is greater than or equal to bar\n"; + assert(!(foo == bar)); + assert(foo != bar); + assert(foo < bar); + assert(!(foo > bar)); + assert(foo <= bar); + assert(!(foo >= bar)); } void testCase5(){ TinySTL::pair foo(10, 'z'); @@ -45,10 +42,8 @@ namespace TinySTL{ foo.swap(bar); - std::cout << "foo : (" << foo.first << ", " << foo.second << ")" << std::endl; - std::cout << "bar : (" << bar.first << ", " << bar.second << ")" << std::endl; - //TinySTL::Test::print_container(foo); - //TinySTL::Test::print_container(bar); + assert(foo.first == 90 && foo.second == 'a'); + assert(bar.first == 10 && bar.second == 'z'); } diff --git a/TinySTL/Test/StringTest.cpp b/TinySTL/Test/StringTest.cpp index 47689dd..6e32d8a 100644 --- a/TinySTL/Test/StringTest.cpp +++ b/TinySTL/Test/StringTest.cpp @@ -1,5 +1,7 @@ #include "StringTest.h" +#include + namespace TinySTL{ namespace StringTest{ @@ -65,12 +67,16 @@ namespace TinySTL{ } void testCase4(){ tsStr str("Test string"); - for (tsStr::iterator it = str.begin(); it != str.end(); ++it) - std::cout << *it; - std::cout << '\n'; - for (tsStr::reverse_iterator it = str.rbegin(); it != str.rend(); ++it) - std::cout << *it; - std::cout << '\n'; + stdStr s(str.begin(), str.end()); + auto i = 0; + for (tsStr::iterator it = str.begin(); it != str.end(); ++it, ++i){ + assert(*it == s[i]); + } + + i = s.size() - 1; + for (tsStr::reverse_iterator it = str.rbegin(); it != str.rend(); ++it, --i){ + assert(*it == s[i]); + } } void testCase5(){ tsStr s; @@ -121,11 +127,11 @@ namespace TinySTL{ s.resize(10); for (auto i = 0; i != s.size(); ++i) s[i] = 'a' + i; - TinySTL::Test::print_container(s); + assert(s == "abcdefghij"); s.back() = 'Z'; s.front() = 'A'; - TinySTL::Test::print_container(s); + assert(s == "AbcdefghiZ"); } void testCase10(){ stdStr s1; @@ -295,15 +301,15 @@ namespace TinySTL{ tsStr seller("goods"); seller.swap(buyer); - TinySTL::Test::print_container(buyer, "buyer"); - TinySTL::Test::print_container(seller, "seller"); + assert(seller == "money"); + assert(buyer == "goods"); } void testCase18(){ char buffer[20]; tsStr str("Test string..."); std::size_t length = str.copy(buffer, 6, 5); buffer[length] = '\0'; - std::cout << "buffer contains: " << buffer << '\n'; + std::equal(std::begin(buffer), std::end(buffer), str.begin()); } void testCase19(){ tsStr str("There are two needles in this haystack with needles."); @@ -432,17 +438,13 @@ namespace TinySTL{ } void testCase29(){ tsStr name; + std::ifstream in(".\\TestData\\string_test.txt"); - std::cout << "Please, enter your name: "; - std::cin >> name; - std::cout << "Hello, " << name << "!\n"; - } - void testCase30(){ - tsStr name; - - std::cout << "Please, enter your full name: "; - TinySTL::getline(std::cin, name); - std::cout << "Hello, " << name << "!\n"; + if (in){ + in >> name; + assert(name == "zouxiaohang"); + in.close(); + } } void testAllCases(){ @@ -475,7 +477,6 @@ namespace TinySTL{ testCase27(); testCase28(); testCase29(); - testCase30(); } } } \ No newline at end of file diff --git a/TinySTL/Test/StringTest.h b/TinySTL/Test/StringTest.h index ddc5977..3e677b2 100644 --- a/TinySTL/Test/StringTest.h +++ b/TinySTL/Test/StringTest.h @@ -43,7 +43,6 @@ namespace TinySTL{ void testCase27(); void testCase28(); void testCase29(); - void testCase30(); void testAllCases(); } diff --git a/TinySTL/Test/VectorTest.cpp b/TinySTL/Test/VectorTest.cpp index b6d1bbd..514b522 100644 --- a/TinySTL/Test/VectorTest.cpp +++ b/TinySTL/Test/VectorTest.cpp @@ -51,15 +51,15 @@ namespace TinySTL{ tsVec myvector; for (int i = 1; i <= 5; i++) myvector.push_back(i); - std::cout << "myvector contains:"; - for (tsVec::iterator it = myvector.begin(); it != myvector.end(); ++it) - std::cout << ' ' << *it; - std::cout << '\n'; - - std::cout << "myvector contains:"; - for (tsVec::const_iterator it = myvector.cbegin(); it != myvector.cend(); ++it) - std::cout << ' ' << *it; - std::cout << '\n'; + auto i = 1; + for (tsVec::iterator it = myvector.begin(); it != myvector.end(); ++it, ++i){ + assert(*it == i); + } + + i = 1; + for (tsVec::const_iterator it = myvector.cbegin(); it != myvector.cend(); ++it, ++i){ + assert(*it == i); + } } void testCase5(){ tsVec myvector(5); // 5 default-constructed ints @@ -68,14 +68,15 @@ namespace TinySTL{ for (; rit != myvector.rend(); ++rit) *rit = ++i; - std::cout << "myvector contains:"; - for (tsVec::iterator it = myvector.begin(); it != myvector.end(); ++it) - std::cout << ' ' << *it; - std::cout << '\n'; - std::cout << "myvector contains(reverse order):"; - for (tsVec::reverse_iterator it = myvector.rbegin(); it != myvector.rend(); ++it) - std::cout << ' ' << *it; - std::cout << '\n'; + i = 5; + for (tsVec::iterator it = myvector.begin(); it != myvector.end(); ++it, --i){ + assert(*it == i); + } + + i = 1; + for (tsVec::reverse_iterator it = myvector.rbegin(); it != myvector.rend(); ++it, ++i){ + assert(*it == i); + } } void testCase6(){ tsVec v(11, 0); @@ -121,13 +122,14 @@ namespace TinySTL{ } void testCase10(){ tsVec foo(3, 100); // three ints with a value of 100 - tsVec bar(5, 200); // five ints with a value of 200 + tsVec bar(2, 200); // five ints with a value of 200 + + assert(TinySTL::Test::container_equal(foo, stdVec < int > { 100, 100, 100 })); + assert(TinySTL::Test::container_equal(bar, stdVec < int > {200, 200})); - TinySTL::Test::print_container(foo, "foo"); - TinySTL::Test::print_container(bar, "bar"); foo.swap(bar); - TinySTL::Test::print_container(foo, "foo"); - TinySTL::Test::print_container(bar, "bar"); + assert(TinySTL::Test::container_equal(bar, stdVec < int > { 100, 100, 100 })); + assert(TinySTL::Test::container_equal(foo, stdVec < int > {200, 200})); } void testCase11(){ stdVec v1; diff --git a/TinySTL/TestData/string_test.txt b/TinySTL/TestData/string_test.txt new file mode 100644 index 0000000..6b18a5d --- /dev/null +++ b/TinySTL/TestData/string_test.txt @@ -0,0 +1 @@ +zouxiaohang \ No newline at end of file diff --git a/TinySTL/TinySTL.vcxproj b/TinySTL/TinySTL.vcxproj index 7bd7aef..c4c559a 100644 --- a/TinySTL/TinySTL.vcxproj +++ b/TinySTL/TinySTL.vcxproj @@ -175,6 +175,7 @@ + diff --git a/TinySTL/TinySTL.vcxproj.filters b/TinySTL/TinySTL.vcxproj.filters index 3c2d0a6..5d06a90 100644 --- a/TinySTL/TinySTL.vcxproj.filters +++ b/TinySTL/TinySTL.vcxproj.filters @@ -300,5 +300,8 @@ TestData + + TestData + \ No newline at end of file -- GitLab