genericopenlibs/cppstdlib/stl/test/unit/rope_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 //Small header to get STLport numerous defines:
       
    18 #include <utility>
       
    19 
       
    20 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
       
    21 #  include <rope>
       
    22 
       
    23 #  if !defined (_STLP_USE_NO_IOSTREAMS)
       
    24 #    include <sstream>
       
    25 #  endif
       
    26 #endif
       
    27 
       
    28 #include "cppunit/cppunit_proxy.h"
       
    29 
       
    30 // #include <stdlib.h> // for rand etc
       
    31 
       
    32 #if defined (_STLP_USE_NAMESPACES)
       
    33 using namespace std;
       
    34 #endif
       
    35 
       
    36 //
       
    37 // TestCase class
       
    38 //
       
    39 class RopeTest : public CPPUNIT_NS::TestCase
       
    40 {
       
    41   CPPUNIT_TEST_SUITE(RopeTest);
       
    42 #if !defined (STLPORT) || defined (_STLP_NO_EXTENSIONS) || defined (_STLP_USE_NO_IOSTREAMS)
       
    43   CPPUNIT_IGNORE;
       
    44 #endif
       
    45   CPPUNIT_TEST(io);
       
    46 #if defined (_STLP_USE_NO_IOSTREAMS)
       
    47   CPPUNIT_STOP_IGNORE;
       
    48 #endif
       
    49   CPPUNIT_TEST(find1);
       
    50   CPPUNIT_TEST(find2);
       
    51   CPPUNIT_TEST(construct_from_char);
       
    52   CPPUNIT_TEST(bug_report);
       
    53 #if !defined (_STLP_MEMBER_TEMPLATES)
       
    54   CPPUNIT_IGNORE;
       
    55 #endif
       
    56   CPPUNIT_TEST(test_saved_rope_iterators);
       
    57   CPPUNIT_TEST_SUITE_END();
       
    58 
       
    59 protected:
       
    60   void io();
       
    61   void find1();
       
    62   void find2();
       
    63   void construct_from_char();
       
    64   void bug_report();
       
    65   void test_saved_rope_iterators();
       
    66 };
       
    67 
       
    68 CPPUNIT_TEST_SUITE_REGISTRATION(RopeTest);
       
    69 
       
    70 //
       
    71 // tests implementation
       
    72 //
       
    73 void RopeTest::io()
       
    74 {
       
    75 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) && !defined (_STLP_USE_NO_IOSTREAMS) 
       
    76   char const* cstr = "rope test string";
       
    77   crope rstr(cstr);
       
    78 
       
    79   {
       
    80     ostringstream ostr;
       
    81     ostr << rstr;
       
    82 
       
    83     CPPUNIT_ASSERT( ostr );
       
    84     CPPUNIT_ASSERT( ostr.str() == cstr );
       
    85   }
       
    86 #endif
       
    87 }
       
    88 
       
    89 void RopeTest::find1()
       
    90 {
       
    91 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 
       
    92   crope r("Fuzzy Wuzzy was a bear");
       
    93   crope::size_type n = r.find( "hair" );
       
    94   CPPUNIT_ASSERT( n == crope::npos );
       
    95 
       
    96   n = r.find("ear");
       
    97 
       
    98   CPPUNIT_ASSERT( n == (r.size() - 3) );
       
    99 #endif
       
   100 }
       
   101 
       
   102 void RopeTest::find2()
       
   103 {
       
   104 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 
       
   105   crope r("Fuzzy Wuzzy was a bear");
       
   106   crope::size_type n = r.find( 'e' );
       
   107   CPPUNIT_ASSERT( n == (r.size() - 3) );
       
   108 #endif
       
   109 }
       
   110 
       
   111 void RopeTest::construct_from_char()
       
   112 {
       
   113 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 
       
   114   crope r('1');
       
   115   char const* s = r.c_str();
       
   116   CPPUNIT_ASSERT( '1' == s[0] && '\0' == s[1] );
       
   117 #endif
       
   118 }
       
   119 
       
   120 // Test used for a bug report from Peter Hercek
       
   121 void RopeTest::bug_report()
       
   122 {
       
   123 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 
       
   124   //first create a rope bigger than crope::_S_copy_max = 23
       
   125   // so that any string addition is added to a new leaf
       
   126   crope evilRope("12345678901234567890123_");
       
   127   //crope* pSevenCharRope( new TRope("1234567") );
       
   128   crope sevenCharRope0("12345678");
       
   129   crope sevenCharRope1("1234567");
       
   130   // add _Rope_RopeRep<c,a>::_S_alloc_granularity-1 = 7 characters
       
   131   evilRope += "1234567"; // creates a new leaf
       
   132   crope sevenCharRope2("1234567");
       
   133   // add one more character to the leaf
       
   134   evilRope += '8'; // here is the write beyond the allocated memory
       
   135   CPPUNIT_ASSERT( strcmp(sevenCharRope2.c_str(), "1234567") == 0 );
       
   136 #endif
       
   137 }
       
   138 
       
   139 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
       
   140 const char str[] = "ilcpsklryvmcpjnbpbwllsrehfmxrkecwitrsglrexvtjmxypu\
       
   141 nbqfgxmuvgfajclfvenhyuhuorjosamibdnjdbeyhkbsomblto\
       
   142 uujdrbwcrrcgbflqpottpegrwvgajcrgwdlpgitydvhedtusip\
       
   143 pyvxsuvbvfenodqasajoyomgsqcpjlhbmdahyviuemkssdslde\
       
   144 besnnngpesdntrrvysuipywatpfoelthrowhfexlwdysvspwlk\
       
   145 fblfdf";
       
   146 
       
   147 crope create_rope( int len )
       
   148 {
       
   149    int l = len/2;
       
   150    crope result;
       
   151    if(l <= 2)
       
   152    {
       
   153       static int j = 0;
       
   154       for(int i = 0; i < len; ++i)
       
   155       {
       
   156          // char c = 'a' + rand() % ('z' - 'a');
       
   157          result.append(1, /* c */ str[j++] );
       
   158          j %= sizeof(str);
       
   159       }
       
   160    }
       
   161    else
       
   162    {
       
   163       result = create_rope(len/2);
       
   164       result.append(create_rope(len/2));
       
   165    }
       
   166    return result;
       
   167 }
       
   168 
       
   169 #endif
       
   170 
       
   171 void RopeTest::test_saved_rope_iterators()
       
   172 {
       
   173 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) && \
       
   174     defined (_STLP_MEMBER_TEMPLATES)
       
   175    //
       
   176    // Try and create a rope with a complex tree structure:
       
   177    //
       
   178    // srand(0);
       
   179    crope r = create_rope(300);
       
   180    string expected(r.begin(), r.end());
       
   181    CPPUNIT_ASSERT(expected.size() == r.size());
       
   182    CPPUNIT_ASSERT(equal(expected.begin(), expected.end(), r.begin()));
       
   183    crope::const_iterator i(r.begin()), j(r.end());
       
   184    int pos = 0;
       
   185    while(i != j)
       
   186    {
       
   187       crope::const_iterator k;
       
   188       // This initial read triggers the bug:
       
   189       CPPUNIT_ASSERT(*i);
       
   190       k = i;
       
   191       int newpos = pos;
       
   192       // Now make sure that i is incremented into the next leaf:
       
   193       while(i != j)
       
   194       {
       
   195          CPPUNIT_ASSERT(*i == expected[newpos]);
       
   196          ++i;
       
   197          ++newpos;
       
   198       }
       
   199       // Back up from stored value and continue:
       
   200       i = k;
       
   201       ++i;
       
   202       ++pos;
       
   203    }
       
   204 #endif
       
   205 }