genericopenlibs/cppstdlib/stl/test/unit/uninitialized_test.cpp
changeset 31 ce057bb09d0b
child 34 5fae379060a7
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 #include <memory>
       
    18 #include <vector>
       
    19 #include <list>
       
    20 
       
    21 #include "cppunit/cppunit_proxy.h"
       
    22 
       
    23 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
       
    24 using namespace std;
       
    25 #endif
       
    26 
       
    27 //
       
    28 // TestCase class
       
    29 //
       
    30 class UninitializedTest : public CPPUNIT_NS::TestCase
       
    31 {
       
    32   CPPUNIT_TEST_SUITE(UninitializedTest);
       
    33   CPPUNIT_TEST(copy_test);
       
    34   //CPPUNIT_TEST(fill_test);
       
    35   //CPPUNIT_TEST(fill_n_test);
       
    36   CPPUNIT_TEST_SUITE_END();
       
    37 
       
    38 protected:
       
    39   void copy_test();
       
    40   void fill_test();
       
    41   void fill_n_test();
       
    42 };
       
    43 
       
    44 CPPUNIT_TEST_SUITE_REGISTRATION(UninitializedTest);
       
    45 
       
    46 struct NotTrivialCopyStruct {
       
    47   NotTrivialCopyStruct() : member(0) {}
       
    48   NotTrivialCopyStruct(NotTrivialCopyStruct const&) : member(1) {}
       
    49 
       
    50   int member;
       
    51 };
       
    52 
       
    53 struct TrivialCopyStruct {
       
    54   TrivialCopyStruct() : member(0) {}
       
    55   TrivialCopyStruct(TrivialCopyStruct const&) : member(1) {}
       
    56 
       
    57   int member;
       
    58 };
       
    59 
       
    60 struct TrivialInitStruct {
       
    61   TrivialInitStruct()
       
    62   { ++nbConstructorCalls; }
       
    63 
       
    64   static size_t nbConstructorCalls;
       
    65 };
       
    66 
       
    67 size_t TrivialInitStruct::nbConstructorCalls = 0;
       
    68 
       
    69 #if defined (STLPORT)
       
    70 namespace std
       
    71 {
       
    72   _STLP_TEMPLATE_NULL
       
    73   struct __type_traits<TrivialCopyStruct> {
       
    74      typedef __false_type has_trivial_default_constructor;
       
    75      //This is a wrong declaration just to check that internaly a simple memcpy is called:
       
    76      typedef __true_type has_trivial_copy_constructor;
       
    77      typedef __true_type has_trivial_assignment_operator;
       
    78      typedef __true_type has_trivial_destructor;
       
    79      typedef __false_type is_POD_type;
       
    80   };
       
    81 
       
    82   _STLP_TEMPLATE_NULL
       
    83   struct __type_traits<TrivialInitStruct> {
       
    84      //This is a wrong declaration just to check that internaly no initialization is done:
       
    85      typedef __true_type has_trivial_default_constructor;
       
    86      typedef __true_type has_trivial_copy_constructor;
       
    87      typedef __true_type has_trivial_assignment_operator;
       
    88      typedef __true_type has_trivial_destructor;
       
    89      typedef __false_type is_POD_type;
       
    90   };
       
    91 }
       
    92 #endif
       
    93 
       
    94 struct base {};
       
    95 struct derived : public base {};
       
    96 
       
    97 //
       
    98 // tests implementation
       
    99 //
       
   100 void UninitializedTest::copy_test()
       
   101 {
       
   102   {
       
   103     //Random iterators
       
   104     {
       
   105       vector<NotTrivialCopyStruct> src(10);
       
   106       vector<NotTrivialCopyStruct> dst(10);
       
   107       uninitialized_copy(src.begin(), src.end(), dst.begin());
       
   108       vector<NotTrivialCopyStruct>::const_iterator it(dst.begin()), end(dst.end());
       
   109       for (; it != end; ++it) {
       
   110         CPPUNIT_ASSERT( (*it).member == 1 );
       
   111       }
       
   112     }
       
   113     {
       
   114       /** Note: we use static arrays here so the iterators are always
       
   115       pointers, even in debug mode. */
       
   116       size_t const count = 10;
       
   117       TrivialCopyStruct src[count];
       
   118       TrivialCopyStruct dst[count];
       
   119 
       
   120       TrivialCopyStruct* it = src + 0;
       
   121       TrivialCopyStruct* end = src + count;
       
   122       for (; it != end; ++it) {
       
   123         (*it).member = 0;
       
   124       }
       
   125 
       
   126       uninitialized_copy(src+0, src+count, dst+0);
       
   127       for (it = dst+0, end = dst+count; it != end; ++it) {
       
   128 #if defined (STLPORT)
       
   129         /* If the member is 1, it means that library has not found any
       
   130         optimization oportunity and called the regular copy-ctor instead. */
       
   131         CPPUNIT_ASSERT( (*it).member == 0 );
       
   132 #else
       
   133         CPPUNIT_ASSERT( (*it).member == 1 );
       
   134 #endif
       
   135       }
       
   136     }
       
   137   }
       
   138 
       
   139   {
       
   140     //Bidirectional iterator
       
   141     {
       
   142       vector<NotTrivialCopyStruct> src(10);
       
   143       list<NotTrivialCopyStruct> dst(10);
       
   144 
       
   145       list<NotTrivialCopyStruct>::iterator it(dst.begin()), end(dst.end());
       
   146       for (; it != end; ++it) {
       
   147         (*it).member = -1;
       
   148       }
       
   149 
       
   150       uninitialized_copy(src.begin(), src.end(), dst.begin());
       
   151 
       
   152       for (it = dst.begin(); it != end; ++it) {
       
   153         CPPUNIT_ASSERT( (*it).member == 1 );
       
   154       }
       
   155     }
       
   156 
       
   157     {
       
   158       list<NotTrivialCopyStruct> src(10);
       
   159       vector<NotTrivialCopyStruct> dst(10);
       
   160 
       
   161       vector<NotTrivialCopyStruct>::iterator it(dst.begin()), end(dst.end());
       
   162       for (; it != end; ++it) {
       
   163         (*it).member = -1;
       
   164       }
       
   165 
       
   166       uninitialized_copy(src.begin(), src.end(), dst.begin());
       
   167 
       
   168       for (it = dst.begin(); it != end; ++it) {
       
   169         CPPUNIT_ASSERT( (*it).member == 1 );
       
   170       }
       
   171     }
       
   172   }
       
   173 
       
   174   {
       
   175     //Using containers of native types:
       
   176 #if !defined (STLPORT) || !defined (_STLP_NO_MEMBER_TEMPLATES)
       
   177     {
       
   178       vector<int> src;
       
   179       int i;
       
   180       for (i = -5; i < 6; ++i) {
       
   181         src.push_back(i);
       
   182       }
       
   183 
       
   184       //Building a vector result in a uninitialized_copy call internally
       
   185       vector<unsigned int> dst(src.begin(), src.end());
       
   186       vector<unsigned int>::const_iterator it(dst.begin());
       
   187       for (i = -5; i < 6; ++i, ++it) {
       
   188         CPPUNIT_ASSERT( *it == (unsigned int)i );
       
   189       }
       
   190     }
       
   191 
       
   192     {
       
   193       vector<char> src;
       
   194       char i;
       
   195       for (i = -5; i < 6; ++i) {
       
   196         src.push_back(i);
       
   197       }
       
   198 
       
   199       //Building a vector result in a uninitialized_copy call internally
       
   200       vector<unsigned int> dst(src.begin(), src.end());
       
   201       vector<unsigned int>::const_iterator it(dst.begin());
       
   202       for (i = -5; i < 6; ++i, ++it) {
       
   203         CPPUNIT_ASSERT( *it == (unsigned int)i );
       
   204       }
       
   205     }
       
   206 
       
   207     {
       
   208       vector<int> src;
       
   209       int i;
       
   210       for (i = -5; i < 6; ++i) {
       
   211         src.push_back(i);
       
   212       }
       
   213 
       
   214       //Building a vector result in a uninitialized_copy call internally
       
   215       vector<float> dst(src.begin(), src.end());
       
   216       vector<float>::const_iterator it(dst.begin());
       
   217       for (i = -5; i < 6; ++i, ++it) {
       
   218         CPPUNIT_ASSERT( *it == (float)i );
       
   219       }
       
   220     }
       
   221 
       
   222     {
       
   223       vector<vector<float>*> src(10);
       
   224       vector<vector<float>*> dst(src.begin(), src.end());
       
   225     }
       
   226 
       
   227     {
       
   228       derived d;
       
   229       //base *pb = &d;
       
   230       derived *pd = &d;
       
   231       //base **ppb = &pd;
       
   232       vector<derived*> src(10, pd);
       
   233       vector<base*> dst(src.begin(), src.end());
       
   234       vector<base*>::iterator it(dst.begin()), end(dst.end());
       
   235       for (; it != end; ++it) {
       
   236         CPPUNIT_ASSERT( (*it) == pd );
       
   237       }
       
   238     }
       
   239 #endif
       
   240   }
       
   241 
       
   242   {
       
   243     //Vector initialization:
       
   244     vector<TrivialInitStruct> vect(10);
       
   245     //Just 1 constructor call for the default value:
       
   246     CPPUNIT_ASSERT( TrivialInitStruct::nbConstructorCalls == 1  );
       
   247   }
       
   248 }
       
   249 
       
   250 /*
       
   251 void UninitializedTest::fill_test()
       
   252 {
       
   253 }
       
   254 
       
   255 void UninitializedTest::fill_n_test()
       
   256 {
       
   257 }
       
   258 */