|
| 1 | +//===--- BridgedStringSpanTests.swift -------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +// RUN: %target-run-stdlib-swift -enable-experimental-feature LifetimeDependence |
| 14 | + |
| 15 | +// REQUIRES: executable_test |
| 16 | +// REQUIRES: objc_interop |
| 17 | +// REQUIRES: swift_feature_LifetimeDependence |
| 18 | + |
| 19 | +import StdlibUnittest |
| 20 | + |
| 21 | +import Foundation |
| 22 | + |
| 23 | +var suite = TestSuite("EagerLazyBridging String Tests") |
| 24 | +defer { runAllTests() } |
| 25 | + |
| 26 | +let strings = [ |
| 27 | + "Hello, World!", |
| 28 | + "A long ASCII string exceeding 16 code units.", |
| 29 | + "π―π΅", |
| 30 | + "πββ
ββοΈβοΈβοΈ", |
| 31 | +] |
| 32 | + |
| 33 | +strings.forEach { expected in |
| 34 | + suite.test("Span from Bridged String Stability: \(expected)") |
| 35 | + .require(.stdlib_6_2).code { |
| 36 | + guard #available(SwiftStdlib 6.2, *) else { return } |
| 37 | + |
| 38 | + let string = NSString(utf8String: expected) |
| 39 | + expectNotNil(string) |
| 40 | + guard let string = NSString(utf8String: expected) else { return } |
| 41 | + |
| 42 | + let bridged = String(string).utf8 |
| 43 | + var p: ObjectIdentifier? = nil |
| 44 | + for (i, j) in zip(0..<3, bridged.indices) { |
| 45 | + let span = bridged.span |
| 46 | + let c = span.withUnsafeBufferPointer { |
| 47 | + let o = unsafeBitCast($0.baseAddress, to: ObjectIdentifier.self) |
| 48 | + if p == nil { |
| 49 | + p = o |
| 50 | + } else { |
| 51 | + expectEqual(p, o) |
| 52 | + } |
| 53 | + return $0[i] |
| 54 | + } |
| 55 | + expectEqual(c, bridged[j]) |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +strings.forEach { expected in |
| 61 | + suite.test("Span from Bridged String: \(expected)") |
| 62 | + .require(.stdlib_6_2).code { |
| 63 | + guard #available(SwiftStdlib 6.2, *) else { return } |
| 64 | + |
| 65 | + let string = NSString(utf8String: expected) |
| 66 | + expectNotNil(string) |
| 67 | + guard let string = NSString(utf8String: expected) else { return } |
| 68 | + |
| 69 | + let bridged = String(string) |
| 70 | + let utf8 = bridged.utf8 |
| 71 | + let span = utf8.span |
| 72 | + expectEqual(span.count, expected.utf8.count) |
| 73 | + for (i,j) in zip(span.indices, expected.utf8.indices) { |
| 74 | + expectEqual(span[i], expected.utf8[j]) |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +strings.forEach { expected in |
| 80 | + suite.test("Span from Bridged String Substring: \(expected)") |
| 81 | + .require(.stdlib_6_2).code { |
| 82 | + guard #available(SwiftStdlib 6.2, *) else { return } |
| 83 | + |
| 84 | + let string = NSString(utf8String: expected) |
| 85 | + expectNotNil(string) |
| 86 | + guard let string = NSString(utf8String: expected) else { return } |
| 87 | + |
| 88 | + let bridged = String(string).dropFirst() |
| 89 | + let utf8 = bridged.utf8 |
| 90 | + let span = utf8.span |
| 91 | + let expected = expected.dropFirst() |
| 92 | + expectEqual(span.count, expected.utf8.count) |
| 93 | + for (i,j) in zip(span.indices, expected.utf8.indices) { |
| 94 | + expectEqual(span[i], expected.utf8[j]) |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments