[Heap] Clean up description

- Don’t print the type name
- Include some information identifying the heap instance
This commit is contained in:
Karoy Lorentey committed 2024-02-02 16:20:21 -08:00
1 parent 37e66d0e49
commit 3c99c84286
2 files changed
+27 -6

No files matched your search

+11 -1
View File
@@ -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)
}
}
}
+16 -5
View File
@@ -68,12 +68,23 @@ final class HeapTests: CollectionTestCase {
func test_descriptions() {
let a: Heap<Int> = []
expectEqual(a.description, "Heap<Int>(count: 0)")
expectEqual(a.debugDescription, "Heap<Int>(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<Int>(count: 2)")
expectEqual(b.debugDescription, "Heap<Int>(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() {