[BitSet] Add Custom[Debug]StringConvertible, CustomReflectable conformances

This commit is contained in:
Karoy Lorentey committed 2022-08-26 14:21:15 -07:00
1 parent bae950193f
commit b9ea3f0dd3
5 files changed
+137 -6

No files matched your search

@@ -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
}
}
@@ -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)
}
}
@@ -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
}
}
@@ -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)
}
+45 -2
View File
@@ -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<T>(_ 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
""")
}
}