github.com/panjjo/go@v0.0.0-20161104043856-d62b31386338/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  	testGoto()
    29  }
    30  
    31  // The indexes of the counters in testPanic are known to main.go
    32  const panicIndex = 3
    33  
    34  // This test appears first because the index of its counters is known to main.go
    35  func testPanic() {
    36  	defer func() {
    37  		recover()
    38  	}()
    39  	check(LINE, 1)
    40  	panic("should not get next line")
    41  	check(LINE, 0) // this is GoCover.Count[panicIndex]
    42  	// The next counter is in testSimple and it will be non-zero.
    43  	// If the panic above does not trigger a counter, the test will fail
    44  	// because GoCover.Count[panicIndex] will be the one in testSimple.
    45  }
    46  
    47  func testSimple() {
    48  	check(LINE, 1)
    49  }
    50  
    51  func testIf() {
    52  	if true {
    53  		check(LINE, 1)
    54  	} else {
    55  		check(LINE, 0)
    56  	}
    57  	if false {
    58  		check(LINE, 0)
    59  	} else {
    60  		check(LINE, 1)
    61  	}
    62  	for i := 0; i < 3; i++ {
    63  		if checkVal(LINE, 3, i) <= 2 {
    64  			check(LINE, 3)
    65  		}
    66  		if checkVal(LINE, 3, i) <= 1 {
    67  			check(LINE, 2)
    68  		}
    69  		if checkVal(LINE, 3, i) <= 0 {
    70  			check(LINE, 1)
    71  		}
    72  	}
    73  	for i := 0; i < 3; i++ {
    74  		if checkVal(LINE, 3, i) <= 1 {
    75  			check(LINE, 2)
    76  		} else {
    77  			check(LINE, 1)
    78  		}
    79  	}
    80  	for i := 0; i < 3; i++ {
    81  		if checkVal(LINE, 3, i) <= 0 {
    82  			check(LINE, 1)
    83  		} else if checkVal(LINE, 2, i) <= 1 {
    84  			check(LINE, 1)
    85  		} else if checkVal(LINE, 1, i) <= 2 {
    86  			check(LINE, 1)
    87  		} else if checkVal(LINE, 0, i) <= 3 {
    88  			check(LINE, 0)
    89  		}
    90  	}
    91  	if func(a, b int) bool { return a < b }(3, 4) {
    92  		check(LINE, 1)
    93  	}
    94  }
    95  
    96  func testFor() {
    97  	for i := 0; i < 10; func() { i++; check(LINE, 10) }() {
    98  		check(LINE, 10)
    99  	}
   100  }
   101  
   102  func testRange() {
   103  	for _, f := range []func(){
   104  		func() { check(LINE, 1) },
   105  	} {
   106  		f()
   107  		check(LINE, 1)
   108  	}
   109  }
   110  
   111  func testBlockRun() {
   112  	check(LINE, 1)
   113  	{
   114  		check(LINE, 1)
   115  	}
   116  	{
   117  		check(LINE, 1)
   118  	}
   119  	check(LINE, 1)
   120  	{
   121  		check(LINE, 1)
   122  	}
   123  	{
   124  		check(LINE, 1)
   125  	}
   126  	check(LINE, 1)
   127  }
   128  
   129  func testSwitch() {
   130  	for i := 0; i < 5; func() { i++; check(LINE, 5) }() {
   131  		switch i {
   132  		case 0:
   133  			check(LINE, 1)
   134  		case 1:
   135  			check(LINE, 1)
   136  		case 2:
   137  			check(LINE, 1)
   138  		default:
   139  			check(LINE, 2)
   140  		}
   141  	}
   142  }
   143  
   144  func testTypeSwitch() {
   145  	var x = []interface{}{1, 2.0, "hi"}
   146  	for _, v := range x {
   147  		switch func() { check(LINE, 3) }(); v.(type) {
   148  		case int:
   149  			check(LINE, 1)
   150  		case float64:
   151  			check(LINE, 1)
   152  		case string:
   153  			check(LINE, 1)
   154  		case complex128:
   155  			check(LINE, 0)
   156  		default:
   157  			check(LINE, 0)
   158  		}
   159  	}
   160  }
   161  
   162  func testSelect1() {
   163  	c := make(chan int)
   164  	go func() {
   165  		for i := 0; i < 1000; i++ {
   166  			c <- i
   167  		}
   168  	}()
   169  	for {
   170  		select {
   171  		case <-c:
   172  			check(LINE, anything)
   173  		case <-c:
   174  			check(LINE, anything)
   175  		default:
   176  			check(LINE, 1)
   177  			return
   178  		}
   179  	}
   180  }
   181  
   182  func testSelect2() {
   183  	c1 := make(chan int, 1000)
   184  	c2 := make(chan int, 1000)
   185  	for i := 0; i < 1000; i++ {
   186  		c1 <- i
   187  		c2 <- i
   188  	}
   189  	for {
   190  		select {
   191  		case <-c1:
   192  			check(LINE, 1000)
   193  		case <-c2:
   194  			check(LINE, 1000)
   195  		default:
   196  			check(LINE, 1)
   197  			return
   198  		}
   199  	}
   200  }
   201  
   202  // Empty control statements created syntax errors. This function
   203  // is here just to be sure that those are handled correctly now.
   204  func testEmptySwitches() {
   205  	check(LINE, 1)
   206  	switch 3 {
   207  	}
   208  	check(LINE, 1)
   209  	switch i := (interface{})(3).(int); i {
   210  	}
   211  	check(LINE, 1)
   212  	c := make(chan int)
   213  	go func() {
   214  		check(LINE, 1)
   215  		c <- 1
   216  		select {}
   217  	}()
   218  	<-c
   219  	check(LINE, 1)
   220  }
   221  
   222  func testFunctionLiteral() {
   223  	a := func(f func()) error {
   224  		f()
   225  		f()
   226  		return nil
   227  	}
   228  
   229  	b := func(f func()) bool {
   230  		f()
   231  		f()
   232  		return true
   233  	}
   234  
   235  	check(LINE, 1)
   236  	a(func() {
   237  		check(LINE, 2)
   238  	})
   239  
   240  	if err := a(func() {
   241  		check(LINE, 2)
   242  	}); err != nil {
   243  	}
   244  
   245  	switch b(func() {
   246  		check(LINE, 2)
   247  	}) {
   248  	}
   249  
   250  	x := 2
   251  	switch x {
   252  	case func() int { check(LINE, 1); return 1 }():
   253  		check(LINE, 0)
   254  		panic("2=1")
   255  	case func() int { check(LINE, 1); return 2 }():
   256  		check(LINE, 1)
   257  	case func() int { check(LINE, 0); return 3 }():
   258  		check(LINE, 0)
   259  		panic("2=3")
   260  	}
   261  }
   262  
   263  func testGoto() {
   264  	for i := 0; i < 2; i++ {
   265  		if i == 0 {
   266  			goto Label
   267  		}
   268  		check(LINE, 1)
   269  	Label:
   270  		check(LINE, 2)
   271  	}
   272  	// Now test that we don't inject empty statements
   273  	// between a label and a loop.
   274  loop:
   275  	for {
   276  		check(LINE, 1)
   277  		break loop
   278  	}
   279  }
   280  
   281  // This comment shouldn't appear in generated go code.
   282  func haha() {
   283  	// Needed for cover to add counter increment here.
   284  	_ = 42
   285  }
   286  
   287  // Some someFunction.
   288  //
   289  //go:nosplit
   290  func someFunction() {
   291  }