From ea60d293b4635e2a9d560251752ebb950959b113 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Tue, 21 Mar 2023 18:43:46 -0700 Subject: [PATCH] Fix test issues with single-module variant --- .../AssertionContexts/Assertions.swift | 2 +- .../RepeatableRandomNumberGenerator.swift | 28 ++++++------- .../SeedableRandomNumberGenerator.swift | 40 ------------------- .../HashTreeCollectionsTests/Utilities.swift | 16 -------- .../{NodeTests.swift => HeapNodeTests.swift} | 8 +++- ...ft => RandomAccessCollection+Extras.swift} | 19 ++------- Tests/RopeModuleTests/TestBigString.swift | 2 +- Tests/RopeModuleTests/TestRope.swift | 2 +- 8 files changed, 27 insertions(+), 90 deletions(-) rename Tests/RopeModuleTests/SeedableRandomNumberGenerator.swift => Sources/_CollectionsTestSupport/Utilities/RepeatableRandomNumberGenerator.swift (88%) delete mode 100644 Sources/_CollectionsTestSupport/Utilities/SeedableRandomNumberGenerator.swift rename Tests/HeapTests/{NodeTests.swift => HeapNodeTests.swift} (86%) rename Tests/OrderedCollectionsTests/OrderedSet/{RandomAccessCollection+Offsets.swift => RandomAccessCollection+Extras.swift} (81%) diff --git a/Sources/_CollectionsTestSupport/AssertionContexts/Assertions.swift b/Sources/_CollectionsTestSupport/AssertionContexts/Assertions.swift index 76db2377..a1ccca5c 100644 --- a/Sources/_CollectionsTestSupport/AssertionContexts/Assertions.swift +++ b/Sources/_CollectionsTestSupport/AssertionContexts/Assertions.swift @@ -27,7 +27,7 @@ public func expectFailure( } } -internal func _expectFailure( +public func _expectFailure( _ diagnostic: String, _ message: () -> String, trapping: Bool, diff --git a/Tests/RopeModuleTests/SeedableRandomNumberGenerator.swift b/Sources/_CollectionsTestSupport/Utilities/RepeatableRandomNumberGenerator.swift similarity index 88% rename from Tests/RopeModuleTests/SeedableRandomNumberGenerator.swift rename to Sources/_CollectionsTestSupport/Utilities/RepeatableRandomNumberGenerator.swift index 4d415f4e..3e285d0d 100644 --- a/Tests/RopeModuleTests/SeedableRandomNumberGenerator.swift +++ b/Sources/_CollectionsTestSupport/Utilities/RepeatableRandomNumberGenerator.swift @@ -9,40 +9,40 @@ // //===----------------------------------------------------------------------===// -var globalSeed: Int { - #if COLLECTIONS_RANDOMIZED_TESTING - 42.hashValue - #else - 0 - #endif -} - public struct RepeatableRandomNumberGenerator: RandomNumberGenerator { + public static var globalSeed: Int { +#if COLLECTIONS_RANDOMIZED_TESTING + 42.hashValue +#else + 0 +#endif + } + // This uses the same linear congruential generator as rand48. // FIXME: Replace with something better. internal static let _m: UInt64 = 1 << 48 internal static let _a: UInt64 = 25214903917 internal static let _c: UInt64 = 11 - + internal var _state: UInt64 - + public init(seed: Int) { self.init(seed: UInt64(truncatingIfNeeded: seed)) } - + public init(seed: UInt64) { // Perturb the seed a little so that the sequence doesn't start with a // zero value in the common case of seed == 0. (Using a zero seed is a // rather silly thing to do, but it's the easy thing.) - let global = UInt64(truncatingIfNeeded: globalSeed) + let global = UInt64(truncatingIfNeeded: Self.globalSeed) _state = seed ^ global ^ 0x536f52616e646f6d // "SoRandom" } - + private mutating func _next() -> UInt64 { _state = (Self._a &* _state &+ Self._c) & (Self._m - 1) return _state &>> 16 } - + public mutating func next() -> UInt64 { return (_next() &<< 32) | _next() } diff --git a/Sources/_CollectionsTestSupport/Utilities/SeedableRandomNumberGenerator.swift b/Sources/_CollectionsTestSupport/Utilities/SeedableRandomNumberGenerator.swift deleted file mode 100644 index f09b89eb..00000000 --- a/Sources/_CollectionsTestSupport/Utilities/SeedableRandomNumberGenerator.swift +++ /dev/null @@ -1,40 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -public struct RepeatableRandomNumberGenerator: RandomNumberGenerator { - // This uses the same linear congruential generator as rand48. - // FIXME: Replace with something better. - internal static let _m: UInt64 = 1 << 48 - internal static let _a: UInt64 = 25214903917 - internal static let _c: UInt64 = 11 - - internal var _state: UInt64 - - public init(seed: Int) { - self.init(seed: UInt64(truncatingIfNeeded: seed)) - } - - public init(seed: UInt64) { - // Perturb the seed a little so that the sequence doesn't start with a - // zero value in the common case of seed == 0. (Using a zero seed is a - // rather silly thing to do, but it's the easy thing.) - _state = seed ^ 0x536f52616e646f6d // "SoRandom" - } - - private mutating func _next() -> UInt64 { - _state = (Self._a &* _state &+ Self._c) & (Self._m - 1) - return _state &>> 16 - } - - public mutating func next() -> UInt64 { - return (_next() &<< 32) | _next() - } -} diff --git a/Tests/HashTreeCollectionsTests/Utilities.swift b/Tests/HashTreeCollectionsTests/Utilities.swift index 216b4fcd..9d0b8d71 100644 --- a/Tests/HashTreeCollectionsTests/Utilities.swift +++ b/Tests/HashTreeCollectionsTests/Utilities.swift @@ -118,22 +118,6 @@ extension LifetimeTracker { } } -func _expectFailure( - _ diagnostic: String, - _ message: () -> String, - trapping: Bool, - file: StaticString, - line: UInt -) { - expectFailure( - """ - \(diagnostic) - \(message()) - """, - trapping: trapping, - file: file, line: line) -} - func expectEqualSets( _ set: TreeSet, _ ref: [Element], diff --git a/Tests/HeapTests/NodeTests.swift b/Tests/HeapTests/HeapNodeTests.swift similarity index 86% rename from Tests/HeapTests/NodeTests.swift rename to Tests/HeapTests/HeapNodeTests.swift index 753e454e..9380ae38 100644 --- a/Tests/HeapTests/NodeTests.swift +++ b/Tests/HeapTests/HeapNodeTests.swift @@ -11,16 +11,20 @@ #if DEBUG // These unit tests need access to HeapModule internals import XCTest +#if COLLECTIONS_SINGLE_MODULE +import Collections +#else @testable import HeapModule +#endif -class NodeTests: XCTestCase { +class HeapNodeTests: XCTestCase { func test_levelCalculation() { // Check alternating min and max levels in the heap var isMin = true for exp in 0...12 { // Check [2^exp, 2^(exp + 1)) for offset in Int(pow(2, Double(exp)) - 1).. Index { - index(startIndex, offsetBy: offset) - } - - @inline(__always) - internal func _offset(of index: Index) -> Int { - distance(from: startIndex, to: index) - } - @inline(__always) internal func _indexRange(at offsets: Range) -> Range { _index(at: offsets.lowerBound) ..< _index(at: offsets.upperBound) @@ -42,11 +36,6 @@ extension RandomAccessCollection { return _offsetRange(of: range.relative(to: self)) } - @inline(__always) - internal subscript(_offset offset: Int) -> Element { - self[_index(at: offset)] - } - @inline(__always) internal subscript(_offsets range: Range) -> SubSequence { self[_indexRange(at: range)] diff --git a/Tests/RopeModuleTests/TestBigString.swift b/Tests/RopeModuleTests/TestBigString.swift index 1b10622b..fd0f6306 100644 --- a/Tests/RopeModuleTests/TestBigString.swift +++ b/Tests/RopeModuleTests/TestBigString.swift @@ -30,7 +30,7 @@ class TestBigString: CollectionTestCase { } override func setUp() { - print("Global seed: \(globalSeed)") + print("Global seed: \(RepeatableRandomNumberGenerator.globalSeed)") super.setUp() } diff --git a/Tests/RopeModuleTests/TestRope.swift b/Tests/RopeModuleTests/TestRope.swift index d0b429b9..471b2944 100644 --- a/Tests/RopeModuleTests/TestRope.swift +++ b/Tests/RopeModuleTests/TestRope.swift @@ -116,7 +116,7 @@ struct Chunk: RopeElement, Equatable, CustomStringConvertible { class TestRope: XCTestCase { override func setUp() { - print("Global seed: \(globalSeed)") + print("Global seed: \(RepeatableRandomNumberGenerator.globalSeed)") } func test_empty() {