Skip to content

Commit e756fe8

Browse files
committed
Add autodiff execution test for modify accessor differentiation
1 parent 053b245 commit e756fe8

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: %target-run-simple-swift
2+
// REQUIRES: executable_test
3+
4+
import _Differentiation
5+
import StdlibUnittest
6+
7+
var ModifyAccessorTests = TestSuite("ModifyAccessor")
8+
9+
ModifyAccessorTests.test("SimpleModifyAccessor") {
10+
struct S: Differentiable {
11+
private var _x : Float
12+
13+
func _endMutation() {}
14+
15+
var x: Float {
16+
get{_x}
17+
set(newValue) { _x = newValue }
18+
_modify {
19+
defer { _endMutation() }
20+
if (x > 0) {
21+
yield &_x
22+
} else {
23+
yield &_x
24+
}
25+
}
26+
}
27+
28+
init(_ x : Float) {
29+
self._x = x
30+
}
31+
}
32+
33+
func modify_struct(_ x : Float) -> Float {
34+
var s = S(x)
35+
s.x *= s.x
36+
return s.x
37+
}
38+
39+
expectEqual((100, 20), valueWithGradient(at: 10, of: modify_struct))
40+
}
41+
42+
runAllTests()
43+

0 commit comments

Comments
 (0)