Commit 08eeb253 authored by Rob Pike's avatar Rob Pike

simplify test to eliminate now-deprecated forms of switch.

R=rsc
DELTA=76  (0 added, 63 deleted, 13 changed)
OCL=26439
CL=26490
parent 1cdcfda1
...@@ -35,7 +35,6 @@ func assert(b bool, s string) { ...@@ -35,7 +35,6 @@ func assert(b bool, s string) {
} }
} }
func f(i int) interface{} { func f(i int) interface{} {
switch i { switch i {
case Bool: case Bool:
...@@ -61,64 +60,37 @@ func f(i int) interface{} { ...@@ -61,64 +60,37 @@ func f(i int) interface{} {
} }
func main() { func main() {
// type guard style
// for i := Bool; i < Last; i++ {
// switch v := f(i); true {
// case x := v.(bool):
// assert(x == true && i == Bool, "switch 1 bool");
// case x := v.(int):
// assert(x == 7 && i == Int, "switch 1 int");
// case x := v.(float):
// assert(x == 7.4 && i == Float, "switch 1 float");
// case x := v.(string):
// assert(x == "hello" && i == String, "switch 1 string");
// case x := v.(S):
// assert(x.a == 1234 && i == Struct, "switch 1 struct");
// case x := v.(chan int):
// assert(x == c && i == Chan, "switch 1 chan");
// case x := v.([]int):
// assert(x[3] == 3 && i == Array, "switch 1 array");
// case x := v.(map[string]int):
// assert(x == m && i == Map, "switch 1 map");
// case x := v.(func(i int) interface{}):
// assert(x == f && i == Func, "switch 1 fun");
// default:
// assert(false, "switch 1 unknown");
// }
// }
// type switch style
for i := Bool; i < Last; i++ { for i := Bool; i < Last; i++ {
switch x := f(i).(type) { switch x := f(i).(type) {
case bool: case bool:
assert(x == true && i == Bool, "switch 2 bool"); assert(x == true && i == Bool, "bool");
case int: case int:
assert(x == 7 && i == Int, "switch 2 int"); assert(x == 7 && i == Int, "int");
case float: case float:
assert(x == 7.4 && i == Float, "switch 2 float"); assert(x == 7.4 && i == Float, "float");
case string: case string:
assert(x == "hello" && i == String, "switch 2 string"); assert(x == "hello"&& i == String, "string");
case S: case S:
assert(x.a == 1234 && i == Struct, "switch 2 struct"); assert(x.a == 1234 && i == Struct, "struct");
case chan int: case chan int:
assert(x == c && i == Chan, "switch 2 chan"); assert(x == c && i == Chan, "chan");
case []int: case []int:
assert(x[3] == 3 && i == Array, "switch 2 array"); assert(x[3] == 3 && i == Array, "array");
case map[string]int: case map[string]int:
assert(x == m && i == Map, "switch 2 map"); assert(x == m && i == Map, "map");
case func(i int) interface{}: case func(i int) interface{}:
assert(x == f && i == Func, "switch 2 fun"); assert(x == f && i == Func, "fun");
default: default:
assert(false, "switch 2 unknown"); assert(false, "unknown");
} }
} }
// catch-all style in various forms // boolean switch (has had bugs in past; worth writing down)
switch { switch {
case true: case true:
assert(true, "switch 3 bool"); assert(true, "switch 2 bool");
default: default:
assert(false, "switch 3 unknown"); assert(false, "switch 2 unknown");
} }
switch true { switch true {
...@@ -135,39 +107,4 @@ func main() { ...@@ -135,39 +107,4 @@ func main() {
assert(false, "switch 4 unknown"); assert(false, "switch 4 unknown");
} }
// switch true {
// case x := f(Int).(float):
// assert(false, "switch 5 type guard wrong type");
// case x := f(Int).(int):
// assert(x == 7, "switch 5 type guard");
// default:
// assert(false, "switch 5 unknown");
// }
m["7"] = 7;
// switch true {
// case x := m["6"]:
// assert(false, "switch 6 map reference wrong");
// case x := m["7"]:
// assert(x == 7, "switch 6 map reference");
// default:
// assert(false, "switch 6 unknown");
// }
go func() { <-c; c <- 77; } ();
// guarantee the channel is ready
c <- 77;
for i := 0; i < 5; i++ {
sys.Gosched();
}
dummyc := make(chan int);
// switch true {
// case x := <-dummyc:
// assert(false, "switch 7 chan wrong");
// case x := <-c:
// assert(x == 77, "switch 7 chan");
// default:
// assert(false, "switch 7 unknown");
// }
} }
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