|
| 1 | +// RUN: %target-run-simple-swift |
| 2 | +// REQUIRES: executable_test |
| 3 | + |
| 4 | +import StdlibUnittest |
| 5 | +defer { runAllTests() } |
| 6 | + |
| 7 | +var StringCreateTests = TestSuite("StringCreateTests") |
| 8 | + |
| 9 | +enum SimpleString: String { |
| 10 | + case smallASCII = "abcdefg" |
| 11 | + case smallUnicode = "abéÏ𓀀" |
| 12 | + case largeASCII = "012345678901234567890" |
| 13 | + case largeUnicode = "abéÏ012345678901234567890𓀀" |
| 14 | + case emoji = "😀😃🤢🤮👩🏿🎤🧛🏻♂️🧛🏻♂️👩👩👦👦" |
| 15 | +} |
| 16 | + |
| 17 | +let simpleStrings: [String] = [ |
| 18 | + SimpleString.smallASCII.rawValue, |
| 19 | + SimpleString.smallUnicode.rawValue, |
| 20 | + SimpleString.largeASCII.rawValue, |
| 21 | + SimpleString.largeUnicode.rawValue, |
| 22 | + SimpleString.emoji.rawValue, |
| 23 | + "", |
| 24 | +] |
| 25 | + |
| 26 | +extension String { |
| 27 | + var utf32: [UInt32] { return unicodeScalars.map { $0.value } } |
| 28 | +} |
| 29 | + |
| 30 | +StringCreateTests.test("String(decoding:as)") { |
| 31 | + func validateDecodingAs(_ str: String) { |
| 32 | + // Non-contiguous (maybe) storage |
| 33 | + expectEqual(str, String(decoding: str.utf8, as: UTF8.self)) |
| 34 | + expectEqual(str, String(decoding: str.utf16, as: UTF16.self)) |
| 35 | + expectEqual(str, String(decoding: str.utf32, as: UTF32.self)) |
| 36 | + |
| 37 | + // Contiguous storage |
| 38 | + expectEqual(str, String(decoding: Array(str.utf8), as: UTF8.self)) |
| 39 | + expectEqual(str, String(decoding: Array(str.utf16), as: UTF16.self)) |
| 40 | + expectEqual(str, String(decoding: Array(str.utf32), as: UTF32.self)) |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + for str in simpleStrings { |
| 45 | + validateDecodingAs(str) |
| 46 | + } |
| 47 | + |
| 48 | + // Corner-case: UBP with null pointer (https://bugs.swift.org/browse/SR-9869) |
| 49 | + expectEqual( |
| 50 | + "", String(decoding: UnsafeBufferPointer(_empty: ()), as: UTF8.self)) |
| 51 | + expectEqual( |
| 52 | + "", String(decoding: UnsafeBufferPointer(_empty: ()), as: UTF16.self)) |
| 53 | + expectEqual( |
| 54 | + "", String(decoding: UnsafeBufferPointer(_empty: ()), as: UTF32.self)) |
| 55 | +} |
| 56 | + |
0 commit comments