|
| 1 | +// RUN: %target-run-simple-swift |
| 2 | +// REQUIRES: executable_test |
| 3 | + |
| 4 | +// REQUIRES: objc_interop |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import StdlibUnittest |
| 8 | + |
| 9 | +var StringBridgeTests = TestSuite("StringBridgeTests") |
| 10 | + |
| 11 | +extension String { |
| 12 | + init(fromCocoa s: String) { |
| 13 | + self = (s as NSString) as String |
| 14 | + } |
| 15 | + |
| 16 | + |
| 17 | +} |
| 18 | + |
| 19 | +func expectSmall(_ str: String, |
| 20 | + stackTrace: SourceLocStack = SourceLocStack(), |
| 21 | + showFrame: Bool = true, |
| 22 | + file: String = #file, line: UInt = #line |
| 23 | + ) { |
| 24 | + expectTrue(str._guts._isSmall, |
| 25 | + stackTrace: stackTrace, showFrame: showFrame, file: file, line: line) |
| 26 | +} |
| 27 | +func expectCocoa(_ str: String, |
| 28 | + stackTrace: SourceLocStack = SourceLocStack(), |
| 29 | + showFrame: Bool = true, |
| 30 | + file: String = #file, line: UInt = #line |
| 31 | + ) { |
| 32 | + expectTrue(str._guts._isCocoa, |
| 33 | + stackTrace: stackTrace, showFrame: showFrame, file: file, line: line) |
| 34 | +} |
| 35 | + |
| 36 | +StringBridgeTests.test("Tagged NSString") { |
| 37 | +#if arch(i386) || arch(arm) |
| 38 | +#else |
| 39 | + // Bridge tagged strings as small |
| 40 | + expectSmall((("0123456" as NSString) as String)) |
| 41 | + expectSmall((("012345678" as NSString) as String)) |
| 42 | + expectSmall((("aaaaaaaaaaa" as NSString) as String)) |
| 43 | + expectSmall((("bbbbbbbbb" as NSString) as String)) |
| 44 | + |
| 45 | + // Bridge non-tagged as non-small even if they fit, for fear of losing |
| 46 | + // associated information |
| 47 | + let bigAs = ("aaaaaaaaaaaa" as NSString) as String |
| 48 | + let bigBs = ("bbbbbbbbbb" as NSString) as String |
| 49 | + let bigQs = ("????????" as NSString) as String |
| 50 | + expectCocoa(bigAs) |
| 51 | + expectCocoa(bigBs) |
| 52 | + expectCocoa(bigQs) |
| 53 | + |
| 54 | +#if false // TODO(SR-7594): re-enable |
| 55 | + let littleAsNSString = ("aa" as NSString) |
| 56 | + var littleAs = littleAsNSString as String |
| 57 | + |
| 58 | + // But become small when appended to |
| 59 | + expectSmall(bigAs + "c") |
| 60 | + expectSmall(bigBs + "c") |
| 61 | + expectSmall("a\(bigAs)") |
| 62 | + expectSmall("a\(bigBs)") |
| 63 | + expectSmall(littleAs + bigQs) |
| 64 | + expectSmall(bigQs + littleAs) |
| 65 | + expectSmall("\(littleAs)bigQs\(littleAs)") |
| 66 | +#endif // false |
| 67 | + |
| 68 | +#endif // not 32bit |
| 69 | +} |
| 70 | + |
| 71 | +runAllTests() |
| 72 | + |
0 commit comments