//===----------------------------------------------------------------------===// // // This source file is part of the Swift Collections open source project // // Copyright (c) 2021 - 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 // // SPDX-License-Identifier: Apache-2.0 WITH Swift-exception // //===----------------------------------------------------------------------===// #if UnstableSortedCollections #if DEBUG import _CollectionsTestSupport @_spi(Testing) @testable import SortedCollections func nodeFromKeys(_ keys: [Int], capacity: Int) -> _Node { let kvPairs = keys.map { (key: $0, value: $0 * 2) } return _Node(_keyValuePairs: kvPairs, capacity: capacity) } func insertSortedValue(_ value: Int, into array: inout [Int]) { var insertionIndex = 0 while insertionIndex < array.count { if array[insertionIndex] > value { break } insertionIndex += 1 } array.insert(value, at: insertionIndex) } func findFirstIndexOf(_ value: Int, in array: [Int]) -> Int { var index = 0 while index < array.count { if array[index] >= value { break } index += 1 } return index } func findLastIndexOf(_ value: Int, in array: [Int]) -> Int { var index = 0 while index < array.count { if array[index] > value { break } index += 1 } return index } /// Generates all shifts of a duplicate run in a node of capacity N. /// - Parameters: /// - capacity: Total capacity of node. /// - keys: The number filled keys in the node. /// - duplicates: The number of duplicates. Must be greater than or equal to 1 /// - Returns: The duplicated key. func withEveryNode( ofCapacity capacity: Int, keys: Int, duplicates: Int, _ body: (_Node, [Int], Int) throws -> Void ) rethrows { let possibleShifts = keys - duplicates + 1 try withEvery("shift", in: 0...Splinter? = node.update { handle in let index = handle.endSlot(forKey: newKey) return handle.insertElement((newKey, newKey * 2), withRightChild: nil, atSlot: index) } insertSortedValue(newKey, into: &array) expectNil(splinter) node.read { handle in let keys = UnsafeBufferPointer(start: handle.keys, count: handle.elementCount) expectEqualElements(keys, array) expectEqual(handle.subtreeCount, count + 1) } } } } } func test_firstIndexOfDuplicates() { withEvery("capacity", in: 2..<10) { capacity in withEvery("keys", in: 0...capacity) { keys in withEvery("duplicates", in: 0...keys) { duplicates in withEveryNode(ofCapacity: capacity, keys: keys, duplicates: duplicates) { node, array, duplicatedKey in node.read { handle in expectEqual( handle.startSlot(forKey: duplicatedKey), findFirstIndexOf(duplicatedKey, in: array) ) } } } } } } func test_lastIndexOfDuplicates() { withEvery("capacity", in: 2..<10) { capacity in withEvery("keys", in: 0...capacity) { keys in withEvery("duplicates", in: 0...keys) { duplicates in withEveryNode(ofCapacity: capacity, keys: keys, duplicates: duplicates) { node, array, duplicatedKey in node.read { handle in expectEqual( handle.endSlot(forKey: duplicatedKey), findLastIndexOf(duplicatedKey, in: array) ) } } } } } } } #endif #endif