Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenrope_test.cpp
Go to the documentation of this file.
00001 //Small header to get STLport numerous defines: 00002 #include <utility> 00003 00004 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 00005 # include <rope> 00006 00007 # if !defined (_STLP_USE_NO_IOSTREAMS) 00008 # include <sstream> 00009 # endif 00010 #endif 00011 00012 #include "cppunit/cppunit_proxy.h" 00013 00014 // #include <stdlib.h> // for rand etc 00015 00016 #if defined (_STLP_USE_NAMESPACES) 00017 using namespace std; 00018 #endif 00019 00020 // 00021 // TestCase class 00022 // 00023 class RopeTest : public CPPUNIT_NS::TestCase 00024 { 00025 CPPUNIT_TEST_SUITE(RopeTest); 00026 #if !defined (STLPORT) || defined (_STLP_NO_EXTENSIONS) || defined (_STLP_USE_NO_IOSTREAMS) 00027 CPPUNIT_IGNORE; 00028 #endif 00029 CPPUNIT_TEST(io); 00030 #if defined (_STLP_USE_NO_IOSTREAMS) 00031 CPPUNIT_STOP_IGNORE; 00032 #endif 00033 CPPUNIT_TEST(find1); 00034 CPPUNIT_TEST(find2); 00035 CPPUNIT_TEST(construct_from_char); 00036 CPPUNIT_TEST(bug_report); 00037 #if !defined (_STLP_MEMBER_TEMPLATES) 00038 CPPUNIT_IGNORE; 00039 #endif 00040 CPPUNIT_TEST(test_saved_rope_iterators); 00041 CPPUNIT_TEST_SUITE_END(); 00042 00043 protected: 00044 void io(); 00045 void find1(); 00046 void find2(); 00047 void construct_from_char(); 00048 void bug_report(); 00049 void test_saved_rope_iterators(); 00050 }; 00051 00052 CPPUNIT_TEST_SUITE_REGISTRATION(RopeTest); 00053 00054 // 00055 // tests implementation 00056 // 00057 void RopeTest::io() 00058 { 00059 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) && !defined (_STLP_USE_NO_IOSTREAMS) 00060 char const* cstr = "rope test string"; 00061 crope rstr(cstr); 00062 00063 { 00064 ostringstream ostr; 00065 ostr << rstr; 00066 00067 CPPUNIT_ASSERT( ostr ); 00068 CPPUNIT_ASSERT( ostr.str() == cstr ); 00069 } 00070 #endif 00071 } 00072 00073 void RopeTest::find1() 00074 { 00075 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 00076 crope r("Fuzzy Wuzzy was a bear"); 00077 crope::size_type n = r.find( "hair" ); 00078 CPPUNIT_ASSERT( n == crope::npos ); 00079 00080 n = r.find("ear"); 00081 00082 CPPUNIT_ASSERT( n == (r.size() - 3) ); 00083 #endif 00084 } 00085 00086 void RopeTest::find2() 00087 { 00088 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 00089 crope r("Fuzzy Wuzzy was a bear"); 00090 crope::size_type n = r.find( 'e' ); 00091 CPPUNIT_ASSERT( n == (r.size() - 3) ); 00092 #endif 00093 } 00094 00095 void RopeTest::construct_from_char() 00096 { 00097 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 00098 crope r('1'); 00099 char const* s = r.c_str(); 00100 CPPUNIT_ASSERT( '1' == s[0] && '\0' == s[1] ); 00101 #endif 00102 } 00103 00104 // Test used for a bug report from Peter Hercek 00105 void RopeTest::bug_report() 00106 { 00107 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 00108 //first create a rope bigger than crope::_S_copy_max = 23 00109 // so that any string addition is added to a new leaf 00110 crope evilRope("12345678901234567890123_"); 00111 //crope* pSevenCharRope( new TRope("1234567") ); 00112 crope sevenCharRope0("12345678"); 00113 crope sevenCharRope1("1234567"); 00114 // add _Rope_RopeRep<c,a>::_S_alloc_granularity-1 = 7 characters 00115 evilRope += "1234567"; // creates a new leaf 00116 crope sevenCharRope2("1234567"); 00117 // add one more character to the leaf 00118 evilRope += '8'; // here is the write beyond the allocated memory 00119 CPPUNIT_ASSERT( strcmp(sevenCharRope2.c_str(), "1234567") == 0 ); 00120 #endif 00121 } 00122 00123 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) 00124 const char str[] = "ilcpsklryvmcpjnbpbwllsrehfmxrkecwitrsglrexvtjmxypu\ 00125 nbqfgxmuvgfajclfvenhyuhuorjosamibdnjdbeyhkbsomblto\ 00126 uujdrbwcrrcgbflqpottpegrwvgajcrgwdlpgitydvhedtusip\ 00127 pyvxsuvbvfenodqasajoyomgsqcpjlhbmdahyviuemkssdslde\ 00128 besnnngpesdntrrvysuipywatpfoelthrowhfexlwdysvspwlk\ 00129 fblfdf"; 00130 00131 crope create_rope( int len ) 00132 { 00133 int l = len/2; 00134 crope result; 00135 if(l <= 2) 00136 { 00137 static int j = 0; 00138 for(int i = 0; i < len; ++i) 00139 { 00140 // char c = 'a' + rand() % ('z' - 'a'); 00141 result.append(1, /* c */ str[j++] ); 00142 j %= sizeof(str); 00143 } 00144 } 00145 else 00146 { 00147 result = create_rope(len/2); 00148 result.append(create_rope(len/2)); 00149 } 00150 return result; 00151 } 00152 00153 #endif 00154 00155 void RopeTest::test_saved_rope_iterators() 00156 { 00157 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) && \ 00158 defined (_STLP_MEMBER_TEMPLATES) 00159 // 00160 // Try and create a rope with a complex tree structure: 00161 // 00162 // srand(0); 00163 crope r = create_rope(300); 00164 string expected(r.begin(), r.end()); 00165 CPPUNIT_ASSERT(expected.size() == r.size()); 00166 CPPUNIT_ASSERT(equal(expected.begin(), expected.end(), r.begin())); 00167 crope::const_iterator i(r.begin()), j(r.end()); 00168 int pos = 0; 00169 while(i != j) 00170 { 00171 crope::const_iterator k; 00172 // This initial read triggers the bug: 00173 CPPUNIT_ASSERT(*i); 00174 k = i; 00175 int newpos = pos; 00176 // Now make sure that i is incremented into the next leaf: 00177 while(i != j) 00178 { 00179 CPPUNIT_ASSERT(*i == expected[newpos]); 00180 ++i; 00181 ++newpos; 00182 } 00183 // Back up from stored value and continue: 00184 i = k; 00185 ++i; 00186 ++pos; 00187 } 00188 #endif 00189 } Generated on Sun May 27 2012 04:35:44 for ReactOS by
1.7.6.1
|