From b9ea3f0dd30fb759d4f681f3cb5d479adf59f049 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Fri, 26 Aug 2022 14:21:15 -0700 Subject: [PATCH] [BitSet] Add Custom[Debug]StringConvertible, CustomReflectable conformances --- .../BitSet+CustomDebugStringConvertible.swift | 32 +++++++++++++ .../BitSet/BitSet+CustomReflectable.swift | 17 +++++++ .../BitSet+CustomStringConvertible.swift | 28 +++++++++++ .../BitCollections/BitSet/BitSet.Index.swift | 19 ++++++-- Tests/BitCollectionsTests/BitSetTests.swift | 47 ++++++++++++++++++- 5 files changed, 137 insertions(+), 6 deletions(-) create mode 100644 Sources/BitCollections/BitSet/BitSet+CustomDebugStringConvertible.swift create mode 100644 Sources/BitCollections/BitSet/BitSet+CustomReflectable.swift create mode 100644 Sources/BitCollections/BitSet/BitSet+CustomStringConvertible.swift diff --git a/Sources/BitCollections/BitSet/BitSet+CustomDebugStringConvertible.swift b/Sources/BitCollections/BitSet/BitSet+CustomDebugStringConvertible.swift new file mode 100644 index 00000000..c1120198 --- /dev/null +++ b/Sources/BitCollections/BitSet/BitSet+CustomDebugStringConvertible.swift @@ -0,0 +1,32 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2022 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 +// +//===----------------------------------------------------------------------===// + +extension BitSet: CustomDebugStringConvertible { + /// A textual representation of this instance, suitable for debugging. + public var debugDescription: String { + _debugDescription(typeName: "BitSet") + } + + internal func _debugDescription(typeName: String) -> String { + var result = "\(typeName)([" + var first = true + for item in self { + if first { + first = false + } else { + result += ", " + } + debugPrint(item, terminator: "", to: &result) + } + result += "])" + return result + } +} diff --git a/Sources/BitCollections/BitSet/BitSet+CustomReflectable.swift b/Sources/BitCollections/BitSet/BitSet+CustomReflectable.swift new file mode 100644 index 00000000..2ed48046 --- /dev/null +++ b/Sources/BitCollections/BitSet/BitSet+CustomReflectable.swift @@ -0,0 +1,17 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2021 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 +// +//===----------------------------------------------------------------------===// + +extension BitSet: CustomReflectable { + /// The custom mirror for this instance. + public var customMirror: Mirror { + Mirror(self, unlabeledChildren: self, displayStyle: .collection) + } +} diff --git a/Sources/BitCollections/BitSet/BitSet+CustomStringConvertible.swift b/Sources/BitCollections/BitSet/BitSet+CustomStringConvertible.swift new file mode 100644 index 00000000..f04f8c82 --- /dev/null +++ b/Sources/BitCollections/BitSet/BitSet+CustomStringConvertible.swift @@ -0,0 +1,28 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2022 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 +// +//===----------------------------------------------------------------------===// + +extension BitSet: CustomStringConvertible { + // A textual representation of this instance. + public var description: String { + var result = "[" + var first = true + for item in self { + if first { + first = false + } else { + result += ", " + } + print(item, terminator: "", to: &result) + } + result += "]" + return result + } +} diff --git a/Sources/BitCollections/BitSet/BitSet.Index.swift b/Sources/BitCollections/BitSet/BitSet.Index.swift index df1fc9ce..0de270d7 100644 --- a/Sources/BitCollections/BitSet/BitSet.Index.swift +++ b/Sources/BitCollections/BitSet/BitSet.Index.swift @@ -39,28 +39,39 @@ extension BitSet { } } -extension BitSet.Index: Comparable, Hashable { +extension BitSet.Index: CustomStringConvertible { + // A textual representation of this instance. + public var description: String { + "\(_value)" + } +} + +extension BitSet.Index: Equatable { /// Returns a Boolean value indicating whether two values are equal. /// /// - Complexity: O(1) - @inlinable + @inlinable @inline(__always) public static func ==(left: Self, right: Self) -> Bool { left._value == right._value } +} +extension BitSet.Index: Comparable { /// Returns a Boolean value indicating whether the first value is ordered /// before the second. /// /// - Complexity: O(1) - @inlinable + @inlinable @inline(__always) public static func < (left: Self, right: Self) -> Bool { left._value < right._value } +} +extension BitSet.Index: Hashable { /// Hashes the essential components of this value by feeding them to the given hasher. /// /// - Complexity: O(1) - @inlinable + @inlinable @inline(__always) public func hash(into hasher: inout Hasher) { hasher.combine(_value) } diff --git a/Tests/BitCollectionsTests/BitSetTests.swift b/Tests/BitCollectionsTests/BitSetTests.swift index b5cd7c93..034ba746 100644 --- a/Tests/BitCollectionsTests/BitSetTests.swift +++ b/Tests/BitCollectionsTests/BitSetTests.swift @@ -137,8 +137,8 @@ final class BitSetTest: CollectionTestCase { // 1% filled for i in 0 ..< c { let set = Set((0 ..< maximum) - .shuffled(using: &rng) - .prefix(count)) + .shuffled(using: &rng) + .prefix(count)) yield("\(desc)/\(i)", set) } } @@ -1041,4 +1041,47 @@ final class BitSetTest: CollectionTestCase { let a = Set((0..<10).map { _ in BitSet.random(upTo: 1000) }) expectEqual(a.count, 10) } + + func test_description() { + let a: BitSet = [] + expectEqual("\(a)", "[]") + + let b: BitSet = [1, 2, 3] + expectEqual("\(b)", "[1, 2, 3]") + + let c: BitSet = [23, 652, 892, 19230] + expectEqual("\(c)", "[23, 652, 892, 19230]") + } + + func test_debugDescription() { + let a: BitSet = [] + expectEqual("\(String(reflecting: a))", "BitSet([])") + + let b: BitSet = [1, 2, 3] + expectEqual("\(String(reflecting: b))", "BitSet([1, 2, 3])") + + let c: BitSet = [23, 652, 892, 19230] + expectEqual("\(String(reflecting: c))", "BitSet([23, 652, 892, 19230])") + } + + func test_mirror() { + func check(_ v: T) -> String { + var str = "" + dump(v, to: &str) + return str + } + + expectEqual(check(BitSet()), """ + - 0 elements + + """) + + expectEqual(check([1, 2, 3] as BitSet), """ + ▿ 3 elements + - 1 + - 2 + - 3 + + """) + } }