mirror of https://github.com/golang/go.git
simplify test to eliminate now-deprecated forms of switch.
R=rsc DELTA=76 (0 added, 63 deleted, 13 changed) OCL=26439 CL=26490
This commit is contained in:
parent
1cdcfda140
commit
08eeb2535d
|
|
@ -35,7 +35,6 @@ func assert(b bool, s string) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
func f(i int) interface{} {
|
||||
switch i {
|
||||
case Bool:
|
||||
|
|
@ -61,64 +60,37 @@ func f(i int) interface{} {
|
|||
}
|
||||
|
||||
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++ {
|
||||
switch x := f(i).(type) {
|
||||
case bool:
|
||||
assert(x == true && i == Bool, "switch 2 bool");
|
||||
assert(x == true && i == Bool, "bool");
|
||||
case int:
|
||||
assert(x == 7 && i == Int, "switch 2 int");
|
||||
assert(x == 7 && i == Int, "int");
|
||||
case float:
|
||||
assert(x == 7.4 && i == Float, "switch 2 float");
|
||||
assert(x == 7.4 && i == Float, "float");
|
||||
case string:
|
||||
assert(x == "hello" && i == String, "switch 2 string");
|
||||
assert(x == "hello"&& i == String, "string");
|
||||
case S:
|
||||
assert(x.a == 1234 && i == Struct, "switch 2 struct");
|
||||
assert(x.a == 1234 && i == Struct, "struct");
|
||||
case chan int:
|
||||
assert(x == c && i == Chan, "switch 2 chan");
|
||||
assert(x == c && i == Chan, "chan");
|
||||
case []int:
|
||||
assert(x[3] == 3 && i == Array, "switch 2 array");
|
||||
assert(x[3] == 3 && i == Array, "array");
|
||||
case map[string]int:
|
||||
assert(x == m && i == Map, "switch 2 map");
|
||||
assert(x == m && i == Map, "map");
|
||||
case func(i int) interface{}:
|
||||
assert(x == f && i == Func, "switch 2 fun");
|
||||
assert(x == f && i == Func, "fun");
|
||||
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 {
|
||||
case true:
|
||||
assert(true, "switch 3 bool");
|
||||
assert(true, "switch 2 bool");
|
||||
default:
|
||||
assert(false, "switch 3 unknown");
|
||||
assert(false, "switch 2 unknown");
|
||||
}
|
||||
|
||||
switch true {
|
||||
|
|
@ -135,39 +107,4 @@ func main() {
|
|||
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");
|
||||
// }
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue