github.com/goplus/gossa@v0.3.25/testdata/complit.go (about) 1 package main 2 3 // Tests of composite literals. 4 5 import "fmt" 6 7 // Map literals. 8 // TODO(adonovan): we can no longer print maps 9 // until the interpreter supports (reflect.Value).MapRange. 10 func _() { 11 type M map[int]int 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 { 15 panic(got) 16 } 17 m2 := []M{{1: 1}, M{2: 2}} 18 if got := fmt.Sprint(m2[0], m2[1]); got != want { 19 panic(got) 20 } 21 } 22 23 // Nonliteral keys in composite literal. 24 func init() { 25 const zero int = 1 26 var v = []int{1 + zero: 42} 27 if x := fmt.Sprint(v); x != "[0 0 42]" { 28 panic(x) 29 } 30 } 31 32 // Test for in-place initialization. 33 func init() { 34 // struct 35 type S struct { 36 a, b int 37 } 38 s := S{1, 2} 39 s = S{b: 3} 40 if s.a != 0 { 41 panic("s.a != 0") 42 } 43 if s.b != 3 { 44 panic("s.b != 3") 45 } 46 s = S{} 47 if s.a != 0 { 48 panic("s.a != 0") 49 } 50 if s.b != 0 { 51 panic("s.b != 0") 52 } 53 54 // array 55 type A [4]int 56 a := A{2, 4, 6, 8} 57 a = A{1: 6, 2: 4} 58 if a[0] != 0 { 59 panic("a[0] != 0") 60 } 61 if a[1] != 6 { 62 panic("a[1] != 6") 63 } 64 if a[2] != 4 { 65 panic("a[2] != 4") 66 } 67 if a[3] != 0 { 68 panic("a[3] != 0") 69 } 70 a = A{} 71 if a[0] != 0 { 72 panic("a[0] != 0") 73 } 74 if a[1] != 0 { 75 panic("a[1] != 0") 76 } 77 if a[2] != 0 { 78 panic("a[2] != 0") 79 } 80 if a[3] != 0 { 81 panic("a[3] != 0") 82 } 83 } 84 85 // Regression test for https://golang.org/issue/10127: 86 // composite literal clobbers destination before reading from it. 87 func init() { 88 // map 89 { 90 type M map[string]int 91 m := M{"x": 1, "y": 2} 92 m = M{"x": m["y"], "y": m["x"]} 93 if m["x"] != 2 || m["y"] != 1 { 94 panic(fmt.Sprint(m)) 95 } 96 97 n := M{"x": 3} 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" { 100 panic(got) 101 } 102 } 103 104 // struct 105 { 106 type T struct{ x, y, z int } 107 t := T{x: 1, y: 2, z: 3} 108 109 t = T{x: t.y, y: t.z, z: t.x} // all fields 110 if got := fmt.Sprint(t); got != "{2 3 1}" { 111 panic(got) 112 } 113 114 t = T{x: t.y, y: t.z + 3} // not all fields 115 if got := fmt.Sprint(t); got != "{3 4 0}" { 116 panic(got) 117 } 118 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}" { 122 panic(got) 123 } 124 } 125 126 // array 127 { 128 a := [3]int{0: 1, 1: 2, 2: 3} 129 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]" { 132 panic(got) 133 } 134 135 a = [3]int{0: a[1], 1: a[2] + 3} // not all elements 136 if got := fmt.Sprint(a); got != "[3 4 0]" { 137 panic(got) 138 } 139 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]" { 143 panic(got) 144 } 145 } 146 147 // slice 148 { 149 s := []int{0: 1, 1: 2, 2: 3} 150 151 s = []int{0: s[1], 1: s[2], 2: s[0]} // all elements 152 if got := fmt.Sprint(s); got != "[2 3 1]" { 153 panic(got) 154 } 155 156 s = []int{0: s[1], 1: s[2] + 3} // not all elements 157 if got := fmt.Sprint(s); got != "[3 4]" { 158 panic(got) 159 } 160 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]" { 164 panic(got) 165 } 166 } 167 } 168 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. 172 func init() { 173 type S struct{ x int } 174 // same as map[*S]bool{&S{x: 1}: true} 175 m := map[*S]bool{{x: 1}: true} 176 for s := range m { 177 if s.x != 1 { 178 panic(s) // wrong key 179 } 180 return 181 } 182 panic("map is empty") 183 } 184 185 func main() { 186 }