1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -D USE_CUSTOM_STRING_API)
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=swift-6 -D USE_CUSTOM_STRING_API)
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift -D USE_CUSTOM_STRING_API)
//
// REQUIRES: executable_test
import StdlibUnittest
import CxxStdlib
#if USE_CUSTOM_STRING_API
import StdString
#endif
var StdStringTestSuite = TestSuite("StdString")
StdStringTestSuite.test("init") {
let s = std.string()
expectEqual(s.size(), 0)
expectTrue(s.empty())
}
StdStringTestSuite.test("push back") {
var s = std.string()
s.push_back(42)
expectEqual(s.size(), 1)
expectFalse(s.empty())
expectEqual(s[0], 42)
}
StdStringTestSuite.test("std::string <=> Swift.String") {
let cxx1 = std.string()
let swift1 = String(cxx1)
expectEqual(swift1, "")
let cxx2 = std.string("something123")
let swift2 = String(cxx2)
expectEqual(swift2, "something123")
let cxx3: std.string = "literal"
expectEqual(cxx3.size(), 7)
// Non-ASCII characters are represented by more than one CChar.
let cxx4: std.string = "тест"
expectEqual(cxx4.size(), 8)
let swift4 = String(cxx4)
expectEqual(swift4, "тест")
let cxx5: std.string = "emoji_🤖"
expectEqual(cxx5.size(), 10)
let swift5 = String(cxx5)
expectEqual(swift5, "emoji_🤖")
let cxx6 = std.string("xyz\0abc")
expectEqual(cxx6.size(), 7)
let swift6 = String(cxx6)
expectEqual(swift6, "xyz\0abc")
var cxx7 = std.string()
let bytes: [UInt8] = [0xE1, 0xC1, 0xAC]
for byte in bytes {
cxx7.push_back(CChar(bitPattern: byte))
}
let swift7 = String(cxx7)
expectEqual(swift7, "���")
}
StdStringTestSuite.test("std::string operators") {
var s1 = std.string("something")
let s2 = std.string("123")
let sum = s1 + s2
expectEqual(sum, std.string("something123"))
expectFalse(s1 == s2)
let s3 = std.string("something123")
expectFalse(s1 == s3)
expectFalse(s2 == s3)
s1 += s2
expectTrue(s1 == std.string("something123"))
expectTrue(s1 == s3)
// Make sure the operators work together with ExpressibleByStringLiteral conformance.
s1 += "literal"
expectTrue(s1 == "something123literal")
}
StdStringTestSuite.test("std::u16string operators") {
var s1 = std.u16string("something")
let s2 = std.u16string("123")
let sum = s1 + s2
expectEqual(sum, std.u16string("something123"))
expectFalse(s1 == s2)
let s3 = std.u16string("something123")
expectFalse(s1 == s3)
expectFalse(s2 == s3)
s1 += s2
expectTrue(s1 == std.u16string("something123"))
expectTrue(s1 == s3)
// Make sure the operators work together with ExpressibleByStringLiteral conformance.
s1 += "literal"
expectTrue(s1 == "something123literal")
}
StdStringTestSuite.test("std::u32string operators") {
var s1 = std.u32string("something")
let s2 = std.u32string("123")
let sum = s1 + s2
expectEqual(sum, std.u32string("something123"))
expectFalse(s1 == s2)
let s3 = std.u32string("something123")
expectFalse(s1 == s3)
expectFalse(s2 == s3)
s1 += s2
expectTrue(s1 == std.u32string("something123"))
expectTrue(s1 == s3)
// Make sure the operators work together with ExpressibleByStringLiteral conformance.
s1 += "literal"
expectTrue(s1 == "something123literal")
}
StdStringTestSuite.test("std::string::append") {
var s1 = std.string("0123")
let s2 = std.string("abc")
s1.append(s2)
expectEqual(s1, std.string("0123abc"))
}
StdStringTestSuite.test("std::u16string::append") {
var s1 = std.u16string("0123")
let s2 = std.u16string("abc")
s1.append(s2)
expectEqual(s1, std.u16string("0123abc"))
}
StdStringTestSuite.test("std::u32string::append") {
var s1 = std.u32string("0123")
let s2 = std.u32string("abc")
s1.append(s2)
expectEqual(s1, std.u32string("0123abc"))
}
StdStringTestSuite.test("std::string as Hashable") {
let s0 = std.string()
let h0 = s0.hashValue
let s1 = std.string("something")
let h1 = s1.hashValue
let s2 = std.string("something123")
let h2 = s2.hashValue
let s3 = std.string("something")
let h3 = s3.hashValue
expectEqual(h1, h3)
expectNotEqual(h0, h1)
expectNotEqual(h0, h2)
expectNotEqual(h0, h3)
expectNotEqual(h1, h2)
expectNotEqual(h2, h3)
}
StdStringTestSuite.test("std::u16string as Hashable") {
let s0 = std.u16string()
let h0 = s0.hashValue
let s1 = std.u16string("something")
let h1 = s1.hashValue
let s2 = std.u16string("something123")
let h2 = s2.hashValue
let s3 = std.u16string("something")
let h3 = s3.hashValue
expectEqual(h1, h3)
expectNotEqual(h0, h1)
expectNotEqual(h0, h2)
expectNotEqual(h0, h3)
expectNotEqual(h1, h2)
expectNotEqual(h2, h3)
}
StdStringTestSuite.test("std::u32string as Hashable") {
let s0 = std.u32string()
let h0 = s0.hashValue
let s1 = std.u32string("something")
let h1 = s1.hashValue
let s2 = std.u32string("something123")
let h2 = s2.hashValue
let s3 = std.u32string("something")
let h3 = s3.hashValue
expectEqual(h1, h3)
expectNotEqual(h0, h1)
expectNotEqual(h0, h2)
expectNotEqual(h0, h3)
expectNotEqual(h1, h2)
expectNotEqual(h2, h3)
}
StdStringTestSuite.test("std::u16string <=> Swift.String") {
let cxx1 = std.u16string()
let swift1 = String(cxx1)
expectEqual(swift1, "")
let cxx2 = std.u16string("something123")
expectEqual(cxx2.size(), 12)
let swift2 = String(cxx2)
expectEqual(swift2, "something123")
let cxx3: std.u16string = "literal"
expectEqual(cxx3.size(), 7)
let cxx4: std.u16string = "тест"
expectEqual(cxx4.size(), 4)
let swift4 = String(cxx4)
expectEqual(swift4, "тест")
// Emojis are represented by more than one CWideChar.
let cxx5: std.u16string = "emoji_🤖"
expectEqual(cxx5.size(), 8)
let swift5 = String(cxx5)
expectEqual(swift5, "emoji_🤖")
let cxx6 = std.u16string("xyz\0abc")
expectEqual(cxx6.size(), 7)
let swift6 = String(cxx6)
expectEqual(swift6, "xyz\0abc")
}
StdStringTestSuite.test("std::u32string <=> Swift.String") {
let cxx1 = std.u32string()
let swift1 = String(cxx1)
expectEqual(swift1, "")
let cxx2 = std.u32string("something123")
expectEqual(cxx2.size(), 12)
let swift2 = String(cxx2)
expectEqual(swift2, "something123")
let cxx3: std.u32string = "literal"
expectEqual(cxx3.size(), 7)
let cxx4: std.u32string = "тест"
expectEqual(cxx4.size(), 4)
let swift4 = String(cxx4)
expectEqual(swift4, "тест")
// Emojis are represented by more than one CWideChar.
let cxx5: std.u32string = "emoji_🤖"
expectEqual(cxx5.size(), 7)
let swift5 = String(cxx5)
expectEqual(swift5, "emoji_🤖")
let cxx6 = std.u32string("xyz\0abc")
expectEqual(cxx6.size(), 7)
let swift6 = String(cxx6)
expectEqual(swift6, "xyz\0abc")
}
StdStringTestSuite.test("std::string as Swift.CustomDebugStringConvertible") {
let cxx1 = std.string()
expectEqual(cxx1.debugDescription, "std.string()")
let cxx2 = std.string("something123")
expectEqual(cxx2.debugDescription, "std.string(something123)")
let bytes: [UInt8] = [0xE1, 0xC1, 0xAC]
var cxx3 = std.string()
for byte in bytes {
cxx3.push_back(CChar(bitPattern: byte))
}
expectEqual(cxx3.debugDescription, "std.string(���)")
}
StdStringTestSuite.test("std::u16string as Swift.CustomDebugStringConvertible") {
let cxx1 = std.u16string()
expectEqual(cxx1.debugDescription, "std.u16string()")
let cxx2 = std.u16string("something123")
expectEqual(cxx2.debugDescription, "std.u16string(something123)")
let scalars: [UInt16] = [97, 55296, 99]
var cxx3 = std.u16string()
for scalar in scalars {
cxx3.push_back(scalar)
}
expectEqual(cxx3.debugDescription, "std.u16string(a�c)")
}
StdStringTestSuite.test("std::u32string as Swift.CustomDebugStringConvertible") {
let cxx1 = std.u32string()
expectEqual(cxx1.debugDescription, "std.u32string()")
let cxx2 = std.u32string("something123")
expectEqual(cxx2.debugDescription, "std.u32string(something123)")
// Since std::u32string does not support pushing back UInt32 directly, we utilize UInt16 instead.
let scalars: [UInt16] = [97, 55296, 99]
var cxx3_16 = std.u16string()
for scalar: UInt16 in scalars {
cxx3_16.push_back(scalar)
}
let cxx3 = std.u32string(String(cxx3_16))
expectEqual(cxx3.debugDescription, "std.u32string(a�c)")
}
StdStringTestSuite.test("std::string as Swift.Sequence") {
let cxx1 = std.string()
var iterated = false
for _ in cxx1 {
iterated = true
}
expectFalse(iterated)
let cxx2 = std.string("abc123")
var chars = 0
var sum = 0
for it in cxx2 {
chars += 1
sum += Int(it)
}
expectEqual(6, chars)
expectEqual(97 + 98 + 99 + 49 + 50 + 51, sum)
}
StdStringTestSuite.test("std::string as CustomStringConvertible") {
let cxx1 = std.string()
expectEqual(cxx1.description, "")
let cxx2 = std.string("something123")
expectEqual(cxx2.description, "something123")
let bytes: [UInt8] = [0xE1, 0xC1, 0xAC]
var cxx3 = std.string()
for byte in bytes {
cxx3.push_back(CChar(bitPattern: byte))
}
expectEqual(cxx3.description, "���")
}
StdStringTestSuite.test("std::u16string as Swift.CustomStringConvertible") {
let cxx1 = std.u16string()
expectEqual(cxx1.description, "")
let cxx2 = std.u16string("something123")
expectEqual(cxx2.description, "something123")
let scalars: [UInt16] = [97, 55296, 99]
var cxx3 = std.u16string()
for scalar in scalars {
cxx3.push_back(scalar)
}
expectEqual(cxx3.description, "a�c")
}
StdStringTestSuite.test("std::u32string as Swift.CustomStringConvertible") {
let cxx1 = std.u32string()
expectEqual(cxx1.description, "")
let cxx2 = std.u32string("something123")
expectEqual(cxx2.description, "something123")
// Since std::u32string does not support pushing back UInt32 directly, we utilize UInt16 instead.
let scalars: [UInt16] = [97, 55296, 99]
var cxx3_16 = std.u16string()
for scalar: UInt16 in scalars {
cxx3_16.push_back(scalar)
}
let cxx3 = std.u32string(String(cxx3_16))
expectEqual(cxx3.description, "a�c")
// For `push_back`
let scalars2: [Unicode.Scalar] = [0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x2C, 0x20, 0x4E16, 0x754C]
.compactMap { Unicode.Scalar($0) }
var cxx4 = std.u32string()
for scalar: Unicode.Scalar in scalars2 {
cxx4.push_back(scalar)
}
expectEqual(cxx4.description, "Hello, 世界")
}
StdStringTestSuite.test("std::string from C string") {
let str = "abc".withCString { ptr in
std.string(ptr)
}
expectEqual(str, std.string("abc"))
}
#if USE_CUSTOM_STRING_API
StdStringTestSuite.test("get from a method") {
let box = HasMethodThatReturnsString()
let str = box.getString()
expectEqual(str.size(), 3)
expectEqual(str, std.string("111"))
}
StdStringTestSuite.test("pass as an argument") {
let s = std.string("a")
let res = takesStringWithDefaultArg(s)
expectEqual(res.size(), 1)
expectEqual(res[0], 97)
}
StdStringTestSuite.test("pass as a default argument") {
let res = takesStringWithDefaultArg()
expectEqual(res.size(), 3)
expectEqual(res[0], 97)
expectEqual(res[1], 98)
expectEqual(res[2], 99)
}
#endif
runAllTests()
|