Skip to content

Commit fcd8249

Browse files
deelawnjaekwon
authored andcommitted
improved append test and added more append deep copies
1 parent ba6e747 commit fcd8249

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

gno.land/cmd/gnoland/testdata/append.txtar

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ gnokey maketx call -pkgpath gno.land/r/append -func Render -gas-fee 1000000ugnot
3939
stdout '("2-3-42-" string)'
4040
stdout OK!
4141

42+
gnokey maketx call -pkgpath gno.land/r/append -func CopyAppend -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
43+
stdout OK!
44+
45+
gnokey maketx call -pkgpath gno.land/r/append -func PopB -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
46+
stdout OK!
47+
48+
# Call render
49+
gnokey maketx call -pkgpath gno.land/r/append -func Render -gas-fee 1000000ugnot -gas-wanted 2000000 -args '' -broadcast -chainid=tendermint_test test1
50+
stdout '("2-3-42-" string)'
51+
stdout OK!
52+
53+
gnokey maketx call -pkgpath gno.land/r/append -func AppendMoreAndC -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
54+
stdout OK!
55+
56+
gnokey maketx call -pkgpath gno.land/r/append -func ReassignC -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
57+
stdout OK!
58+
59+
gnokey maketx call -pkgpath gno.land/r/append -func Render -gas-fee 1000000ugnot -gas-wanted 2000000 -args '' -broadcast -chainid=tendermint_test test1
60+
stdout '("2-3-42-70-100-" string)'
61+
stdout OK!
62+
4263
-- append.gno --
4364
package append
4465

@@ -48,7 +69,9 @@ import (
4869

4970
type T struct{ i int }
5071

51-
var a []T
72+
var a, b []T
73+
var c = []T{{i: 100}}
74+
5275

5376
func init() {
5477
a = make([]T, 0, 1)
@@ -62,6 +85,25 @@ func Append(i int) {
6285
a = append(a, T{i: i})
6386
}
6487

88+
func CopyAppend() {
89+
b = append(a, T{i: 50}, T{i: 60})
90+
}
91+
92+
func PopB() {
93+
b = append(b[:0], b[1:]...)
94+
}
95+
96+
func AppendMoreAndC() {
97+
// Fill to capacity
98+
a = append(a, T{i: 70})
99+
// Above capacity; make new array
100+
a = append(a, c...)
101+
}
102+
103+
func ReassignC() {
104+
c[0] = T{i: 200}
105+
}
106+
65107
func Render(path string) string {
66108
var s string
67109
for i:=0;i<len(a);i++{

gnovm/pkg/gnolang/uverse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ func UverseNode() *PackageNode {
376376
if 0 < xvl {
377377
if xvb.Data == nil {
378378
for i := 0; i < xvl; i++ {
379-
list[i] = xvb.List[xvo+i].Copy(m.Alloc)
379+
list[i] = xvb.List[xvo+i].DeepCopy(m.Alloc, m.Store)
380380
}
381381
} else {
382382
panic("should not happen")
@@ -392,7 +392,7 @@ func UverseNode() *PackageNode {
392392
if 0 < argsl {
393393
if argsb.Data == nil {
394394
for i := 0; i < argsl; i++ {
395-
list[xvl+i] = argsb.List[argso+i].Copy(m.Alloc)
395+
list[xvl+i] = argsb.List[argso+i].DeepCopy(m.Alloc, m.Store)
396396
}
397397
} else {
398398
copyDataToList(

gnovm/tests/files/a47.gno

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
type S struct {
4+
i int
5+
}
6+
7+
func main() {
8+
sArr := make([]S, 0, 4)
9+
sArr = append(sArr, S{1}, S{2}, S{3})
10+
println(sArr[0].i, sArr[1].i, sArr[2].i)
11+
12+
newArr := append(sArr[:0], sArr[1:]...)
13+
14+
// The append modified the underlying array because it was within capacity.
15+
println(len(sArr) == 3)
16+
println(sArr[0].i, sArr[1].i, sArr[2].i)
17+
18+
// It generated a new slice that references the same array.
19+
println(len(newArr) == 2)
20+
println(newArr[0].i, newArr[1].i)
21+
22+
// The struct should have been copied, not referenced.
23+
println(&sArr[2] != &newArr[1])
24+
}
25+
26+
// Output:
27+
// 1 2 3
28+
// true
29+
// 2 3 3
30+
// true
31+
// 2 3
32+
// true

0 commit comments

Comments
 (0)