Home | History | Annotate | Download | only in unit
      1 #include <algorithm>
      2 #include <cstring>
      3 #include <vector>
      4 #include <iterator>
      5 
      6 #include "cppunit/cppunit_proxy.h"
      7 
      8 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
      9 using namespace std;
     10 #endif
     11 
     12 //
     13 // TestCase class
     14 //
     15 class CopyTest : public CPPUNIT_NS::TestCase
     16 {
     17   CPPUNIT_TEST_SUITE(CopyTest);
     18   CPPUNIT_TEST(copy_array);
     19   CPPUNIT_TEST(copy_volatile);
     20   CPPUNIT_TEST(copy_vector);
     21   CPPUNIT_TEST(copy_insert);
     22   CPPUNIT_TEST(copy_back);
     23   CPPUNIT_TEST(copy_back_array);
     24   CPPUNIT_TEST_SUITE_END();
     25 
     26 protected:
     27   void copy_array();
     28   void copy_volatile();
     29   void copy_vector();
     30   void copy_insert();
     31   void copy_back();
     32   void copy_back_array();
     33 };
     34 
     35 CPPUNIT_TEST_SUITE_REGISTRATION(CopyTest);
     36 
     37 //
     38 // tests implementation
     39 //
     40 void CopyTest::copy_array()
     41 {
     42   char string[23] = "A string to be copied.";
     43   char result[23];
     44   copy(string, string + 23, result);
     45   CPPUNIT_ASSERT(!strncmp(string, result, 23));
     46 }
     47 
     48 void CopyTest::copy_volatile()
     49 {
     50   {
     51     int a[] = {0, 1, 2, 3, 4, 5};
     52     const size_t size = sizeof(a) / sizeof(a[0]);
     53     volatile int va[size];
     54     copy(a, a + size, va);
     55     for (size_t i = 0; i != size; ++i) {
     56       CPPUNIT_ASSERT( a[i] == va[i] );
     57     }
     58   }
     59 
     60   {
     61     const int a[] = {0, 1, 2, 3, 4, 5};
     62     const size_t size = sizeof(a) / sizeof(a[0]);
     63     volatile int va[size];
     64     copy(a, a + size, va);
     65     for (size_t i = 0; i != size; ++i) {
     66       CPPUNIT_ASSERT( a[i] == va[i] );
     67     }
     68   }
     69 
     70   // Following code can be activated to check that it doesn't compiled
     71 #if 0
     72   {
     73     int a[] = {0, 1, 2, 3, 4, 5};
     74     const size_t size = sizeof(a) / sizeof(a[0]);
     75     const volatile int va[size] = {5, 4, 3, 2, 1, 0};
     76     copy(a, a + size, va);
     77     for (size_t i = 0; i != size; ++i) {
     78       CPPUNIT_ASSERT( a[i] == va[i] );
     79     }
     80   }
     81 #endif
     82 }
     83 
     84 void CopyTest::copy_vector()
     85 {
     86   vector<int> v1(10);
     87   for (int i = 0; (size_t)i < v1.size(); ++i)
     88     v1[i] = i;
     89 
     90   vector<int> v2(v1.size());
     91   copy(v1.begin(), v1.end(), v2.begin());
     92 
     93   CPPUNIT_ASSERT( v2 == v1 );
     94 }
     95 
     96 void CopyTest::copy_insert() {
     97   vector<int> v1(10);
     98   for (int loc = 0; (size_t)loc < v1.size(); ++loc)
     99     v1[loc] = loc;
    100   vector<int> v2;
    101   insert_iterator<vector<int> > i(v2, v2.begin());
    102   copy(v1.begin(), v1.end(), i);
    103 
    104   CPPUNIT_ASSERT( v2 == v1 );
    105 }
    106 
    107 void CopyTest::copy_back()
    108 {
    109   vector<int> v1(10);
    110   for (int i = 0; (size_t)i < v1.size(); ++i)
    111     v1[i] = i;
    112   vector<int> v2(v1.size());
    113   copy_backward(v1.begin(), v1.end(), v2.end());
    114 
    115   CPPUNIT_ASSERT( v2 == v1 );
    116 }
    117 
    118 void CopyTest::copy_back_array()
    119 {
    120   int numbers[5] = { 1, 2, 3, 4, 5 };
    121 
    122   int result[5];
    123   copy_backward(numbers, numbers + 5, (int*)result + 5);
    124   CPPUNIT_ASSERT(result[0]==numbers[0]);
    125   CPPUNIT_ASSERT(result[1]==numbers[1]);
    126   CPPUNIT_ASSERT(result[2]==numbers[2]);
    127   CPPUNIT_ASSERT(result[3]==numbers[3]);
    128   CPPUNIT_ASSERT(result[4]==numbers[4]);
    129 }
    130