mirror of
https://github.com/apple/swift-collections
synced 2026-11-29 05:14:20 +00:00
- Allow bitmap visualization to be included/omitted as needed
- Visualize chain length distribution for both successful and unsuccessful lookups
Check out this sample test output for Robin Hood hashing:
```
⌗ large@11 1536/1792/2048, maxProbeLength: 11, totalProbeLength: 3712, maxOccupiedRun: 68
├╴[00000000] □□■■■■□□∙□■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■□■■■■■■∙□□□■□■□□∙□□□■■□■■∙■■■■■■■■
├╴[00000064] □□■□□■■■∙■■■■□■□□∙□■■■■■■■∙■■■■■■■■∙■■□□□□■□∙■□■□■■■■∙■■■■■■□□∙■■■■■■■■
├╴[00000128] ■■■■■■■■∙■■■□□□■□∙■■□■□■■□∙□■■■■■□■∙■■■■■■■■∙□■■■■■■■∙■■■■□■□■∙■□■□□■□□
├╴[00000192] ■□■■□■■■∙■■■■□■□■∙□□□□□■■□∙■■■■■■■■∙■■■□■■□■∙□■□■■□□■∙■■■■■■■■∙■■■■■■■■
├╴[00000256] ■■■■■■□□∙■■■■■■■□∙□■■■■■■□∙□■■■■■■□∙□□■■□□■□∙□□□■■■■□∙□■■■■□■■∙■■■■■■■■
├╴[00000320] ■■■■■■■■∙□■■■■■■■∙■■■■■■■□∙□■■□□■□□∙□□□■■□□■∙■■■■■□■□∙■□■■■■■□∙■■■■■■■■
├╴[00000384] ■■■■■■■■∙■■■■■□□■∙□□■■■■□□∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙□■■□□■□■∙□□■■□■■■
├╴[00000448] ■■■■□■■■∙■■■□■■■□∙□□□■□■■■∙■■■□□■■■∙■■■□□□□□∙□■■■■■■■∙■■■■■■■■∙■■■■■■■■
├╴[00000512] ■■■■■□□□∙□□■■□■■■∙■■■□■■■■∙■■■■■■■■∙■■■■■■■□∙□■■■■□□■∙■■■■■■■■∙■■■■■■■■
├╴[00000576] □□□■■■□■∙■■■□■■■■∙■■■■■■■■∙■■■■■■■■∙■■□■■■■■∙■■■■■■■■∙■■□□■□□□∙■■■□□□■□
├╴[00000640] □■■□□■■□∙□□□□■■■■∙□■■□■■■□∙□■■□■■■■∙■■■■■■■■∙■□□■■■■■∙■■■■■□□□∙■■■■■■■□
├╴[00000704] ■■■■■■■□∙■■■■□□□■∙■■□■□■■■∙■■■■■■■■∙■■■■□■■■∙□□□■□■■■∙□□□■□□□□∙□□□■□■■■
├╴[00000768] □■■■■■□■∙□■■□□□■■∙■■■■■■■□∙■■■■■■■■∙■■■■■■■□∙□■■■□■■□∙■□■■□■■■∙■■■■□■□■
├╴[00000832] ■□□■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■□□∙□□■■■■■■∙□□■■■■■■∙■■■■■■■■∙□■□■■■■■
├╴[00000896] ■■■■■■■■∙■■■■■□□■∙■■■□□□□■∙■■□□■■■■∙■□□■■■■□∙■■■□□■■□∙□□■■□■■■∙□■□□■■■■
├╴[00000960] ■■■■■□■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■□∙□■□■■■■■∙■■■■□□■■
├╴[00001024] ■■■■■■□□∙■■■■□■■□∙■■■■■■■□∙□□□■■■□□∙■■■□■■□■∙■□■■□□□■∙■■■■■■■■∙□□□□□□□□
├╴[00001088] ■■□□□□■□∙■■□□□□□■∙■□□■■■■■∙■■■□□□□■∙■■■■■■■■∙■■■■■■■■∙■■■■□□□□∙■■□■□■■■
├╴[00001152] ■■■□□■■□∙□■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■□■■□■■■∙■□■■□■■□∙□■■■■□■■
├╴[00001216] ■■■■■■■■∙■■□■□■■■∙■■■■■■■■∙□□■■□□□■∙□□□□□□□□∙□□■■■■■■∙■■■■■■■■∙■■■□□■■■
├╴[00001280] ■■■■■■■□∙■■□■■■■■∙■■■■■■□□∙■□□■■■■■∙■■■■■■■■∙■□□■□■□■∙■■■■■■■■∙■■■■■■■■
├╴[00001344] ■■■■■■■■∙■□■□■■□□∙■■■■□■□□∙□■■□■□□■∙□■□■■■■■∙■■■□■■■□∙□□■■■■■■∙■■■■■■■■
├╴[00001408] ■■■■■■■■∙■■■■■■■■∙■■■■■□□■∙■■■□■■■■∙■■□■■■■■∙■■■□■■■■∙■■□□■□□□∙■□■■■■■■
├╴[00001472] ■■■■■□■□∙■■□□□□□■∙□□□□□■■□∙■□□■■□■□∙■■■■■■■■∙■■■■■■■■∙□□□□■■■■∙■□■□□■■■
├╴[00001536] ■■■■■■■■∙□□□■■□■□∙□■■□■■□□∙□■□■■□□□∙■■■■■□□■∙■□■■■■■■∙■■■■■■■■∙■□□■□□□□
├╴[00001600] ■■■■■■■■∙■■■■□□■□∙□□■■□■■■∙■■■■■■■□∙□■□□■■□■∙■□■■■□■■∙■■■■■■■■∙□□■■■■■■
├╴[00001664] ■■■■■■■■∙□■■■□□■■∙■■■□■□■□∙■■■■■■□■∙■■■□■■■■∙■■■■■■■□∙□□□□■■■■∙■■■■□■■■
├╴[00001728] ■■■■■■■■∙■□□■■■■■∙■■□□■■□□∙■■■■■■■■∙■■■□□□■■∙□□■■■■■■∙■■■■■■■■∙■■■■■■■■
├╴[00001792] ■■■■■■■■∙■■■■■■■■∙■□□□■■□■∙□□■■□□□□∙□■■□■□□■∙□□■■■■□□∙■■■■■■■■∙■■■■■■■■
├╴[00001856] ■■■■■■■□∙■■■■■■■□∙□■□■□■■□∙■□□□■■■■∙■□■■□■■□∙□□■□■□■■∙■□□□□■■■∙■■■■■■■■
├╴[00001920] ■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■■■■■■■■∙■□□■□■□□
└╴[00001984] ■□□■□□■■∙□■■■■■■□∙□□□□■■■■∙■□□□■■■■∙■■■■■■■■∙■■□■□■■■∙■■■□■■□□∙■□□■■□■□
Chain length distribution (for successful lookups):
AVG: 2.416
1: **************************************************************************** 549
2: ******************************************************** 408
3: ************************************* 264
4: ******************** 142
5: ************** 96
6: ******* 51
7: *** 17
8: * 4
9: * 1
10: * 3
11: * 1
Chain length distribution (for unsuccessful lookups):
AVG: 4.428
0: **************************************************************************** 511
1: **************************************** 269
2: ***************************** 191
3: ******************* 129
4: ***************** 111
5: ************** 94
6: ************* 85
7: *********** 70
8: ********* 60
9: ********* 55
10: ******** 50
11: ************************************************************** 422
```
229 lines
7.2 KiB
Swift
229 lines
7.2 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 2026 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import XCTest
|
|
#if COLLECTIONS_SINGLE_MODULE
|
|
import Collections
|
|
#else
|
|
import _CollectionsTestSupport
|
|
import BasicContainers
|
|
#endif
|
|
|
|
#if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS
|
|
|
|
class RigidSetTests: CollectionTestCase {
|
|
func test_empty() {
|
|
let s = RigidSet<Int>()
|
|
expectEqual(s.count, 0)
|
|
expectTrue(s.isEmpty)
|
|
expectTrue(s.isFull)
|
|
expectEqual(s.capacity, 0)
|
|
expectEqual(s.freeCapacity, 0)
|
|
}
|
|
|
|
func test_init_capacity() {
|
|
withSome("capacity", in: 0 ..< 1000) { capacity in
|
|
let s = RigidSet<Int>(capacity: capacity)
|
|
expectEqual(s.count, 0)
|
|
expectTrue(s.isEmpty)
|
|
expectEqual(s.isFull, capacity == 0)
|
|
expectEqual(s.capacity, capacity)
|
|
expectEqual(s.freeCapacity, capacity)
|
|
}
|
|
}
|
|
|
|
func test_insert_one() {
|
|
withLifetimeTracking { tracker in
|
|
var s = RigidSet<LifetimeTracked<Int>>(capacity: 20)
|
|
let first = tracker.instance(for: 42)
|
|
expectNil(s.insert(first))
|
|
let second = tracker.instance(for: 42)
|
|
expectIdentical(s.insert(second), second)
|
|
|
|
expectTrue(s.contains(first))
|
|
expectTrue(s.contains(second))
|
|
_ = consume first
|
|
expectTrue(s.contains(second))
|
|
|
|
expectEqual(s.count, 1)
|
|
expectFalse(s.isEmpty)
|
|
expectFalse(s.isFull)
|
|
expectEqual(s.capacity, 20)
|
|
expectEqual(s.freeCapacity, 19)
|
|
}
|
|
}
|
|
|
|
func test_update_one() {
|
|
withLifetimeTracking { tracker in
|
|
var s = RigidSet<LifetimeTracked<Int>>(capacity: 20)
|
|
let first = tracker.instance(for: 42)
|
|
expectNil(s.update(with: first))
|
|
let second = tracker.instance(for: 42)
|
|
expectIdentical(s.update(with: second), first)
|
|
|
|
expectTrue(s.contains(first))
|
|
expectTrue(s.contains(second))
|
|
_ = consume second
|
|
expectTrue(s.contains(first))
|
|
|
|
expectEqual(s.count, 1)
|
|
expectFalse(s.isEmpty)
|
|
expectFalse(s.isFull)
|
|
expectEqual(s.capacity, 20)
|
|
expectEqual(s.freeCapacity, 19)
|
|
}
|
|
}
|
|
|
|
func test_insert_full() {
|
|
withEvery("capacity", in: [0, 1, 2, 10, 100, 1000]) { capacity in
|
|
withLifetimeTracking { tracker in
|
|
var s = RigidSet<LifetimeTracked<Int>>(capacity: capacity)
|
|
for i in 0 ..< capacity {
|
|
let new = tracker.instance(for: i)
|
|
let remnant = s.insert(new)
|
|
expectNil(remnant)
|
|
}
|
|
expectEqual(s.count, capacity)
|
|
expectEqual(s.isEmpty, capacity == 0)
|
|
expectTrue(s.isFull)
|
|
expectEqual(s.capacity, capacity)
|
|
expectEqual(s.freeCapacity, 0)
|
|
|
|
for i in 0 ..< capacity {
|
|
let dupe = tracker.instance(for: i)
|
|
if s.contains(dupe) {
|
|
expectIdentical(s.insert(dupe), dupe)
|
|
} else {
|
|
expectFailure("\(dupe.payload) not found")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func test_update_full() {
|
|
withEvery("capacity", in: [0, 1, 2, 10, 100, 200, 1000]) { capacity in
|
|
withLifetimeTracking { tracker in
|
|
var s = RigidSet<LifetimeTracked<Int>>(capacity: capacity)
|
|
for i in 0 ..< capacity {
|
|
let new = tracker.instance(for: i)
|
|
let old = s.update(with: new)
|
|
expectNil(old)
|
|
}
|
|
expectEqual(s.count, capacity)
|
|
expectEqual(s.isEmpty, capacity == 0)
|
|
expectTrue(s.isFull)
|
|
expectEqual(s.capacity, capacity)
|
|
expectEqual(s.freeCapacity, 0)
|
|
|
|
for i in 0 ..< capacity {
|
|
let new = tracker.instance(for: i)
|
|
let old = s.update(with: new)
|
|
expectNotNil(old) { old in
|
|
expectNotIdentical(old, new)
|
|
}
|
|
}
|
|
expectEqual(s.count, capacity)
|
|
expectEqual(s.isEmpty, capacity == 0)
|
|
expectTrue(s.isFull)
|
|
expectEqual(s.capacity, capacity)
|
|
expectEqual(s.freeCapacity, 0)
|
|
|
|
for i in 0 ..< capacity {
|
|
let dupe = tracker.instance(for: i)
|
|
expectTrue(s.contains(dupe), "\(dupe) not found")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func test_bucketIterator_consistency() {
|
|
withEvery("capacity", in: [0, 1, 2, 3, 4, 10, 100, 200]) { capacity in
|
|
withEvery("maximumCount", in: [1, 2, 3, Int.max]) { maximumCount in
|
|
withLifetimeTracking { tracker in
|
|
var s = RigidSet<LifetimeTracked<Int>>(capacity: capacity)
|
|
withEvery("i", in: 0 ..< capacity) { i in
|
|
s.insert(tracker.instance(for: i))
|
|
|
|
var it = s._table.makeBucketIterator()
|
|
var j = s._table.startBucket
|
|
while let next = it.nextOccupiedRegion(maximumCount: maximumCount) {
|
|
expectFalse(next.isEmpty, "Empty chunk; j: \(j), next: \(next)")
|
|
expectLessThanOrEqual(
|
|
next.upperBound.offset - next.lowerBound.offset,
|
|
maximumCount,
|
|
"Overlong chunk")
|
|
if maximumCount == Int.max, j > s._table.startBucket {
|
|
expectGreaterThan(
|
|
next.lowerBound, j,
|
|
"Unnecessarily split run of occupied buckets")
|
|
}
|
|
while j < next.lowerBound {
|
|
context.withTrace("j: \(j)") {
|
|
expectFalse(s._table.bitmap.isOccupied(j))
|
|
}
|
|
s._table.formBucket(after: &j)
|
|
}
|
|
while j < next.upperBound {
|
|
context.withTrace("j: \(j)") {
|
|
expectTrue(s._table.isOccupied(j))
|
|
}
|
|
s._table.formBucket(after: &j)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func test_probeLengths() {
|
|
// FIXME: This isn't really testing anything; figure out how to handle this
|
|
let c1 = 1000
|
|
let scale = _HTable.minimumScale(forCapacity: c1)
|
|
let c2 = _HTable.maximumCapacity(forScale: scale)
|
|
var set = RigidSet<Int>(capacity: c2)
|
|
for i in 0 ..< 1536 {
|
|
set.insert(i)
|
|
}
|
|
set._dump(chains: true)
|
|
}
|
|
|
|
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
|
func test_iterate() {
|
|
withEvery("capacity", in: [0, 1, 2, 3, 4, 10, 100, 1000]) { capacity in
|
|
withEvery("maximumCount", in: [1, 2, 3, Int.max]) { maximumCount in
|
|
withLifetimeTracking { tracker in
|
|
var s = RigidSet<LifetimeTracked<Int>>(capacity: capacity)
|
|
withEvery("i", in: 0 ..< capacity) { i in
|
|
s.insert(tracker.instance(for: i))
|
|
|
|
var expected = Set(0 ... i)
|
|
var it = s.makeBorrowingIterator()
|
|
while true {
|
|
let next = it.nextSpan(maximumCount: maximumCount)
|
|
guard !next.isEmpty else { break }
|
|
expectLessThanOrEqual(next.count, maximumCount)
|
|
for j in next.indices {
|
|
let v = next[j].payload
|
|
expectEqual(expected.remove(v), v, "Unexpected item \(v)")
|
|
}
|
|
}
|
|
expectEqual(expected.count, 0, "Iterator skipped items \(expected)")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
#endif
|