Skip to content

Commit 5dab0f6

Browse files
committed
Add evaluation tests for multiple assignments
1 parent 65c428b commit 5dab0f6

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

tests/run/multiple-assignment.check

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
after swap: (2, 4)
2+
after swap: (2, 4)
3+
after swap: (2, 4)
4+
load 1
5+
load 2
6+
load 2
7+
load 4 from 2
8+
load 1
9+
load 2 from 1
10+
store 4 to 1
11+
store 2 to 2
12+
after swap: (4, 2)

tests/run/multiple-assignment.scala

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
object Test:
2+
3+
class Container[T](val id: Int, var x: T):
4+
5+
def y: T =
6+
println(s"load ${x} from ${id}")
7+
x
8+
9+
def y_=(newValue: T): Unit =
10+
println(s"store ${newValue} to ${id}")
11+
this.x_=(newValue)
12+
13+
def main(args: Array[String]): Unit =
14+
// simple swap
15+
var x1 = 4
16+
var x2 = 2
17+
(x1, x2) = (x2, x1)
18+
println(s"after swap: (${x1}, ${x2})")
19+
20+
// swap in a container
21+
val a = Array(4, 2)
22+
(a(0), a(1)) = (a(1), a(0))
23+
println(s"after swap: (${a(0)}, ${a(1)})")
24+
25+
// swap fields with effectless left-hand sides
26+
var c1 = Container(1, 4)
27+
var c2 = Container(2, 2)
28+
(c1.x, c2.x) = (c2.x, c1.x)
29+
println(s"after swap: (${c1.x}, ${c2.x})")
30+
31+
// swap fields with side effectful left-hand sides
32+
def f(n: Int): Container[Int] =
33+
println(s"load ${n}")
34+
n match
35+
case 1 => c1
36+
case 2 => c2
37+
(f(1).y, f(2).y) = (f(2).y, f(1).y)
38+
println(s"after swap: (${c1.x}, ${c2.x})")

0 commit comments

Comments
 (0)