Skip to content

Commit 500686f

Browse files
authored
Merge pull request #64664 from hborla/parameter-pack-library-test
2 parents 8990a12 + 58abfb8 commit 500686f

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
public struct Key: Hashable {
2+
static var index = 0
3+
4+
public let id: Int
5+
6+
init() {
7+
self.id = Self.index
8+
Self.index += 1
9+
}
10+
}
11+
12+
public struct Variable<Value> {
13+
public let key = Key()
14+
}
15+
16+
public struct Bindings {
17+
private var storage: [Key : Any] = [:]
18+
19+
public init<each T>(
20+
_ value: repeat (Variable<each T>, each T)
21+
) {
22+
_ = (repeat storage[(each value).0.key] = (each value).1)
23+
}
24+
}
25+
26+
public protocol Expression<Result> {
27+
associatedtype Result
28+
func evaluate(_ bindings: Bindings) throws -> Result
29+
}
30+
31+
public struct True: Expression {
32+
public init() {}
33+
34+
public func evaluate(_ bindings: Bindings) throws -> Bool {
35+
true
36+
}
37+
}
38+
39+
public struct Predicate<each Input> {
40+
var variables: (repeat Variable<each Input>)
41+
var expression: any Expression<Bool>
42+
43+
public init<Expr>(
44+
builder: (repeat Variable<each Input>) -> Expr
45+
) where Expr: Expression<Bool> {
46+
self.variables = (repeat Variable<each Input>())
47+
self.expression = builder(repeat each variables.element)
48+
}
49+
50+
public func evaluate(
51+
_ input: repeat each Input
52+
) throws -> Bool {
53+
return try expression.evaluate(
54+
.init(repeat (each variables.element, each input))
55+
)
56+
}
57+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-build-swift-dylib(%t/%target-library-name(variadic_generic_library)) -Xfrontend -disable-availability-checking -enable-experimental-feature VariadicGenerics -enable-library-evolution %S/Inputs/variadic_generic_library.swift -emit-module -emit-module-path %t/variadic_generic_library.swiftmodule -module-name variadic_generic_library
4+
// RUN: %target-codesign %t/%target-library-name(variadic_generic_library)
5+
6+
// RUN: %target-build-swift %s -Xfrontend -disable-availability-checking -enable-experimental-feature VariadicGenerics -lvariadic_generic_library -I %t -L %t -o %t/main %target-rpath(%t)
7+
// RUN: %target-codesign %t/main
8+
9+
// RUN: %target-run %t/main %t/%target-library-name(variadic_generic_library)
10+
11+
// REQUIRES: executable_test
12+
13+
// FIXME: Optimizations disabled for all parameter pack tests in test/Interpreter/
14+
// REQUIRES: swift_test_mode_optimize_none
15+
16+
// Because of -enable-experimental-feature VariadicGenerics
17+
// REQUIRES: asserts
18+
19+
20+
import variadic_generic_library
21+
import StdlibUnittest
22+
23+
var ImportParameterPackTests = TestSuite("ImportParameterPackTests")
24+
25+
ImportParameterPackTests.test("basic") {
26+
let closure: (Variable<Int>) -> _ = { a in
27+
True()
28+
}
29+
30+
let predicate = Predicate.init(builder: closure)
31+
let result = try! predicate.evaluate(1)
32+
expectEqual(result, true)
33+
}
34+
35+
ImportParameterPackTests.test("no inputs") {
36+
let closure: () -> _ = {
37+
True()
38+
}
39+
40+
let predicate = Predicate.init(builder: closure)
41+
let result = try! predicate.evaluate()
42+
expectEqual(result, true)
43+
}
44+
45+
46+
ImportParameterPackTests.test("multi-input") {
47+
let closure: (Variable<Int>, Variable<String>, Variable<Bool>) -> _ = { a, b, c in
48+
True()
49+
}
50+
51+
let predicate = Predicate<Int, String, Bool>(builder: closure)
52+
let result = try! predicate.evaluate(1, "hello", true)
53+
expectEqual(result, true)
54+
}
55+
56+
runAllTests()

0 commit comments

Comments
 (0)