完成pair的test
This commit is contained in:
@@ -1,44 +1,64 @@
|
|||||||
#include <iostream>
|
#include "PairTest.h"
|
||||||
|
|
||||||
#include "..\Utility.h"
|
namespace TinySTL{
|
||||||
#include "..\String.h"
|
namespace PairTest{
|
||||||
|
template<class Container1, class Container2>
|
||||||
using namespace TinySTL;
|
static inline bool container_equal(const Container1& pair1, const Container2& pair2){
|
||||||
|
return (pair1.first == pair2.first && pair1.second == pair2.second);
|
||||||
void testPair(){
|
}
|
||||||
// test ctor
|
void testCase1(){
|
||||||
pair <string, double> product1; // default constructor
|
stdPair<int> p1(5, 5);
|
||||||
pair <string, double> product2("tomatoes", 2.30); // value init
|
tsPair<int> p2(5, 5);
|
||||||
pair <string, double> product3(product2); // copy constructor
|
assert(container_equal(p1, p2));
|
||||||
product1 = make_pair(string("lightbulbs"), 0.99); // using make_pair (move)
|
}
|
||||||
product2.first = "shoes"; // the type of first is string
|
void testCase2(){
|
||||||
product2.second = 39.90; // the type of second is double
|
stdPair<int> p1(stdPair<int>(0, 0));
|
||||||
std::cout << "The price of " << product1.first << " is $" << product1.second << '\n';
|
tsPair<int> p2(tsPair<int>(0, 0));
|
||||||
std::cout << "The price of " << product2.first << " is $" << product2.second << '\n';
|
assert(container_equal(p1, p2));
|
||||||
std::cout << "The price of " << product3.first << " is $" << product3.second << '\n';
|
}
|
||||||
|
void testCase3(){
|
||||||
//test operator =
|
stdPair<std::string> temp1 = std::make_pair(std::string("zxh"), std::string("zxh"));
|
||||||
pair <string, int> planet, homeplanet;
|
stdPair<std::string> p1 = temp1;
|
||||||
planet = make_pair(string("Earth"), 6371);
|
|
||||||
homeplanet = planet;
|
tsPair<std::string> temp2 = TinySTL::make_pair(std::string("zxh"), std::string("zxh"));
|
||||||
std::cout << "Home planet: " << homeplanet.first << '\n';
|
tsPair<std::string> p2 = temp2;
|
||||||
std::cout << "Planet size: " << homeplanet.second << '\n';
|
|
||||||
|
assert(container_equal(p1, p2));
|
||||||
//test swap
|
}
|
||||||
pair<int, char> foo1(10, 'a');
|
void testCase4(){
|
||||||
pair<int, char> bar1(90, 'z');
|
TinySTL::pair<int, char> foo(10, 'z');
|
||||||
//foo1.swap(bar1);
|
TinySTL::pair<int, char> bar(90, 'a');
|
||||||
swap(foo1, bar1);
|
|
||||||
std::cout << "foo contains: " << foo1.first;
|
//foo and bar are not equal
|
||||||
std::cout << " and " << foo1.second << '\n';
|
//foo is less than bar
|
||||||
|
//foo is less than or equal to bar
|
||||||
//test relational operators
|
if (foo == bar) std::cout << "foo and bar are equal\n";
|
||||||
pair<int, char> foo(10, 'z');
|
if (foo != bar) std::cout << "foo and bar are not equal\n";
|
||||||
pair<int, char> bar(90, 'a');
|
if (foo< bar) std::cout << "foo is less than bar\n";
|
||||||
if (foo == bar) std::cout << "foo and bar are equal\n";
|
if (foo> bar) std::cout << "foo is greater than bar\n";
|
||||||
if (foo != bar) std::cout << "foo and bar are not equal\n";
|
if (foo <= bar) std::cout << "foo is less than or equal to bar\n";
|
||||||
if (foo< bar) std::cout << "foo is less than bar\n";
|
if (foo >= bar) std::cout << "foo is greater than or equal to 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";
|
void testCase5(){
|
||||||
if (foo >= bar) std::cout << "foo is greater than or equal to bar\n";
|
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;
|
||||||
|
//}
|
||||||
@@ -1,10 +1,27 @@
|
|||||||
#ifndef _PAIR_TEST_H_
|
#ifndef _PAIR_TEST_H_
|
||||||
#define _PAIR_TEST_H_
|
#define _PAIR_TEST_H_
|
||||||
|
|
||||||
void testCase1();
|
#include "TestUtil.h"
|
||||||
void testCase2();
|
|
||||||
void testCase3();
|
|
||||||
void testCase4();
|
|
||||||
void testCase5();
|
|
||||||
|
|
||||||
|
#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
|
#endif
|
||||||
@@ -102,6 +102,7 @@
|
|||||||
<ClInclude Include="ReverseIterator.h" />
|
<ClInclude Include="ReverseIterator.h" />
|
||||||
<ClInclude Include="Stack.h" />
|
<ClInclude Include="Stack.h" />
|
||||||
<ClInclude Include="String.h" />
|
<ClInclude Include="String.h" />
|
||||||
|
<ClInclude Include="Test\PairTest.h" />
|
||||||
<ClInclude Include="Test\TestUtil.h" />
|
<ClInclude Include="Test\TestUtil.h" />
|
||||||
<ClInclude Include="TypeTraits.h" />
|
<ClInclude Include="TypeTraits.h" />
|
||||||
<ClInclude Include="UninitializedFunctions.h" />
|
<ClInclude Include="UninitializedFunctions.h" />
|
||||||
|
|||||||
@@ -101,6 +101,9 @@
|
|||||||
<ClInclude Include="Test\TestUtil.h">
|
<ClInclude Include="Test\TestUtil.h">
|
||||||
<Filter>Test</Filter>
|
<Filter>Test</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Test\PairTest.h">
|
||||||
|
<Filter>Test</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="..\README.md" />
|
<None Include="..\README.md" />
|
||||||
|
|||||||
Reference in New Issue
Block a user