github.com/euank/go@v0.0.0-20160829210321-495514729181/src/cmd/cover/testdata/test.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This program is processed by the cover command, and then testAll is called.
     6  // The test driver in main.go can then compare the coverage statistics with expectation.
     7  
     8  // The word LINE is replaced by the line number in this file. When the file is executed,
     9  // the coverage processing has changed the line numbers, so we can't use runtime.Caller.
    10  
    11  package main
    12  
    13  const anything = 1e9 // Just some unlikely value that means "we got here, don't care how often"
    14  
    15  func testAll() {
    16  	testSimple()
    17  	testBlockRun()
    18  	testIf()
    19  	testFor()
    20  	testRange()
    21  	testSwitch()
    22  	testTypeSwitch()
    23  	testSelect1()
    24  	testSelect2()
    25  	testPanic()
    26  	testEmptySwitches()
    27  	testFunctionLiteral()
    28  }
    29  
    30  // The indexes of the counters in testPanic are known to main.go
    31  const panicIndex = 3
    32  
    33  // This test appears first because the index of its counters is known to main.go
    34  func testPanic() {
    35  	defer func() {
    36  		recover()
    37  	}()
    38  	check(LINE, 1)
    39  	panic("should not get next line")
    40  	check(LINE, 0) // this is GoCover.Count[panicIndex]
    41  	// The next counter is in testSimple and it will be non-zero.
    42  	// If the panic above does not trigger a counter, the test will fail
    43  	// because GoCover.Count[panicIndex] will be the one in testSimple.
    44  }
    45  
    46  func testSimple() {
    47  	check(LINE, 1)
    48  }
    49  
    50  func testIf() {
    51  	if true {
    52  		check(LINE, 1)
    53  	} else {
    54  		check(LINE, 0)
    55  	}
    56  	if false {
    57  		check(LINE, 0)
    58  	} else {
    59  		check(LINE, 1)
    60  	}
    61  	for i := 0; i < 3; i++ {
    62  		if checkVal(LINE, 3, i) <= 2 {
    63  			check(LINE, 3)
    64  		}
    65  		if checkVal(LINE, 3, i) <= 1 {
    66  			check(LINE, 2)
    67  		}
    68  		if checkVal(LINE, 3, i) <= 0 {
    69  			check(LINE, 1)
    70  		}
    71  	}
    72  	for i := 0; i < 3; i++ {
    73  		if checkVal(LINE, 3, i) <= 1 {
    74  			check(LINE, 2)
    75  		} else {
    76  			check(LINE, 1)
    77  		}
    78  	}
    79  	for i := 0; i < 3; i++ {
    80  		if checkVal(LINE, 3, i) <= 0 {
    81  			check(LINE, 1)
    82  		} else if checkVal(LINE, 2, i) <= 1 {
    83  			check(LINE, 1)
    84  		} else if checkVal(LINE, 1, i) <= 2 {
    85  			check(LINE, 1)
    86  		} else if checkVal(LINE, 0, i) <= 3 {
    87  			check(LINE, 0)
    88  		}
    89  	}
    90  	if func(a, b int) bool { return a < b }(3, 4) {
    91  		check(LINE, 1)
    92  	}
    93  }
    94  
    95  func testFor() {
    96  	for i := 0; i < 10; func() { i++; check(LINE, 10) }() {
    97  		check(LINE, 10)
    98  	}
    99  }
   100  
   101  func testRange() {
   102  	for _, f := range []func(){
   103  		func() { check(LINE, 1) },
   104  	} {
   105  		f()
   106  		check(LINE, 1)
   107  	}
   108  }
   109  
   110  func testBlockRun() {
   111  	check(LINE, 1)
   112  	{
   113  		check(LINE, 1)
   114  	}
   115  	{
   116  		check(LINE, 1)
   117  	}
   118  	check(LINE, 1)
   119  	{
   120  		check(LINE, 1)
   121  	}
   122  	{
   123  		check(LINE, 1)
   124  	}
   125  	check(LINE, 1)
   126  }
   127  
   128  func testSwitch() {
   129  	for i := 0; i < 5; func() { i++; check(LINE, 5) }() {
   130  		switch i {
   131  		case 0:
   132  			check(LINE, 1)
   133  		case 1:
   134  			check(LINE, 1)
   135  		case 2:
   136  			check(LINE, 1)
   137  		default:
   138  			check(LINE, 2)
   139  		}
   140  	}
   141  }
   142  
   143  func testTypeSwitch() {
   144  	var x = []interface{}{1, 2.0, "hi"}
   145  	for _, v := range x {
   146  		switch func() { check(LINE, 3) }(); v.(type) {
   147  		case int:
   148  			check(LINE, 1)
   149  		case float64:
   150  			check(LINE, 1)
   151  		case string:
   152  			check(LINE, 1)
   153  		case complex128:
   154  			check(LINE, 0)
   155  		default:
   156  			check(LINE, 0)
   157  		}
   158  	}
   159  }
   160  
   161  func testSelect1() {
   162  	c := make(chan int)
   163  	go func() {
   164  		for i := 0; i < 1000; i++ {
   165  			c <- i
   166  		}
   167  	}()
   168  	for {
   169  		select {
   170  		case <-c:
   171  			check(LINE, anything)
   172  		case <-c:
   173  			check(LINE, anything)
   174  		default:
   175  			check(LINE, 1)
   176  			return
   177  		}
   178  	}
   179  }
   180  
   181  func testSelect2() {
   182  	c1 := make(chan int, 1000)
   183  	c2 := make(chan int, 1000)
   184  	for i := 0; i < 1000; i++ {
   185  		c1 <- i
   186  		c2 <- i
   187  	}
   188  	for {
   189  		select {
   190  		case <-c1:
   191  			check(LINE, 1000)
   192  		case <-c2:
   193  			check(LINE, 1000)
   194  		default:
   195  			check(LINE, 1)
   196  			return
   197  		}
   198  	}
   199  }
   200  
   201  // Empty control statements created syntax errors. This function
   202  // is here just to be sure that those are handled correctly now.
   203  func testEmptySwitches() {
   204  	check(LINE, 1)
   205  	switch 3 {
   206  	}
   207  	check(LINE, 1)
   208  	switch i := (interface{})(3).(int); i {
   209  	}
   210  	check(LINE, 1)
   211  	c := make(chan int)
   212  	go func() {
   213  		check(LINE, 1)
   214  		c <- 1
   215  		select {}
   216  	}()
   217  	<-c
   218  	check(LINE, 1)
   219  }
   220  
   221  func testFunctionLiteral() {
   222  	a := func(f func()) error {
   223  		f()
   224  		f()
   225  		return nil
   226  	}
   227  
   228  	b := func(f func()) bool {
   229  		f()
   230  		f()
   231  		return true
   232  	}
   233  
   234  	check(LINE, 1)
   235  	a(func() {
   236  		check(LINE, 2)
   237  	})
   238  
   239  	if err := a(func() {
   240  		check(LINE, 2)
   241  	}); err != nil {
   242  	}
   243  
   244  	switch b(func() {
   245  		check(LINE, 2)
   246  	}) {
   247  	}
   248  }