From 3c99c842869e98cb8485399c19dc2f3cdf70fbae Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Thu, 1 Feb 2024 21:22:50 -0800 Subject: [PATCH] [Heap] Clean up description MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Don’t print the type name - Include some information identifying the heap instance --- Sources/HeapModule/Heap+Descriptions.swift | 12 +++++++++++- Tests/HeapTests/HeapTests.swift | 21 ++++++++++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Sources/HeapModule/Heap+Descriptions.swift b/Sources/HeapModule/Heap+Descriptions.swift index 12edec3d..ac413046 100644 --- a/Sources/HeapModule/Heap+Descriptions.swift +++ b/Sources/HeapModule/Heap+Descriptions.swift @@ -12,7 +12,17 @@ extension Heap: CustomStringConvertible { /// A textual representation of this instance. public var description: String { - "Heap<\(Element.self)>(count: \(count))" + "<\(count) item\(count == 1 ? "" : "s") @\(_idString)>" + } + + internal var _idString: String { + // "<32 items @0x2787abcf>" + _storage.withUnsafeBytes { + guard let p = $0.baseAddress else { + return "nil" + } + return String(UInt(bitPattern: p), radix: 16) + } } } diff --git a/Tests/HeapTests/HeapTests.swift b/Tests/HeapTests/HeapTests.swift index ddf896b2..aaf12c22 100644 --- a/Tests/HeapTests/HeapTests.swift +++ b/Tests/HeapTests/HeapTests.swift @@ -68,12 +68,23 @@ final class HeapTests: CollectionTestCase { func test_descriptions() { let a: Heap = [] - expectEqual(a.description, "Heap(count: 0)") - expectEqual(a.debugDescription, "Heap(count: 0)") + expectTrue( + a.description.starts(with: "<0 items @"), + "\(a.description)") + expectTrue( + a.debugDescription.starts(with: "<0 items @"), + "\(a.debugDescription)") - let b: Heap = [1, 2] - expectEqual(b.description, "Heap(count: 2)") - expectEqual(b.debugDescription, "Heap(count: 2)") + let b: Heap = [1] + expectTrue( + b.description.starts(with: "<1 item @"), + "\(b.description)") + expectTrue( + b.debugDescription.starts(with: "<1 item @")) + + let c: Heap = [1, 2] + expectTrue(c.description.starts(with: "<2 items @")) + expectTrue(c.debugDescription.starts(with: "<2 items @")) } func test_unordered() {