2020-08-02 11:42:29 +00:00
|
|
|
package compiler_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddressOfLiteral(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
type Foo struct { A int }
|
|
|
|
func Main() int {
|
|
|
|
f := &Foo{}
|
|
|
|
setA(f, 3)
|
|
|
|
return f.A
|
|
|
|
}
|
|
|
|
func setA(s *Foo, a int) { s.A = a }`
|
|
|
|
eval(t, src, big.NewInt(3))
|
|
|
|
}
|
2020-08-02 11:56:47 +00:00
|
|
|
|
|
|
|
func TestPointerDereference(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
type Foo struct { A int }
|
|
|
|
func Main() int {
|
|
|
|
f := &Foo{A: 4}
|
|
|
|
setA(*f, 3)
|
|
|
|
return f.A
|
|
|
|
}
|
|
|
|
func setA(s Foo, a int) { s.A = a }`
|
|
|
|
eval(t, src, big.NewInt(4))
|
|
|
|
}
|
2020-08-05 06:33:18 +00:00
|
|
|
|
|
|
|
func TestStructArgCopy(t *testing.T) {
|
|
|
|
t.Run("Simple", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
type Foo struct { A int }
|
|
|
|
func Main() int {
|
|
|
|
f := Foo{A: 4}
|
|
|
|
setA(f, 3)
|
|
|
|
return f.A
|
|
|
|
}
|
|
|
|
func setA(s Foo, a int) { s.A = a }`
|
|
|
|
eval(t, src, big.NewInt(4))
|
|
|
|
})
|
|
|
|
t.Run("StructField", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
type Bar struct { A int }
|
|
|
|
type Foo struct { B Bar }
|
|
|
|
func Main() int {
|
|
|
|
f := Foo{B: Bar{A: 4}}
|
|
|
|
setA(f, 3)
|
|
|
|
return f.B.A
|
|
|
|
}
|
|
|
|
func setA(s Foo, a int) { s.B.A = a }`
|
|
|
|
eval(t, src, big.NewInt(4))
|
|
|
|
})
|
|
|
|
t.Run("StructPointerField", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
type Bar struct { A int }
|
|
|
|
type Foo struct { B *Bar }
|
|
|
|
func Main() int {
|
|
|
|
f := Foo{B: &Bar{A: 4}}
|
|
|
|
setA(f, 3)
|
|
|
|
return f.B.A
|
|
|
|
}
|
|
|
|
func setA(s Foo, a int) { s.B.A = a }`
|
|
|
|
eval(t, src, big.NewInt(3))
|
|
|
|
})
|
|
|
|
}
|