os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/copy_test.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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_vector);
    20   CPPUNIT_TEST(copy_insert);
    21   CPPUNIT_TEST(copy_back);
    22   CPPUNIT_TEST(copy_back_array);
    23   CPPUNIT_TEST_SUITE_END();
    24 
    25 protected:
    26   void copy_array();
    27   void copy_vector();
    28   void copy_insert();
    29   void copy_back();
    30   void copy_back_array();
    31 };
    32 
    33 CPPUNIT_TEST_SUITE_REGISTRATION(CopyTest);
    34 
    35 //
    36 // tests implementation
    37 //
    38 void CopyTest::copy_array()
    39 {
    40   char string[23] = "A string to be copied.";
    41   char result[23];
    42   copy(string, string + 23, result);
    43   CPPUNIT_ASSERT(!strncmp(string, result, 23));
    44 }
    45 
    46 void CopyTest::copy_vector()
    47 {
    48   vector<int> v1(10);
    49   for (int i = 0; (size_t)i < v1.size(); ++i)
    50     v1[i] = i;
    51 
    52   vector<int> v2(v1.size());
    53   copy(v1.begin(), v1.end(), v2.begin());
    54 
    55   CPPUNIT_ASSERT( v2 == v1 );
    56 }
    57 
    58 void CopyTest::copy_insert() {
    59   vector<int> v1(10);
    60   for (int loc = 0; (size_t)loc < v1.size(); ++loc)
    61     v1[loc] = loc;
    62   vector<int> v2;
    63   insert_iterator<vector<int> > i(v2, v2.begin());
    64   copy(v1.begin(), v1.end(), i);
    65 
    66   CPPUNIT_ASSERT( v2 == v1 );
    67 }
    68 
    69 void CopyTest::copy_back()
    70 {
    71   vector<int> v1(10);
    72   for (int i = 0; (size_t)i < v1.size(); ++i)
    73     v1[i] = i;
    74   vector<int> v2(v1.size());
    75   copy_backward(v1.begin(), v1.end(), v2.end());
    76 
    77   CPPUNIT_ASSERT( v2 == v1 );
    78 }
    79 
    80 void CopyTest::copy_back_array()
    81 {
    82   int numbers[5] = { 1, 2, 3, 4, 5 };
    83 
    84   int result[5];
    85   copy_backward(numbers, numbers + 5, (int*)result + 5);
    86   CPPUNIT_ASSERT(result[0]==numbers[0]);
    87   CPPUNIT_ASSERT(result[1]==numbers[1]);
    88   CPPUNIT_ASSERT(result[2]==numbers[2]);
    89   CPPUNIT_ASSERT(result[3]==numbers[3]);
    90   CPPUNIT_ASSERT(result[4]==numbers[4]);
    91 }