Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ltzmaxwell committed Jul 1, 2024
1 parent 30386a8 commit c6411d6
Show file tree
Hide file tree
Showing 62 changed files with 1,655 additions and 0 deletions.
26 changes: 26 additions & 0 deletions gnovm/tests/files/closure10.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import "fmt"

func bar() func() func() int {
x := 1

// First level of closure, modifies x
return func() func() int {
//x++
// Second level of closure, returns x
return func() int {
return x
}
}
}

func main() {
f := bar() // f is the first-level closure
g := f() // g is the second-level closure, x is incremented here

fmt.Println(g()) // prints the value of x after being modified by the first-level closure
}

// Output:
// 1
26 changes: 26 additions & 0 deletions gnovm/tests/files/closure10_a.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import "fmt"

func bar() func() func() int {
x := 1

// First level of closure, modifies x
return func() func() int {
x++
// Second level of closure, returns x
return func() int {
return x
}
}
}

func main() {
f := bar() // f is the first-level closure
g := f() // g is the second-level closure, x is incremented here

fmt.Println(g()) // prints the value of x after being modified by the first-level closure
}

// Output:
// 2
25 changes: 25 additions & 0 deletions gnovm/tests/files/closure11.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

func main() {
var fns []func() int

for i := 0; i < 2; i++ {
x := i
f := func() int {
if true {
if true {
return x
}
}
return 0
}
fns = append(fns, f)
}
for _, fn := range fns {
println(fn())
}
}

// Output:
// 0
// 1
23 changes: 23 additions & 0 deletions gnovm/tests/files/closure11_a.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

func main() {
var fns []func() int

for i := 0; i < 2; i++ {
x := i
f := func() int {
if true {
return x
}
return 0
}
fns = append(fns, f)
}
for _, fn := range fns {
println(fn())
}
}

// Output:
// 0
// 1
27 changes: 27 additions & 0 deletions gnovm/tests/files/closure11_b.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

func main() {
var fns []func() int

for i := 0; i < 2; i++ {
x := i
y := 0
f := func() int {
if true {
x += y
if true {
return x
}
}
return 0
}
fns = append(fns, f)
}
for _, fn := range fns {
println(fn())
}
}

// Output:
// 0
// 1
27 changes: 27 additions & 0 deletions gnovm/tests/files/closure11_c.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

func main() {
var fns []func() int

for i := 0; i < 2; i++ {
x := i
y := 0
f := func() int {
if true {
x += y
if true {
return x
}
}
return 0
}
fns = append(fns, f)
}
for _, fn := range fns {
println(fn())
}
}

// Output:
// 0
// 1
22 changes: 22 additions & 0 deletions gnovm/tests/files/closure12.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

func main() {
var fns []func() int

for i := 0; i < 2; i++ {
x := i
f := func() int {
{
return x
}
}
fns = append(fns, f)
}
for _, fn := range fns {
println(fn())
}
}

// Output:
// 0
// 1
23 changes: 23 additions & 0 deletions gnovm/tests/files/closure12_a.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

func main() {
var fns []func() int

for i := 0; i < 2; i++ {
x := i
f := func() int {
for i := 0; i < 1; i++ {
x++
}
return x
}
fns = append(fns, f)
}
for _, fn := range fns {
println(fn())
}
}

// Output:
// 1
// 2
25 changes: 25 additions & 0 deletions gnovm/tests/files/closure12_b.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

func main() {
var fns []func() int

for i := 0; i < 2; i++ {
x := i
s := []int{1, 2}

f := func() int {
for _, v := range s {
x += v
}
return x
}
fns = append(fns, f)
}
for _, fn := range fns {
println(fn())
}
}

// Output:
// 3
// 4
27 changes: 27 additions & 0 deletions gnovm/tests/files/closure12_c.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

func main() {
var fns []func() int

for i := 0; i < 2; i++ {
x := i
y := 1
f := func() int {
switch y {
case 1:
x += 1
default:
x += 0
}
return x
}
fns = append(fns, f)
}
for _, fn := range fns {
println(fn())
}
}

// Output:
// 1
// 2
27 changes: 27 additions & 0 deletions gnovm/tests/files/closure12_e.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

type queueOnePass struct {
sparse []uint32
dense []uint32
size, nextIndex uint32
}

func newQueue(size int) (q *queueOnePass) {
return &queueOnePass{
sparse: make([]uint32, size),
dense: make([]uint32, size),
}
}
func main() {
var (
visitQueue = newQueue(10)
)
f := func() {
println(visitQueue.size)
}

f()
}

// Output:
// 0
20 changes: 20 additions & 0 deletions gnovm/tests/files/closure12_f.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

func main() {
s := []int{1, 2}

f := func() {
for i, v := range s {
println(i)
println(v)
}
}

f()
}

// Output:
// 0
// 1
// 1
// 2
14 changes: 14 additions & 0 deletions gnovm/tests/files/closure12_g.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

func main() {
f := func(a int) bool {
println(a)
return true
}

println(f(5))
}

// Output:
// 5
// true
25 changes: 25 additions & 0 deletions gnovm/tests/files/closure12_h.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

func main() {
s := 1
f := func() {
i := 0 // no capture for i
var j = s // s should be captured, j not
k := s // s should be captured, k not
m, n := s, 0
println(i)
println(j)
println(k)
println(m)
println(n)
}

f()
}

// Output:
// 0
// 1
// 1
// 1
// 0
15 changes: 15 additions & 0 deletions gnovm/tests/files/closure12_i.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

func main() {
s := []int{1, 2}

f := func() {
if len(s) == 2 {
println("ok")
}
}
f()
}

// Output:
// ok
24 changes: 24 additions & 0 deletions gnovm/tests/files/closure13.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import "fmt"

func main() {
var recursiveFunc func(int) int
var recursiveFunc2 func(int) int

recursiveFunc = func(num int) int {
recursiveFunc2 = recursiveFunc

if num <= 0 {
return 1
}

return num * recursiveFunc2(num-1)
}

result := recursiveFunc(5)
fmt.Println("Factorial of 5 is:", result) // Output: 120
}

// Output:
// Factorial of 5 is: 120
Loading

0 comments on commit c6411d6

Please sign in to comment.