mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-30 13:47:10 +01:00
initial import
This commit is contained in:
143
Foundation/testsuite/src/AnyTest.cpp
Normal file
143
Foundation/testsuite/src/AnyTest.cpp
Normal file
@@ -0,0 +1,143 @@
|
||||
//
|
||||
// AnyTest.cpp
|
||||
//
|
||||
// $Id: //poco/Main/Foundation/testsuite/src/AnyTest.cpp#5 $
|
||||
//
|
||||
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "AnyTest.h"
|
||||
|
||||
#include "CppUnit/TestCaller.h"
|
||||
#include "CppUnit/TestSuite.h"
|
||||
|
||||
#include "Foundation/Exception.h"
|
||||
#include "Foundation/Any.h"
|
||||
#include "Foundation/Bugcheck.h"
|
||||
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
using namespace Foundation;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public:
|
||||
int i;
|
||||
std::string str;
|
||||
SomeClass(int h, std::string s): i (h), str(s)
|
||||
{
|
||||
};
|
||||
bool operator==(const SomeClass& other) const
|
||||
{
|
||||
return i == other.i && str == other.str;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
AnyTest::AnyTest(const std::string& name ): CppUnit::TestCase(name)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AnyTest::~AnyTest()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void AnyTest::testInt()
|
||||
{
|
||||
Any a = 13;
|
||||
poco_assert (a.type() == typeid(int) );
|
||||
int* i = AnyCast < int > (&a);
|
||||
poco_assert ( *i == 13 );
|
||||
Any b = a;
|
||||
poco_assert ( b.type() == typeid(int) );
|
||||
int *cpyI = AnyCast < int > (&b);
|
||||
poco_assert ( *cpyI == *i );
|
||||
*cpyI = 20;
|
||||
poco_assert ( *cpyI != *i );
|
||||
std::string* s = AnyCast < std::string > (&a);
|
||||
poco_assert ( s == NULL);
|
||||
|
||||
int tmp = AnyCast < int > (a);
|
||||
const Any c = a;
|
||||
tmp = AnyCast < int > (a);
|
||||
}
|
||||
|
||||
|
||||
void AnyTest::testComplexType()
|
||||
{
|
||||
SomeClass str(13,std::string("hello"));
|
||||
Any a = str;
|
||||
Any b = a;
|
||||
poco_assert (a.type() == typeid(SomeClass) );
|
||||
poco_assert (b.type() == typeid(SomeClass) );
|
||||
SomeClass str2 = AnyCast < SomeClass > (a);
|
||||
poco_assert ( str == str2 );
|
||||
const SomeClass& strCRef = RefAnyCast < SomeClass > (a);
|
||||
poco_assert ( str == strCRef );
|
||||
SomeClass& strRef = RefAnyCast < SomeClass > (a);
|
||||
poco_assert ( str == strRef );
|
||||
}
|
||||
|
||||
|
||||
void AnyTest::testVector()
|
||||
{
|
||||
vector < int > tmp;
|
||||
tmp.push_back( 1 );
|
||||
tmp.push_back( 2 );
|
||||
tmp.push_back( 3 );
|
||||
Any a = tmp;
|
||||
poco_assert (a.type() == typeid(vector < int >) );
|
||||
vector < int > tmp2 = AnyCast < vector < int > >(a);
|
||||
const vector < int >& vecCRef = RefAnyCast < vector < int > >(a);
|
||||
vector < int >& vecRef = RefAnyCast < vector < int > >(a);
|
||||
vecRef[0] = 0;
|
||||
poco_assert( vecRef[0] == vecCRef[0] );
|
||||
}
|
||||
|
||||
|
||||
void AnyTest::setUp()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void AnyTest::tearDown()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
CppUnit::Test* AnyTest::suite()
|
||||
{
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("AnyTest");
|
||||
|
||||
CppUnit_addTest(pSuite, AnyTest, testInt);
|
||||
CppUnit_addTest(pSuite, AnyTest, testComplexType);
|
||||
CppUnit_addTest(pSuite, AnyTest, testVector);
|
||||
|
||||
return pSuite;
|
||||
}
|
||||
Reference in New Issue
Block a user