//===--- RawSpanTests.swift -----------------------------------------------===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2024 - 2025 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// // RUN: %target-run-stdlib-swift // REQUIRES: executable_test import StdlibUnittest var suite = TestSuite("Span Tests") defer { runAllTests() } suite.test("Initialize with Span") .skip(.custom( { if #available(SwiftStdlib 6.2, *) { false } else { true } }, reason: "Requires Swift 6.2's standard library" )) .code { guard #available(SwiftStdlib 6.2, *) else { return } let capacity = 4 Array(0...stride) expectFalse(span.isEmpty) span = RawSpan() expectTrue(span.isEmpty) } let a: [Int] = [] a.withUnsafeBufferPointer { let intSpan = Span(_unsafeElements: $0) let span = RawSpan(_elements: intSpan) expectTrue(span.isEmpty) } } suite.test("Initialize with UnsafeRawBufferPointer") .skip(.custom( { if #available(SwiftStdlib 6.2, *) { false } else { true } }, reason: "Requires Swift 6.2's standard library" )) .code { guard #available(SwiftStdlib 6.2, *) else { return } let capacity = 4 var a = Array(0...stride) } a.withUnsafeMutableBytes { let span = RawSpan(_unsafeBytes: $0) expectEqual(span.byteCount, capacity*MemoryLayout.stride) } } suite.test("Initialize with UnsafeRawPointer") .skip(.custom( { if #available(SwiftStdlib 6.2, *) { false } else { true } }, reason: "Requires Swift 6.2's standard library" )) .code { guard #available(SwiftStdlib 6.2, *) else { return } let capacity = 4 var a = Array(0...stride ) expectEqual(span.byteCount, $0.count) } a.withUnsafeMutableBytes { let pointer = $0.baseAddress! let span = RawSpan( _unsafeStart: pointer, byteCount: capacity*MemoryLayout.stride ) expectEqual(span.byteCount, $0.count) } } suite.test("unsafeLoad(as:)") .skip(.custom( { if #available(SwiftStdlib 6.2, *) { false } else { true } }, reason: "Requires Swift 6.2's standard library" )) .code { guard #available(SwiftStdlib 6.2, *) else { return } let capacity = 4 let s = (0...stride let s0 = span.unsafeLoad(as: String.self) expectEqual(s0.contains("0"), true) let s1 = span.unsafeLoad(fromByteOffset: stride, as: String.self) expectEqual(s1.contains("1"), true) let s2 = span.unsafeLoad(fromUncheckedByteOffset: 2*stride, as: String.self) expectEqual(s2.contains("2"), true) } } suite.test("unsafeLoadUnaligned(as:)") .skip(.custom( { if #available(SwiftStdlib 6.2, *) { false } else { true } }, reason: "Requires Swift 6.2's standard library" )) .code { guard #available(SwiftStdlib 6.2, *) else { return } let capacity = 64 let a = Array(0..? bounds = span.byteOffsets(of: subSpan1) expectEqual(bounds, span.byteOffsets.prefix(6)) bounds = span.byteOffsets(of: subSpan2) expectEqual(bounds, span.byteOffsets.suffix(6)) bounds = subSpan2.byteOffsets(of: subSpan1) expectNil(bounds) bounds = subSpan1.byteOffsets(of: subSpan2) expectNil(bounds) bounds = subSpan2.byteOffsets(of: span) expectNil(bounds) bounds = nilSpan.byteOffsets(of: emptySpan) expectNil(bounds) bounds = span.byteOffsets(of: nilSpan) expectNil(bounds) bounds = nilSpan.byteOffsets(of: nilSpan) expectEqual(bounds, 0..<0) }