Browse Source

test: add slice tests

pull/48/head
Ayke van Laethem 6 years ago
parent
commit
f9edf7cc5c
No known key found for this signature in database GPG Key ID: E97FF5335DFDFDED
  1. 29
      testdata/slice.go
  2. 6
      testdata/slice.txt

29
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
}

6
testdata/slice.txt

@ -0,0 +1,6 @@
foo: len=4 cap=4 data: 1 2 4 5
bar: len=3 cap=5 data: 0 0 0
foo[1:2]: len=1 cap=3 data: 2
sum foo: 12
copy foo -> bar: 3
bar: len=3 cap=5 data: 1 2 4
Loading…
Cancel
Save