Skip to content

Commit

Permalink
chore(gnolang): add challenges (#765)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle authored Apr 20, 2023
1 parent 4e69657 commit 32bc631
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 0 deletions.
12 changes: 12 additions & 0 deletions gnovm/tests/challenges/map0.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

func main() {
m := map[string]string{
"hello": "foo",
"world": "bar",
"hello": "",
}
}

// Error:
// duplicate key "hello" in map literal
16 changes: 16 additions & 0 deletions gnovm/tests/challenges/panic0b.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

func main() {
f()
}

func f() {
defer func() {
panic("second")
}()
panic("first")
}

// Error:
// first
// second
27 changes: 27 additions & 0 deletions gnovm/tests/challenges/recover5b.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

func main() {
f()
}

func f() {
defer func() { println("f recover", recover()) }()
defer g()
panic("wtf")
}

func g() {
defer func() {
// g() shouldn't be able to recover from f()'s panic, because the recover
// is declared in a deferred closure that is absent from the stack at the
// time of the panic.
// See go behavior here https://go.dev/play/p/CcMGgY606O-
// See Rob's examples https://groups.google.com/g/golang-nuts/c/HOXNBQu5c-Q/m/Je0qo1hbxIsJ
println("g recover", recover())
}()
}

// Output:
// g recover undefined
// f recover wtf
// false
8 changes: 8 additions & 0 deletions gnovm/tests/challenges/slice0.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

func main() {
_ = []string{0: "foo", 2: "bar", 2: "baz"}
}

// Error:
// duplicate index 2 in array or slice literal
14 changes: 14 additions & 0 deletions gnovm/tests/files/recover1b.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import "fmt"

func main() {
defer func() {
recover()
panic("other panic")
}()
panic("test panic")
}

// Error:
// other panic
19 changes: 19 additions & 0 deletions gnovm/tests/files/recover5.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

func main() {
f()
}

func f() {
defer func() { println("f recover", recover()) }()
defer g()
panic("wtf")
}

func g() {
println("g recover", recover())
}

// Output:
// g recover wtf
// f recover undefined
30 changes: 30 additions & 0 deletions gnovm/tests/files/recover6.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"errors"
)

func main() {
println(f(false))
println(f(true))
}

func f(dopanic bool) (err error) {
defer func() {
if x := recover(); x != nil {
err = x.(error)
}
}()
q(dopanic)
return
}

func q(dopanic bool) {
if dopanic {
panic(errors.New("wtf"))
}
}

// Output:
// undefined
// wtf

0 comments on commit 32bc631

Please sign in to comment.