github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gotools/go/ssa/interp/testdata/complit.go (about)

     1  package main
     2  
     3  // Tests of composite literals.
     4  
     5  import "fmt"
     6  
     7  // Map literals.
     8  func init() {
     9  	type M map[int]int
    10  	m1 := []*M{{1: 1}, &M{2: 2}}
    11  	want := "map[1:1] map[2:2]"
    12  	if got := fmt.Sprint(*m1[0], *m1[1]); got != want {
    13  		panic(got)
    14  	}
    15  	m2 := []M{{1: 1}, M{2: 2}}
    16  	if got := fmt.Sprint(m2[0], m2[1]); got != want {
    17  		panic(got)
    18  	}
    19  }
    20  
    21  // Nonliteral keys in composite literal.
    22  func init() {
    23  	const zero int = 1
    24  	var v = []int{1 + zero: 42}
    25  	if x := fmt.Sprint(v); x != "[0 0 42]" {
    26  		panic(x)
    27  	}
    28  }
    29  
    30  // Test for in-place initialization.
    31  func init() {
    32  	// struct
    33  	type S struct {
    34  		a, b int
    35  	}
    36  	s := S{1, 2}
    37  	s = S{b: 3}
    38  	if s.a != 0 {
    39  		panic("s.a != 0")
    40  	}
    41  	if s.b != 3 {
    42  		panic("s.b != 3")
    43  	}
    44  	s = S{}
    45  	if s.a != 0 {
    46  		panic("s.a != 0")
    47  	}
    48  	if s.b != 0 {
    49  		panic("s.b != 0")
    50  	}
    51  
    52  	// array
    53  	type A [4]int
    54  	a := A{2, 4, 6, 8}
    55  	a = A{1: 6, 2: 4}
    56  	if a[0] != 0 {
    57  		panic("a[0] != 0")
    58  	}
    59  	if a[1] != 6 {
    60  		panic("a[1] != 6")
    61  	}
    62  	if a[2] != 4 {
    63  		panic("a[2] != 4")
    64  	}
    65  	if a[3] != 0 {
    66  		panic("a[3] != 0")
    67  	}
    68  	a = A{}
    69  	if a[0] != 0 {
    70  		panic("a[0] != 0")
    71  	}
    72  	if a[1] != 0 {
    73  		panic("a[1] != 0")
    74  	}
    75  	if a[2] != 0 {
    76  		panic("a[2] != 0")
    77  	}
    78  	if a[3] != 0 {
    79  		panic("a[3] != 0")
    80  	}
    81  }
    82  
    83  // Regression test for https://github.com/golang/go/issues/10127:
    84  // composite literal clobbers destination before reading from it.
    85  func init() {
    86  	// map
    87  	{
    88  		type M map[string]int
    89  		m := M{"x": 1, "y": 2}
    90  		m = M{"x": m["y"], "y": m["x"]}
    91  		if m["x"] != 2 || m["y"] != 1 {
    92  			panic(fmt.Sprint(m))
    93  		}
    94  
    95  		n := M{"x": 3}
    96  		m, n = M{"x": n["x"]}, M{"x": m["x"]} // parallel assignment
    97  		if got := fmt.Sprint(m["x"], n["x"]); got != "3 2" {
    98  			panic(got)
    99  		}
   100  	}
   101  
   102  	// struct
   103  	{
   104  		type T struct{ x, y, z int }
   105  		t := T{x: 1, y: 2, z: 3}
   106  
   107  		t = T{x: t.y, y: t.z, z: t.x} // all fields
   108  		if got := fmt.Sprint(t); got != "{2 3 1}" {
   109  			panic(got)
   110  		}
   111  
   112  		t = T{x: t.y, y: t.z + 3} // not all fields
   113  		if got := fmt.Sprint(t); got != "{3 4 0}" {
   114  			panic(got)
   115  		}
   116  
   117  		u := T{x: 5, y: 6, z: 7}
   118  		t, u = T{x: u.x}, T{x: t.x} // parallel assignment
   119  		if got := fmt.Sprint(t, u); got != "{5 0 0} {3 0 0}" {
   120  			panic(got)
   121  		}
   122  	}
   123  
   124  	// array
   125  	{
   126  		a := [3]int{0: 1, 1: 2, 2: 3}
   127  
   128  		a = [3]int{0: a[1], 1: a[2], 2: a[0]} //  all elements
   129  		if got := fmt.Sprint(a); got != "[2 3 1]" {
   130  			panic(got)
   131  		}
   132  
   133  		a = [3]int{0: a[1], 1: a[2] + 3} //  not all elements
   134  		if got := fmt.Sprint(a); got != "[3 4 0]" {
   135  			panic(got)
   136  		}
   137  
   138  		b := [3]int{0: 5, 1: 6, 2: 7}
   139  		a, b = [3]int{0: b[0]}, [3]int{0: a[0]} // parallel assignment
   140  		if got := fmt.Sprint(a, b); got != "[5 0 0] [3 0 0]" {
   141  			panic(got)
   142  		}
   143  	}
   144  
   145  	// slice
   146  	{
   147  		s := []int{0: 1, 1: 2, 2: 3}
   148  
   149  		s = []int{0: s[1], 1: s[2], 2: s[0]} //  all elements
   150  		if got := fmt.Sprint(s); got != "[2 3 1]" {
   151  			panic(got)
   152  		}
   153  
   154  		s = []int{0: s[1], 1: s[2] + 3} //  not all elements
   155  		if got := fmt.Sprint(s); got != "[3 4]" {
   156  			panic(got)
   157  		}
   158  
   159  		t := []int{0: 5, 1: 6, 2: 7}
   160  		s, t = []int{0: t[0]}, []int{0: s[0]} // parallel assignment
   161  		if got := fmt.Sprint(s, t); got != "[5] [3]" {
   162  			panic(got)
   163  		}
   164  	}
   165  }
   166  
   167  func main() {
   168  }