[BasicContainers] Make some tiny headway into UniqueDictionary test coverage

This commit is contained in:
Karoy Lorentey committed 2026-02-24 16:48:51 -08:00
1 parent 4452b1c960
commit e8cbb2f528
2 files changed
+112

No files matched your search

@@ -21,6 +21,114 @@ import ContainersPreview
#if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS
class UniqueDictionaryTests: CollectionTestCase {
func test_empty() {
let s = UniqueDictionary<Int, String>()
expectEqual(s.count, 0)
expectTrue(s.isEmpty)
expectTrue(s.isFull)
expectEqual(s.capacity, 0)
expectEqual(s.freeCapacity, 0)
}
func test_init_minimumCapacity() {
withSome("capacity", in: 0 ..< 1000) { capacity in
let d = UniqueDictionary<Int, String>(minimumCapacity: capacity)
expectEqual(d.count, 0)
expectTrue(d.isEmpty)
expectEqual(d.isFull, capacity == 0)
expectGreaterThanOrEqual(d.capacity, capacity)
expectEqual(d.freeCapacity, d.capacity)
}
}
func test_insert_one() {
typealias Key = LifetimeTracked<Int>
typealias Value = LifetimeTracked<String>
withLifetimeTracking { tracker in
var s = UniqueDictionary<Key, Value>()
let firstKey = tracker.instance(for: 42)
let firstValue = tracker.instance(for: "42")
expectNil(s.insertValue(firstValue, forKey: firstKey))
expectTrue(s.containsKey(firstKey))
expectNotNil(s.withValue(forKey: firstKey) { $0 }) {
expectIdentical($0, firstValue)
}
let secondKey = tracker.instance(for: 42)
let secondValue = tracker.instance(for: "42")
expectIdentical(s.insertValue(secondValue, forKey: secondKey), secondValue)
expectTrue(s.containsKey(firstKey))
expectTrue(s.containsKey(secondKey))
_ = consume firstKey
expectTrue(s.containsKey(secondKey))
expectNotNil(s.withValue(forKey: secondKey) { $0 }) {
expectIdentical($0, firstValue)
}
expectEqual(s.count, 1)
expectFalse(s.isEmpty)
expectGreaterThanOrEqual(s.capacity, 1)
}
}
func test_update_one() {
typealias Key = LifetimeTracked<Int>
typealias Value = LifetimeTracked<String>
withLifetimeTracking { tracker in
var s = UniqueDictionary<Key, Value>()
let firstKey = tracker.instance(for: 42)
let firstValue = tracker.instance(for: "42")
expectNil(s.updateValue(firstValue, forKey: firstKey))
expectTrue(s.containsKey(firstKey))
expectNotNil(s.withValue(forKey: firstKey) { $0 }) {
expectIdentical($0, firstValue)
}
let secondKey = tracker.instance(for: 42)
let secondValue = tracker.instance(for: "42")
expectIdentical(s.updateValue(secondValue, forKey: secondKey), firstValue)
expectTrue(s.containsKey(firstKey))
expectTrue(s.containsKey(secondKey))
_ = consume secondKey
expectTrue(s.containsKey(firstKey))
expectNotNil(s.withValue(forKey: firstKey) { $0 }) {
expectIdentical($0, secondValue)
}
expectEqual(s.count, 1)
expectFalse(s.isEmpty)
expectGreaterThanOrEqual(s.capacity, 1)
}
}
func test_init_minimumCapacity_growth() {
// These are the storage capacities we expect to see for a UniqueSet
// up to size 1000.
let expected = [0, 1, 2, 4, 7, 14, 28, 56, 112, 224, 448, 896, 1792]
var actual: Set<Int> = []
// Check that we get the expected capacities for each size. UniqueSet
// should not prematurely allocate more than the size of the closest
// expected size.
var j = 0
withEvery("i", in: 0 ..< 1000) { i in
let set = UniqueDictionary<Int, String>(minimumCapacity: i)
if i > expected[j] {
j += 1
}
expectEqual(set.capacity, expected[j])
actual.insert(set.capacity)
}
expectEqual(actual.sorted(), expected)
}
func test_withKeys() {
typealias Key = LifetimeTracked<Int>
typealias Value = LifetimeTracked<String>
@@ -510,6 +510,7 @@
7DEB315A2F4E78F800F9AF28 /* RigidDictionary+Hashable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB31592F4E78F200F9AF28 /* RigidDictionary+Hashable.swift */; };
7DEB315C2F4E7A2500F9AF28 /* UniqueDictionary+Equatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB315B2F4E7A2000F9AF28 /* UniqueDictionary+Equatable.swift */; };
7DEB315E2F4E7B8700F9AF28 /* UniqueDictionary+Hashable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB315D2F4E7B8200F9AF28 /* UniqueDictionary+Hashable.swift */; };
7DEB31602F4E7CEF00F9AF28 /* UniqueDictionaryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB315F2F4E7CEF00F9AF28 /* UniqueDictionaryTests.swift */; };
7DEBDB6E29CCE44A00ADC226 /* SortedCollectionAPIChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEBDB3029CCE43600ADC226 /* SortedCollectionAPIChecker.swift */; };
7DEBDB6F29CCE44A00ADC226 /* MinimalMutableRangeReplaceableRandomAccessCollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEBDB3F29CCE43600ADC226 /* MinimalMutableRangeReplaceableRandomAccessCollection.swift */; };
7DEBDB7029CCE44A00ADC226 /* ResettableValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEBDB4229CCE43600ADC226 /* ResettableValue.swift */; };
@@ -1106,6 +1107,7 @@
7DEB31592F4E78F200F9AF28 /* RigidDictionary+Hashable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RigidDictionary+Hashable.swift"; sourceTree = "<group>"; };
7DEB315B2F4E7A2000F9AF28 /* UniqueDictionary+Equatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDictionary+Equatable.swift"; sourceTree = "<group>"; };
7DEB315D2F4E7B8200F9AF28 /* UniqueDictionary+Hashable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDictionary+Hashable.swift"; sourceTree = "<group>"; };
7DEB315F2F4E7CEF00F9AF28 /* UniqueDictionaryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UniqueDictionaryTests.swift; sourceTree = "<group>"; };
7DEBDB2129CCE43600ADC226 /* Combinatorics.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Combinatorics.swift; sourceTree = "<group>"; };
7DEBDB2229CCE43600ADC226 /* TestContext.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestContext.swift; sourceTree = "<group>"; };
7DEBDB2329CCE43600ADC226 /* Assertions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Assertions.swift; sourceTree = "<group>"; };
@@ -2309,6 +2311,7 @@
7D7246912F3D9E6800112FD0 /* RigidSetTests.swift */,
7D479CB42F452D6700A8CB59 /* UniqueSetTests.swift */,
7DC85F8E2F43D05F004A4E3D /* RigidDictionaryTests.swift */,
7DEB315F2F4E7CEF00F9AF28 /* UniqueDictionaryTests.swift */,
);
path = BasicContainersTests;
sourceTree = "<group>";
@@ -2907,6 +2910,7 @@
7D48BCF22E51599200D7F6F7 /* CheckContainer.swift in Sources */,
7DE9221429CA8576004483EB /* OrderedSet Diffing Tests.swift in Sources */,
7DEBDB8529CCE44A00ADC226 /* AllOnesRandomNumberGenerator.swift in Sources */,
7DEB31602F4E7CEF00F9AF28 /* UniqueDictionaryTests.swift in Sources */,
7DE921FD29CA8576004483EB /* CombinatoricsChecks.swift in Sources */,
7DEBDB8B29CCE44A00ADC226 /* _CollectionState.swift in Sources */,
7DE9220229CA8576004483EB /* TreeDictionary Tests.swift in Sources */,