aboutsummaryrefslogtreecommitdiff
path: root/test/values.tm
blob: 9f86c012343c0ae2c1bed7f8ed197b35b8fdb59e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Tests for ensuring immutable value nature in various contexts
struct Inner(xs:[Int32])

struct Outer(inner:Inner)

enum HoldsList(HasList(xs:[Int32]))

func sneaky(outer:Outer)
    (&outer.inner.xs)[1] = 99

func sneaky2(outer:&Outer)
    (&outer.inner.xs)[1] = 99

func main()
    do
        xs := [10, 20, 30]
        copy := xs
        (&xs)[1] = 99
        assert xs == [99, 20, 30]
        assert copy == [10, 20, 30]

    do
        t := {"A":10, "B":20}
        copy := t
        (&t)["A"] = 99
        assert t == {"A":99, "B":20}
        assert copy == {"A":10, "B":20}

    do
        foo := Outer(Inner([10, 20, 30]))
        copy := foo
        (&foo.inner.xs)[1] = 99
        assert foo.inner.xs == [99, 20, 30]
        assert copy.inner.xs == [10, 20, 30]

    do
        foo := Outer(Inner([10, 20, 30]))
        copy := foo
        sneaky(foo)
        assert foo.inner.xs == [10, 20, 30]
        assert copy.inner.xs == [10, 20, 30]

    do
        foo := Outer(Inner([10, 20, 30]))
        copy := foo
        sneaky2(&foo)
        assert foo.inner.xs == [99, 20, 30]
        assert copy.inner.xs == [10, 20, 30]

    do
        x := HoldsList.HasList([10, 20, 30])
        when x is HasList(list)
            (&list)[1] = 99

        assert x == HoldsList.HasList([10, 20, 30])