Black Lives Matter. Support the Equal Justice Initiative.

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  		goto label2
   136  	label1:
   137  		goto label1
   138  	label2:
   139  		switch i {
   140  		case 0:
   141  			check(LINE, 1)
   142  		case 1:
   143  			check(LINE, 1)
   144  		case 2:
   145  			check(LINE, 1)
   146  		default:
   147  			check(LINE, 2)
   148  		}
   149  	}
   150  }
   151  
   152  func testTypeSwitch() {
   153  	var x = []interface{}{1, 2.0, "hi"}
   154  	for _, v := range x {
   155  		switch func() { check(LINE, 3) }(); v.(type) {
   156  		case int:
   157  			check(LINE, 1)
   158  		case float64:
   159  			check(LINE, 1)
   160  		case string:
   161  			check(LINE, 1)
   162  		case complex128:
   163  			check(LINE, 0)
   164  		default:
   165  			check(LINE, 0)
   166  		}
   167  	}
   168  }
   169  
   170  func testSelect1() {
   171  	c := make(chan int)
   172  	go func() {
   173  		for i := 0; i < 1000; i++ {
   174  			c <- i
   175  		}
   176  	}()
   177  	for {
   178  		select {
   179  		case <-c:
   180  			check(LINE, anything)
   181  		case <-c:
   182  			check(LINE, anything)
   183  		default:
   184  			check(LINE, 1)
   185  			return
   186  		}
   187  	}
   188  }
   189  
   190  func testSelect2() {
   191  	c1 := make(chan int, 1000)
   192  	c2 := make(chan int, 1000)
   193  	for i := 0; i < 1000; i++ {
   194  		c1 <- i
   195  		c2 <- i
   196  	}
   197  	for {
   198  		select {
   199  		case <-c1:
   200  			check(LINE, 1000)
   201  		case <-c2:
   202  			check(LINE, 1000)
   203  		default:
   204  			check(LINE, 1)
   205  			return
   206  		}
   207  	}
   208  }
   209  
   210  // Empty control statements created syntax errors. This function
   211  // is here just to be sure that those are handled correctly now.
   212  func testEmptySwitches() {
   213  	check(LINE, 1)
   214  	switch 3 {
   215  	}
   216  	check(LINE, 1)
   217  	switch i := (interface{})(3).(int); i {
   218  	}
   219  	check(LINE, 1)
   220  	c := make(chan int)
   221  	go func() {
   222  		check(LINE, 1)
   223  		c <- 1
   224  		select {}
   225  	}()
   226  	<-c
   227  	check(LINE, 1)
   228  }
   229  
   230  func testFunctionLiteral() {
   231  	a := func(f func()) error {
   232  		f()
   233  		f()
   234  		return nil
   235  	}
   236  
   237  	b := func(f func()) bool {
   238  		f()
   239  		f()
   240  		return true
   241  	}
   242  
   243  	check(LINE, 1)
   244  	a(func() {
   245  		check(LINE, 2)
   246  	})
   247  
   248  	if err := a(func() {
   249  		check(LINE, 2)
   250  	}); err != nil {
   251  	}
   252  
   253  	switch b(func() {
   254  		check(LINE, 2)
   255  	}) {
   256  	}
   257  
   258  	x := 2
   259  	switch x {
   260  	case func() int { check(LINE, 1); return 1 }():
   261  		check(LINE, 0)
   262  		panic("2=1")
   263  	case func() int { check(LINE, 1); return 2 }():
   264  		check(LINE, 1)
   265  	case func() int { check(LINE, 0); return 3 }():
   266  		check(LINE, 0)
   267  		panic("2=3")
   268  	}
   269  }
   270  
   271  func testGoto() {
   272  	for i := 0; i < 2; i++ {
   273  		if i == 0 {
   274  			goto Label
   275  		}
   276  		check(LINE, 1)
   277  	Label:
   278  		check(LINE, 2)
   279  	}
   280  	// Now test that we don't inject empty statements
   281  	// between a label and a loop.
   282  loop:
   283  	for {
   284  		check(LINE, 1)
   285  		break loop
   286  	}
   287  }
   288  
   289  // This comment didn't appear in generated go code.
   290  func haha() {
   291  	// Needed for cover to add counter increment here.
   292  	_ = 42
   293  }
   294  
   295  // Some someFunction.
   296  //
   297  //go:nosplit
   298  func someFunction() {
   299  }
   300  

View as plain text