From fd8eb146ec9cc00405192c05ee0ef74408c3e324 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Sun, 17 Aug 2025 13:19:14 -0700 Subject: [PATCH] [TestSupport] Add TestPrintable protocol --- .../Utilities/TestPrintable.swift | 126 ++++++++++++++++++ Xcode/Collections.xcodeproj/project.pbxproj | 4 + 2 files changed, 130 insertions(+) create mode 100644 Tests/_CollectionsTestSupport/Utilities/TestPrintable.swift diff --git a/Tests/_CollectionsTestSupport/Utilities/TestPrintable.swift b/Tests/_CollectionsTestSupport/Utilities/TestPrintable.swift new file mode 100644 index 00000000..a2ded08b --- /dev/null +++ b/Tests/_CollectionsTestSupport/Utilities/TestPrintable.swift @@ -0,0 +1,126 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 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 +// +//===----------------------------------------------------------------------===// + +#if COLLECTIONS_SINGLE_MODULE +import Collections +#else +import ContainersPreview +#endif + +#if compiler(>=6.2) +/// A silly variant of `CustomDebugStringConvertible` that supports noncopyable +/// and nonescapable conforming types. +/// +/// This is expected to be replaced by the stdlib's solution, once we have one. +/// (It can be as simple as generalizing the standard protocol, but for it serve +/// its purpose, we also need to be able to perform runtime conformance checks +/// on noncopyable/nonescapable entities (or otherwise detect and invoke this +/// property. Additionally, materializing a full `String` instance up front can +/// be too expensive in some environments -- we also need a solution that +/// streams descriptions into some caller-provided buffer, eliminating +/// memory overhead.) +public protocol TestPrintable: ~Copyable, ~Escapable { + var testDescription: String { get } +} + +extension _Pointer { + internal var _description: String { + let hex = String(UInt(bitPattern: self), radix: 16) + let len = MemoryLayout.size * 2 + return "0x" + String(repeating: "0", count: len - hex.count) + hex + } +} + +internal func _bufferDescription( + _ typename: String, start: P?, count: Int +) -> String { + "\(typename)(start: \(start?._description ?? "nil"), count: \(count))" +} + +extension UnsafeRawBufferPointer: TestPrintable { + public var testDescription: String { + _bufferDescription( + "UnsafeRawBufferPointer", start: baseAddress, count: count) + } +} +extension UnsafeMutableRawBufferPointer: TestPrintable { + public var testDescription: String { + _bufferDescription( + "UnsafeMutableRawBufferPointer", start: baseAddress, count: count) + } +} +extension UnsafeBufferPointer: TestPrintable +where Element: ~Copyable +{ + public var testDescription: String { + _bufferDescription("UnsafeBufferPointer", start: baseAddress, count: count) + } +} +extension UnsafeMutableBufferPointer: TestPrintable +where Element: ~Copyable +{ + public var testDescription: String { + _bufferDescription( + "UnsafeMutableBufferPointer", start: baseAddress, count: count) + } +} + +@available(SwiftStdlib 5.0, *) +extension Span: TestPrintable where Element: ~Copyable { + public var testDescription: String { + self.withUnsafeBufferPointer { buffer in + _bufferDescription("Span", start: buffer.baseAddress, count: buffer.count) + } + } +} +@available(SwiftStdlib 5.0, *) +extension MutableSpan: TestPrintable where Element: ~Copyable { + public var testDescription: String { + self.withUnsafeBufferPointer { buffer in + _bufferDescription("MutableSpan", start: buffer.baseAddress, count: buffer.count) + } + } +} +#if compiler(>=6.2) && (compiler(>=6.3) || !os(Windows)) // FIXME: [2025-08-17] Windows has no 6.2 snapshot with OutputSpan +@available(SwiftStdlib 5.0, *) +extension OutputSpan: TestPrintable where Element: ~Copyable { + public var testDescription: String { + let capacity = self.capacity + return self.span.withUnsafeBufferPointer { buffer in + """ + OutputSpan(\ + start: \(buffer.baseAddress?._description ?? "nil"), \ + capacity: \(capacity), \ + count: \(buffer.count)) + """ + } + } +} +#endif + +#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW +@available(SwiftStdlib 5.0, *) +extension InputSpan: TestPrintable where Element: ~Copyable { + public var testDescription: String { + let capacity = self.capacity + return self.span.withUnsafeBufferPointer { buffer in + """ + InputSpan(\ + start: \(buffer.baseAddress?._description ?? "nil"), \ + capacity: \(capacity), \ + count: \(buffer.count)) + """ + } + } +} +#endif +#endif diff --git a/Xcode/Collections.xcodeproj/project.pbxproj b/Xcode/Collections.xcodeproj/project.pbxproj index 9fa838b3..6148a272 100644 --- a/Xcode/Collections.xcodeproj/project.pbxproj +++ b/Xcode/Collections.xcodeproj/project.pbxproj @@ -17,6 +17,7 @@ 7D48BCD82E51524200D7F6F7 /* Borrow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D48BCC72E51524200D7F6F7 /* Borrow.swift */; }; 7D48BCD92E51524200D7F6F7 /* Inout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D48BCCA2E51524200D7F6F7 /* Inout.swift */; }; 7D48BCDC2E51554900D7F6F7 /* ClassBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D48BCDB2E51554900D7F6F7 /* ClassBox.swift */; }; + 7D48BCDF2E51558700D7F6F7 /* TestPrintable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D48BCDD2E51558700D7F6F7 /* TestPrintable.swift */; }; 7D48BCE02E51558700D7F6F7 /* LifetimeTrackedStruct.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D48BCDE2E51558700D7F6F7 /* LifetimeTrackedStruct.swift */; }; 7D48BCE92E5158A100D7F6F7 /* DynamicArrayTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D48BCE22E5158A100D7F6F7 /* DynamicArrayTests.swift */; }; 7D48BCEA2E5158A100D7F6F7 /* ArrayLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D48BCE12E5158A100D7F6F7 /* ArrayLayout.swift */; }; @@ -460,6 +461,7 @@ 7D48BCCB2E51524200D7F6F7 /* InputSpan.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputSpan.swift; sourceTree = ""; }; 7D48BCCD2E51524200D7F6F7 /* Shared.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Shared.swift; sourceTree = ""; }; 7D48BCDB2E51554900D7F6F7 /* ClassBox.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClassBox.swift; sourceTree = ""; }; + 7D48BCDD2E51558700D7F6F7 /* TestPrintable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestPrintable.swift; sourceTree = ""; }; 7D48BCDE2E51558700D7F6F7 /* LifetimeTrackedStruct.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LifetimeTrackedStruct.swift; sourceTree = ""; }; 7D48BCE12E5158A100D7F6F7 /* ArrayLayout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrayLayout.swift; sourceTree = ""; }; 7D48BCE22E5158A100D7F6F7 /* DynamicArrayTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DynamicArrayTests.swift; sourceTree = ""; }; @@ -1816,6 +1818,7 @@ children = ( 7DEBDB2C29CCE43600ADC226 /* AllOnesRandomNumberGenerator.swift */, 7D48BCDB2E51554900D7F6F7 /* ClassBox.swift */, + 7D48BCDD2E51558700D7F6F7 /* TestPrintable.swift */, 7DEBDB2A29CCE43600ADC226 /* DictionaryAPIChecker.swift */, 7DEBDB3129CCE43600ADC226 /* HashableBox.swift */, 7DEBDB2829CCE43600ADC226 /* IndexRangeCollection.swift */, @@ -2420,6 +2423,7 @@ 7DEBDB0E29CBF68900ADC226 /* Integer rank.swift in Sources */, 7DE9220729CA8576004483EB /* BitSet.Counted Tests.swift in Sources */, 7DE9220F29CA8576004483EB /* OrderedDictionary Tests.swift in Sources */, + 7D48BCDF2E51558700D7F6F7 /* TestPrintable.swift in Sources */, 7D48BCE02E51558700D7F6F7 /* LifetimeTrackedStruct.swift in Sources */, 7DE9221029CA8576004483EB /* OrderedDictionary Utils.swift in Sources */, );