From 7e78002909f63cebaaba8be396a430ae0d22e4e9 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Wed, 7 Sep 2022 19:36:15 -0700 Subject: [PATCH] [PersistentCollections] Move BitmapSmokeTests into its own file --- .../BitmapSmokeTests.swift | 66 +++++++++++++++++++ .../PersistentCollections Smoke Tests.swift | 52 --------------- 2 files changed, 66 insertions(+), 52 deletions(-) create mode 100644 Tests/PersistentCollectionsTests/BitmapSmokeTests.swift diff --git a/Tests/PersistentCollectionsTests/BitmapSmokeTests.swift b/Tests/PersistentCollectionsTests/BitmapSmokeTests.swift new file mode 100644 index 00000000..e565970e --- /dev/null +++ b/Tests/PersistentCollectionsTests/BitmapSmokeTests.swift @@ -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()) + } +} diff --git a/Tests/PersistentCollectionsTests/PersistentCollections Smoke Tests.swift b/Tests/PersistentCollectionsTests/PersistentCollections Smoke Tests.swift index f3f974d2..ed092521 100644 --- a/Tests/PersistentCollectionsTests/PersistentCollections Smoke Tests.swift +++ b/Tests/PersistentCollectionsTests/PersistentCollections Smoke Tests.swift @@ -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()) - } -}