2013-06-21 15:35:09 -06:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// This program is processed by the cover command, and then testAll is called.
|
|
|
|
// The test driver in main.go can then compare the coverage statistics with expectation.
|
|
|
|
|
|
|
|
// The word LINE is replaced by the line number in this file. When the file is executed,
|
|
|
|
// the coverage processing has changed the line numbers, so we can't use runtime.Caller.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
const anything = 1e9 // Just some unlikely value that means "we got here, don't care how often"
|
|
|
|
|
|
|
|
func testAll() {
|
|
|
|
testSimple()
|
|
|
|
testBlockRun()
|
2013-07-09 23:20:52 -06:00
|
|
|
testIf()
|
2013-06-21 15:35:09 -06:00
|
|
|
testFor()
|
2013-10-11 11:32:36 -06:00
|
|
|
testRange()
|
2013-06-21 15:35:09 -06:00
|
|
|
testSwitch()
|
2013-07-09 23:20:52 -06:00
|
|
|
testTypeSwitch()
|
2013-06-21 15:35:09 -06:00
|
|
|
testSelect1()
|
|
|
|
testSelect2()
|
2015-03-16 15:52:01 -06:00
|
|
|
testEmptySwitches()
|
2013-06-21 15:35:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func testSimple() {
|
|
|
|
check(LINE, 1)
|
|
|
|
}
|
|
|
|
|
2013-07-09 23:20:52 -06:00
|
|
|
func testIf() {
|
|
|
|
if true {
|
|
|
|
check(LINE, 1)
|
|
|
|
} else {
|
|
|
|
check(LINE, 0)
|
|
|
|
}
|
|
|
|
if false {
|
|
|
|
check(LINE, 0)
|
|
|
|
} else {
|
|
|
|
check(LINE, 1)
|
|
|
|
}
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
if checkVal(LINE, 3, i) <= 2 {
|
|
|
|
check(LINE, 3)
|
|
|
|
}
|
|
|
|
if checkVal(LINE, 3, i) <= 1 {
|
|
|
|
check(LINE, 2)
|
|
|
|
}
|
|
|
|
if checkVal(LINE, 3, i) <= 0 {
|
|
|
|
check(LINE, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
if checkVal(LINE, 3, i) <= 1 {
|
|
|
|
check(LINE, 2)
|
|
|
|
} else {
|
|
|
|
check(LINE, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
if checkVal(LINE, 3, i) <= 0 {
|
|
|
|
check(LINE, 1)
|
|
|
|
} else if checkVal(LINE, 2, i) <= 1 {
|
|
|
|
check(LINE, 1)
|
|
|
|
} else if checkVal(LINE, 1, i) <= 2 {
|
|
|
|
check(LINE, 1)
|
|
|
|
} else if checkVal(LINE, 0, i) <= 3 {
|
|
|
|
check(LINE, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 15:35:09 -06:00
|
|
|
func testFor() {
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
check(LINE, 10)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-11 11:32:36 -06:00
|
|
|
func testRange() {
|
|
|
|
for _, f := range []func(){
|
|
|
|
func() { check(LINE, 1) },
|
|
|
|
} {
|
|
|
|
f()
|
|
|
|
check(LINE, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 15:35:09 -06:00
|
|
|
func testBlockRun() {
|
|
|
|
check(LINE, 1)
|
|
|
|
{
|
|
|
|
check(LINE, 1)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
check(LINE, 1)
|
|
|
|
}
|
|
|
|
check(LINE, 1)
|
|
|
|
{
|
|
|
|
check(LINE, 1)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
check(LINE, 1)
|
|
|
|
}
|
|
|
|
check(LINE, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSwitch() {
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
switch i {
|
|
|
|
case 0:
|
|
|
|
check(LINE, 1)
|
|
|
|
case 1:
|
|
|
|
check(LINE, 1)
|
|
|
|
case 2:
|
|
|
|
check(LINE, 1)
|
|
|
|
default:
|
|
|
|
check(LINE, 2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-09 23:20:52 -06:00
|
|
|
func testTypeSwitch() {
|
|
|
|
var x = []interface{}{1, 2.0, "hi"}
|
|
|
|
for _, v := range x {
|
|
|
|
switch v.(type) {
|
|
|
|
case int:
|
|
|
|
check(LINE, 1)
|
|
|
|
case float64:
|
|
|
|
check(LINE, 1)
|
|
|
|
case string:
|
|
|
|
check(LINE, 1)
|
|
|
|
case complex128:
|
|
|
|
check(LINE, 0)
|
|
|
|
default:
|
|
|
|
check(LINE, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 15:35:09 -06:00
|
|
|
func testSelect1() {
|
|
|
|
c := make(chan int)
|
|
|
|
go func() {
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
c <- i
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c:
|
|
|
|
check(LINE, anything)
|
|
|
|
case <-c:
|
|
|
|
check(LINE, anything)
|
|
|
|
default:
|
|
|
|
check(LINE, 1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSelect2() {
|
|
|
|
c1 := make(chan int, 1000)
|
|
|
|
c2 := make(chan int, 1000)
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
c1 <- i
|
|
|
|
c2 <- i
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c1:
|
|
|
|
check(LINE, 1000)
|
|
|
|
case <-c2:
|
|
|
|
check(LINE, 1000)
|
|
|
|
default:
|
|
|
|
check(LINE, 1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-16 15:52:01 -06:00
|
|
|
|
|
|
|
// Empty control statements created syntax errors. This function
|
|
|
|
// is here just to be sure that those are handled correctly now.
|
|
|
|
func testEmptySwitches() {
|
|
|
|
check(LINE, 1)
|
|
|
|
switch 3 {
|
|
|
|
}
|
|
|
|
check(LINE, 1)
|
|
|
|
switch i := (interface{})(3).(int); i {
|
|
|
|
}
|
|
|
|
check(LINE, 1)
|
2015-03-17 07:15:38 -06:00
|
|
|
c := make(chan int)
|
2015-03-16 15:52:01 -06:00
|
|
|
go func() {
|
|
|
|
check(LINE, 1)
|
2015-03-17 07:15:38 -06:00
|
|
|
c <- 1
|
2015-03-16 15:52:01 -06:00
|
|
|
select {}
|
|
|
|
}()
|
2015-03-17 07:15:38 -06:00
|
|
|
<-c
|
2015-03-16 15:52:01 -06:00
|
|
|
check(LINE, 1)
|
|
|
|
}
|