diff options
| author | Keith Randall <khr@golang.org> | 2015-08-24 23:52:03 -0700 |
|---|---|---|
| committer | David Chase <drchase@google.com> | 2015-08-25 17:14:57 +0000 |
| commit | 3526cf586be92cb4c741aed54ccfd37cf00ddfc5 (patch) | |
| tree | d8ecce8f07271ca23777623d43013516908a2bde /src/cmd/compile/internal/gc/testdata/string_ssa.go | |
| parent | 8e601b23cd77f687407a358d2baba672f5a8e4d6 (diff) | |
| download | go-3526cf586be92cb4c741aed54ccfd37cf00ddfc5.tar.xz | |
[dev.ssa] cmd/compile: implement OSLICESTR
Add a new function and generic operation to handle
bounds checking for slices. Unlike the index
bounds checking the index can be equal to the upper
bound.
Do gc-friendly slicing that generates proper code for
0-length result slices.
This is a takeover of Alexandru's original change,
(https://go-review.googlesource.com/#/c/12764/)
submittable now that the decompose phase is in.
Change-Id: I17d164cf42ed7839f84ca949c6ad3289269c9160
Reviewed-on: https://go-review.googlesource.com/13903
Reviewed-by: David Chase <drchase@google.com>
Diffstat (limited to 'src/cmd/compile/internal/gc/testdata/string_ssa.go')
| -rw-r--r-- | src/cmd/compile/internal/gc/testdata/string_ssa.go | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/gc/testdata/string_ssa.go b/src/cmd/compile/internal/gc/testdata/string_ssa.go new file mode 100644 index 0000000000..5987412933 --- /dev/null +++ b/src/cmd/compile/internal/gc/testdata/string_ssa.go @@ -0,0 +1,92 @@ +// string_ssa.go tests string operations. +package main + +var failed = false + +func testStringSlice1_ssa(a string, i, j int) string { + switch { // prevent inlining + } + return a[i:] +} + +func testStringSlice2_ssa(a string, i, j int) string { + switch { // prevent inlining + } + return a[:j] +} + +func testStringSlice12_ssa(a string, i, j int) string { + switch { // prevent inlining + } + return a[i:j] +} + +func testStringSlice() { + tests := [...]struct { + fn func(string, int, int) string + s string + low, high int + want string + }{ + // -1 means the value is not used. + {testStringSlice1_ssa, "foobar", 0, -1, "foobar"}, + {testStringSlice1_ssa, "foobar", 3, -1, "bar"}, + {testStringSlice1_ssa, "foobar", 6, -1, ""}, + {testStringSlice2_ssa, "foobar", -1, 0, ""}, + {testStringSlice2_ssa, "foobar", -1, 3, "foo"}, + {testStringSlice2_ssa, "foobar", -1, 6, "foobar"}, + {testStringSlice12_ssa, "foobar", 0, 6, "foobar"}, + {testStringSlice12_ssa, "foobar", 0, 0, ""}, + {testStringSlice12_ssa, "foobar", 6, 6, ""}, + {testStringSlice12_ssa, "foobar", 1, 5, "ooba"}, + {testStringSlice12_ssa, "foobar", 3, 3, ""}, + {testStringSlice12_ssa, "", 0, 0, ""}, + } + + for i, t := range tests { + if got := t.fn(t.s, t.low, t.high); t.want != got { + println("#", i, " ", t.s, "[", t.low, ":", t.high, "] = ", got, " want ", t.want) + failed = true + } + } +} + +type prefix struct { + prefix string +} + +func (p *prefix) slice_ssa() { + p.prefix = p.prefix[:3] +} + +func testStructSlice() { + switch { + } + p := &prefix{"prefix"} + p.slice_ssa() + if "pre" != p.prefix { + println("wrong field slice: wanted %s got %s", "pre", p.prefix) + } +} + +func testStringSlicePanic() { + defer func() { + if r := recover(); r != nil { + println("paniced as expected") + } + }() + + str := "foobar" + println("got ", testStringSlice12_ssa(str, 3, 9)) + println("expected to panic, but didn't") + failed = true +} + +func main() { + testStringSlice() + testStringSlicePanic() + + if failed { + panic("failed") + } +} |
