//===----------------------------------------------------------------------===// // // This source file is part of the Swift Collections open source project // // Copyright (c) 2025 - 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 compiler(>=6.4) && UnstableContainersPreview import XCTest #if COLLECTIONS_SINGLE_MODULE import Collections #else import ContainersPreview #endif @available(SwiftStdlib 5.0, *) @inlinable public func checkIterable< S: Iterable_ & ~Copyable & ~Escapable, Expected: Sequence >( _ iterable: borrowing S, expectedContents: Expected, file: StaticString = #filePath, line: UInt = #line ) throws(S.Failure_) where S.Element_: Equatable { try checkIterable( iterable, expectedContents: expectedContents, by: ==, file: file, line: line) } @available(SwiftStdlib 5.0, *) @inlinable public func checkIterable< S: Iterable_ & ~Copyable & ~Escapable, Expected: Sequence >( _ iterable: borrowing S, expectedContents: Expected, by areEquivalent: (S.Element_, S.Element_) -> Bool, file: StaticString = #filePath, line: UInt = #line ) throws(S.Failure_) where S.Element_: Equatable { let entry = TestContext.current.push("checkIterable", file: file, line: line) defer { TestContext.current.pop(entry) } let expectedContents = Array(expectedContents) expectLessThanOrEqual(iterable.underestimatedCount_, expectedContents.count) // Check that the spans seem plausibly sized and that the indices are monotonic. let spanShapes: [Range] = try { () throws(S.Failure_) in var r: [Range] = [] var pos = 0 var it = iterable.makeIterableIterator_() while true { let origPos = pos let span = try it.nextSpan_() pos += span.count if span.isEmpty { break } r.append(origPos ..< pos) } return r }() expectEqual( spanShapes.Swift::reduce(into: 0, { $0 += $1.count }), expectedContents.count, "Container's count does not match the sum of its spans") // Check that the spans have stable sizes and the expected contents. do { var pos = 0 var it = iterable.makeIterableIterator_() var spanIndex = 0 while true { let span = try it.nextSpan_() if span.isEmpty { break } expectEqual( span.count, spanShapes[spanIndex].count, "Container has nondeterministic span sizes") for i in 0 ..< span.count { expectEqual(span[i], expectedContents[pos]) pos += 1 } spanIndex += 1 } expectEqual(spanIndex, spanShapes.endIndex) expectEqual(pos, expectedContents.count) } // Check that we can iterate one by one. do { var pos = 0 var it = iterable.makeIterableIterator_() while true { let span = try it.nextSpan_(maximumCount: 1) if span.isEmpty { break } expectEqual(span.count, 1) for i in 0 ..< span.count { expectEqual(span[i], expectedContents[pos]) pos += 1 } } expectEqual(pos, expectedContents.count) } // Check that we can iterate with huge maximum counts do { var pos = 0 var it = iterable.makeIterableIterator_() var spanIndex = 0 while true { let span = try it.nextSpan_(maximumCount: Int.max) if span.isEmpty { break } expectEqual( span.count, spanShapes[spanIndex].count, "Container has inconsistent/nondeterministic span sizes") for i in 0 ..< span.count { expectEqual(span[i], expectedContents[pos]) pos += 1 } spanIndex += 1 } expectEqual(spanIndex, spanShapes.endIndex) expectEqual(pos, expectedContents.count) } } #endif