Thread: [Cppunit-cvs] cppunit2/src/cpputtest cpputtest.vcproj,1.11,1.12 main.cpp,1.11,1.12 testfunctor.cpp,1
Brought to you by:
blep
From: Baptiste L. <bl...@us...> - 2005-02-27 14:38:40
|
Update of /cvsroot/cppunit/cppunit2/src/cpputtest In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11085/src/cpputtest Modified Files: cpputtest.vcproj main.cpp testfunctor.cpp testtestcase.cpp Log Message: * changed cppunit 2 to use the new functors (cpptl/functor.h). bind_cfn & bind_memfn simplified some of the test code. Index: main.cpp =================================================================== RCS file: /cvsroot/cppunit/cppunit2/src/cpputtest/main.cpp,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** main.cpp 26 Feb 2005 11:40:58 -0000 1.11 --- main.cpp 27 Feb 2005 14:38:28 -0000 1.12 *************** *** 19,23 **** bool testTestCase(); bool testFunctors(); - bool testFunctors3(); --- 19,22 ---- *************** *** 26,30 **** return testBasicAssertions() && testFunctors() && - testFunctors3() && // testTestRunResult() && // testTestContext() && --- 25,28 ---- Index: testtestcase.cpp =================================================================== RCS file: /cvsroot/cppunit/cppunit2/src/cpputtest/testtestcase.cpp,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** testtestcase.cpp 27 Feb 2005 10:17:06 -0000 1.12 --- testtestcase.cpp 27 Feb 2005 14:38:28 -0000 1.13 *************** *** 89,94 **** int testCall = 0; CppUT::TestPtr test = CppUT::makeTestCase( ! CppUT::bind1( CppUT::makeCFn1( &callback ), ! &testCall ), "Test 1" ); CPPUT_ASSERT_EQUAL( "Test 1", test->name() ); --- 89,93 ---- int testCall = 0; CppUT::TestPtr test = CppUT::makeTestCase( ! CppTL::bind_cfn( &callback, &testCall ), "Test 1" ); CPPUT_ASSERT_EQUAL( "Test 1", test->name() ); *************** *** 114,120 **** { return CppUT::makeTestCase( ! CppUT::bind1( CppUT::makeCFn1(setUpCallback), testSetUpCall ), ! CppUT::bind1( CppUT::makeCFn1(runCallback), testRunCall ), ! CppUT::bind1( CppUT::makeCFn1(tearDownCallback), testTearDownCall ), name ); } --- 113,119 ---- { return CppUT::makeTestCase( ! CppTL::bind_cfn(setUpCallback, testSetUpCall ), ! CppTL::bind_cfn(runCallback, testRunCall ), ! CppTL::bind_cfn(tearDownCallback, testTearDownCall ), name ); } *************** *** 200,205 **** CppUT::TestPtr test = CppUT::makeFixtureTestCase( fixture, ! CppUT::makeMemFn0( fixture, ! &TestTestCaseFixture::run ), "Test1" ); CPPUT_ASSERT( test->isTestCase() ); --- 199,203 ---- CppUT::TestPtr test = CppUT::makeFixtureTestCase( fixture, ! CppTL::memfn0( fixture, &TestTestCaseFixture::run ), "Test1" ); CPPUT_ASSERT( test->isTestCase() ); *************** *** 217,223 **** TestTestCaseFixturePtr fixture = TestTestCaseFixturePtr( new TestTestCaseFixture() ); CppUT::TestPtr test = CppUT::makeTestCase( ! CppUT::bind1( CppUT::makeMemFn1( fixture, &TestTestCaseFixture::runWithParameter ), 1 ), ! CppUT::bind1( CppUT::makeMemFn1( fixture, &TestTestCaseFixture::runWithParameter ), 10 ), ! CppUT::bind1( CppUT::makeMemFn1( fixture, &TestTestCaseFixture::runWithParameter ), 100 ), "Test1" ); CPPUT_ASSERT( test->isTestCase() ); --- 215,221 ---- TestTestCaseFixturePtr fixture = TestTestCaseFixturePtr( new TestTestCaseFixture() ); CppUT::TestPtr test = CppUT::makeTestCase( ! CppTL::bind_memfn( fixture, &TestTestCaseFixture::runWithParameter, 1 ), ! CppTL::bind_memfn( fixture, &TestTestCaseFixture::runWithParameter, 10 ), ! CppTL::bind_memfn( fixture, &TestTestCaseFixture::runWithParameter, 100 ), "Test1" ); CPPUT_ASSERT( test->isTestCase() ); Index: cpputtest.vcproj =================================================================== RCS file: /cvsroot/cppunit/cppunit2/src/cpputtest/cpputtest.vcproj,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** cpputtest.vcproj 27 Feb 2005 10:17:54 -0000 1.11 --- cpputtest.vcproj 27 Feb 2005 14:38:28 -0000 1.12 *************** *** 239,245 **** </File> <File - RelativePath=".\testfunctor3.cpp"> - </File> - <File RelativePath=".\testtestcase.cpp"> </File> --- 239,242 ---- Index: testfunctor.cpp =================================================================== RCS file: /cvsroot/cppunit/cppunit2/src/cpputtest/testfunctor.cpp,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** testfunctor.cpp 26 Feb 2005 11:45:50 -0000 1.7 --- testfunctor.cpp 27 Feb 2005 14:38:28 -0000 1.8 *************** *** 1,4 **** #include <cpput/assert.h> ! #include <cpput/functor.h> #include <iostream> --- 1,4 ---- #include <cpput/assert.h> ! #include <cpptl/functor.h> #include <iostream> *************** *** 31,34 **** --- 31,39 ---- } + static double returnCallback1( int x ) + { + return x / 2.0; + } + struct TestFunctor { *************** *** 51,54 **** --- 56,79 ---- { bool flag_; + int value_; + + HelperObject() + : flag_( false ) + , value_( 0 ) + { + } + + HelperObject( const HelperObject &other ) + : flag_( other.flag_ ) + , value_( other.value_ + 1 ) + { + } + + HelperObject &operator =( const HelperObject &other ) + { + flag_ = other.flag_; + value_ = other.value_ + 1; + return *this; + } void setFlagOn() *************** *** 61,64 **** --- 86,94 ---- flag_ = value; } + + double compute1( int x ) + { + return (x + value_) / 2.0; + } }; *************** *** 68,72 **** static void testFunctor() { ! CppUT::Functor0 fn = CppUT::makeFn0( &callback0 ); callbackCalled = false; fn(); --- 98,102 ---- static void testFunctor() { ! CppTL::Functor0 fn = CppTL::cfn0( &callback0 ); callbackCalled = false; fn(); *************** *** 75,79 **** callbackCalled = false; TestFunctor testFunctor; ! fn = CppUT::makeFn0( testFunctor ); fn(); CPPUT_ASSERT( callbackCalled, "Functor() did not call functor object." ); --- 105,109 ---- callbackCalled = false; TestFunctor testFunctor; ! fn = CppTL::fn0( testFunctor ); fn(); CPPUT_ASSERT( callbackCalled, "Functor() did not call functor object." ); *************** *** 81,90 **** CppTL::SharedPtr<HelperObject> helper( new HelperObject() ); helper->flag_ = false; ! fn = CppUT::makeMemFn0( helper, &HelperObject::setFlagOn ); fn(); CPPUT_ASSERT( helper->flag_, "Functor() did not call object method (shared_ptr)." ); helper->flag_ = false; ! fn = CppUT::makeMemFn0( helper.get(), &HelperObject::setFlagOn ); fn(); CPPUT_ASSERT( helper->flag_, "Functor() did not call object method (ptr)." ); --- 111,120 ---- CppTL::SharedPtr<HelperObject> helper( new HelperObject() ); helper->flag_ = false; ! fn = CppTL::memfn0( helper, &HelperObject::setFlagOn ); fn(); CPPUT_ASSERT( helper->flag_, "Functor() did not call object method (shared_ptr)." ); helper->flag_ = false; ! fn = CppTL::memfn0( helper.get(), &HelperObject::setFlagOn ); fn(); CPPUT_ASSERT( helper->flag_, "Functor() did not call object method (ptr)." ); *************** *** 94,98 **** static void testFunctor1() { ! CppUT::Functor1<bool> fn = CppUT::makeCFn1( &callback1 ); callbackCalled = false; fn( true ); --- 124,128 ---- static void testFunctor1() { ! CppTL::Functor1<bool> fn = CppTL::cfn1( &callback1 ); callbackCalled = false; fn( true ); *************** *** 103,107 **** CppTL::SharedPtr<HelperObject> helper( new HelperObject() ); helper->flag_ = false; ! fn = CppUT::makeMemFn1( helper, &HelperObject::setFlag ); fn( true ); CPPUT_ASSERT( helper->flag_, "Functor1(true) did not call object method." ); --- 133,137 ---- CppTL::SharedPtr<HelperObject> helper( new HelperObject() ); helper->flag_ = false; ! fn = CppTL::memfn1( helper, &HelperObject::setFlag ); fn( true ); CPPUT_ASSERT( helper->flag_, "Functor1(true) did not call object method." ); *************** *** 111,126 **** namespace { - static std::string callWithRefParam0; ! void callWithConstRef( const std::string &str ) { ! callWithRefParam0 = str; } ! void callWithRef( std::string &str ) { ! callWithRefParam0 = str; ! str += "called"; } --- 141,168 ---- + static void testFunctor1R() + { + CppTL::Functor1R<double,int> fn = CppTL::cfn1r( &returnCallback1 ); + callbackCalled = false; + CPPUT_ASSERT_EQUAL( 1.0, fn( 2 ) ); + + CppTL::SharedPtr<HelperObject> helper( new HelperObject() ); + helper->value_ = 0; + fn = CppTL::memfn1r( helper, &HelperObject::compute1 ); + CPPUT_ASSERT_EQUAL( 1.0, fn( 2 ) ); + helper->value_ = 10; + CPPUT_ASSERT_EQUAL( 6.0, fn( 2 ) ); + } + namespace { ! int extractHelperObjectValue( const HelperObject &object ) { ! return object.value_; } ! void doubleHelperObjectValue( HelperObject &object ) { ! object.value_ *= 2; } *************** *** 128,180 **** ! static void testConstReferenceSupport() { ! CppUT::Functor1<const std::string &> fncref1( CppUT::makeCFn1( &callWithConstRef ) ); ! callWithRefParam0 = ""; ! const std::string &s = "test1"; ! fncref1( s ); ! CPPUT_ASSERT_EQUAL( "test1", callWithRefParam0 ); } ! static void testReferenceSupport() { ! CppUT::Functor1<std::string &> fnref1( CppUT::makeCFn1( &callWithRef ) ); ! callWithRefParam0 = ""; ! std::string s = "test1"; ! fnref1( s ); ! CPPUT_ASSERT_EQUAL( "test1", callWithRefParam0 ); ! CPPUT_ASSERT_EQUAL( "test1called", s ); } static void testBindValue() { ! CppUT::Functor1<const std::string &> fn1 = CppUT::makeCFn1( &callback1string ); ! CppUT::Functor0 fn0 = CppUT::bind1( fn1, "str" ); ! callbackCalled = false; ! badValue = false; ! expected = "str"; ! fn0(); ! CPPUT_ASSERT( callbackCalled, "bind1() did not call C function." ); ! CPPUT_ASSERT( badValue, "bind1() did not pass the binded value." ); ! ! callbackCalled = false; ! CppUT::Functor0 fbind12 = CppUT::bind( CppUT::makeCFn2( &callback12 ), ! "str", ! 1234 ); ! fbind12(); ! CPPUT_ASSERT( callbackCalled, "bind(f,x,y) did not call C function." ); } - static void testBindConstRef() { ! CppUT::Functor1<const std::string &> fncref1 = CppUT::makeCFn1( &callWithConstRef ); ! std::string s = "test2"; ! CppUT::Functor0 fn0 = CppUT::bind1( fncref1, CppUT::cref(s) ); ! callWithRefParam0 = ""; ! fn0(); ! CPPUT_ASSERT_EQUAL( "test2", callWithRefParam0 ); } --- 170,214 ---- ! static void testConstReferenceParameterSupport() { ! CppTL::Functor1R<int,const HelperObject &> fncref1( CppTL::cfn1r( &extractHelperObjectValue ) ); ! HelperObject object; ! object.value_ = 12345678; ! CPPUT_ASSERT_EQUAL( 12345678, fncref1( object ) ); } ! static void testReferenceParameterSupport() { ! CppTL::Functor1<HelperObject &> fnref1( CppTL::cfn1( &doubleHelperObjectValue ) ); ! HelperObject object; ! object.value_ = 1111; ! fnref1( object ); ! CPPUT_ASSERT_EQUAL( 1111*2, object.value_ ); } + namespace { + int multiply( int x, int y ) + { + return x * y; + } + } + static void testBindValue() { ! CppTL::Functor2R<int,int,int> fn2 = CppTL::cfn2r( &multiply ); ! CppTL::Functor1R<int,int> fn1 = CppTL::fn1r( CppTL::bind2r( fn2, 3 ) ); ! CPPUT_ASSERT_EQUAL( 2*3, fn1(2) ); ! CPPUT_ASSERT_EQUAL( 5*3, fn1(5) ); } static void testBindConstRef() { ! CppTL::Functor1R<int,const HelperObject &> fn1( CppTL::cfn1r( &extractHelperObjectValue ) ); ! HelperObject object; ! object.value_ = 12345678; ! CppTL::Functor0R<int> fn0 = CppTL::fn0r( CppTL::bind1r( fn1, CppTL::cref(object) ) ); ! CPPUT_ASSERT_EQUAL( 12345678, fn0() ); } *************** *** 182,196 **** static void testBindRef() { ! CppUT::Functor1<std::string &> fnref1 = CppUT::makeCFn1( &callWithRef ); ! std::string s = "test3"; ! CppUT::Functor0 fn0 = CppUT::bind1( fnref1, CppUT::ref(s) ); ! callWithRefParam0 = ""; fn0(); ! CPPUT_ASSERT_EQUAL( "test3", callWithRefParam0 ); ! CPPUT_ASSERT_EQUAL( "test3called", s ); } - bool testFunctors() { --- 216,228 ---- static void testBindRef() { ! CppTL::Functor1<HelperObject &> fnref1( CppTL::cfn1( &doubleHelperObjectValue ) ); ! HelperObject object; ! object.value_ = 1111; ! CppTL::Functor0 fn0 = CppTL::fn0( CppTL::bind1( fnref1, CppTL::ref( object ) ) ); fn0(); ! CPPUT_ASSERT_EQUAL( 1111*2, object.value_ ); } bool testFunctors() { *************** *** 200,205 **** testFunctor(); testFunctor1(); ! testConstReferenceSupport(); ! testReferenceSupport(); testBindValue(); testBindConstRef(); --- 232,239 ---- testFunctor(); testFunctor1(); ! testFunctor1R(); ! ! testConstReferenceParameterSupport(); ! testReferenceParameterSupport(); testBindValue(); testBindConstRef(); |