Skip to content

Commit b2720a3

Browse files
authored
[AutoDiff] Move AD leak checking utilities to shared location. (swiftlang#26373)
Move AD leak checking utilities to DifferentiableUnittest so they can be used by multiple tests.
1 parent 761d372 commit b2720a3

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

stdlib/private/DifferentiationUnittest/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ add_swift_target_library(swiftDifferentiationUnittest ${SWIFT_STDLIB_LIBRARY_BUI
22
# This file should be listed first. Module name is inferred from the filename.
33
DifferentiationUnittest.swift
44

5-
SWIFT_COMPILE_FLAGS
5+
SWIFT_MODULE_DEPENDS StdlibUnittest
66
INSTALL_IN_COMPONENT stdlib-experimental
77
DARWIN_INSTALL_NAME_DIR "${SWIFT_DARWIN_STDLIB_PRIVATE_INSTALL_NAME_DIR}")

stdlib/private/DifferentiationUnittest/DifferentiationUnittest.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,44 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import StdlibUnittest
14+
1315
public enum _GlobalLeakCount {
1416
public static var count = 0
1517
}
1618

19+
/// Execute body and check expected leak count.
20+
public func withLeakChecking(
21+
expectedLeakCount: Int = 0, file: String = #file, line: UInt = #line,
22+
_ body: () -> Void
23+
) {
24+
// Note: compare expected leak count with relative leak count after
25+
// running `body`.
26+
// This approach is more robust than comparing leak count with zero
27+
// and resetting leak count to zero, which is stateful and causes issues.
28+
let beforeLeakCount = _GlobalLeakCount.count
29+
body()
30+
let leakCount = _GlobalLeakCount.count - beforeLeakCount
31+
expectEqual(
32+
expectedLeakCount, leakCount, "Leaks detected: \(leakCount)",
33+
file: file, line: line)
34+
}
35+
36+
public extension TestSuite {
37+
/// Execute test function and check expected leak count.
38+
func testWithLeakChecking(
39+
_ name: String,
40+
expectedLeakCount: Int = 0,
41+
file: String = #file, line: UInt = #line,
42+
_ testFunction: @escaping () -> Void
43+
) {
44+
test(name, file: file, line: line) {
45+
withLeakChecking(expectedLeakCount: expectedLeakCount, file: file,
46+
line: line, testFunction)
47+
}
48+
}
49+
}
50+
1751
/// A type that tracks the number of live instances of a wrapped value type.
1852
///
1953
/// `Tracked<T>` is used to check for memory leaks in functions created via

test/AutoDiff/leakchecking.swift

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,6 @@ import DifferentiationUnittest
88

99
var LeakCheckingTests = TestSuite("LeakChecking")
1010

11-
/// Execute body and check expected leak count.
12-
func withLeakChecking(
13-
expectedLeakCount: Int = 0, file: String = #file, line: UInt = #line,
14-
_ body: () -> Void
15-
) {
16-
// Note: compare expected leak count with relative leak count after
17-
// running `body`.
18-
// This approach is more robust than comparing leak count with zero
19-
// and resetting leak count to zero, which is stateful and causes issues.
20-
let beforeLeakCount = _GlobalLeakCount.count
21-
body()
22-
let leakCount = _GlobalLeakCount.count - beforeLeakCount
23-
expectEqual(
24-
expectedLeakCount, leakCount, "Leaks detected: \(leakCount)",
25-
file: file, line: line)
26-
}
27-
28-
extension TestSuite {
29-
func testWithLeakChecking(
30-
_ name: String,
31-
expectedLeakCount: Int = 0,
32-
file: String = #file, line: UInt = #line,
33-
_ testFunction: @escaping () -> Void
34-
) {
35-
test(name, file: file, line: line) {
36-
withLeakChecking(expectedLeakCount: expectedLeakCount, file: file,
37-
line: line, testFunction)
38-
}
39-
}
40-
}
41-
4211
struct ExampleLeakModel : Differentiable {
4312
var bias: Tracked<Float> = 2.0
4413
func applied(to input: Tracked<Float>) -> Tracked<Float> {

0 commit comments

Comments
 (0)