|
| 1 | +// RUN: %target-run-simple-swift |
| 2 | +// REQUIRES: executable_test |
| 3 | + |
| 4 | +import StdlibUnittest |
| 5 | +defer { runAllTests() } |
| 6 | + |
| 7 | +var StringDeconstructTests = TestSuite("StringDeconstructTests") |
| 8 | + |
| 9 | +enum ExpectedDeconstruction { |
| 10 | + case scratchIfAvailable |
| 11 | + case interiorPointer |
| 12 | + case extraAllocation |
| 13 | +} |
| 14 | + |
| 15 | +func expectDeconstruct( |
| 16 | + _ str: String, |
| 17 | + _ expectDeconstruct: ExpectedDeconstruction, |
| 18 | + stackTrace: SourceLocStack = SourceLocStack(), |
| 19 | + showFrame: Bool = true, |
| 20 | + file: String = #file, line: UInt = #line |
| 21 | +) { |
| 22 | + var stackTrace = stackTrace.pushIf(showFrame, file: file, line: line) |
| 23 | + let expectBytes = Array(str.utf8) |
| 24 | + |
| 25 | + _ = Array<UInt8>(unsafeUninitializedCapacity: 16) { |
| 26 | + buffer, initializedCount in |
| 27 | + // Deconstruct with a provided scratch space |
| 28 | + |
| 29 | + // WS == with scratch, N == nil |
| 30 | + let scratch = UnsafeMutableRawBufferPointer(buffer) |
| 31 | + let (ownerWS, ptrWS, lengthWS, usedScratchWS, allocatedMemoryWS) |
| 32 | + : (AnyObject?, UnsafePointer<UInt8>, Int, Bool, Bool) |
| 33 | + = str._deconstructUTF8(scratch: scratch) |
| 34 | + let (ownerN, ptrN, lengthN, usedScratchN, allocatedMemoryN) |
| 35 | + : (AnyObject?, UnsafePointer<UInt8>, Int, Bool, Bool) |
| 36 | + = str._deconstructUTF8(scratch: nil) |
| 37 | + |
| 38 | + let rawBytesWS = UnsafeRawBufferPointer(start: ptrWS, count: lengthWS) |
| 39 | + let rawBytesN = UnsafeRawBufferPointer(start: ptrN, count: lengthN) |
| 40 | + |
| 41 | + expectEqualSequence(expectBytes, rawBytesWS, stackTrace: stackTrace) |
| 42 | + expectEqualSequence(rawBytesWS, rawBytesN, stackTrace: stackTrace) |
| 43 | + |
| 44 | + switch expectDeconstruct { |
| 45 | + case .scratchIfAvailable: |
| 46 | + expectNil(ownerWS, stackTrace: stackTrace) |
| 47 | + expectNotNil(ownerN, stackTrace: stackTrace) |
| 48 | + |
| 49 | + expectEqual(scratch.baseAddress, rawBytesWS.baseAddress, |
| 50 | + stackTrace: stackTrace) |
| 51 | + expectNotEqual(scratch.baseAddress, rawBytesN.baseAddress, |
| 52 | + stackTrace: stackTrace) |
| 53 | + |
| 54 | + expectTrue(lengthWS < scratch.count, stackTrace: stackTrace) |
| 55 | + expectTrue(lengthN < scratch.count, stackTrace: stackTrace) |
| 56 | + |
| 57 | + expectTrue(usedScratchWS, stackTrace: stackTrace) |
| 58 | + expectFalse(usedScratchN, stackTrace: stackTrace) |
| 59 | + |
| 60 | + expectFalse(allocatedMemoryWS, stackTrace: stackTrace) |
| 61 | + expectTrue(allocatedMemoryN, stackTrace: stackTrace) |
| 62 | + |
| 63 | + case .interiorPointer: |
| 64 | + // TODO: owner == (immortal ? nil : StringObject.largeAddress) |
| 65 | + expectTrue(str.isContiguousUTF8, stackTrace: stackTrace) |
| 66 | + var copy = str |
| 67 | + copy.withUTF8 { |
| 68 | + expectEqual($0.baseAddress, ptrWS, stackTrace: stackTrace) |
| 69 | + expectEqual($0.baseAddress, ptrN, stackTrace: stackTrace) |
| 70 | + expectEqual($0.count, lengthWS, stackTrace: stackTrace) |
| 71 | + expectEqual($0.count, lengthN, stackTrace: stackTrace) |
| 72 | + } |
| 73 | + |
| 74 | + expectFalse(usedScratchWS, stackTrace: stackTrace) |
| 75 | + expectFalse(usedScratchN, stackTrace: stackTrace) |
| 76 | + expectFalse(allocatedMemoryWS, stackTrace: stackTrace) |
| 77 | + expectFalse(allocatedMemoryN, stackTrace: stackTrace) |
| 78 | + case .extraAllocation: |
| 79 | + expectFalse(str.isContiguousUTF8, stackTrace: stackTrace) |
| 80 | + expectNotNil(ownerWS, stackTrace: stackTrace) |
| 81 | + expectNotNil(ownerN, stackTrace: stackTrace) |
| 82 | + expectFalse(usedScratchWS, stackTrace: stackTrace) |
| 83 | + expectFalse(usedScratchN, stackTrace: stackTrace) |
| 84 | + expectTrue(allocatedMemoryWS, stackTrace: stackTrace) |
| 85 | + expectTrue(allocatedMemoryN, stackTrace: stackTrace) |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +@inline(never) |
| 91 | +func id<T>(_ a: T) -> T { a } |
| 92 | + |
| 93 | +StringDeconstructTests.test("deconstruct") { |
| 94 | + let smallASCII = "abcd" |
| 95 | + |
| 96 | +#if arch(i386) || arch(arm) || arch(wasm32) |
| 97 | + let smallUTF8 = "ジッパ" |
| 98 | +#else |
| 99 | + let smallUTF8 = "ジッパー" |
| 100 | +#endif |
| 101 | + |
| 102 | + let large = "the quick fox jumped over the lazy brown dog" |
| 103 | + |
| 104 | + var largeMortal = large |
| 105 | + largeMortal.append(id("🧟♀️")) |
| 106 | + largeMortal.append(id(largeMortal.last!)) |
| 107 | + |
| 108 | + expectDeconstruct(smallASCII, .scratchIfAvailable) |
| 109 | + expectDeconstruct(smallUTF8, .scratchIfAvailable) |
| 110 | + expectDeconstruct(large, .interiorPointer) |
| 111 | + expectDeconstruct(largeMortal, .interiorPointer) |
| 112 | +} |
| 113 | + |
| 114 | +#if _runtime(_ObjC) |
| 115 | +import Foundation |
| 116 | +StringDeconstructTests.test("deconstruct cocoa") { |
| 117 | + let smallCocoa: NSString = "aaa" |
| 118 | + let largeASCIICocoa: NSString = "the quick fox jumped over the lazy brown dog" |
| 119 | + let largeCocoa: NSString = "the quick 🧟♀️ ate the slow 🧠" |
| 120 | + |
| 121 | +#if arch(i386) || arch(arm) || arch(wasm32) |
| 122 | + expectDeconstruct(smallCocoa as String, .interiorPointer) |
| 123 | +#else |
| 124 | + expectDeconstruct(smallCocoa as String, .scratchIfAvailable) |
| 125 | +#endif |
| 126 | + |
| 127 | + expectDeconstruct(largeASCIICocoa as String, .interiorPointer) |
| 128 | + expectDeconstruct(largeCocoa as String, .extraAllocation) |
| 129 | +} |
| 130 | +#endif |
| 131 | + |
0 commit comments