Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(gnolang): add challenges #765

Merged
merged 1 commit into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// second
// second

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got ninja'd by manfred... shouldn't be too much of a problem though, if somebody does fix the challenges they'll probably have to adjust the output anyway.

yes :)

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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