1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/bind_test.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,121 @@
1.4 +#include <algorithm>
1.5 +#include <functional>
1.6 +
1.7 +#include "cppunit/cppunit_proxy.h"
1.8 +
1.9 +#if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
1.10 +using namespace std;
1.11 +#endif
1.12 +
1.13 +//
1.14 +// TestCase class
1.15 +//
1.16 +class BindTest : public CPPUNIT_NS::TestCase
1.17 +{
1.18 + CPPUNIT_TEST_SUITE(BindTest);
1.19 + CPPUNIT_TEST(bind1st1);
1.20 + CPPUNIT_TEST(bind2nd1);
1.21 + CPPUNIT_TEST(bind2nd2);
1.22 +#if !defined (STLPORT) || \
1.23 + defined (_STLP_NO_EXTENSIONS) || !defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
1.24 + CPPUNIT_IGNORE;
1.25 +#endif
1.26 + CPPUNIT_TEST(bind2nd3);
1.27 + CPPUNIT_TEST(bind_memfn);
1.28 + CPPUNIT_TEST_SUITE_END();
1.29 +
1.30 +protected:
1.31 + void bind1st1();
1.32 + void bind2nd1();
1.33 + void bind2nd2();
1.34 + void bind2nd3();
1.35 + void bind_memfn();
1.36 +};
1.37 +
1.38 +CPPUNIT_TEST_SUITE_REGISTRATION(BindTest);
1.39 +
1.40 +//
1.41 +// tests implementation
1.42 +//
1.43 +void BindTest::bind1st1()
1.44 +{
1.45 + int array [3] = { 1, 2, 3 };
1.46 + int* p = remove_if((int*)array, (int*)array + 3, bind1st(less<int>(), 2));
1.47 +
1.48 + CPPUNIT_ASSERT(p==&array[2]);
1.49 + CPPUNIT_ASSERT(array[0]==1);
1.50 + CPPUNIT_ASSERT(array[1]==2);
1.51 +}
1.52 +void BindTest::bind2nd1()
1.53 +{
1.54 + int array [3] = { 1, 2, 3 };
1.55 + replace_if(array, array + 3, binder2nd<greater<int> >(greater<int>(), 2), 4);
1.56 +
1.57 + CPPUNIT_ASSERT(array[0]==1);
1.58 + CPPUNIT_ASSERT(array[1]==2);
1.59 + CPPUNIT_ASSERT(array[2]==4);
1.60 +}
1.61 +void BindTest::bind2nd2()
1.62 +{
1.63 + int array [3] = { 1, 2, 3 };
1.64 + replace_if(array, array + 3, bind2nd(greater<int>(), 2), 4);
1.65 + CPPUNIT_ASSERT(array[0]==1);
1.66 + CPPUNIT_ASSERT(array[1]==2);
1.67 + CPPUNIT_ASSERT(array[2]==4);
1.68 +}
1.69 +
1.70 +int test_func1 (const int ¶m1, const int ¶m2) {
1.71 + return param1 + param2;
1.72 +}
1.73 +
1.74 +int test_func2 (int ¶m1, int param2) {
1.75 + param1 += param2;
1.76 + return param1 + param2;
1.77 +}
1.78 +
1.79 +void BindTest::bind2nd3()
1.80 +{
1.81 +#if defined (STLPORT) && \
1.82 + !defined (_STLP_NO_EXTENSIONS) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
1.83 + int array[3] = { 1, 2, 3 };
1.84 + transform(array, array + 3, array, bind2nd(ptr_fun(test_func1), 1));
1.85 + transform(array, array + 3, array, bind1st(ptr_fun(test_func1), -1));
1.86 + CPPUNIT_ASSERT(array[0] == 1);
1.87 + CPPUNIT_ASSERT(array[1] == 2);
1.88 + CPPUNIT_ASSERT(array[2] == 3);
1.89 +
1.90 + transform(array, array + 3, array, bind2nd(ptr_fun(test_func2), 10));
1.91 + CPPUNIT_ASSERT(array[0] == 21);
1.92 + CPPUNIT_ASSERT(array[1] == 22);
1.93 + CPPUNIT_ASSERT(array[2] == 23);
1.94 +#endif
1.95 +}
1.96 +
1.97 +class A
1.98 +{
1.99 + public:
1.100 + A() :
1.101 + m_n( 0 )
1.102 + {}
1.103 +
1.104 + void f( int n ) const
1.105 + { m_n = n; }
1.106 +
1.107 + int v() const
1.108 + { return m_n; }
1.109 +
1.110 + private:
1.111 + mutable int m_n;
1.112 +};
1.113 +
1.114 +void BindTest::bind_memfn()
1.115 +{
1.116 +#if defined (STLPORT) && \
1.117 + !defined (_STLP_NO_EXTENSIONS) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
1.118 + A array[3];
1.119 +
1.120 + for_each( array, array + 3, bind2nd( mem_fun_ref(&A::f), 12 ) );
1.121 +
1.122 + CPPUNIT_CHECK( array[0].v() == 12 );
1.123 +#endif
1.124 +}