github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/src/runtime/race/testdata/slice_test.go (about)

     1  // Copyright 2012 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  package race_test
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  func TestRaceSliceRW(t *testing.T) {
    12  	ch := make(chan bool, 1)
    13  	a := make([]int, 2)
    14  	go func() {
    15  		a[1] = 1
    16  		ch <- true
    17  	}()
    18  	_ = a[1]
    19  	<-ch
    20  }
    21  
    22  func TestNoRaceSliceRW(t *testing.T) {
    23  	ch := make(chan bool, 1)
    24  	a := make([]int, 2)
    25  	go func() {
    26  		a[0] = 1
    27  		ch <- true
    28  	}()
    29  	_ = a[1]
    30  	<-ch
    31  }
    32  
    33  func TestRaceSliceWW(t *testing.T) {
    34  	a := make([]int, 10)
    35  	ch := make(chan bool, 1)
    36  	go func() {
    37  		a[1] = 1
    38  		ch <- true
    39  	}()
    40  	a[1] = 2
    41  	<-ch
    42  }
    43  
    44  func TestNoRaceArrayWW(t *testing.T) {
    45  	var a [5]int
    46  	ch := make(chan bool, 1)
    47  	go func() {
    48  		a[0] = 1
    49  		ch <- true
    50  	}()
    51  	a[1] = 2
    52  	<-ch
    53  }
    54  
    55  func TestRaceArrayWW(t *testing.T) {
    56  	var a [5]int
    57  	ch := make(chan bool, 1)
    58  	go func() {
    59  		a[1] = 1
    60  		ch <- true
    61  	}()
    62  	a[1] = 2
    63  	<-ch
    64  }
    65  
    66  func TestNoRaceSliceWriteLen(t *testing.T) {
    67  	ch := make(chan bool, 1)
    68  	a := make([]bool, 1)
    69  	go func() {
    70  		a[0] = true
    71  		ch <- true
    72  	}()
    73  	_ = len(a)
    74  	<-ch
    75  }
    76  
    77  func TestNoRaceSliceWriteCap(t *testing.T) {
    78  	ch := make(chan bool, 1)
    79  	a := make([]uint64, 100)
    80  	go func() {
    81  		a[50] = 123
    82  		ch <- true
    83  	}()
    84  	_ = cap(a)
    85  	<-ch
    86  }
    87  
    88  func TestRaceSliceCopyRead(t *testing.T) {
    89  	ch := make(chan bool, 1)
    90  	a := make([]int, 10)
    91  	b := make([]int, 10)
    92  	go func() {
    93  		_ = a[5]
    94  		ch <- true
    95  	}()
    96  	copy(a, b)
    97  	<-ch
    98  }
    99  
   100  func TestNoRaceSliceWriteCopy(t *testing.T) {
   101  	ch := make(chan bool, 1)
   102  	a := make([]int, 10)
   103  	b := make([]int, 10)
   104  	go func() {
   105  		a[5] = 1
   106  		ch <- true
   107  	}()
   108  	copy(a[:5], b[:5])
   109  	<-ch
   110  }
   111  
   112  func TestRaceSliceCopyWrite2(t *testing.T) {
   113  	ch := make(chan bool, 1)
   114  	a := make([]int, 10)
   115  	b := make([]int, 10)
   116  	go func() {
   117  		b[5] = 1
   118  		ch <- true
   119  	}()
   120  	copy(a, b)
   121  	<-ch
   122  }
   123  
   124  func TestRaceSliceCopyWrite3(t *testing.T) {
   125  	ch := make(chan bool, 1)
   126  	a := make([]byte, 10)
   127  	go func() {
   128  		a[7] = 1
   129  		ch <- true
   130  	}()
   131  	copy(a, "qwertyqwerty")
   132  	<-ch
   133  }
   134  
   135  func TestNoRaceSliceCopyRead(t *testing.T) {
   136  	ch := make(chan bool, 1)
   137  	a := make([]int, 10)
   138  	b := make([]int, 10)
   139  	go func() {
   140  		_ = b[5]
   141  		ch <- true
   142  	}()
   143  	copy(a, b)
   144  	<-ch
   145  }
   146  
   147  func TestRacePointerSliceCopyRead(t *testing.T) {
   148  	ch := make(chan bool, 1)
   149  	a := make([]*int, 10)
   150  	b := make([]*int, 10)
   151  	go func() {
   152  		_ = a[5]
   153  		ch <- true
   154  	}()
   155  	copy(a, b)
   156  	<-ch
   157  }
   158  
   159  func TestNoRacePointerSliceWriteCopy(t *testing.T) {
   160  	ch := make(chan bool, 1)
   161  	a := make([]*int, 10)
   162  	b := make([]*int, 10)
   163  	go func() {
   164  		a[5] = new(int)
   165  		ch <- true
   166  	}()
   167  	copy(a[:5], b[:5])
   168  	<-ch
   169  }
   170  
   171  func TestRacePointerSliceCopyWrite2(t *testing.T) {
   172  	ch := make(chan bool, 1)
   173  	a := make([]*int, 10)
   174  	b := make([]*int, 10)
   175  	go func() {
   176  		b[5] = new(int)
   177  		ch <- true
   178  	}()
   179  	copy(a, b)
   180  	<-ch
   181  }
   182  
   183  func TestNoRacePointerSliceCopyRead(t *testing.T) {
   184  	ch := make(chan bool, 1)
   185  	a := make([]*int, 10)
   186  	b := make([]*int, 10)
   187  	go func() {
   188  		_ = b[5]
   189  		ch <- true
   190  	}()
   191  	copy(a, b)
   192  	<-ch
   193  }
   194  
   195  func TestNoRaceSliceWriteSlice2(t *testing.T) {
   196  	ch := make(chan bool, 1)
   197  	a := make([]float64, 10)
   198  	go func() {
   199  		a[2] = 1.0
   200  		ch <- true
   201  	}()
   202  	_ = a[0:5]
   203  	<-ch
   204  }
   205  
   206  func TestRaceSliceWriteSlice(t *testing.T) {
   207  	ch := make(chan bool, 1)
   208  	a := make([]float64, 10)
   209  	go func() {
   210  		a[2] = 1.0
   211  		ch <- true
   212  	}()
   213  	a = a[5:10]
   214  	<-ch
   215  }
   216  
   217  func TestNoRaceSliceWriteSlice(t *testing.T) {
   218  	ch := make(chan bool, 1)
   219  	a := make([]float64, 10)
   220  	go func() {
   221  		a[2] = 1.0
   222  		ch <- true
   223  	}()
   224  	_ = a[5:10]
   225  	<-ch
   226  }
   227  
   228  func TestNoRaceSliceLenCap(t *testing.T) {
   229  	ch := make(chan bool, 1)
   230  	a := make([]struct{}, 10)
   231  	go func() {
   232  		_ = len(a)
   233  		ch <- true
   234  	}()
   235  	_ = cap(a)
   236  	<-ch
   237  }
   238  
   239  func TestNoRaceStructSlicesRangeWrite(t *testing.T) {
   240  	type Str struct {
   241  		a []int
   242  		b []int
   243  	}
   244  	ch := make(chan bool, 1)
   245  	var s Str
   246  	s.a = make([]int, 10)
   247  	s.b = make([]int, 10)
   248  	go func() {
   249  		for range s.a {
   250  		}
   251  		ch <- true
   252  	}()
   253  	s.b[5] = 5
   254  	<-ch
   255  }
   256  
   257  func TestRaceSliceDifferent(t *testing.T) {
   258  	c := make(chan bool, 1)
   259  	s := make([]int, 10)
   260  	s2 := s
   261  	go func() {
   262  		s[3] = 3
   263  		c <- true
   264  	}()
   265  	// false negative because s2 is PAUTO w/o PHEAP
   266  	// so we do not instrument it
   267  	s2[3] = 3
   268  	<-c
   269  }
   270  
   271  func TestRaceSliceRangeWrite(t *testing.T) {
   272  	c := make(chan bool, 1)
   273  	s := make([]int, 10)
   274  	go func() {
   275  		s[3] = 3
   276  		c <- true
   277  	}()
   278  	for _, v := range s {
   279  		_ = v
   280  	}
   281  	<-c
   282  }
   283  
   284  func TestNoRaceSliceRangeWrite(t *testing.T) {
   285  	c := make(chan bool, 1)
   286  	s := make([]int, 10)
   287  	go func() {
   288  		s[3] = 3
   289  		c <- true
   290  	}()
   291  	for range s {
   292  	}
   293  	<-c
   294  }
   295  
   296  func TestRaceSliceRangeAppend(t *testing.T) {
   297  	c := make(chan bool, 1)
   298  	s := make([]int, 10)
   299  	go func() {
   300  		s = append(s, 3)
   301  		c <- true
   302  	}()
   303  	for range s {
   304  	}
   305  	<-c
   306  }
   307  
   308  func TestNoRaceSliceRangeAppend(t *testing.T) {
   309  	c := make(chan bool, 1)
   310  	s := make([]int, 10)
   311  	go func() {
   312  		_ = append(s, 3)
   313  		c <- true
   314  	}()
   315  	for range s {
   316  	}
   317  	<-c
   318  }
   319  
   320  func TestRaceSliceVarWrite(t *testing.T) {
   321  	c := make(chan bool, 1)
   322  	s := make([]int, 10)
   323  	go func() {
   324  		s[3] = 3
   325  		c <- true
   326  	}()
   327  	s = make([]int, 20)
   328  	<-c
   329  }
   330  
   331  func TestRaceSliceVarRead(t *testing.T) {
   332  	c := make(chan bool, 1)
   333  	s := make([]int, 10)
   334  	go func() {
   335  		_ = s[3]
   336  		c <- true
   337  	}()
   338  	s = make([]int, 20)
   339  	<-c
   340  }
   341  
   342  func TestRaceSliceVarRange(t *testing.T) {
   343  	c := make(chan bool, 1)
   344  	s := make([]int, 10)
   345  	go func() {
   346  		for range s {
   347  		}
   348  		c <- true
   349  	}()
   350  	s = make([]int, 20)
   351  	<-c
   352  }
   353  
   354  func TestRaceSliceVarAppend(t *testing.T) {
   355  	c := make(chan bool, 1)
   356  	s := make([]int, 10)
   357  	go func() {
   358  		_ = append(s, 10)
   359  		c <- true
   360  	}()
   361  	s = make([]int, 20)
   362  	<-c
   363  }
   364  
   365  func TestRaceSliceVarCopy(t *testing.T) {
   366  	c := make(chan bool, 1)
   367  	s := make([]int, 10)
   368  	go func() {
   369  		s2 := make([]int, 10)
   370  		copy(s, s2)
   371  		c <- true
   372  	}()
   373  	s = make([]int, 20)
   374  	<-c
   375  }
   376  
   377  func TestRaceSliceVarCopy2(t *testing.T) {
   378  	c := make(chan bool, 1)
   379  	s := make([]int, 10)
   380  	go func() {
   381  		s2 := make([]int, 10)
   382  		copy(s2, s)
   383  		c <- true
   384  	}()
   385  	s = make([]int, 20)
   386  	<-c
   387  }
   388  
   389  func TestRaceSliceAppend(t *testing.T) {
   390  	c := make(chan bool, 1)
   391  	s := make([]int, 10, 20)
   392  	go func() {
   393  		_ = append(s, 1)
   394  		c <- true
   395  	}()
   396  	_ = append(s, 2)
   397  	<-c
   398  }
   399  
   400  func TestRaceSliceAppendWrite(t *testing.T) {
   401  	c := make(chan bool, 1)
   402  	s := make([]int, 10)
   403  	go func() {
   404  		_ = append(s, 1)
   405  		c <- true
   406  	}()
   407  	s[0] = 42
   408  	<-c
   409  }
   410  
   411  func TestRaceSliceAppendSlice(t *testing.T) {
   412  	c := make(chan bool, 1)
   413  	s := make([]int, 10)
   414  	go func() {
   415  		s2 := make([]int, 10)
   416  		_ = append(s, s2...)
   417  		c <- true
   418  	}()
   419  	s[0] = 42
   420  	<-c
   421  }
   422  
   423  func TestRaceSliceAppendSlice2(t *testing.T) {
   424  	c := make(chan bool, 1)
   425  	s := make([]int, 10)
   426  	s2foobar := make([]int, 10)
   427  	go func() {
   428  		_ = append(s, s2foobar...)
   429  		c <- true
   430  	}()
   431  	s2foobar[5] = 42
   432  	<-c
   433  }
   434  
   435  func TestRaceSliceAppendString(t *testing.T) {
   436  	c := make(chan bool, 1)
   437  	s := make([]byte, 10)
   438  	go func() {
   439  		_ = append(s, "qwerty"...)
   440  		c <- true
   441  	}()
   442  	s[0] = 42
   443  	<-c
   444  }
   445  
   446  func TestRacePointerSliceAppend(t *testing.T) {
   447  	c := make(chan bool, 1)
   448  	s := make([]*int, 10, 20)
   449  	go func() {
   450  		_ = append(s, new(int))
   451  		c <- true
   452  	}()
   453  	_ = append(s, new(int))
   454  	<-c
   455  }
   456  
   457  func TestRacePointerSliceAppendWrite(t *testing.T) {
   458  	c := make(chan bool, 1)
   459  	s := make([]*int, 10)
   460  	go func() {
   461  		_ = append(s, new(int))
   462  		c <- true
   463  	}()
   464  	s[0] = new(int)
   465  	<-c
   466  }
   467  
   468  func TestRacePointerSliceAppendSlice(t *testing.T) {
   469  	c := make(chan bool, 1)
   470  	s := make([]*int, 10)
   471  	go func() {
   472  		s2 := make([]*int, 10)
   473  		_ = append(s, s2...)
   474  		c <- true
   475  	}()
   476  	s[0] = new(int)
   477  	<-c
   478  }
   479  
   480  func TestRacePointerSliceAppendSlice2(t *testing.T) {
   481  	c := make(chan bool, 1)
   482  	s := make([]*int, 10)
   483  	s2foobar := make([]*int, 10)
   484  	go func() {
   485  		_ = append(s, s2foobar...)
   486  		c <- true
   487  	}()
   488  	println("WRITE:", &s2foobar[5])
   489  	s2foobar[5] = nil
   490  	<-c
   491  }
   492  
   493  func TestNoRaceSliceIndexAccess(t *testing.T) {
   494  	c := make(chan bool, 1)
   495  	s := make([]int, 10)
   496  	v := 0
   497  	go func() {
   498  		_ = v
   499  		c <- true
   500  	}()
   501  	s[v] = 1
   502  	<-c
   503  }
   504  
   505  func TestNoRaceSliceIndexAccess2(t *testing.T) {
   506  	c := make(chan bool, 1)
   507  	s := make([]int, 10)
   508  	v := 0
   509  	go func() {
   510  		_ = v
   511  		c <- true
   512  	}()
   513  	_ = s[v]
   514  	<-c
   515  }
   516  
   517  func TestRaceSliceIndexAccess(t *testing.T) {
   518  	c := make(chan bool, 1)
   519  	s := make([]int, 10)
   520  	v := 0
   521  	go func() {
   522  		v = 1
   523  		c <- true
   524  	}()
   525  	s[v] = 1
   526  	<-c
   527  }
   528  
   529  func TestRaceSliceIndexAccess2(t *testing.T) {
   530  	c := make(chan bool, 1)
   531  	s := make([]int, 10)
   532  	v := 0
   533  	go func() {
   534  		v = 1
   535  		c <- true
   536  	}()
   537  	_ = s[v]
   538  	<-c
   539  }
   540  
   541  func TestRaceSliceByteToString(t *testing.T) {
   542  	c := make(chan string)
   543  	s := make([]byte, 10)
   544  	go func() {
   545  		c <- string(s)
   546  	}()
   547  	s[0] = 42
   548  	<-c
   549  }
   550  
   551  func TestRaceSliceRuneToString(t *testing.T) {
   552  	c := make(chan string)
   553  	s := make([]rune, 10)
   554  	go func() {
   555  		c <- string(s)
   556  	}()
   557  	s[9] = 42
   558  	<-c
   559  }
   560  
   561  func TestRaceConcatString(t *testing.T) {
   562  	s := "hello"
   563  	c := make(chan string, 1)
   564  	go func() {
   565  		c <- s + " world"
   566  	}()
   567  	s = "world"
   568  	<-c
   569  }
   570  
   571  func TestRaceCompareString(t *testing.T) {
   572  	s1 := "hello"
   573  	s2 := "world"
   574  	c := make(chan bool, 1)
   575  	go func() {
   576  		c <- s1 == s2
   577  	}()
   578  	s1 = s2
   579  	<-c
   580  }
   581  
   582  func TestRaceSlice3(t *testing.T) {
   583  	done := make(chan bool)
   584  	x := make([]int, 10)
   585  	i := 2
   586  	go func() {
   587  		i = 3
   588  		done <- true
   589  	}()
   590  	_ = x[:1:i]
   591  	<-done
   592  }