@@ -84,6 +84,26 @@ StdStringOverlayTestSuite.test("std::u16string operators") {
8484 expectTrue ( s1 == " something123literal " )
8585}
8686
87+ StdStringOverlayTestSuite . test ( " std::u32string operators " ) {
88+ var s1 = std. u32string ( " something " )
89+ let s2 = std. u32string ( " 123 " )
90+ let sum = s1 + s2
91+ expectEqual ( sum, std. u32string ( " something123 " ) )
92+
93+ expectFalse ( s1 == s2)
94+ let s3 = std. u32string ( " something123 " )
95+ expectFalse ( s1 == s3)
96+ expectFalse ( s2 == s3)
97+
98+ s1 += s2
99+ expectTrue ( s1 == std. u32string ( " something123 " ) )
100+ expectTrue ( s1 == s3)
101+
102+ // Make sure the operators work together with ExpressibleByStringLiteral conformance.
103+ s1 += " literal "
104+ expectTrue ( s1 == " something123literal " )
105+ }
106+
87107StdStringOverlayTestSuite . test ( " std::string::append " ) {
88108 var s1 = std. string ( " 0123 " )
89109 let s2 = std. string ( " abc " )
@@ -98,6 +118,13 @@ StdStringOverlayTestSuite.test("std::u16string::append") {
98118 expectEqual ( s1, std. u16string ( " 0123abc " ) )
99119}
100120
121+ StdStringOverlayTestSuite . test ( " std::u32string::append " ) {
122+ var s1 = std. u32string ( " 0123 " )
123+ let s2 = std. u32string ( " abc " )
124+ s1. append ( s2)
125+ expectEqual ( s1, std. u32string ( " 0123abc " ) )
126+ }
127+
101128StdStringOverlayTestSuite . test ( " std::string as Hashable " ) {
102129 let s0 = std. string ( )
103130 let h0 = s0. hashValue
@@ -140,6 +167,27 @@ StdStringOverlayTestSuite.test("std::u16string as Hashable") {
140167 expectNotEqual ( h2, h3)
141168}
142169
170+ StdStringOverlayTestSuite . test ( " std::u32string as Hashable " ) {
171+ let s0 = std. u32string ( )
172+ let h0 = s0. hashValue
173+
174+ let s1 = std. u32string ( " something " )
175+ let h1 = s1. hashValue
176+
177+ let s2 = std. u32string ( " something123 " )
178+ let h2 = s2. hashValue
179+
180+ let s3 = std. u32string ( " something " )
181+ let h3 = s3. hashValue
182+
183+ expectEqual ( h1, h3)
184+ expectNotEqual ( h0, h1)
185+ expectNotEqual ( h0, h2)
186+ expectNotEqual ( h0, h3)
187+ expectNotEqual ( h1, h2)
188+ expectNotEqual ( h2, h3)
189+ }
190+
143191StdStringOverlayTestSuite . test ( " std::u16string <=> Swift.String " ) {
144192 let cxx1 = std. u16string ( )
145193 let swift1 = String ( cxx1)
@@ -170,6 +218,36 @@ StdStringOverlayTestSuite.test("std::u16string <=> Swift.String") {
170218 expectEqual ( swift6, " xyz \0 abc " )
171219}
172220
221+ StdStringOverlayTestSuite . test ( " std::u32string <=> Swift.String " ) {
222+ let cxx1 = std. u32string ( )
223+ let swift1 = String ( cxx1)
224+ expectEqual ( swift1, " " )
225+
226+ let cxx2 = std. u32string ( " something123 " )
227+ expectEqual ( cxx2. size ( ) , 12 )
228+ let swift2 = String ( cxx2)
229+ expectEqual ( swift2, " something123 " )
230+
231+ let cxx3 : std . u32string = " literal "
232+ expectEqual ( cxx3. size ( ) , 7 )
233+
234+ let cxx4 : std . u32string = " тест "
235+ expectEqual ( cxx4. size ( ) , 4 )
236+ let swift4 = String ( cxx4)
237+ expectEqual ( swift4, " тест " )
238+
239+ // Emojis are represented by more than one CWideChar.
240+ let cxx5 : std . u32string = " emoji_🤖 "
241+ expectEqual ( cxx5. size ( ) , 7 )
242+ let swift5 = String ( cxx5)
243+ expectEqual ( swift5, " emoji_🤖 " )
244+
245+ let cxx6 = std. u32string ( " xyz \0 abc " )
246+ expectEqual ( cxx6. size ( ) , 7 )
247+ let swift6 = String ( cxx6)
248+ expectEqual ( swift6, " xyz \0 abc " )
249+ }
250+
173251StdStringOverlayTestSuite . test ( " std::string as Swift.CustomDebugStringConvertible " ) {
174252 let cxx1 = std. string ( )
175253 expectEqual ( cxx1. debugDescription, " std.string() " )
@@ -200,6 +278,23 @@ StdStringOverlayTestSuite.test("std::u16string as Swift.CustomDebugStringConvert
200278 expectEqual ( cxx3. debugDescription, " std.u16string(a�c) " )
201279}
202280
281+ StdStringOverlayTestSuite . test ( " std::u32string as Swift.CustomDebugStringConvertible " ) {
282+ let cxx1 = std. u32string ( )
283+ expectEqual ( cxx1. debugDescription, " std.u32string() " )
284+
285+ let cxx2 = std. u32string ( " something123 " )
286+ expectEqual ( cxx2. debugDescription, " std.u32string(something123) " )
287+
288+ // Since std::u32string does not support pushing back UInt32 directly, we utilize UInt16 instead.
289+ let scalars : [ UInt16 ] = [ 97 , 55296 , 99 ]
290+ var cxx3_16 = std. u16string ( )
291+ for scalar : UInt16 in scalars {
292+ cxx3_16. push_back ( scalar)
293+ }
294+ let cxx3 = std. u32string ( String ( cxx3_16) )
295+ expectEqual ( cxx3. debugDescription, " std.u32string(a�c) " )
296+ }
297+
203298StdStringOverlayTestSuite . test ( " std::string as Swift.Sequence " ) {
204299 let cxx1 = std. string ( )
205300 var iterated = false
@@ -249,6 +344,32 @@ StdStringOverlayTestSuite.test("std::u16string as Swift.CustomStringConvertible"
249344 expectEqual ( cxx3. description, " a�c " )
250345}
251346
347+ StdStringOverlayTestSuite . test ( " std::u32string as Swift.CustomStringConvertible " ) {
348+ let cxx1 = std. u32string ( )
349+ expectEqual ( cxx1. description, " " )
350+
351+ let cxx2 = std. u32string ( " something123 " )
352+ expectEqual ( cxx2. description, " something123 " )
353+
354+ // Since std::u32string does not support pushing back UInt32 directly, we utilize UInt16 instead.
355+ let scalars : [ UInt16 ] = [ 97 , 55296 , 99 ]
356+ var cxx3_16 = std. u16string ( )
357+ for scalar : UInt16 in scalars {
358+ cxx3_16. push_back ( scalar)
359+ }
360+ let cxx3 = std. u32string ( String ( cxx3_16) )
361+ expectEqual ( cxx3. description, " a�c " )
362+
363+ // For `push_back`
364+ let scalars2 : [ Unicode . Scalar ] = [ 0x48 , 0x65 , 0x6C , 0x6C , 0x6F , 0x2C , 0x20 , 0x4E16 , 0x754C ]
365+ . compactMap { Unicode . Scalar ( $0) }
366+ var cxx4 = std. u32string ( )
367+ for scalar : Unicode . Scalar in scalars2 {
368+ cxx4. push_back ( scalar)
369+ }
370+ expectEqual ( cxx4. description, " Hello, 世界 " )
371+ }
372+
252373StdStringOverlayTestSuite . test ( " std::string from C string " ) {
253374 let str = " abc " . withCString { ptr in
254375 std. string ( ptr)
0 commit comments