Skip to content

Commit 4f80127

Browse files
committed
Test kwargs
1 parent 906cb66 commit 4f80127

File tree

2 files changed

+64
-19
lines changed

2 files changed

+64
-19
lines changed

src/copyable_task.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ mutable struct TapedTask{Tdynamic_scope,Tfargs,Tmc<:MistyClosure}
4747
end
4848

4949
"""
50-
TapedTask(dynamic_scope::Any, f, args...)
50+
TapedTask(dynamic_scope::Any, f, args...; kwargs...)
5151
5252
Construct a `TapedTask` with the specified `dynamic_scope`, for function `f` and positional
5353
arguments `args`.
@@ -162,10 +162,11 @@ julia> consume(t)
162162
`Int`s have been used here, but it is permissible to set the value returned by
163163
[`Libtask.get_dynamic_scope`](@ref) to anything you like.
164164
"""
165-
function TapedTask(dynamic_scope::Any, fargs...)
165+
function TapedTask(dynamic_scope::Any, fargs...; kwargs...)
166166
seed_id!()
167-
mc, count_ref = build_callable(typeof(fargs))
168-
return TapedTask(dynamic_scope, fargs, mc, count_ref)
167+
all_args = isempty(kwargs) ? fargs : (Core.kwcall, getfield(kwargs, :data), fargs...)
168+
mc, count_ref = build_callable(typeof(all_args))
169+
return TapedTask(dynamic_scope, all_args, mc, count_ref)
169170
end
170171

171172
"""

src/test_utils.jl

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@ struct Testcase
88
name::String
99
dynamic_scope::Any
1010
fargs::Tuple
11+
kwargs::Union{NamedTuple,Nothing}
1112
expected_iteration_results::Vector
1213
end
1314

1415
function (case::Testcase)()
1516
testset = @testset "$(case.name)" begin
1617

1718
# Construct the task.
18-
t = TapedTask(case.dynamic_scope, case.fargs...)
19+
if case.kwargs === nothing
20+
t = TapedTask(case.dynamic_scope, case.fargs...)
21+
else
22+
t = TapedTask(case.dynamic_scope, case.fargs...; case.kwargs...)
23+
end
1924

2025
# Iterate through t. Record the results, and take a copy after each iteration.
2126
iteration_results = []
@@ -42,52 +47,89 @@ function test_cases()
4247
"single block",
4348
nothing,
4449
(single_block, 5.0),
50+
nothing,
4551
[sin(5.0), sin(sin(5.0)), sin(sin(sin(5.0))), sin(sin(sin(sin(5.0))))],
4652
),
47-
Testcase("produce old", nothing, (produce_old_value, 5.0), [sin(5.0), sin(5.0)]),
48-
Testcase("branch on old value l", nothing, (branch_on_old_value, 2.0), [true, 2.0]),
4953
Testcase(
50-
"branch on old value r", nothing, (branch_on_old_value, -1.0), [false, -2.0]
54+
"produce old", nothing, (produce_old_value, 5.0), nothing, [sin(5.0), sin(5.0)]
55+
),
56+
Testcase(
57+
"branch on old value l",
58+
nothing,
59+
(branch_on_old_value, 2.0),
60+
nothing,
61+
[true, 2.0],
62+
),
63+
Testcase(
64+
"branch on old value r",
65+
nothing,
66+
(branch_on_old_value, -1.0),
67+
nothing,
68+
[false, -2.0],
5169
),
52-
Testcase("no produce", nothing, (no_produce_test, 5.0, 4.0), []),
53-
Testcase("new object", nothing, (new_object_test, 5, 4), [C(5, 4), C(5, 4)]),
70+
Testcase("no produce", nothing, (no_produce_test, 5.0, 4.0), nothing, []),
5471
Testcase(
55-
"branching test l", nothing, (branching_test, 5.0, 4.0), [string(sin(5.0))]
72+
"new object", nothing, (new_object_test, 5, 4), nothing, [C(5, 4), C(5, 4)]
5673
),
5774
Testcase(
58-
"branching test r", nothing, (branching_test, 4.0, 5.0), [sin(4.0) * cos(5.0)]
75+
"branching test l",
76+
nothing,
77+
(branching_test, 5.0, 4.0),
78+
nothing,
79+
[string(sin(5.0))],
5980
),
60-
Testcase("unused argument test", nothing, (unused_argument_test, 3), [1]),
61-
Testcase("test with const", nothing, (test_with_const,), [1]),
62-
Testcase("while loop", nothing, (while_loop,), collect(1:9)),
81+
Testcase(
82+
"branching test r",
83+
nothing,
84+
(branching_test, 4.0, 5.0),
85+
nothing,
86+
[sin(4.0) * cos(5.0)],
87+
),
88+
Testcase("unused argument test", nothing, (unused_argument_test, 3), nothing, [1]),
89+
Testcase("test with const", nothing, (test_with_const,), nothing, [1]),
90+
Testcase("while loop", nothing, (while_loop,), nothing, collect(1:9)),
6391
Testcase(
6492
"foreigncall tester",
6593
nothing,
6694
(foreigncall_tester, "hi"),
95+
nothing,
6796
[Ptr{UInt8}, Ptr{UInt8}],
6897
),
69-
Testcase("dynamic scope 1", 5, (dynamic_scope_tester_1,), [5]),
70-
Testcase("dynamic scope 2", 6, (dynamic_scope_tester_1,), [6]),
71-
Testcase("nested (static)", nothing, (static_nested_outer,), [true, false]),
98+
Testcase("dynamic scope 1", 5, (dynamic_scope_tester_1,), nothing, [5]),
99+
Testcase("dynamic scope 2", 6, (dynamic_scope_tester_1,), nothing, [6]),
100+
Testcase(
101+
"nested (static)", nothing, (static_nested_outer,), nothing, [true, false]
102+
),
72103
Testcase(
73104
"nested (static + used)",
74105
nothing,
75106
(static_nested_outer_use_produced,),
107+
nothing,
76108
[true, 1],
77109
),
78110
Testcase(
79111
"nested (dynamic)",
80112
nothing,
81113
(dynamic_nested_outer, Ref{Any}(nested_inner)),
114+
nothing,
82115
[true, false],
83116
),
84117
Testcase(
85118
"nested (dynamic + used)",
86119
nothing,
87120
(dynamic_nested_outer_use_produced, Ref{Any}(nested_inner)),
121+
nothing,
88122
[true, 1],
89123
),
90-
Testcase("callable struct", nothing, (CallableStruct(5), 4), [5, 4, 9]),
124+
Testcase("callable struct", nothing, (CallableStruct(5), 4), nothing, [5, 4, 9]),
125+
Testcase(
126+
"kwarg tester 1",
127+
nothing,
128+
(Core.kwcall, (; y=5.0), kwarg_tester, 4.0),
129+
nothing,
130+
[],
131+
),
132+
Testcase("kwargs tester 2", nothing, (kwarg_tester, 4.0), (; y=5.0), []),
91133
]
92134
end
93135

@@ -222,4 +264,6 @@ function (c::CallableStruct)(y)
222264
return nothing
223265
end
224266

267+
kwarg_tester(x; y) = x + y
268+
225269
end

0 commit comments

Comments
 (0)