OLD | NEW |
1 /******************************************************************** | 1 /******************************************************************** |
2 * COPYRIGHT: | 2 * COPYRIGHT: |
3 * Copyright (c) 1997-2014, International Business Machines Corporation and | 3 * Copyright (c) 1997-2015, International Business Machines Corporation and |
4 * others. All Rights Reserved. | 4 * others. All Rights Reserved. |
5 ********************************************************************/ | 5 ********************************************************************/ |
6 | 6 |
7 #include "ustrtest.h" | 7 #include "ustrtest.h" |
8 #include "unicode/appendable.h" | 8 #include "unicode/appendable.h" |
9 #include "unicode/std_string.h" | 9 #include "unicode/std_string.h" |
10 #include "unicode/unistr.h" | 10 #include "unicode/unistr.h" |
11 #include "unicode/uchar.h" | 11 #include "unicode/uchar.h" |
12 #include "unicode/ustring.h" | 12 #include "unicode/ustring.h" |
13 #include "unicode/locid.h" | 13 #include "unicode/locid.h" |
14 #include "unicode/ucnv.h" | 14 #include "unicode/ucnv.h" |
15 #include "unicode/uenum.h" | 15 #include "unicode/uenum.h" |
16 #include "unicode/utf16.h" | 16 #include "unicode/utf16.h" |
17 #include "cmemory.h" | 17 #include "cmemory.h" |
18 #include "charstr.h" | 18 #include "charstr.h" |
19 | 19 |
20 #if 0 | 20 #if 0 |
21 #include "unicode/ustream.h" | 21 #include "unicode/ustream.h" |
22 | 22 |
23 #include <iostream> | 23 #include <iostream> |
24 using namespace std; | 24 using namespace std; |
25 | 25 |
26 #endif | 26 #endif |
27 | 27 |
28 UnicodeStringTest::~UnicodeStringTest() {} | 28 UnicodeStringTest::~UnicodeStringTest() {} |
29 | 29 |
30 void UnicodeStringTest::runIndexedTest( int32_t index, UBool exec, const char* &
name, char *par) | 30 void UnicodeStringTest::runIndexedTest( int32_t index, UBool exec, const char* &
name, char *par) |
31 { | 31 { |
32 if (exec) logln("TestSuite UnicodeStringTest: "); | 32 if (exec) logln("TestSuite UnicodeStringTest: "); |
33 switch (index) { | 33 TESTCASE_AUTO_BEGIN; |
34 case 0: | 34 TESTCASE_AUTO_CLASS(StringCaseTest); |
35 name = "StringCaseTest"; | 35 TESTCASE_AUTO(TestBasicManipulation); |
36 if (exec) { | 36 TESTCASE_AUTO(TestCompare); |
37 logln("StringCaseTest---"); logln(""); | 37 TESTCASE_AUTO(TestExtract); |
38 StringCaseTest test; | 38 TESTCASE_AUTO(TestRemoveReplace); |
39 callTest(test, par); | 39 TESTCASE_AUTO(TestSearching); |
40 } | 40 TESTCASE_AUTO(TestSpacePadding); |
41 break; | 41 TESTCASE_AUTO(TestPrefixAndSuffix); |
42 case 1: name = "TestBasicManipulation"; if (exec) TestBasicManipulation(
); break; | 42 TESTCASE_AUTO(TestFindAndReplace); |
43 case 2: name = "TestCompare"; if (exec) TestCompare(); break; | 43 TESTCASE_AUTO(TestBogus); |
44 case 3: name = "TestExtract"; if (exec) TestExtract(); break; | 44 TESTCASE_AUTO(TestReverse); |
45 case 4: name = "TestRemoveReplace"; if (exec) TestRemoveReplace(); break
; | 45 TESTCASE_AUTO(TestMiscellaneous); |
46 case 5: name = "TestSearching"; if (exec) TestSearching(); break; | 46 TESTCASE_AUTO(TestStackAllocation); |
47 case 6: name = "TestSpacePadding"; if (exec) TestSpacePadding(); break; | 47 TESTCASE_AUTO(TestUnescape); |
48 case 7: name = "TestPrefixAndSuffix"; if (exec) TestPrefixAndSuffix(); b
reak; | 48 TESTCASE_AUTO(TestCountChar32); |
49 case 8: name = "TestFindAndReplace"; if (exec) TestFindAndReplace(); bre
ak; | 49 TESTCASE_AUTO(TestStringEnumeration); |
50 case 9: name = "TestBogus"; if (exec) TestBogus(); break; | 50 TESTCASE_AUTO(TestNameSpace); |
51 case 10: name = "TestReverse"; if (exec) TestReverse(); break; | 51 TESTCASE_AUTO(TestUTF32); |
52 case 11: name = "TestMiscellaneous"; if (exec) TestMiscellaneous(); brea
k; | 52 TESTCASE_AUTO(TestUTF8); |
53 case 12: name = "TestStackAllocation"; if (exec) TestStackAllocation();
break; | 53 TESTCASE_AUTO(TestReadOnlyAlias); |
54 case 13: name = "TestUnescape"; if (exec) TestUnescape(); break; | 54 TESTCASE_AUTO(TestAppendable); |
55 case 14: name = "TestCountChar32"; if (exec) TestCountChar32(); break; | 55 TESTCASE_AUTO(TestUnicodeStringImplementsAppendable); |
56 case 15: name = "TestStringEnumeration"; if (exec) TestStringEnumeration
(); break; | 56 TESTCASE_AUTO(TestSizeofUnicodeString); |
57 case 16: name = "TestNameSpace"; if (exec) TestNameSpace(); break; | 57 TESTCASE_AUTO(TestStartsWithAndEndsWithNulTerminated); |
58 case 17: name = "TestUTF32"; if (exec) TestUTF32(); break; | 58 TESTCASE_AUTO(TestMoveSwap); |
59 case 18: name = "TestUTF8"; if (exec) TestUTF8(); break; | 59 TESTCASE_AUTO_END; |
60 case 19: name = "TestReadOnlyAlias"; if (exec) TestReadOnlyAlias(); brea
k; | |
61 case 20: name = "TestAppendable"; if (exec) TestAppendable(); break; | |
62 case 21: name = "TestUnicodeStringImplementsAppendable"; if (exec) TestU
nicodeStringImplementsAppendable(); break; | |
63 case 22: name = "TestSizeofUnicodeString"; if (exec) TestSizeofUnicodeSt
ring(); break; | |
64 case 23: name = "TestStartsWithAndEndsWithNulTerminated"; if (exec) Test
StartsWithAndEndsWithNulTerminated(); break; | |
65 | |
66 default: name = ""; break; //needed to end loop | |
67 } | |
68 } | 60 } |
69 | 61 |
70 void | 62 void |
71 UnicodeStringTest::TestBasicManipulation() | 63 UnicodeStringTest::TestBasicManipulation() |
72 { | 64 { |
73 UnicodeString test1("Now is the time for all men to come swiftly to the ai
d of the party.\n"); | 65 UnicodeString test1("Now is the time for all men to come swiftly to the ai
d of the party.\n"); |
74 UnicodeString expectedValue; | 66 UnicodeString expectedValue; |
75 UnicodeString *c; | 67 UnicodeString *c; |
76 | 68 |
77 c=(UnicodeString *)test1.clone(); | 69 c=(UnicodeString *)test1.clone(); |
(...skipping 2013 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2091 void | 2083 void |
2092 UnicodeStringTest::TestUnicodeStringImplementsAppendable() { | 2084 UnicodeStringTest::TestUnicodeStringImplementsAppendable() { |
2093 UnicodeString dest; | 2085 UnicodeString dest; |
2094 UnicodeStringAppendable app(dest); | 2086 UnicodeStringAppendable app(dest); |
2095 doTestAppendable(dest, app); | 2087 doTestAppendable(dest, app); |
2096 } | 2088 } |
2097 | 2089 |
2098 void | 2090 void |
2099 UnicodeStringTest::TestSizeofUnicodeString() { | 2091 UnicodeStringTest::TestSizeofUnicodeString() { |
2100 // See the comments in unistr.h near the declaration of UnicodeString's fiel
ds. | 2092 // See the comments in unistr.h near the declaration of UnicodeString's fiel
ds. |
| 2093 // See the API comments for UNISTR_OBJECT_SIZE. |
2101 size_t sizeofUniStr=sizeof(UnicodeString); | 2094 size_t sizeofUniStr=sizeof(UnicodeString); |
2102 size_t expected; | 2095 size_t expected=UNISTR_OBJECT_SIZE; |
2103 switch(sizeof(void *)) { | 2096 if(expected!=sizeofUniStr) { |
2104 case 4: | 2097 // Possible cause: UNISTR_OBJECT_SIZE may not be a multiple of sizeof(po
inter), |
2105 expected=32; | 2098 // of the compiler might add more internal padding than expected. |
2106 break; | 2099 errln("sizeof(UnicodeString)=%d, expected UNISTR_OBJECT_SIZE=%d", |
2107 case 8: | 2100 (int)sizeofUniStr, (int)expected); |
2108 expected=40; | |
2109 break; | |
2110 default: | |
2111 logln("This platform has neither 32-bit nor 64-bit pointers."); | |
2112 return; | |
2113 } | 2101 } |
2114 if(expected!=sizeofUniStr) { | 2102 if(sizeofUniStr<32) { |
2115 errln("sizeof(UnicodeString)=%d, expected %d", (int)sizeofUniStr, (int)e
xpected); | 2103 errln("sizeof(UnicodeString)=%d < 32, probably too small", (int)sizeofUn
iStr); |
| 2104 } |
| 2105 // We assume that the entire UnicodeString object, |
| 2106 // minus the vtable pointer and 2 bytes for flags and short length, |
| 2107 // is available for internal storage of UChars. |
| 2108 int32_t expectedStackBufferLength=((int32_t)UNISTR_OBJECT_SIZE-sizeof(void *
)-2)/U_SIZEOF_UCHAR; |
| 2109 UnicodeString s; |
| 2110 const UChar *emptyBuffer=s.getBuffer(); |
| 2111 for(int32_t i=0; i<expectedStackBufferLength; ++i) { |
| 2112 s.append((UChar)0x2e); |
| 2113 } |
| 2114 const UChar *fullBuffer=s.getBuffer(); |
| 2115 if(fullBuffer!=emptyBuffer) { |
| 2116 errln("unexpected reallocation when filling with assumed stack buffer si
ze of %d", |
| 2117 expectedStackBufferLength); |
| 2118 } |
| 2119 const UChar *terminatedBuffer=s.getTerminatedBuffer(); |
| 2120 if(terminatedBuffer==emptyBuffer) { |
| 2121 errln("unexpected keeping stack buffer when overfilling assumed stack bu
ffer size of %d", |
| 2122 expectedStackBufferLength); |
2116 } | 2123 } |
2117 } | 2124 } |
| 2125 |
| 2126 void |
| 2127 UnicodeStringTest::TestMoveSwap() { |
| 2128 static const UChar abc[3] = { 0x61, 0x62, 0x63 }; // "abc" |
| 2129 UnicodeString s1(FALSE, abc, UPRV_LENGTHOF(abc)); // read-only alias |
| 2130 UnicodeString s2(100, 0x7a, 100); // 100 * 'z' should be on the heap |
| 2131 UnicodeString s3("defg", 4, US_INV); // in stack buffer |
| 2132 const UChar *p = s2.getBuffer(); |
| 2133 s1.swap(s2); |
| 2134 if(s1.getBuffer() != p || s1.length() != 100 || s2.getBuffer() != abc || s2.
length() != 3) { |
| 2135 errln("UnicodeString.swap() did not swap"); |
| 2136 } |
| 2137 swap(s2, s3); |
| 2138 if(s2 != UNICODE_STRING_SIMPLE("defg") || s3.getBuffer() != abc || s3.length
() != 3) { |
| 2139 errln("swap(UnicodeString) did not swap back"); |
| 2140 } |
| 2141 UnicodeString s4; |
| 2142 s4.moveFrom(s1); |
| 2143 if(s4.getBuffer() != p || s4.length() != 100 || !s1.isBogus()) { |
| 2144 errln("UnicodeString.moveFrom(heap) did not move"); |
| 2145 } |
| 2146 UnicodeString s5; |
| 2147 s5.moveFrom(s2); |
| 2148 if(s5 != UNICODE_STRING_SIMPLE("defg")) { |
| 2149 errln("UnicodeString.moveFrom(stack) did not move"); |
| 2150 } |
| 2151 UnicodeString s6; |
| 2152 s6.moveFrom(s3); |
| 2153 if(s6.getBuffer() != abc || s6.length() != 3) { |
| 2154 errln("UnicodeString.moveFrom(alias) did not move"); |
| 2155 } |
| 2156 #if U_HAVE_RVALUE_REFERENCES |
| 2157 infoln("TestMoveSwap() with rvalue references"); |
| 2158 s1 = static_cast<UnicodeString &&>(s6); |
| 2159 if(s1.getBuffer() != abc || s1.length() != 3) { |
| 2160 errln("UnicodeString move assignment operator did not move"); |
| 2161 } |
| 2162 UnicodeString s7(static_cast<UnicodeString &&>(s4)); |
| 2163 if(s7.getBuffer() != p || s7.length() != 100 || !s4.isBogus()) { |
| 2164 errln("UnicodeString move constructor did not move"); |
| 2165 } |
| 2166 #else |
| 2167 infoln("TestMoveSwap() without rvalue references"); |
| 2168 UnicodeString s7; |
| 2169 #endif |
| 2170 |
| 2171 // Move self assignment leaves the object valid but in an undefined state. |
| 2172 // Do it to make sure there is no crash, |
| 2173 // but do not check for any particular resulting value. |
| 2174 s1.moveFrom(s1); |
| 2175 s2.moveFrom(s2); |
| 2176 s3.moveFrom(s3); |
| 2177 s4.moveFrom(s4); |
| 2178 s5.moveFrom(s5); |
| 2179 s6.moveFrom(s6); |
| 2180 s7.moveFrom(s7); |
| 2181 // Simple copy assignment must work. |
| 2182 UnicodeString simple = UNICODE_STRING_SIMPLE("simple"); |
| 2183 s1 = s6 = s4 = s7 = simple; |
| 2184 if(s1 != simple || s4 != simple || s6 != simple || s7 != simple) { |
| 2185 errln("UnicodeString copy after self-move did not work"); |
| 2186 } |
| 2187 } |
OLD | NEW |