Skip to content

Commit

Permalink
cap
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Aug 19, 2024
1 parent cf404c6 commit a089650
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
64 changes: 64 additions & 0 deletions gnovm/pkg/gnolang/uverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,67 @@ func main() {
assertOutput(t, tc.code, tc.expected)
}
}

func TestGetCapacityPointerSlice(t *testing.T) {
tests := []struct {
name string
code string
expected string
}{
{
name: "cap of pointer to array",
code: `
package test
func main() {
exp := [...]string{"HELLO"}
x := cap(&exp)
println(x)
}`,
expected: "1\n",
},
{
name: "cap of array",
code: `
package test
func main() {
exp := [...]int{1, 2, 3, 4, 5}
println(cap(exp))
}`,
expected: "5\n",
},
{
name: "cap of slice",
code: `
package test
func main() {
slice := make([]int, 3, 5)
println(cap(slice))
}`,
expected: "5\n",
},
{
name: "cap of nil slice",
code: `
package test
func main() {
var slice []int
println(cap(slice))
}`,
expected: "0\n",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
m := NewMachine("test", nil)
n := MustParseFile("main.go", tc.code)
m.RunFiles(n)
m.RunMain()
assertOutput(t, tc.code, tc.expected)
})
}
}
5 changes: 5 additions & 0 deletions gnovm/pkg/gnolang/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -2183,6 +2183,11 @@ func (tv *TypedValue) GetCapacity() int {
return cv.GetCapacity()
case *NativeValue:
return cv.Value.Cap()
case PointerValue:
if av, ok := cv.TV.V.(*ArrayValue); ok {
return av.GetCapacity()
}
panic(fmt.Sprintf("unexpected pointer value for cap(): %s", tv.T.String()))

Check warning on line 2190 in gnovm/pkg/gnolang/values.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/values.go#L2190

Added line #L2190 was not covered by tests
default:
panic(fmt.Sprintf("unexpected type for cap(): %s",
tv.T.String()))
Expand Down
9 changes: 9 additions & 0 deletions gnovm/tests/files/cap1.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
exp := [...]int{1, 2, 3, 4, 5}
println(cap(exp))
}

// Output:
// 5

0 comments on commit a089650

Please sign in to comment.