From f9edf7cc5ce075361172c23cf4b6301a863a24ce Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Thu, 18 Oct 2018 15:02:53 +0200 Subject: [PATCH] test: add slice tests --- testdata/slice.go | 29 +++++++++++++++++++++++++++++ testdata/slice.txt | 6 ++++++ 2 files changed, 35 insertions(+) create mode 100644 testdata/slice.go create mode 100644 testdata/slice.txt diff --git a/testdata/slice.go b/testdata/slice.go new file mode 100644 index 00000000..215c213e --- /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 +} diff --git a/testdata/slice.txt b/testdata/slice.txt new file mode 100644 index 00000000..d598d432 --- /dev/null +++ b/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