Skip to content

Commit

Permalink
[Heap] Enable heap tests in optimized builds (apple#101)
Browse files Browse the repository at this point in the history
@testable import is replaced with @_spi(Testing) import.
A test for _Node is moved to another file because this test needs access to internals of PriorityQueueModule and this test will not work in optimized builds.
  • Loading branch information
just-gull committed Oct 7, 2021
1 parent 52d426a commit cfb6dd1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
17 changes: 1 addition & 16 deletions Tests/PriorityQueueTests/HeapTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
//
//===----------------------------------------------------------------------===//

#if DEBUG
import XCTest
@testable import PriorityQueueModule
@_spi(Testing) import PriorityQueueModule

final class HeapTests: XCTestCase {
func test_isEmpty() {
Expand Down Expand Up @@ -349,19 +348,6 @@ final class HeapTests: XCTestCase {

// MARK: -

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)..<Int(pow(2, Double(exp + 1)) - 1) {
let node = _Node(offset: offset)
XCTAssertEqual(node.isMinLevel, isMin)
}
isMin.toggle()
}
}

func test_initializer_fromCollection() {
var heap = Heap((1...20).shuffled())
XCTAssertEqual(heap.max(), 20)
Expand Down Expand Up @@ -420,4 +406,3 @@ final class HeapTests: XCTestCase {
}
}
}
#endif
30 changes: 30 additions & 0 deletions Tests/PriorityQueueTests/NodeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#if DEBUG // These unit tests need access to PriorityQueueModule internals
import XCTest
@_spi(Testing) @testable import PriorityQueueModule

class NodeTests: 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)..<Int(pow(2, Double(exp + 1)) - 1) {
let node = _Node(offset: offset)
XCTAssertEqual(node.isMinLevel, isMin)
}
isMin.toggle()
}
}
}
#endif // DEBUG

0 comments on commit cfb6dd1

Please sign in to comment.