3 // Tests of composite literals.
8 // TODO(adonovan): we can no longer print maps
9 // until the interpreter supports (reflect.Value).MapRange.
12 m1 := []*M{{1: 1}, &M{2: 2}}
13 want := "map[1:1] map[2:2]"
14 if got := fmt.Sprint(*m1[0], *m1[1]); got != want {
17 m2 := []M{{1: 1}, M{2: 2}}
18 if got := fmt.Sprint(m2[0], m2[1]); got != want {
23 // Nonliteral keys in composite literal.
26 var v = []int{1 + zero: 42}
27 if x := fmt.Sprint(v); x != "[0 0 42]" {
32 // Test for in-place initialization.
85 // Regression test for https://golang.org/issue/10127:
86 // composite literal clobbers destination before reading from it.
91 m := M{"x": 1, "y": 2}
92 m = M{"x": m["y"], "y": m["x"]}
93 if m["x"] != 2 || m["y"] != 1 {
98 m, n = M{"x": n["x"]}, M{"x": m["x"]} // parallel assignment
99 if got := fmt.Sprint(m["x"], n["x"]); got != "3 2" {
106 type T struct{ x, y, z int }
107 t := T{x: 1, y: 2, z: 3}
109 t = T{x: t.y, y: t.z, z: t.x} // all fields
110 if got := fmt.Sprint(t); got != "{2 3 1}" {
114 t = T{x: t.y, y: t.z + 3} // not all fields
115 if got := fmt.Sprint(t); got != "{3 4 0}" {
119 u := T{x: 5, y: 6, z: 7}
120 t, u = T{x: u.x}, T{x: t.x} // parallel assignment
121 if got := fmt.Sprint(t, u); got != "{5 0 0} {3 0 0}" {
128 a := [3]int{0: 1, 1: 2, 2: 3}
130 a = [3]int{0: a[1], 1: a[2], 2: a[0]} // all elements
131 if got := fmt.Sprint(a); got != "[2 3 1]" {
135 a = [3]int{0: a[1], 1: a[2] + 3} // not all elements
136 if got := fmt.Sprint(a); got != "[3 4 0]" {
140 b := [3]int{0: 5, 1: 6, 2: 7}
141 a, b = [3]int{0: b[0]}, [3]int{0: a[0]} // parallel assignment
142 if got := fmt.Sprint(a, b); got != "[5 0 0] [3 0 0]" {
149 s := []int{0: 1, 1: 2, 2: 3}
151 s = []int{0: s[1], 1: s[2], 2: s[0]} // all elements
152 if got := fmt.Sprint(s); got != "[2 3 1]" {
156 s = []int{0: s[1], 1: s[2] + 3} // not all elements
157 if got := fmt.Sprint(s); got != "[3 4]" {
161 t := []int{0: 5, 1: 6, 2: 7}
162 s, t = []int{0: t[0]}, []int{0: s[0]} // parallel assignment
163 if got := fmt.Sprint(s, t); got != "[5] [3]" {
169 // Regression test for https://golang.org/issue/13341:
170 // within a map literal, if a key expression is a composite literal,
171 // Go 1.5 allows its type to be omitted. An & operation may be implied.
173 type S struct{ x int }
174 // same as map[*S]bool{&S{x: 1}: true}
175 m := map[*S]bool{{x: 1}: true}
178 panic(s) // wrong key
182 panic("map is empty")