diff options
author | Ayke van Laethem <[email protected]> | 2018-10-18 15:02:53 +0200 |
---|---|---|
committer | Ayke van Laethem <[email protected]> | 2018-10-18 15:02:53 +0200 |
commit | f9edf7cc5ce075361172c23cf4b6301a863a24ce (patch) | |
tree | 9a988b69c17c7f7ab705991077d996bfde43ef77 /testdata/slice.go | |
parent | 72390c21cb40777c3b621eac82d790da3716b228 (diff) | |
download | tinygo-f9edf7cc5ce075361172c23cf4b6301a863a24ce.tar.gz tinygo-f9edf7cc5ce075361172c23cf4b6301a863a24ce.zip |
test: add slice tests
Diffstat (limited to 'testdata/slice.go')
-rw-r--r-- | testdata/slice.go | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/testdata/slice.go b/testdata/slice.go new file mode 100644 index 000000000..215c213e1 --- /dev/null +++ b/testdata/slice.go @@ -0,0 +1,29 @@ +package main + +func main() { + l := 5 + foo := []int{1, 2, 4, 5} + bar := make([]int, l-2, l) + printslice("foo", foo) + printslice("bar", bar) + printslice("foo[1:2]", foo[1:2]) + println("sum foo:", sum(foo)) + println("copy foo -> bar:", copy(bar, foo)) + printslice("bar", bar) +} + +func printslice(name string, s []int) { + print(name, ": len=", len(s), " cap=", cap(s), " data:") + for _, n := range s { + print(" ", n) + } + println() +} + +func sum(l []int) int { + sum := 0 + for _, n := range l { + sum += n + } + return sum +} |