os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/rope_test.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cppstdlib/stl/test/unit/rope_test.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,189 @@
     1.4 +//Small header to get STLport numerous defines:
     1.5 +#include <utility>
     1.6 +
     1.7 +#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
     1.8 +#  include <rope>
     1.9 +
    1.10 +#  if !defined (_STLP_USE_NO_IOSTREAMS)
    1.11 +#    include <sstream>
    1.12 +#  endif
    1.13 +#endif
    1.14 +
    1.15 +#include "cppunit/cppunit_proxy.h"
    1.16 +
    1.17 +// #include <stdlib.h> // for rand etc
    1.18 +
    1.19 +#if defined (_STLP_USE_NAMESPACES)
    1.20 +using namespace std;
    1.21 +#endif
    1.22 +
    1.23 +//
    1.24 +// TestCase class
    1.25 +//
    1.26 +class RopeTest : public CPPUNIT_NS::TestCase
    1.27 +{
    1.28 +  CPPUNIT_TEST_SUITE(RopeTest);
    1.29 +#if !defined (STLPORT) || defined (_STLP_NO_EXTENSIONS) || defined (_STLP_USE_NO_IOSTREAMS)
    1.30 +  CPPUNIT_IGNORE;
    1.31 +#endif
    1.32 +  CPPUNIT_TEST(io);
    1.33 +#if defined (_STLP_USE_NO_IOSTREAMS)
    1.34 +  CPPUNIT_STOP_IGNORE;
    1.35 +#endif
    1.36 +  CPPUNIT_TEST(find1);
    1.37 +  CPPUNIT_TEST(find2);
    1.38 +  CPPUNIT_TEST(construct_from_char);
    1.39 +  CPPUNIT_TEST(bug_report);
    1.40 +#if !defined (_STLP_MEMBER_TEMPLATES)
    1.41 +  CPPUNIT_IGNORE;
    1.42 +#endif
    1.43 +  CPPUNIT_TEST(test_saved_rope_iterators);
    1.44 +  CPPUNIT_TEST_SUITE_END();
    1.45 +
    1.46 +protected:
    1.47 +  void io();
    1.48 +  void find1();
    1.49 +  void find2();
    1.50 +  void construct_from_char();
    1.51 +  void bug_report();
    1.52 +  void test_saved_rope_iterators();
    1.53 +};
    1.54 +
    1.55 +CPPUNIT_TEST_SUITE_REGISTRATION(RopeTest);
    1.56 +
    1.57 +//
    1.58 +// tests implementation
    1.59 +//
    1.60 +void RopeTest::io()
    1.61 +{
    1.62 +#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) && !defined (_STLP_USE_NO_IOSTREAMS) 
    1.63 +  char const* cstr = "rope test string";
    1.64 +  crope rstr(cstr);
    1.65 +
    1.66 +  {
    1.67 +    ostringstream ostr;
    1.68 +    ostr << rstr;
    1.69 +
    1.70 +    CPPUNIT_ASSERT( ostr );
    1.71 +    CPPUNIT_ASSERT( ostr.str() == cstr );
    1.72 +  }
    1.73 +#endif
    1.74 +}
    1.75 +
    1.76 +void RopeTest::find1()
    1.77 +{
    1.78 +#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 
    1.79 +  crope r("Fuzzy Wuzzy was a bear");
    1.80 +  crope::size_type n = r.find( "hair" );
    1.81 +  CPPUNIT_ASSERT( n == crope::npos );
    1.82 +
    1.83 +  n = r.find("ear");
    1.84 +
    1.85 +  CPPUNIT_ASSERT( n == (r.size() - 3) );
    1.86 +#endif
    1.87 +}
    1.88 +
    1.89 +void RopeTest::find2()
    1.90 +{
    1.91 +#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 
    1.92 +  crope r("Fuzzy Wuzzy was a bear");
    1.93 +  crope::size_type n = r.find( 'e' );
    1.94 +  CPPUNIT_ASSERT( n == (r.size() - 3) );
    1.95 +#endif
    1.96 +}
    1.97 +
    1.98 +void RopeTest::construct_from_char()
    1.99 +{
   1.100 +#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 
   1.101 +  crope r('1');
   1.102 +  char const* s = r.c_str();
   1.103 +  CPPUNIT_ASSERT( '1' == s[0] && '\0' == s[1] );
   1.104 +#endif
   1.105 +}
   1.106 +
   1.107 +// Test used for a bug report from Peter Hercek
   1.108 +void RopeTest::bug_report()
   1.109 +{
   1.110 +#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 
   1.111 +  //first create a rope bigger than crope::_S_copy_max = 23
   1.112 +  // so that any string addition is added to a new leaf
   1.113 +  crope evilRope("12345678901234567890123_");
   1.114 +  //crope* pSevenCharRope( new TRope("1234567") );
   1.115 +  crope sevenCharRope0("12345678");
   1.116 +  crope sevenCharRope1("1234567");
   1.117 +  // add _Rope_RopeRep<c,a>::_S_alloc_granularity-1 = 7 characters
   1.118 +  evilRope += "1234567"; // creates a new leaf
   1.119 +  crope sevenCharRope2("1234567");
   1.120 +  // add one more character to the leaf
   1.121 +  evilRope += '8'; // here is the write beyond the allocated memory
   1.122 +  CPPUNIT_ASSERT( strcmp(sevenCharRope2.c_str(), "1234567") == 0 );
   1.123 +#endif
   1.124 +}
   1.125 +
   1.126 +#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS)
   1.127 +const char str[] = "ilcpsklryvmcpjnbpbwllsrehfmxrkecwitrsglrexvtjmxypu\
   1.128 +nbqfgxmuvgfajclfvenhyuhuorjosamibdnjdbeyhkbsomblto\
   1.129 +uujdrbwcrrcgbflqpottpegrwvgajcrgwdlpgitydvhedtusip\
   1.130 +pyvxsuvbvfenodqasajoyomgsqcpjlhbmdahyviuemkssdslde\
   1.131 +besnnngpesdntrrvysuipywatpfoelthrowhfexlwdysvspwlk\
   1.132 +fblfdf";
   1.133 +
   1.134 +crope create_rope( int len )
   1.135 +{
   1.136 +   int l = len/2;
   1.137 +   crope result;
   1.138 +   if(l <= 2)
   1.139 +   {
   1.140 +      static int j = 0;
   1.141 +      for(int i = 0; i < len; ++i)
   1.142 +      {
   1.143 +         // char c = 'a' + rand() % ('z' - 'a');
   1.144 +         result.append(1, /* c */ str[j++] );
   1.145 +         j %= sizeof(str);
   1.146 +      }
   1.147 +   }
   1.148 +   else
   1.149 +   {
   1.150 +      result = create_rope(len/2);
   1.151 +      result.append(create_rope(len/2));
   1.152 +   }
   1.153 +   return result;
   1.154 +}
   1.155 +
   1.156 +#endif
   1.157 +
   1.158 +void RopeTest::test_saved_rope_iterators()
   1.159 +{
   1.160 +#if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) && \
   1.161 +    defined (_STLP_MEMBER_TEMPLATES)
   1.162 +   //
   1.163 +   // Try and create a rope with a complex tree structure:
   1.164 +   //
   1.165 +   // srand(0);
   1.166 +   crope r = create_rope(300);
   1.167 +   string expected(r.begin(), r.end());
   1.168 +   CPPUNIT_ASSERT(expected.size() == r.size());
   1.169 +   CPPUNIT_ASSERT(equal(expected.begin(), expected.end(), r.begin()));
   1.170 +   crope::const_iterator i(r.begin()), j(r.end());
   1.171 +   int pos = 0;
   1.172 +   while(i != j)
   1.173 +   {
   1.174 +      crope::const_iterator k;
   1.175 +      // This initial read triggers the bug:
   1.176 +      CPPUNIT_ASSERT(*i);
   1.177 +      k = i;
   1.178 +      int newpos = pos;
   1.179 +      // Now make sure that i is incremented into the next leaf:
   1.180 +      while(i != j)
   1.181 +      {
   1.182 +         CPPUNIT_ASSERT(*i == expected[newpos]);
   1.183 +         ++i;
   1.184 +         ++newpos;
   1.185 +      }
   1.186 +      // Back up from stored value and continue:
   1.187 +      i = k;
   1.188 +      ++i;
   1.189 +      ++pos;
   1.190 +   }
   1.191 +#endif
   1.192 +}