Home | History | Annotate | Download | only in unit
      1 #include <vector>
      2 #include <algorithm>
      3 #include <functional>
      4 
      5 #include "cppunit/cppunit_proxy.h"
      6 
      7 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
      8 using namespace std;
      9 #endif
     10 
     11 //
     12 // TestCase class
     13 //
     14 class Ptr2Test : public CPPUNIT_NS::TestCase
     15 {
     16   CPPUNIT_TEST_SUITE(Ptr2Test);
     17   CPPUNIT_TEST(ptrbin1);
     18   CPPUNIT_TEST(ptrbin2);
     19   CPPUNIT_TEST(ptrun1);
     20   CPPUNIT_TEST(ptrun2);
     21   CPPUNIT_TEST_SUITE_END();
     22 
     23 protected:
     24   void ptrbin1();
     25   void ptrbin2();
     26   void ptrun1();
     27   void ptrun2();
     28 };
     29 
     30 CPPUNIT_TEST_SUITE_REGISTRATION(Ptr2Test);
     31 
     32 //
     33 // tests implementation
     34 //
     35 static int sum(int x_, int y_)
     36 {
     37   return x_ + y_;
     38 }
     39 bool even(int n_)
     40 {
     41   return(n_ % 2) == 0;
     42 }
     43 void Ptr2Test::ptrbin1()
     44 {
     45   int input1 [4] = { 7, 2, 3, 5 };
     46   int input2 [4] = { 1, 5, 5, 8 };
     47 
     48   int output [4];
     49   transform((int*)input1, (int*)input1 + 4, (int*)input2, (int*)output, pointer_to_binary_function<int, int, int>(sum));
     50 
     51   CPPUNIT_ASSERT(output[0]==8);
     52   CPPUNIT_ASSERT(output[1]==7);
     53   CPPUNIT_ASSERT(output[2]==8);
     54   CPPUNIT_ASSERT(output[3]==13);
     55 }
     56 void Ptr2Test::ptrbin2()
     57 {
     58   int input1 [4] = { 7, 2, 3, 5 };
     59   int input2 [4] = { 1, 5, 5, 8 };
     60 
     61   int output [4];
     62   transform((int*)input1, (int*)input1 + 4, (int*)input2, (int*)output, ptr_fun(sum));
     63 
     64   CPPUNIT_ASSERT(output[0]==8);
     65   CPPUNIT_ASSERT(output[1]==7);
     66   CPPUNIT_ASSERT(output[2]==8);
     67   CPPUNIT_ASSERT(output[3]==13);
     68 }
     69 void Ptr2Test::ptrun1()
     70 {
     71   int array [3] = { 1, 2, 3 };
     72 
     73   int* p = find_if((int*)array, (int*)array + 3, pointer_to_unary_function<int, bool>(even));
     74   CPPUNIT_ASSERT(p != array+3);
     75   CPPUNIT_ASSERT(*p==2);
     76 }
     77 void Ptr2Test::ptrun2()
     78 {
     79   int array [3] = { 1, 2, 3 };
     80 
     81   int* p = find_if((int*)array, (int*)array + 3, ptr_fun(even));
     82   CPPUNIT_ASSERT(p != array+3);
     83   CPPUNIT_ASSERT(*p==2);
     84 }
     85