...
Run Format

Source file src/cmd/cover/testdata/test.go

Documentation: cmd/cover/testdata

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

View as plain text