Commit 79f7ccf2 authored by Keith Randall's avatar Keith Randall

cmd/compile: add constant fold comparison tests

Increases generic.rules coverage from 91% to 95%.

Change-Id: I981eb94f3cd10d2f87c836576a43786787a25d83
Reviewed-on: https://go-review.googlesource.com/23556
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarTodd Neal <todd@tneal.org>
parent b859a78e
...@@ -208,6 +208,89 @@ func main() { ...@@ -208,6 +208,89 @@ func main() {
} }
} }
} }
// Constant folding for comparisons
for _, s := range szs {
fmt.Fprintf(w, "func TestConstFoldCompare%s(t *testing.T) {\n", s.name)
for _, x := range s.i {
for _, y := range s.i {
fmt.Fprintf(w, "\t{\n")
fmt.Fprintf(w, "\t\tvar x %s = %d\n", s.name, x)
fmt.Fprintf(w, "\t\tvar y %s = %d\n", s.name, y)
if x == y {
fmt.Fprintf(w, "\t\tif !(x == y) { t.Errorf(\"!(%%d == %%d)\", x, y) }\n")
} else {
fmt.Fprintf(w, "\t\tif x == y { t.Errorf(\"%%d == %%d\", x, y) }\n")
}
if x != y {
fmt.Fprintf(w, "\t\tif !(x != y) { t.Errorf(\"!(%%d != %%d)\", x, y) }\n")
} else {
fmt.Fprintf(w, "\t\tif x != y { t.Errorf(\"%%d != %%d\", x, y) }\n")
}
if x < y {
fmt.Fprintf(w, "\t\tif !(x < y) { t.Errorf(\"!(%%d < %%d)\", x, y) }\n")
} else {
fmt.Fprintf(w, "\t\tif x < y { t.Errorf(\"%%d < %%d\", x, y) }\n")
}
if x > y {
fmt.Fprintf(w, "\t\tif !(x > y) { t.Errorf(\"!(%%d > %%d)\", x, y) }\n")
} else {
fmt.Fprintf(w, "\t\tif x > y { t.Errorf(\"%%d > %%d\", x, y) }\n")
}
if x <= y {
fmt.Fprintf(w, "\t\tif !(x <= y) { t.Errorf(\"!(%%d <= %%d)\", x, y) }\n")
} else {
fmt.Fprintf(w, "\t\tif x <= y { t.Errorf(\"%%d <= %%d\", x, y) }\n")
}
if x >= y {
fmt.Fprintf(w, "\t\tif !(x >= y) { t.Errorf(\"!(%%d >= %%d)\", x, y) }\n")
} else {
fmt.Fprintf(w, "\t\tif x >= y { t.Errorf(\"%%d >= %%d\", x, y) }\n")
}
fmt.Fprintf(w, "\t}\n")
}
}
for _, x := range s.u {
for _, y := range s.u {
fmt.Fprintf(w, "\t{\n")
fmt.Fprintf(w, "\t\tvar x %s = %d\n", s.name, x)
fmt.Fprintf(w, "\t\tvar y %s = %d\n", s.name, y)
if x == y {
fmt.Fprintf(w, "\t\tif !(x == y) { t.Errorf(\"!(%%d == %%d)\", x, y) }\n")
} else {
fmt.Fprintf(w, "\t\tif x == y { t.Errorf(\"%%d == %%d\", x, y) }\n")
}
if x != y {
fmt.Fprintf(w, "\t\tif !(x != y) { t.Errorf(\"!(%%d != %%d)\", x, y) }\n")
} else {
fmt.Fprintf(w, "\t\tif x != y { t.Errorf(\"%%d != %%d\", x, y) }\n")
}
if x < y {
fmt.Fprintf(w, "\t\tif !(x < y) { t.Errorf(\"!(%%d < %%d)\", x, y) }\n")
} else {
fmt.Fprintf(w, "\t\tif x < y { t.Errorf(\"%%d < %%d\", x, y) }\n")
}
if x > y {
fmt.Fprintf(w, "\t\tif !(x > y) { t.Errorf(\"!(%%d > %%d)\", x, y) }\n")
} else {
fmt.Fprintf(w, "\t\tif x > y { t.Errorf(\"%%d > %%d\", x, y) }\n")
}
if x <= y {
fmt.Fprintf(w, "\t\tif !(x <= y) { t.Errorf(\"!(%%d <= %%d)\", x, y) }\n")
} else {
fmt.Fprintf(w, "\t\tif x <= y { t.Errorf(\"%%d <= %%d\", x, y) }\n")
}
if x >= y {
fmt.Fprintf(w, "\t\tif !(x >= y) { t.Errorf(\"!(%%d >= %%d)\", x, y) }\n")
} else {
fmt.Fprintf(w, "\t\tif x >= y { t.Errorf(\"%%d >= %%d\", x, y) }\n")
}
fmt.Fprintf(w, "\t}\n")
}
}
fmt.Fprintf(w, "}\n")
}
// gofmt result // gofmt result
b := w.Bytes() b := w.Bytes()
src, err := format.Source(b) src, err := format.Source(b)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment