完成pair的test

This commit is contained in:
邹晓航
2014-12-20 10:16:01 +08:00
parent f5873cb963
commit 9f1b5cb12d
4 changed files with 89 additions and 48 deletions

View File

@@ -1,44 +1,64 @@
#include <iostream>
#include "PairTest.h"
#include "..\Utility.h"
#include "..\String.h"
using namespace TinySTL;
void testPair(){
// test ctor
pair <string, double> product1; // default constructor
pair <string, double> product2("tomatoes", 2.30); // value init
pair <string, double> product3(product2); // copy constructor
product1 = make_pair(string("lightbulbs"), 0.99); // using make_pair (move)
product2.first = "shoes"; // the type of first is string
product2.second = 39.90; // the type of second is double
std::cout << "The price of " << product1.first << " is $" << product1.second << '\n';
std::cout << "The price of " << product2.first << " is $" << product2.second << '\n';
std::cout << "The price of " << product3.first << " is $" << product3.second << '\n';
//test operator =
pair <string, int> planet, homeplanet;
planet = make_pair(string("Earth"), 6371);
homeplanet = planet;
std::cout << "Home planet: " << homeplanet.first << '\n';
std::cout << "Planet size: " << homeplanet.second << '\n';
//test swap
pair<int, char> foo1(10, 'a');
pair<int, char> bar1(90, 'z');
//foo1.swap(bar1);
swap(foo1, bar1);
std::cout << "foo contains: " << foo1.first;
std::cout << " and " << foo1.second << '\n';
//test relational operators
pair<int, char> foo(10, 'z');
pair<int, char> bar(90, 'a');
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";
}
namespace TinySTL{
namespace PairTest{
template<class Container1, class Container2>
static inline bool container_equal(const Container1& pair1, const Container2& pair2){
return (pair1.first == pair2.first && pair1.second == pair2.second);
}
void testCase1(){
stdPair<int> p1(5, 5);
tsPair<int> p2(5, 5);
assert(container_equal(p1, p2));
}
void testCase2(){
stdPair<int> p1(stdPair<int>(0, 0));
tsPair<int> p2(tsPair<int>(0, 0));
assert(container_equal(p1, p2));
}
void testCase3(){
stdPair<std::string> temp1 = std::make_pair(std::string("zxh"), std::string("zxh"));
stdPair<std::string> p1 = temp1;
tsPair<std::string> temp2 = TinySTL::make_pair(std::string("zxh"), std::string("zxh"));
tsPair<std::string> p2 = temp2;
assert(container_equal(p1, p2));
}
void testCase4(){
TinySTL::pair<int, char> foo(10, 'z');
TinySTL::pair<int, char> 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";
}
void testCase5(){
TinySTL::pair<int, char> foo(10, 'z');
TinySTL::pair<int, char> bar(90, 'a');
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);
}
}
}
//using namespace TinySTL::PairTest;
//int main(){
// testCase1();
// testCase2();
// testCase3();
// testCase4();
// testCase5();
// system("pause");
// return 0;
//}

View File

@@ -1,10 +1,27 @@
#ifndef _PAIR_TEST_H_
#define _PAIR_TEST_H_
void testCase1();
void testCase2();
void testCase3();
void testCase4();
void testCase5();
#include "TestUtil.h"
#include "../Utility.h"
#include <utility>
#include <cassert>
#include <iostream>
#include <string>
namespace TinySTL{
namespace PairTest{
template<typename T>
using stdPair = std::pair < T, T > ;
template<typename T>
using tsPair = TinySTL::pair < T, T > ;
void testCase1();
void testCase2();
void testCase3();
void testCase4();
void testCase5();
}
}
#endif

View File

@@ -102,6 +102,7 @@
<ClInclude Include="ReverseIterator.h" />
<ClInclude Include="Stack.h" />
<ClInclude Include="String.h" />
<ClInclude Include="Test\PairTest.h" />
<ClInclude Include="Test\TestUtil.h" />
<ClInclude Include="TypeTraits.h" />
<ClInclude Include="UninitializedFunctions.h" />

View File

@@ -101,6 +101,9 @@
<ClInclude Include="Test\TestUtil.h">
<Filter>Test</Filter>
</ClInclude>
<ClInclude Include="Test\PairTest.h">
<Filter>Test</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\README.md" />