github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/runtime/race/testdata/comp_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  type P struct {
    12  	x, y int
    13  }
    14  
    15  type S struct {
    16  	s1, s2 P
    17  }
    18  
    19  func TestNoRaceComp(t *testing.T) {
    20  	c := make(chan bool, 1)
    21  	var s S
    22  	go func() {
    23  		s.s2.x = 1
    24  		c <- true
    25  	}()
    26  	s.s2.y = 2
    27  	<-c
    28  }
    29  
    30  func TestNoRaceComp2(t *testing.T) {
    31  	c := make(chan bool, 1)
    32  	var s S
    33  	go func() {
    34  		s.s1.x = 1
    35  		c <- true
    36  	}()
    37  	s.s1.y = 2
    38  	<-c
    39  }
    40  
    41  func TestRaceComp(t *testing.T) {
    42  	c := make(chan bool, 1)
    43  	var s S
    44  	go func() {
    45  		s.s2.y = 1
    46  		c <- true
    47  	}()
    48  	s.s2.y = 2
    49  	<-c
    50  }
    51  
    52  func TestRaceComp2(t *testing.T) {
    53  	c := make(chan bool, 1)
    54  	var s S
    55  	go func() {
    56  		s.s1.x = 1
    57  		c <- true
    58  	}()
    59  	s = S{}
    60  	<-c
    61  }
    62  
    63  func TestRaceComp3(t *testing.T) {
    64  	c := make(chan bool, 1)
    65  	var s S
    66  	go func() {
    67  		s.s2.y = 1
    68  		c <- true
    69  	}()
    70  	s = S{}
    71  	<-c
    72  }
    73  
    74  func TestRaceCompArray(t *testing.T) {
    75  	c := make(chan bool, 1)
    76  	s := make([]S, 10)
    77  	x := 4
    78  	go func() {
    79  		s[x].s2.y = 1
    80  		c <- true
    81  	}()
    82  	x = 5
    83  	<-c
    84  }
    85  
    86  type Ptr struct {
    87  	s1, s2 *P
    88  }
    89  
    90  func TestNoRaceCompPtr(t *testing.T) {
    91  	c := make(chan bool, 1)
    92  	p := Ptr{&P{}, &P{}}
    93  	go func() {
    94  		p.s1.x = 1
    95  		c <- true
    96  	}()
    97  	p.s1.y = 2
    98  	<-c
    99  }
   100  
   101  func TestNoRaceCompPtr2(t *testing.T) {
   102  	c := make(chan bool, 1)
   103  	p := Ptr{&P{}, &P{}}
   104  	go func() {
   105  		p.s1.x = 1
   106  		c <- true
   107  	}()
   108  	_ = p
   109  	<-c
   110  }
   111  
   112  func TestRaceCompPtr(t *testing.T) {
   113  	c := make(chan bool, 1)
   114  	p := Ptr{&P{}, &P{}}
   115  	go func() {
   116  		p.s2.x = 1
   117  		c <- true
   118  	}()
   119  	p.s2.x = 2
   120  	<-c
   121  }
   122  
   123  func TestRaceCompPtr2(t *testing.T) {
   124  	c := make(chan bool, 1)
   125  	p := Ptr{&P{}, &P{}}
   126  	go func() {
   127  		p.s2.x = 1
   128  		c <- true
   129  	}()
   130  	p.s2 = &P{}
   131  	<-c
   132  }