1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        tests/strings/strings.cpp
3 // Purpose:     wxString unit test
4 // Author:      Vadim Zeitlin, Wlodzimierz ABX Skiba
5 // Created:     2004-04-19
6 // Copyright:   (c) 2004 Vadim Zeitlin, Wlodzimierz Skiba
7 ///////////////////////////////////////////////////////////////////////////////
8 
9 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12 
13 #include "testprec.h"
14 
15 #ifdef __BORLANDC__
16     #pragma hdrstop
17 #endif
18 
19 #ifndef WX_PRECOMP
20     #include "wx/wx.h"
21 #endif // WX_PRECOMP
22 
23 // ----------------------------------------------------------------------------
24 // test class
25 // ----------------------------------------------------------------------------
26 
27 class StringTestCase : public CppUnit::TestCase
28 {
29 public:
30     StringTestCase();
31 
32 private:
33     CPPUNIT_TEST_SUITE( StringTestCase );
34         CPPUNIT_TEST( String );
35         CPPUNIT_TEST( PChar );
36         CPPUNIT_TEST( Format );
37         CPPUNIT_TEST( FormatUnicode );
38         CPPUNIT_TEST( Constructors );
39         CPPUNIT_TEST( StaticConstructors );
40         CPPUNIT_TEST( Extraction );
41         CPPUNIT_TEST( Trim );
42         CPPUNIT_TEST( Find );
43         CPPUNIT_TEST( Replace );
44         CPPUNIT_TEST( Match );
45         CPPUNIT_TEST( CaseChanges );
46         CPPUNIT_TEST( Compare );
47         CPPUNIT_TEST( CompareNoCase );
48         CPPUNIT_TEST( Contains );
49         CPPUNIT_TEST( ToLong );
50         CPPUNIT_TEST( ToULong );
51 #ifdef wxLongLong_t
52         CPPUNIT_TEST( ToLongLong );
53         CPPUNIT_TEST( ToULongLong );
54 #endif // wxLongLong_t
55         CPPUNIT_TEST( ToDouble );
56         CPPUNIT_TEST( FromDouble );
57         CPPUNIT_TEST( StringBuf );
58         CPPUNIT_TEST( UTF8Buf );
59         CPPUNIT_TEST( CStrDataTernaryOperator );
60         CPPUNIT_TEST( CStrDataOperators );
61         CPPUNIT_TEST( CStrDataImplicitConversion );
62         CPPUNIT_TEST( ExplicitConversion );
63         CPPUNIT_TEST( IndexedAccess );
64         CPPUNIT_TEST( BeforeAndAfter );
65         CPPUNIT_TEST( ScopedBuffers );
66     CPPUNIT_TEST_SUITE_END();
67 
68     void String();
69     void PChar();
70     void Format();
71     void FormatUnicode();
72     void Constructors();
73     void StaticConstructors();
74     void Extraction();
75     void Trim();
76     void Find();
77     void Replace();
78     void Match();
79     void CaseChanges();
80     void Compare();
81     void CompareNoCase();
82     void Contains();
83     void ToLong();
84     void ToULong();
85 #ifdef wxLongLong_t
86     void ToLongLong();
87     void ToULongLong();
88 #endif // wxLongLong_t
89     void ToDouble();
90     void FromDouble();
91     void StringBuf();
92     void UTF8Buf();
93     void CStrDataTernaryOperator();
94     void DoCStrDataTernaryOperator(bool cond);
95     void CStrDataOperators();
96     void CStrDataImplicitConversion();
97     void ExplicitConversion();
98     void IndexedAccess();
99     void BeforeAndAfter();
100     void ScopedBuffers();
101 
102     DECLARE_NO_COPY_CLASS(StringTestCase)
103 };
104 
105 // register in the unnamed registry so that these tests are run by default
106 CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase );
107 
108 // also include in its own registry so that these tests can be run alone
109 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" );
110 
StringTestCase()111 StringTestCase::StringTestCase()
112 {
113 }
114 
String()115 void StringTestCase::String()
116 {
117     wxString a, b, c;
118 
119     a.reserve (128);
120     b.reserve (128);
121     c.reserve (128);
122 
123     for (int i = 0; i < 2; ++i)
124     {
125         a = wxT("Hello");
126         b = wxT(" world");
127         c = wxT("! How'ya doin'?");
128         a += b;
129         a += c;
130         c = wxT("Hello world! What's up?");
131         CPPUNIT_ASSERT( c != a );
132     }
133 }
134 
PChar()135 void StringTestCase::PChar()
136 {
137     wxChar a [128];
138     wxChar b [128];
139     wxChar c [128];
140 
141     for (int i = 0; i < 2; ++i)
142     {
143         wxStrcpy (a, wxT("Hello"));
144         wxStrcpy (b, wxT(" world"));
145         wxStrcpy (c, wxT("! How'ya doin'?"));
146         wxStrcat (a, b);
147         wxStrcat (a, c);
148         wxStrcpy (c, wxT("Hello world! What's up?"));
149         CPPUNIT_ASSERT( wxStrcmp (c, a) != 0 );
150     }
151 }
152 
Format()153 void StringTestCase::Format()
154 {
155     wxString s1,s2;
156     s1.Printf(wxT("%03d"), 18);
157     CPPUNIT_ASSERT( s1 == wxString::Format(wxT("%03d"), 18) );
158     s2.Printf(wxT("Number 18: %s\n"), s1.c_str());
159     CPPUNIT_ASSERT( s2 == wxString::Format(wxT("Number 18: %s\n"), s1.c_str()) );
160 
161     static const size_t lengths[] = { 1, 512, 1024, 1025, 2048, 4096, 4097 };
162     for ( size_t n = 0; n < WXSIZEOF(lengths); n++ )
163     {
164         const size_t len = lengths[n];
165 
166         wxString s(wxT('Z'), len);
167         CPPUNIT_ASSERT_EQUAL( len, wxString::Format(wxT("%s"), s.c_str()).length());
168     }
169 
170 
171     CPPUNIT_ASSERT_EQUAL
172     (
173         "two one",
174         wxString::Format(wxT("%2$s %1$s"), wxT("one"), wxT("two"))
175     );
176 }
177 
FormatUnicode()178 void StringTestCase::FormatUnicode()
179 {
180 #if wxUSE_UNICODE
181     const char *UNICODE_STR = "Iestat\xC4\xAB %i%i";
182     //const char *UNICODE_STR = "Iestat\xCC\x84 %i%i";
183 
184     wxString fmt = wxString::FromUTF8(UNICODE_STR);
185     wxString s = wxString::Format(fmt, 1, 1);
186     wxString expected(fmt);
187     expected.Replace("%i", "1");
188     CPPUNIT_ASSERT_EQUAL( expected, s );
189 #endif // wxUSE_UNICODE
190 }
191 
Constructors()192 void StringTestCase::Constructors()
193 {
194     CPPUNIT_ASSERT_EQUAL( "", wxString('Z', 0) );
195     CPPUNIT_ASSERT_EQUAL( "Z", wxString('Z') );
196     CPPUNIT_ASSERT_EQUAL( "ZZZZ", wxString('Z', 4) );
197     CPPUNIT_ASSERT_EQUAL( "Hell", wxString("Hello", 4) );
198     CPPUNIT_ASSERT_EQUAL( "Hello", wxString("Hello", 5) );
199 
200 #if wxUSE_UNICODE
201     CPPUNIT_ASSERT_EQUAL( L"", wxString(L'Z', 0) );
202     CPPUNIT_ASSERT_EQUAL( L"Z", wxString(L'Z') );
203     CPPUNIT_ASSERT_EQUAL( L"ZZZZ", wxString(L'Z', 4) );
204     CPPUNIT_ASSERT_EQUAL( L"Hell", wxString(L"Hello", 4) );
205     CPPUNIT_ASSERT_EQUAL( L"Hello", wxString(L"Hello", 5) );
206 #endif // wxUSE_UNICODE
207 
208     CPPUNIT_ASSERT_EQUAL( 0, wxString(wxString(), 17).length() );
209 
210 #if wxUSE_UNICODE_UTF8
211     // This string has 3 characters (<h>, <e'> and <l>), not 4 when using UTF-8
212     // locale!
213     if ( wxConvLibc.IsUTF8() )
214     {
215         wxString s3("h\xc3\xa9llo", 4);
216         CPPUNIT_ASSERT_EQUAL( 3, s3.length() );
217         CPPUNIT_ASSERT_EQUAL( 'l', (char)s3[2] );
218     }
219 #endif // wxUSE_UNICODE_UTF8
220 
221 
222     static const char *s = "?really!";
223     const char *start = wxStrchr(s, 'r');
224     const char *end = wxStrchr(s, '!');
225     CPPUNIT_ASSERT_EQUAL( "really", wxString(start, end) );
226 
227     // test if creating string from NULL C pointer works:
228     CPPUNIT_ASSERT_EQUAL( "", wxString((const char *)NULL) );
229 }
230 
StaticConstructors()231 void StringTestCase::StaticConstructors()
232 {
233     CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("") );
234     CPPUNIT_ASSERT_EQUAL( "", wxString::FromAscii("Hello", 0) );
235     CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromAscii("Hello", 4) );
236     CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello", 5) );
237     CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromAscii("Hello") );
238 
239     // FIXME: this doesn't work currently but should!
240     //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromAscii("", 1).length() );
241 
242 
243     CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("") );
244     CPPUNIT_ASSERT_EQUAL( "", wxString::FromUTF8("Hello", 0) );
245     CPPUNIT_ASSERT_EQUAL( "Hell", wxString::FromUTF8("Hello", 4) );
246     CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello", 5) );
247     CPPUNIT_ASSERT_EQUAL( "Hello", wxString::FromUTF8("Hello") );
248 
249     CPPUNIT_ASSERT_EQUAL( 2, wxString::FromUTF8("h\xc3\xa9llo", 3).length() );
250 
251 
252     //CPPUNIT_ASSERT_EQUAL( 1, wxString::FromUTF8("", 1).length() );
253 }
254 
Extraction()255 void StringTestCase::Extraction()
256 {
257     wxString s(wxT("Hello, world!"));
258 
259     CPPUNIT_ASSERT( wxStrcmp( s.c_str() , wxT("Hello, world!") ) == 0 );
260     CPPUNIT_ASSERT( wxStrcmp( s.Left(5).c_str() , wxT("Hello") ) == 0 );
261     CPPUNIT_ASSERT( wxStrcmp( s.Right(6).c_str() , wxT("world!") ) == 0 );
262     CPPUNIT_ASSERT( wxStrcmp( s(3, 5).c_str() , wxT("lo, w") ) == 0 );
263     CPPUNIT_ASSERT( wxStrcmp( s.Mid(3).c_str() , wxT("lo, world!") ) == 0 );
264     CPPUNIT_ASSERT( wxStrcmp( s.substr(3, 5).c_str() , wxT("lo, w") ) == 0 );
265     CPPUNIT_ASSERT( wxStrcmp( s.substr(3).c_str() , wxT("lo, world!") ) == 0 );
266 
267 #if wxUSE_UNICODE
268     static const char *germanUTF8 = "Oberfl\303\244che";
269     wxString strUnicode(wxString::FromUTF8(germanUTF8));
270 
271     CPPUNIT_ASSERT( strUnicode.Mid(0, 10) == strUnicode );
272     CPPUNIT_ASSERT( strUnicode.Mid(7, 2) == "ch" );
273 #endif // wxUSE_UNICODE
274 
275     wxString rest;
276 
277     #define TEST_STARTS_WITH(prefix, correct_rest, result)                    \
278         CPPUNIT_ASSERT_EQUAL(result, s.StartsWith(prefix, &rest));            \
279         if ( result )                                                         \
280             CPPUNIT_ASSERT_EQUAL(correct_rest, rest)
281 
282     TEST_STARTS_WITH( wxT("Hello"),           wxT(", world!"),      true  );
283     TEST_STARTS_WITH( wxT("Hello, "),         wxT("world!"),        true  );
284     TEST_STARTS_WITH( wxT("Hello, world!"),   wxT(""),              true  );
285     TEST_STARTS_WITH( wxT("Hello, world!!!"), wxT(""),              false );
286     TEST_STARTS_WITH( wxT(""),                wxT("Hello, world!"), true  );
287     TEST_STARTS_WITH( wxT("Goodbye"),         wxT(""),              false );
288     TEST_STARTS_WITH( wxT("Hi"),              wxT(""),              false );
289 
290     #undef TEST_STARTS_WITH
291 
292     rest = "Hello world";
293     CPPUNIT_ASSERT( rest.StartsWith("Hello ", &rest) );
294     CPPUNIT_ASSERT_EQUAL("world", rest);
295 
296     #define TEST_ENDS_WITH(suffix, correct_rest, result)                      \
297         CPPUNIT_ASSERT_EQUAL(result, s.EndsWith(suffix, &rest));              \
298         if ( result )                                                         \
299             CPPUNIT_ASSERT_EQUAL(correct_rest, rest)
300 
301     TEST_ENDS_WITH( wxT(""),                 wxT("Hello, world!"), true  );
302     TEST_ENDS_WITH( wxT("!"),                wxT("Hello, world"),  true  );
303     TEST_ENDS_WITH( wxT(", world!"),         wxT("Hello"),         true  );
304     TEST_ENDS_WITH( wxT("ello, world!"),     wxT("H"),             true  );
305     TEST_ENDS_WITH( wxT("Hello, world!"),    wxT(""),              true  );
306     TEST_ENDS_WITH( wxT("very long string"), wxT(""),              false );
307     TEST_ENDS_WITH( wxT("?"),                wxT(""),              false );
308     TEST_ENDS_WITH( wxT("Hello, world"),     wxT(""),              false );
309     TEST_ENDS_WITH( wxT("Gello, world!"),    wxT(""),              false );
310 
311     #undef TEST_ENDS_WITH
312 }
313 
Trim()314 void StringTestCase::Trim()
315 {
316     #define TEST_TRIM( str , dir , result )  \
317         CPPUNIT_ASSERT( wxString(str).Trim(dir) == result )
318 
319     TEST_TRIM( wxT("  Test  "),  true, wxT("  Test") );
320     TEST_TRIM( wxT("    "),      true, wxT("")       );
321     TEST_TRIM( wxT(" "),         true, wxT("")       );
322     TEST_TRIM( wxT(""),          true, wxT("")       );
323 
324     TEST_TRIM( wxT("  Test  "),  false, wxT("Test  ") );
325     TEST_TRIM( wxT("    "),      false, wxT("")       );
326     TEST_TRIM( wxT(" "),         false, wxT("")       );
327     TEST_TRIM( wxT(""),          false, wxT("")       );
328 
329     #undef TEST_TRIM
330 }
331 
Find()332 void StringTestCase::Find()
333 {
334     #define TEST_FIND( str , start , result )  \
335         CPPUNIT_ASSERT( wxString(str).find(wxT("ell"), start) == result );
336 
337     TEST_FIND( wxT("Well, hello world"),  0, 1              );
338     TEST_FIND( wxT("Well, hello world"),  6, 7              );
339     TEST_FIND( wxT("Well, hello world"),  9, wxString::npos );
340 
341     #undef TEST_FIND
342 }
343 
Replace()344 void StringTestCase::Replace()
345 {
346     #define TEST_REPLACE( original , pos , len , replacement , result ) \
347         { \
348             wxString s = original; \
349             s.replace( pos , len , replacement ); \
350             CPPUNIT_ASSERT_EQUAL( result, s ); \
351         }
352 
353     TEST_REPLACE( wxT("012-AWORD-XYZ"), 4, 5, wxT("BWORD"),  wxT("012-BWORD-XYZ") );
354     TEST_REPLACE( wxT("increase"),      0, 2, wxT("de"),     wxT("decrease")      );
355     TEST_REPLACE( wxT("wxWindow"),      8, 0, wxT("s"),      wxT("wxWindows")     );
356     TEST_REPLACE( wxT("foobar"),        3, 0, wxT("-"),      wxT("foo-bar")       );
357     TEST_REPLACE( wxT("barfoo"),        0, 6, wxT("foobar"), wxT("foobar")        );
358 
359 
360     #define TEST_NULLCHARREPLACE( o , olen, pos , len , replacement , r, rlen ) \
361         { \
362             wxString s(o,olen); \
363             s.replace( pos , len , replacement ); \
364             CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \
365         }
366 
367     TEST_NULLCHARREPLACE( wxT("null\0char"), 9, 5, 1, wxT("d"),
368                           wxT("null\0dhar"), 9 );
369 
370     #define TEST_WXREPLACE( o , olen, olds, news, all, r, rlen ) \
371         { \
372             wxString s(o,olen); \
373             s.Replace( olds, news, all ); \
374             CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \
375         }
376 
377     TEST_WXREPLACE( wxT("null\0char"), 9, wxT("c"), wxT("de"), true,
378                           wxT("null\0dehar"), 10 );
379 
380     TEST_WXREPLACE( wxT("null\0dehar"), 10, wxT("de"), wxT("c"), true,
381                           wxT("null\0char"), 9 );
382 
383     TEST_WXREPLACE( "life", 4, "f", "", false, "lie", 3 );
384     TEST_WXREPLACE( "life", 4, "f", "", true, "lie", 3 );
385     TEST_WXREPLACE( "life", 4, "fe", "ve", true, "live", 4 );
386     TEST_WXREPLACE( "xx", 2, "x", "yy", true, "yyyy", 4 );
387     TEST_WXREPLACE( "xxx", 3, "xx", "z", true, "zx", 2 );
388 
389     #undef TEST_WXREPLACE
390     #undef TEST_NULLCHARREPLACE
391     #undef TEST_REPLACE
392 }
393 
Match()394 void StringTestCase::Match()
395 {
396     #define TEST_MATCH( s1 , s2 , result ) \
397         CPPUNIT_ASSERT( wxString(s1).Matches(s2) == result )
398 
399     TEST_MATCH( "foobar",       "foo*",        true  );
400     TEST_MATCH( "foobar",       "*oo*",        true  );
401     TEST_MATCH( "foobar",       "*bar",        true  );
402     TEST_MATCH( "foobar",       "??????",      true  );
403     TEST_MATCH( "foobar",       "f??b*",       true  );
404     TEST_MATCH( "foobar",       "f?b*",        false );
405     TEST_MATCH( "foobar",       "*goo*",       false );
406     TEST_MATCH( "foobar",       "*foo",        false );
407     TEST_MATCH( "foobarfoo",    "*foo",        true  );
408     TEST_MATCH( "",             "*",           true  );
409     TEST_MATCH( "",             "?",           false );
410 
411     #undef TEST_MATCH
412 }
413 
414 
CaseChanges()415 void StringTestCase::CaseChanges()
416 {
417     wxString s1(wxT("Hello!"));
418     wxString s1u(s1);
419     wxString s1l(s1);
420     s1u.MakeUpper();
421     s1l.MakeLower();
422 
423     CPPUNIT_ASSERT_EQUAL( wxT("HELLO!"), s1u );
424     CPPUNIT_ASSERT_EQUAL( wxT("hello!"), s1l );
425 
426     wxString s2u, s2l;
427     s2u.MakeUpper();
428     s2l.MakeLower();
429 
430     CPPUNIT_ASSERT_EQUAL( "", s2u );
431     CPPUNIT_ASSERT_EQUAL( "", s2l );
432 
433 
434     wxString s3("good bye");
435     CPPUNIT_ASSERT_EQUAL( "Good bye", s3.Capitalize() );
436     s3.MakeCapitalized();
437     CPPUNIT_ASSERT_EQUAL( "Good bye", s3 );
438 
439     CPPUNIT_ASSERT_EQUAL( "Abc", wxString("ABC").Capitalize() );
440 
441     CPPUNIT_ASSERT_EQUAL( "", wxString().Capitalize() );
442 }
443 
Compare()444 void StringTestCase::Compare()
445 {
446     wxString s1 = wxT("AHH");
447     wxString eq = wxT("AHH");
448     wxString neq1 = wxT("HAH");
449     wxString neq2 = wxT("AH");
450     wxString neq3 = wxT("AHHH");
451     wxString neq4 = wxT("AhH");
452 
453     CPPUNIT_ASSERT( s1 == eq );
454     CPPUNIT_ASSERT( s1 != neq1 );
455     CPPUNIT_ASSERT( s1 != neq2 );
456     CPPUNIT_ASSERT( s1 != neq3 );
457     CPPUNIT_ASSERT( s1 != neq4 );
458 
459     CPPUNIT_ASSERT( s1 == wxT("AHH") );
460     CPPUNIT_ASSERT( s1 != wxT("no") );
461     CPPUNIT_ASSERT( s1 < wxT("AZ") );
462     CPPUNIT_ASSERT( s1 <= wxT("AZ") );
463     CPPUNIT_ASSERT( s1 <= wxT("AHH") );
464     CPPUNIT_ASSERT( s1 > wxT("AA") );
465     CPPUNIT_ASSERT( s1 >= wxT("AA") );
466     CPPUNIT_ASSERT( s1 >= wxT("AHH") );
467 
468     // test comparison with C strings in Unicode build (must work in ANSI as
469     // well, of course):
470     CPPUNIT_ASSERT( s1 == "AHH" );
471     CPPUNIT_ASSERT( s1 != "no" );
472     CPPUNIT_ASSERT( s1 < "AZ" );
473     CPPUNIT_ASSERT( s1 <= "AZ" );
474     CPPUNIT_ASSERT( s1 <= "AHH" );
475     CPPUNIT_ASSERT( s1 > "AA" );
476     CPPUNIT_ASSERT( s1 >= "AA" );
477     CPPUNIT_ASSERT( s1 >= "AHH" );
478 
479 //    wxString _s1 = wxT("A\0HH");
480 //    wxString _eq = wxT("A\0HH");
481 //    wxString _neq1 = wxT("H\0AH");
482 //    wxString _neq2 = wxT("A\0H");
483 //    wxString _neq3 = wxT("A\0HHH");
484 //    wxString _neq4 = wxT("A\0hH");
485     s1.insert(1,1,'\0');
486     eq.insert(1,1,'\0');
487     neq1.insert(1,1,'\0');
488     neq2.insert(1,1,'\0');
489     neq3.insert(1,1,'\0');
490     neq4.insert(1,1,'\0');
491 
492     CPPUNIT_ASSERT( s1 == eq );
493     CPPUNIT_ASSERT( s1 != neq1 );
494     CPPUNIT_ASSERT( s1 != neq2 );
495     CPPUNIT_ASSERT( s1 != neq3 );
496     CPPUNIT_ASSERT( s1 != neq4 );
497 
498     CPPUNIT_ASSERT( wxString("\n").Cmp(" ") < 0 );
499     CPPUNIT_ASSERT( wxString("'").Cmp("!") > 0 );
500     CPPUNIT_ASSERT( wxString("!").Cmp("z") < 0 );
501 }
502 
CompareNoCase()503 void StringTestCase::CompareNoCase()
504 {
505     wxString s1 = wxT("AHH");
506     wxString eq = wxT("AHH");
507     wxString eq2 = wxT("AhH");
508     wxString eq3 = wxT("ahh");
509     wxString neq = wxT("HAH");
510     wxString neq2 = wxT("AH");
511     wxString neq3 = wxT("AHHH");
512 
513     #define CPPUNIT_CNCEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) == 0)
514     #define CPPUNIT_CNCNEQ_ASSERT(s1, s2) CPPUNIT_ASSERT( s1.CmpNoCase(s2) != 0)
515 
516     CPPUNIT_CNCEQ_ASSERT( s1, eq );
517     CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
518     CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
519 
520     CPPUNIT_CNCNEQ_ASSERT( s1, neq );
521     CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
522     CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
523 
524 
525 //    wxString _s1 = wxT("A\0HH");
526 //    wxString _eq = wxT("A\0HH");
527 //    wxString _eq2 = wxT("A\0hH");
528 //    wxString _eq3 = wxT("a\0hh");
529 //    wxString _neq = wxT("H\0AH");
530 //    wxString _neq2 = wxT("A\0H");
531 //    wxString _neq3 = wxT("A\0HHH");
532 
533     s1.insert(1,1,'\0');
534     eq.insert(1,1,'\0');
535     eq2.insert(1,1,'\0');
536     eq3.insert(1,1,'\0');
537     neq.insert(1,1,'\0');
538     neq2.insert(1,1,'\0');
539     neq3.insert(1,1,'\0');
540 
541     CPPUNIT_CNCEQ_ASSERT( s1, eq );
542     CPPUNIT_CNCEQ_ASSERT( s1, eq2 );
543     CPPUNIT_CNCEQ_ASSERT( s1, eq3 );
544 
545     CPPUNIT_CNCNEQ_ASSERT( s1, neq );
546     CPPUNIT_CNCNEQ_ASSERT( s1, neq2 );
547     CPPUNIT_CNCNEQ_ASSERT( s1, neq3 );
548 
549     CPPUNIT_ASSERT( wxString("\n").CmpNoCase(" ") < 0 );
550     CPPUNIT_ASSERT( wxString("'").CmpNoCase("!") > 0);
551     CPPUNIT_ASSERT( wxString("!").Cmp("Z") < 0 );
552 }
553 
Contains()554 void StringTestCase::Contains()
555 {
556     static const struct ContainsData
557     {
558         const wxChar *hay;
559         const wxChar *needle;
560         bool contains;
561     } containsData[] =
562     {
563         { wxT(""),       wxT(""),         true  },
564         { wxT(""),       wxT("foo"),      false },
565         { wxT("foo"),    wxT(""),         true  },
566         { wxT("foo"),    wxT("f"),        true  },
567         { wxT("foo"),    wxT("o"),        true  },
568         { wxT("foo"),    wxT("oo"),       true  },
569         { wxT("foo"),    wxT("ooo"),      false },
570         { wxT("foo"),    wxT("oooo"),     false },
571         { wxT("foo"),    wxT("fooo"),     false },
572     };
573 
574     for ( size_t n = 0; n < WXSIZEOF(containsData); n++ )
575     {
576         const ContainsData& cd = containsData[n];
577         CPPUNIT_ASSERT_EQUAL( cd.contains, wxString(cd.hay).Contains(cd.needle) );
578     }
579 }
580 
581 // flags used in ToLongData.flags
582 enum
583 {
584     Number_Ok       = 0,
585     Number_Invalid  = 1,
586     Number_Unsigned = 2,    // if not specified, works for signed conversion
587     Number_Signed   = 4,    // if not specified, works for unsigned
588     Number_LongLong = 8,    // only for long long tests
589     Number_Long     = 16    // only for long tests
590 };
591 
592 #ifdef wxLongLong_t
593 typedef wxLongLong_t TestValue_t;
594 #else
595 typedef long TestValue_t;
596 #endif
597 
598 static const struct ToLongData
599 {
600     const wxChar *str;
601     TestValue_t value;
602     int flags;
603     int base;
604 
LValueToLongData605     long LValue() const { return value; }
ULValueToLongData606     unsigned long ULValue() const { return value; }
607 #ifdef wxLongLong_t
LLValueToLongData608     wxLongLong_t LLValue() const { return value; }
ULLValueToLongData609     wxULongLong_t ULLValue() const { return (wxULongLong_t)value; }
610 #endif // wxLongLong_t
611 
IsOkToLongData612     bool IsOk() const { return !(flags & Number_Invalid); }
613 } longData[] =
614 {
615     { wxT("1"), 1, Number_Ok },
616     { wxT("0"), 0, Number_Ok },
617     { wxT("a"), 0, Number_Invalid },
618     { wxT("12345"), 12345, Number_Ok },
619     { wxT("--1"), 0, Number_Invalid },
620 
621     { wxT("-1"), -1, Number_Signed | Number_Long },
622     // this is surprising but consistent with strtoul() behaviour
623     { wxT("-1"), (TestValue_t)ULONG_MAX, Number_Unsigned | Number_Long },
624 
625     // this must overflow, even with 64 bit long
626     { wxT("922337203685477580711"), 0, Number_Invalid },
627 
628 #ifdef wxLongLong_t
629     { wxT("2147483648"), wxLL(2147483648), Number_LongLong },
630     { wxT("-2147483648"), wxLL(-2147483648), Number_LongLong | Number_Signed },
631     { wxT("9223372036854775808"),
632       TestValue_t(wxULL(9223372036854775808)),
633       Number_LongLong | Number_Unsigned },
634 #endif // wxLongLong_t
635 
636     // Base tests.
637     { wxT("010"),  10, Number_Ok, 10 },
638     { wxT("010"),   8, Number_Ok,  0 },
639     { wxT("010"),   8, Number_Ok,  8 },
640     { wxT("010"),  16, Number_Ok, 16 },
641 
642     { wxT("0010"), 10, Number_Ok, 10 },
643     { wxT("0010"),  8, Number_Ok,  0 },
644     { wxT("0010"),  8, Number_Ok,  8 },
645     { wxT("0010"), 16, Number_Ok, 16 },
646 
647     { wxT("0x11"),  0, Number_Invalid, 10 },
648     { wxT("0x11"), 17, Number_Ok,       0 },
649     { wxT("0x11"),  0, Number_Invalid,  8 },
650     { wxT("0x11"), 17, Number_Ok,      16 },
651 };
652 
ToLong()653 void StringTestCase::ToLong()
654 {
655     long l;
656     for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
657     {
658         const ToLongData& ld = longData[n];
659 
660         if ( ld.flags & (Number_LongLong | Number_Unsigned) )
661             continue;
662 
663         // NOTE: unless you're using some exotic locale, ToCLong and ToLong
664         //       should behave the same for our test data set:
665 
666         CPPUNIT_ASSERT_EQUAL( ld.IsOk(),
667                               wxString(ld.str).ToCLong(&l, ld.base) );
668         if ( ld.IsOk() )
669             CPPUNIT_ASSERT_EQUAL( ld.LValue(), l );
670 
671         CPPUNIT_ASSERT_EQUAL( ld.IsOk(),
672                               wxString(ld.str).ToLong(&l, ld.base) );
673         if ( ld.IsOk() )
674             CPPUNIT_ASSERT_EQUAL( ld.LValue(), l );
675     }
676 
677     // special case: check that the output is not modified if the parsing
678     // failed completely
679     l = 17;
680     CPPUNIT_ASSERT( !wxString("foo").ToLong(&l) );
681     CPPUNIT_ASSERT_EQUAL( 17, l );
682 
683     // also check that it is modified if we did parse something successfully in
684     // the beginning of the string
685     CPPUNIT_ASSERT( !wxString("9 cats").ToLong(&l) );
686     CPPUNIT_ASSERT_EQUAL( 9, l );
687 }
688 
ToULong()689 void StringTestCase::ToULong()
690 {
691     unsigned long ul;
692     for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
693     {
694         const ToLongData& ld = longData[n];
695 
696         if ( ld.flags & (Number_LongLong | Number_Signed) )
697             continue;
698 
699         // NOTE: unless you're using some exotic locale, ToCLong and ToLong
700         //       should behave the same for our test data set:
701 
702         CPPUNIT_ASSERT_EQUAL( ld.IsOk(),
703                               wxString(ld.str).ToCULong(&ul, ld.base) );
704         if ( ld.IsOk() )
705             CPPUNIT_ASSERT_EQUAL( ld.ULValue(), ul );
706 
707         CPPUNIT_ASSERT_EQUAL( ld.IsOk(),
708                               wxString(ld.str).ToULong(&ul, ld.base) );
709         if ( ld.IsOk() )
710             CPPUNIT_ASSERT_EQUAL( ld.ULValue(), ul );
711     }
712 }
713 
714 #ifdef wxLongLong_t
715 
ToLongLong()716 void StringTestCase::ToLongLong()
717 {
718     wxLongLong_t l;
719     for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
720     {
721         const ToLongData& ld = longData[n];
722 
723         if ( ld.flags & (Number_Long | Number_Unsigned) )
724             continue;
725 
726         CPPUNIT_ASSERT_EQUAL( ld.IsOk(),
727                               wxString(ld.str).ToLongLong(&l, ld.base) );
728         if ( ld.IsOk() )
729             CPPUNIT_ASSERT_EQUAL( ld.LLValue(), l );
730     }
731 }
732 
ToULongLong()733 void StringTestCase::ToULongLong()
734 {
735     wxULongLong_t ul;
736     for ( size_t n = 0; n < WXSIZEOF(longData); n++ )
737     {
738         const ToLongData& ld = longData[n];
739 
740         if ( ld.flags & (Number_Long | Number_Signed) )
741             continue;
742 
743         CPPUNIT_ASSERT_EQUAL( ld.IsOk(),
744                               wxString(ld.str).ToULongLong(&ul, ld.base) );
745         if ( ld.IsOk() )
746             CPPUNIT_ASSERT_EQUAL( ld.ULLValue(), ul );
747     }
748 }
749 
750 #endif // wxLongLong_t
751 
ToDouble()752 void StringTestCase::ToDouble()
753 {
754     double d;
755     static const struct ToDoubleData
756     {
757         const wxChar *str;
758         double value;
759         bool ok;
760     } doubleData[] =
761     {
762         { wxT("1"), 1, true },
763         { wxT("1.23"), 1.23, true },
764         { wxT(".1"), .1, true },
765         { wxT("1."), 1, true },
766         { wxT("1.."), 0, false },
767         { wxT("0"), 0, true },
768         { wxT("a"), 0, false },
769         { wxT("12345"), 12345, true },
770         { wxT("-1"), -1, true },
771         { wxT("--1"), 0, false },
772         { wxT("-3E-5"), -3E-5, true },
773         { wxT("-3E-abcde5"), 0, false },
774     };
775 
776     // test ToCDouble() first:
777 
778     size_t n;
779     for ( n = 0; n < WXSIZEOF(doubleData); n++ )
780     {
781         const ToDoubleData& ld = doubleData[n];
782         CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToCDouble(&d) );
783         if ( ld.ok )
784             CPPUNIT_ASSERT_EQUAL( ld.value, d );
785     }
786 
787 
788     // test ToDouble() now:
789     // NOTE: for the test to be reliable, we need to set the locale explicitly
790     //       so that we know the decimal point character to use
791 
792     if (!wxLocale::IsAvailable(wxLANGUAGE_FRENCH))
793         return;     // you should have french support installed to continue this test!
794 
795     wxLocale locale;
796 
797     // don't load default catalog, it may be unavailable:
798     CPPUNIT_ASSERT( locale.Init(wxLANGUAGE_FRENCH, wxLOCALE_DONT_LOAD_DEFAULT) );
799 
800     static const struct ToDoubleData doubleData2[] =
801     {
802         { wxT("1"), 1, true },
803         { wxT("1,23"), 1.23, true },
804         { wxT(",1"), .1, true },
805         { wxT("1,"), 1, true },
806         { wxT("1,,"), 0, false },
807         { wxT("0"), 0, true },
808         { wxT("a"), 0, false },
809         { wxT("12345"), 12345, true },
810         { wxT("-1"), -1, true },
811         { wxT("--1"), 0, false },
812         { wxT("-3E-5"), -3E-5, true },
813         { wxT("-3E-abcde5"), 0, false },
814     };
815 
816     for ( n = 0; n < WXSIZEOF(doubleData2); n++ )
817     {
818         const ToDoubleData& ld = doubleData2[n];
819         CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToDouble(&d) );
820         if ( ld.ok )
821             CPPUNIT_ASSERT_EQUAL( ld.value, d );
822     }
823 }
824 
FromDouble()825 void StringTestCase::FromDouble()
826 {
827     static const struct FromDoubleTestData
828     {
829         double value;
830         int prec;
831         const char *str;
832     } testData[] =
833     {
834         { 1.23,             -1, "1.23" },
835         // All MSVC versions until MSVC 14 used 3 digits for the exponent
836         // unnecessarily, account for this non-standard behaviour.
837 #if defined(wxUSING_VC_CRT_IO) && !wxCHECK_VISUALC_VERSION(14)
838         { -3e-10,           -1, "-3e-010" },
839 #else
840         { -3e-10,           -1, "-3e-10" },
841 #endif
842         { -0.45678,         -1, "-0.45678" },
843         { 1.2345678,         0, "1" },
844         { 1.2345678,         1, "1.2" },
845         { 1.2345678,         2, "1.23" },
846         { 1.2345678,         3, "1.235" },
847     };
848 
849     for ( unsigned n = 0; n < WXSIZEOF(testData); n++ )
850     {
851         const FromDoubleTestData& td = testData[n];
852         CPPUNIT_ASSERT_EQUAL( td.str, wxString::FromCDouble(td.value, td.prec) );
853     }
854 
855     if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH) )
856         return;
857 
858     wxLocale locale;
859     CPPUNIT_ASSERT( locale.Init(wxLANGUAGE_FRENCH, wxLOCALE_DONT_LOAD_DEFAULT) );
860 
861     for ( unsigned m = 0; m < WXSIZEOF(testData); m++ )
862     {
863         const FromDoubleTestData& td = testData[m];
864 
865         wxString str(td.str);
866         str.Replace(".", ",");
867         CPPUNIT_ASSERT_EQUAL( str, wxString::FromDouble(td.value, td.prec) );
868     }
869 }
870 
StringBuf()871 void StringTestCase::StringBuf()
872 {
873     // check that buffer can be used to write into the string
874     wxString s;
875     wxStrcpy(wxStringBuffer(s, 10), wxT("foo"));
876 
877     CPPUNIT_ASSERT_EQUAL(3, s.length());
878     CPPUNIT_ASSERT(wxT('f') == s[0u]);
879     CPPUNIT_ASSERT(wxT('o') == s[1]);
880     CPPUNIT_ASSERT(wxT('o') == s[2]);
881 
882     {
883         // also check that the buffer initially contains the original string
884         // contents
885         wxStringBuffer buf(s, 10);
886         CPPUNIT_ASSERT_EQUAL( wxT('f'), buf[0] );
887         CPPUNIT_ASSERT_EQUAL( wxT('o'), buf[1] );
888         CPPUNIT_ASSERT_EQUAL( wxT('o'), buf[2] );
889         CPPUNIT_ASSERT_EQUAL( wxT('\0'), buf[3] );
890     }
891 
892     {
893         wxStringBufferLength buf(s, 10);
894         CPPUNIT_ASSERT_EQUAL( wxT('f'), buf[0] );
895         CPPUNIT_ASSERT_EQUAL( wxT('o'), buf[1] );
896         CPPUNIT_ASSERT_EQUAL( wxT('o'), buf[2] );
897         CPPUNIT_ASSERT_EQUAL( wxT('\0'), buf[3] );
898 
899         // and check that it can be used to write only the specified number of
900         // characters to the string
901         wxStrcpy(buf, wxT("barrbaz"));
902         buf.SetLength(4);
903     }
904 
905     CPPUNIT_ASSERT_EQUAL(4, s.length());
906     CPPUNIT_ASSERT(wxT('b') == s[0u]);
907     CPPUNIT_ASSERT(wxT('a') == s[1]);
908     CPPUNIT_ASSERT(wxT('r') == s[2]);
909     CPPUNIT_ASSERT(wxT('r') == s[3]);
910 
911     // check that creating buffer of length smaller than string works, i.e. at
912     // least doesn't crash (it would if we naively copied the entire original
913     // string contents in the buffer)
914     *wxStringBuffer(s, 1) = '!';
915 }
916 
UTF8Buf()917 void StringTestCase::UTF8Buf()
918 {
919 #if wxUSE_UNICODE
920     // "czech" in Czech ("cestina"):
921     static const char *textUTF8 = "\304\215e\305\241tina";
922     static const wchar_t textUTF16[] = {0x10D, 0x65, 0x161, 0x74, 0x69, 0x6E, 0x61, 0};
923 
924     wxString s;
925     wxStrcpy(wxUTF8StringBuffer(s, 9), textUTF8);
926     CPPUNIT_ASSERT(s == textUTF16);
927 
928     {
929         wxUTF8StringBufferLength buf(s, 20);
930         wxStrcpy(buf, textUTF8);
931         buf.SetLength(5);
932     }
933     CPPUNIT_ASSERT(s == wxString(textUTF16, 0, 3));
934 #endif // wxUSE_UNICODE
935 }
936 
937 
938 
CStrDataTernaryOperator()939 void StringTestCase::CStrDataTernaryOperator()
940 {
941     DoCStrDataTernaryOperator(true);
942     DoCStrDataTernaryOperator(false);
943 }
944 
CheckStr(const wxString & expected,T s)945 template<typename T> bool CheckStr(const wxString& expected, T s)
946 {
947     return expected == wxString(s);
948 }
949 
DoCStrDataTernaryOperator(bool cond)950 void StringTestCase::DoCStrDataTernaryOperator(bool cond)
951 {
952     // test compilation of wxCStrData when used with operator?: (the asserts
953     // are not very important, we're testing if the code compiles at all):
954 
955     wxString s("foo");
956 
957     const wchar_t *wcStr = L"foo";
958     CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : wcStr)) );
959     CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : L"foo")) );
960     CPPUNIT_ASSERT( CheckStr(s, (cond ? wcStr : s.c_str())) );
961     CPPUNIT_ASSERT( CheckStr(s, (cond ? L"foo" : s.c_str())) );
962 
963     const char *mbStr = "foo";
964     CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : mbStr)) );
965     CPPUNIT_ASSERT( CheckStr(s, (cond ? s.c_str() : "foo")) );
966     CPPUNIT_ASSERT( CheckStr(s, (cond ? mbStr : s.c_str())) );
967     CPPUNIT_ASSERT( CheckStr(s, (cond ? "foo" : s.c_str())) );
968 
969     wxString empty("");
970     CPPUNIT_ASSERT( CheckStr(empty, (cond ? empty.c_str() : wxEmptyString)) );
971     CPPUNIT_ASSERT( CheckStr(empty, (cond ? wxEmptyString : empty.c_str())) );
972 }
973 
CStrDataOperators()974 void StringTestCase::CStrDataOperators()
975 {
976     wxString s("hello");
977 
978     CPPUNIT_ASSERT( s.c_str()[0] == 'h' );
979     CPPUNIT_ASSERT( s.c_str()[1] == 'e' );
980 
981     // IMPORTANT: at least with the CRT coming with MSVC++ 2008 trying to access
982     //            the final character results in an assert failure (with debug CRT)
983     //CPPUNIT_ASSERT( s.c_str()[5] == '\0' );
984 
985     CPPUNIT_ASSERT( *s.c_str() == 'h' );
986     CPPUNIT_ASSERT( *(s.c_str() + 2) == 'l' );
987     //CPPUNIT_ASSERT( *(s.c_str() + 5) == '\0' );
988 }
989 
CheckStrChar(const wxString & expected,char * s)990 bool CheckStrChar(const wxString& expected, char *s)
991     { return CheckStr(expected, s); }
CheckStrWChar(const wxString & expected,wchar_t * s)992 bool CheckStrWChar(const wxString& expected, wchar_t *s)
993     { return CheckStr(expected, s); }
CheckStrConstChar(const wxString & expected,const char * s)994 bool CheckStrConstChar(const wxString& expected, const char *s)
995     { return CheckStr(expected, s); }
CheckStrConstWChar(const wxString & expected,const wchar_t * s)996 bool CheckStrConstWChar(const wxString& expected, const wchar_t *s)
997     { return CheckStr(expected, s); }
998 
CStrDataImplicitConversion()999 void StringTestCase::CStrDataImplicitConversion()
1000 {
1001     wxString s("foo");
1002 
1003     CPPUNIT_ASSERT( CheckStrConstWChar(s, s.c_str()) );
1004     CPPUNIT_ASSERT( CheckStrConstChar(s, s.c_str()) );
1005 
1006     // implicit conversion of wxString is not available in STL build
1007 #if !wxUSE_STL
1008     CPPUNIT_ASSERT( CheckStrConstWChar(s, s) );
1009     CPPUNIT_ASSERT( CheckStrConstChar(s, s) );
1010 #endif
1011 }
1012 
ExplicitConversion()1013 void StringTestCase::ExplicitConversion()
1014 {
1015     wxString s("foo");
1016 
1017     CPPUNIT_ASSERT( CheckStr(s, s.mb_str()) );
1018     CPPUNIT_ASSERT( CheckStrConstChar(s, s.mb_str()) );
1019     CPPUNIT_ASSERT( CheckStrChar(s, s.char_str()) );
1020 
1021     CPPUNIT_ASSERT( CheckStr(s, s.wc_str()) );
1022     CPPUNIT_ASSERT( CheckStrConstWChar(s, s.wc_str()) );
1023     CPPUNIT_ASSERT( CheckStrWChar(s, s.wchar_str()) );
1024 }
1025 
IndexedAccess()1026 void StringTestCase::IndexedAccess()
1027 {
1028     wxString s("bar");
1029     CPPUNIT_ASSERT_EQUAL( 'r', (char)s[2] );
1030 
1031     // this tests for a possible bug in UTF-8 based wxString implementation:
1032     // the 3rd character of the underlying byte string is going to change, but
1033     // the 3rd character of wxString should remain the same
1034     s[0] = L'\xe9';
1035     CPPUNIT_ASSERT_EQUAL( 'r', (char)s[2] );
1036 }
1037 
BeforeAndAfter()1038 void StringTestCase::BeforeAndAfter()
1039 {
1040     // Construct a string with 2 equal signs in it by concatenating its three
1041     // parts: before the first "=", in between the two "="s and after the last
1042     // one. This allows to avoid duplicating the string contents (which has to
1043     // be different for Unicode and ANSI builds) in the tests below.
1044 #if wxUSE_UNICODE
1045     #define FIRST_PART L"letter"
1046     #define MIDDLE_PART L"\xe9;\xe7a"
1047     #define LAST_PART L"l\xe0"
1048 #else // !wxUSE_UNICODE
1049     #define FIRST_PART "letter"
1050     #define MIDDLE_PART "e;ca"
1051     #define LAST_PART "la"
1052 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1053 
1054     const wxString s(FIRST_PART wxT("=") MIDDLE_PART wxT("=") LAST_PART);
1055 
1056     wxString r;
1057 
1058     CPPUNIT_ASSERT_EQUAL( FIRST_PART, s.BeforeFirst('=', &r) );
1059     CPPUNIT_ASSERT_EQUAL( MIDDLE_PART wxT("=") LAST_PART, r );
1060 
1061     CPPUNIT_ASSERT_EQUAL( s, s.BeforeFirst('!', &r) );
1062     CPPUNIT_ASSERT_EQUAL( "", r );
1063 
1064 
1065     CPPUNIT_ASSERT_EQUAL( FIRST_PART wxT("=") MIDDLE_PART, s.BeforeLast('=', &r) );
1066     CPPUNIT_ASSERT_EQUAL( LAST_PART, r );
1067 
1068     CPPUNIT_ASSERT_EQUAL( "", s.BeforeLast('!', &r) );
1069     CPPUNIT_ASSERT_EQUAL( s, r );
1070 
1071 
1072     CPPUNIT_ASSERT_EQUAL( MIDDLE_PART wxT("=") LAST_PART, s.AfterFirst('=') );
1073     CPPUNIT_ASSERT_EQUAL( "", s.AfterFirst('!') );
1074 
1075 
1076     CPPUNIT_ASSERT_EQUAL( LAST_PART, s.AfterLast('=') );
1077     CPPUNIT_ASSERT_EQUAL( s, s.AfterLast('!') );
1078 
1079     #undef LAST_PART
1080     #undef MIDDLE_PART
1081     #undef FIRST_PART
1082 }
1083 
ScopedBuffers()1084 void StringTestCase::ScopedBuffers()
1085 {
1086     // wxString relies on efficient buffers, verify they work as they should
1087 
1088     const char *literal = "Hello World!";
1089 
1090     // non-owned buffer points to the string passed to it
1091     wxScopedCharBuffer sbuf = wxScopedCharBuffer::CreateNonOwned(literal);
1092     CPPUNIT_ASSERT( sbuf.data() == literal );
1093 
1094     // a copy of scoped non-owned buffer still points to the same string
1095     wxScopedCharBuffer sbuf2(sbuf);
1096     CPPUNIT_ASSERT( sbuf.data() == sbuf2.data() );
1097 
1098     // but assigning it to wxCharBuffer makes a full copy
1099     wxCharBuffer buf(sbuf);
1100     CPPUNIT_ASSERT( buf.data() != literal );
1101     CPPUNIT_ASSERT_EQUAL( literal, buf.data() );
1102 
1103     wxCharBuffer buf2 = sbuf;
1104     CPPUNIT_ASSERT( buf2.data() != literal );
1105     CPPUNIT_ASSERT_EQUAL( literal, buf.data() );
1106 
1107     // Check that extending the buffer keeps it NUL-terminated.
1108     size_t len = 10;
1109 
1110     wxCharBuffer buf3(len);
1111     CPPUNIT_ASSERT_EQUAL('\0', buf3.data()[len]);
1112 
1113     wxCharBuffer buf4;
1114     buf4.extend(len);
1115     CPPUNIT_ASSERT_EQUAL('\0', buf4.data()[len]);
1116 
1117     wxCharBuffer buf5(5);
1118     buf5.extend(len);
1119     CPPUNIT_ASSERT_EQUAL('\0', buf5.data()[len]);
1120 }
1121