ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

bind_test.cpp
Go to the documentation of this file.
00001 #include <algorithm>
00002 #include <functional>
00003 
00004 #include "cppunit/cppunit_proxy.h"
00005 
00006 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
00007 using namespace std;
00008 #endif
00009 
00010 //
00011 // TestCase class
00012 //
00013 class BindTest : public CPPUNIT_NS::TestCase
00014 {
00015   CPPUNIT_TEST_SUITE(BindTest);
00016   CPPUNIT_TEST(bind1st1);
00017   CPPUNIT_TEST(bind2nd1);
00018   CPPUNIT_TEST(bind2nd2);
00019 #if !defined (STLPORT) || \
00020     defined (_STLP_NO_EXTENSIONS) || !defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
00021   CPPUNIT_IGNORE;
00022 #endif
00023   CPPUNIT_TEST(bind2nd3);
00024   CPPUNIT_TEST(bind_memfn);
00025   CPPUNIT_TEST_SUITE_END();
00026 
00027 protected:
00028   void bind1st1();
00029   void bind2nd1();
00030   void bind2nd2();
00031   void bind2nd3();
00032   void bind_memfn();
00033 };
00034 
00035 CPPUNIT_TEST_SUITE_REGISTRATION(BindTest);
00036 
00037 class pre_increment : public binary_function<int, int, int> {
00038 public:
00039   int operator()(int incr, int& val) const
00040   { return val += incr; }
00041 };
00042 
00043 class post_increment : public binary_function<int, int, int> {
00044 public:
00045   int operator()(int& val, int incr) const
00046   { return val += incr; }
00047 };
00048 
00049 
00050 //
00051 // tests implementation
00052 //
00053 void BindTest::bind1st1()
00054 {
00055   int array [3] = { 1, 2, 3 };
00056   int* p = remove_if((int*)array, (int*)array + 3, bind1st(less<int>(), 2));
00057 
00058   CPPUNIT_ASSERT(p == &array[2]);
00059   CPPUNIT_ASSERT(array[0] == 1);
00060   CPPUNIT_ASSERT(array[1] == 2);
00061 
00062   for_each((int*)array, (int*)array + 3, bind1st(pre_increment(), 1));
00063   CPPUNIT_ASSERT(array[0] == 2);
00064   CPPUNIT_ASSERT(array[1] == 3);
00065   CPPUNIT_ASSERT(array[2] == 4);
00066 
00067   for_each((int*)array, (int*)array + 3, bind2nd(post_increment(), 1));
00068   CPPUNIT_ASSERT(array[0] == 3);
00069   CPPUNIT_ASSERT(array[1] == 4);
00070   CPPUNIT_ASSERT(array[2] == 5);
00071 }
00072 
00073 void BindTest::bind2nd1()
00074 {
00075   int array [3] = { 1, 2, 3 };
00076   replace_if(array, array + 3, binder2nd<greater<int> >(greater<int>(), 2), 4);
00077 
00078   CPPUNIT_ASSERT(array[0]==1);
00079   CPPUNIT_ASSERT(array[1]==2);
00080   CPPUNIT_ASSERT(array[2]==4);
00081 }
00082 void BindTest::bind2nd2()
00083 {
00084   int array [3] = { 1, 2, 3 };
00085   replace_if(array, array + 3, bind2nd(greater<int>(), 2), 4);
00086   CPPUNIT_ASSERT(array[0]==1);
00087   CPPUNIT_ASSERT(array[1]==2);
00088   CPPUNIT_ASSERT(array[2]==4);
00089 }
00090 
00091 int test_func1 (const int &param1, const int &param2) {
00092   return param1 + param2;
00093 }
00094 
00095 int test_func2 (int &param1, int param2) {
00096   param1 += param2;
00097   return param1 + param2;
00098 }
00099 
00100 void BindTest::bind2nd3()
00101 {
00102 #if defined (STLPORT) && \
00103     !defined (_STLP_NO_EXTENSIONS) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
00104   int array[3] = { 1, 2, 3 };
00105   transform(array, array + 3, array, bind2nd(ptr_fun(test_func1), 1));
00106   transform(array, array + 3, array, bind1st(ptr_fun(test_func1), -1));
00107   CPPUNIT_ASSERT(array[0] == 1);
00108   CPPUNIT_ASSERT(array[1] == 2);
00109   CPPUNIT_ASSERT(array[2] == 3);
00110 
00111   transform(array, array + 3, array, bind2nd(ptr_fun(test_func2), 10));
00112   CPPUNIT_ASSERT(array[0] == 21);
00113   CPPUNIT_ASSERT(array[1] == 22);
00114   CPPUNIT_ASSERT(array[2] == 23);
00115 #endif
00116 }
00117 
00118 class A
00119 {
00120   public:
00121     A() : m_n( 0 )
00122     {}
00123 
00124     void f( int n ) const {
00125 #if defined (STLPORT)
00126       _STLP_MUTABLE(A, m_n) = n;
00127 #else
00128       m_n = n;
00129 #endif
00130     }
00131 
00132     int v() const
00133     { return m_n; }
00134 
00135   private:
00136     mutable int m_n;
00137 };
00138 
00139 void BindTest::bind_memfn()
00140 {
00141 #if defined (STLPORT) && \
00142     !defined (_STLP_NO_EXTENSIONS) && defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
00143   A array[3];
00144 
00145   for_each( array, array + 3, bind2nd( mem_fun_ref(&A::f), 12 ) );
00146 
00147   CPPUNIT_CHECK( array[0].v() == 12 );
00148 #endif
00149 }

Generated on Thu May 24 2012 04:35:45 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.