From e8cbb2f528e33e2694bad69d5b58ebf52f5e4bfb Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Tue, 24 Feb 2026 16:48:51 -0800 Subject: [PATCH] [BasicContainers] Make some tiny headway into UniqueDictionary test coverage --- .../UniqueDictionaryTests.swift | 108 ++++++++++++++++++ Xcode/Collections.xcodeproj/project.pbxproj | 4 + 2 files changed, 112 insertions(+) diff --git a/Tests/BasicContainersTests/UniqueDictionaryTests.swift b/Tests/BasicContainersTests/UniqueDictionaryTests.swift index 5d049f3a..a76ee5e6 100644 --- a/Tests/BasicContainersTests/UniqueDictionaryTests.swift +++ b/Tests/BasicContainersTests/UniqueDictionaryTests.swift @@ -21,6 +21,114 @@ import ContainersPreview #if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS class UniqueDictionaryTests: CollectionTestCase { + func test_empty() { + let s = UniqueDictionary() + 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(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 + typealias Value = LifetimeTracked + withLifetimeTracking { tracker in + var s = UniqueDictionary() + 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 + typealias Value = LifetimeTracked + withLifetimeTracking { tracker in + var s = UniqueDictionary() + 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 = [] + + // 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(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 typealias Value = LifetimeTracked diff --git a/Xcode/Collections.xcodeproj/project.pbxproj b/Xcode/Collections.xcodeproj/project.pbxproj index 5800b969..6d1f6ff6 100644 --- a/Xcode/Collections.xcodeproj/project.pbxproj +++ b/Xcode/Collections.xcodeproj/project.pbxproj @@ -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 = ""; }; 7DEB315B2F4E7A2000F9AF28 /* UniqueDictionary+Equatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDictionary+Equatable.swift"; sourceTree = ""; }; 7DEB315D2F4E7B8200F9AF28 /* UniqueDictionary+Hashable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDictionary+Hashable.swift"; sourceTree = ""; }; + 7DEB315F2F4E7CEF00F9AF28 /* UniqueDictionaryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UniqueDictionaryTests.swift; sourceTree = ""; }; 7DEBDB2129CCE43600ADC226 /* Combinatorics.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Combinatorics.swift; sourceTree = ""; }; 7DEBDB2229CCE43600ADC226 /* TestContext.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestContext.swift; sourceTree = ""; }; 7DEBDB2329CCE43600ADC226 /* Assertions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Assertions.swift; sourceTree = ""; }; @@ -2309,6 +2311,7 @@ 7D7246912F3D9E6800112FD0 /* RigidSetTests.swift */, 7D479CB42F452D6700A8CB59 /* UniqueSetTests.swift */, 7DC85F8E2F43D05F004A4E3D /* RigidDictionaryTests.swift */, + 7DEB315F2F4E7CEF00F9AF28 /* UniqueDictionaryTests.swift */, ); path = BasicContainersTests; sourceTree = ""; @@ -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 */,