mirror of
https://github.com/apple/swift-collections
synced 2026-11-28 05:04:21 +00:00
[PersistentCollections] Move BitmapSmokeTests into its own file
This commit is contained in:
1 parent
1a4993dfda
commit
7e78002909
2 files changed
+66
-52
No files matched your search
@@ -0,0 +1,66 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2022 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import _CollectionsTestSupport
|
||||
import XCTest
|
||||
@testable import PersistentCollections
|
||||
|
||||
final class BitmapSmokeTests: CollectionTestCase {
|
||||
func test_BitPartitionSize_isValid() {
|
||||
expectTrue(bitPartitionSize > 0)
|
||||
expectTrue((2 << (bitPartitionSize - 1)) != 0)
|
||||
expectTrue((2 << (bitPartitionSize - 1)) <= Bitmap.bitWidth)
|
||||
}
|
||||
|
||||
func test_Capacity_isValid() {
|
||||
expectTrue(Int(2 << bitPartitionSize) <= Int(Capacity.max))
|
||||
}
|
||||
|
||||
func test_Bitmap_nonzeroBits() {
|
||||
let bitmap: Bitmap = 0b0100_0000_0000_1011
|
||||
|
||||
expectEqual(Array(bitmap._nonzeroBits()), [0, 1, 3, 14])
|
||||
expectEqual(
|
||||
Array(bitmap._zeroBits()),
|
||||
(0 ..< Bitmap.bitWidth).filter { ![0, 1, 3, 14].contains($0) })
|
||||
}
|
||||
|
||||
func test_Bitmap_nonzeroBitsToArray() {
|
||||
let bitmap: Bitmap = 0b0100_0000_0000_1011
|
||||
|
||||
let counts = bitmap._nonzeroBits().reduce(
|
||||
into: Array(repeating: 0, count: Bitmap.bitWidth)
|
||||
) { counts, index in
|
||||
counts[index] = 1
|
||||
}
|
||||
|
||||
expectEqual(counts.count, Bitmap.bitWidth)
|
||||
expectEqual(counts.reduce(0, +), bitmap.nonzeroBitCount)
|
||||
expectEqual(counts.reduce(0, +), 4)
|
||||
expectEqual(counts[0], 1)
|
||||
expectEqual(counts[1], 1)
|
||||
expectEqual(counts[3], 1)
|
||||
expectEqual(counts[14], 1)
|
||||
}
|
||||
|
||||
func test_Bitmap_enumerateCompactedArray() {
|
||||
let bitmap: Bitmap = 0b0100_0000_0000_1011
|
||||
let elements: [String] = ["zero", "one", "three", "fourteen"]
|
||||
|
||||
var zipIterator = zip(bitmap._nonzeroBits(), elements).makeIterator()
|
||||
|
||||
expectEqual(zipIterator.next()!, (0, "zero"))
|
||||
expectEqual(zipIterator.next()!, (1, "one"))
|
||||
expectEqual(zipIterator.next()!, (3, "three"))
|
||||
expectEqual(zipIterator.next()!, (14, "fourteen"))
|
||||
expectNil(zipIterator.next())
|
||||
}
|
||||
}
|
||||
@@ -613,55 +613,3 @@ final class CapsuleSmokeTests: CollectionTestCase {
|
||||
expectTrue(expectedPositions.isEmpty)
|
||||
}
|
||||
}
|
||||
|
||||
final class BitmapSmokeTests: CollectionTestCase {
|
||||
func test_BitPartitionSize_isValid() {
|
||||
expectTrue(bitPartitionSize > 0)
|
||||
expectTrue((2 << (bitPartitionSize - 1)) != 0)
|
||||
expectTrue((2 << (bitPartitionSize - 1)) <= Bitmap.bitWidth)
|
||||
}
|
||||
|
||||
func test_Capacity_isValid() {
|
||||
expectTrue(Int(2 << bitPartitionSize) <= Int(Capacity.max))
|
||||
}
|
||||
|
||||
func test_Bitmap_nonzeroBits() {
|
||||
let bitmap: Bitmap = 0b0100_0000_0000_1011
|
||||
|
||||
expectEqual(Array(bitmap.nonzeroBits()), [0, 1, 3, 14])
|
||||
expectEqual(
|
||||
Array(bitmap.zeroBits()),
|
||||
(0 ..< Bitmap.bitWidth).filter { ![0, 1, 3, 14].contains($0) })
|
||||
}
|
||||
|
||||
func test_Bitmap_nonzeroBitsToArray() {
|
||||
let bitmap: Bitmap = 0b0100_0000_0000_1011
|
||||
|
||||
let counts = bitmap.nonzeroBits().reduce(
|
||||
into: Array(repeating: 0, count: Bitmap.bitWidth)
|
||||
) { counts, index in
|
||||
counts[index] = 1
|
||||
}
|
||||
|
||||
expectEqual(counts.count, Bitmap.bitWidth)
|
||||
expectEqual(counts.reduce(0, +), bitmap.nonzeroBitCount)
|
||||
expectEqual(counts.reduce(0, +), 4)
|
||||
expectEqual(counts[0], 1)
|
||||
expectEqual(counts[1], 1)
|
||||
expectEqual(counts[3], 1)
|
||||
expectEqual(counts[14], 1)
|
||||
}
|
||||
|
||||
func test_Bitmap_enumerateCompactedArray() {
|
||||
let bitmap: Bitmap = 0b0100_0000_0000_1011
|
||||
let elements: [String] = ["zero", "one", "three", "fourteen"]
|
||||
|
||||
var zipIterator = zip(bitmap.nonzeroBits(), elements).makeIterator()
|
||||
|
||||
expectEqual(zipIterator.next()!, (0, "zero"))
|
||||
expectEqual(zipIterator.next()!, (1, "one"))
|
||||
expectEqual(zipIterator.next()!, (3, "three"))
|
||||
expectEqual(zipIterator.next()!, (14, "fourteen"))
|
||||
expectNil(zipIterator.next())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user