|
1 | 1 | package tests
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "reflect"
|
5 | 6 | "testing"
|
6 | 7 | "unsafe"
|
@@ -121,3 +122,113 @@ func TestNilPrototypeNotModifiedByReflectGrow(t *testing.T) {
|
121 | 122 | println("s2:", s2)
|
122 | 123 | }
|
123 | 124 | }
|
| 125 | + |
| 126 | +func TestConversionFromSliceToArray(t *testing.T) { |
| 127 | + t.Run(`nil byte slice to zero byte array`, func(t *testing.T) { |
| 128 | + s := []byte(nil) |
| 129 | + _ = [0]byte(s) // should not have runtime panic |
| 130 | + }) |
| 131 | + |
| 132 | + t.Run(`empty byte slice to zero byte array`, func(t *testing.T) { |
| 133 | + s := []byte{} |
| 134 | + _ = [0]byte(s) // should not have runtime panic |
| 135 | + }) |
| 136 | + |
| 137 | + t.Run(`3 byte slice to 3 byte array`, func(t *testing.T) { |
| 138 | + s := []byte{12, 34, 56} |
| 139 | + a := [3]byte(s) |
| 140 | + if s[0] != a[0] || s[1] != a[1] || s[2] != a[2] { |
| 141 | + t.Errorf("slice and array are not equal after conversion:\n\tslice: %#v\n\tarray: %#v", s, a) |
| 142 | + } |
| 143 | + }) |
| 144 | + |
| 145 | + t.Run(`4 byte slice to 4 byte array`, func(t *testing.T) { |
| 146 | + s := []byte{12, 34, 56, 78} |
| 147 | + a := [4]byte(s) |
| 148 | + if s[0] != a[0] || s[1] != a[1] || s[2] != a[2] || s[3] != a[3] { |
| 149 | + t.Errorf("slice and array are not equal after conversion:\n\tslice: %#v\n\tarray: %#v", s, a) |
| 150 | + } |
| 151 | + }) |
| 152 | + |
| 153 | + t.Run(`5 byte slice to 5 byte array`, func(t *testing.T) { |
| 154 | + s := []byte{12, 34, 56, 78, 90} |
| 155 | + a := [5]byte(s) |
| 156 | + if s[0] != a[0] || s[1] != a[1] || s[2] != a[2] || s[3] != a[3] || s[4] != a[4] { |
| 157 | + t.Errorf("slice and array are not equal after conversion:\n\tslice: %#v\n\tarray: %#v", s, a) |
| 158 | + } |
| 159 | + }) |
| 160 | + |
| 161 | + t.Run(`larger 5 byte slice to smaller 4 byte array`, func(t *testing.T) { |
| 162 | + s := []byte{12, 34, 56, 78, 90} |
| 163 | + a := [4]byte(s) |
| 164 | + if s[0] != a[0] || s[1] != a[1] || s[2] != a[2] || s[3] != a[3] { |
| 165 | + t.Errorf("slice and array are not equal after conversion:\n\tslice: %#v\n\tarray: %#v", s, a) |
| 166 | + } |
| 167 | + }) |
| 168 | + |
| 169 | + t.Run(`larger 4 byte slice to smaller zero byte array`, func(t *testing.T) { |
| 170 | + s := []byte{12, 34, 56, 78} |
| 171 | + _ = [0]byte(s) // should not have runtime panic |
| 172 | + }) |
| 173 | + |
| 174 | + t.Run(`smaller 3 byte slice to larger 4 byte array`, func(t *testing.T) { |
| 175 | + defer func() { |
| 176 | + if r := recover(); r != nil { |
| 177 | + err := fmt.Sprintf(`%v`, r) |
| 178 | + exp := `runtime error: cannot convert slice with length 3 to array or pointer to array with length 4` |
| 179 | + if err != exp { |
| 180 | + t.Error(`unexpected panic message:`, r) |
| 181 | + t.Log("\texpected:", exp) |
| 182 | + } |
| 183 | + } |
| 184 | + }() |
| 185 | + |
| 186 | + s := []byte{12, 34, 56} |
| 187 | + a := [4]byte(s) |
| 188 | + t.Errorf("expected a runtime panic:\n\tslice: %#v\n\tarray: %#v", s, a) |
| 189 | + }) |
| 190 | + |
| 191 | + t.Run(`nil byte slice to 5 byte array`, func(t *testing.T) { |
| 192 | + defer func() { |
| 193 | + if r := recover(); r != nil { |
| 194 | + err := fmt.Sprintf(`%v`, r) |
| 195 | + exp := `runtime error: cannot convert slice with length 0 to array or pointer to array with length 5` |
| 196 | + if err != exp { |
| 197 | + t.Error(`unexpected panic message:`, r) |
| 198 | + t.Log("\texpected:", exp) |
| 199 | + } |
| 200 | + } |
| 201 | + }() |
| 202 | + |
| 203 | + s := []byte(nil) |
| 204 | + a := [5]byte(s) |
| 205 | + t.Errorf("expected a runtime panic:\n\tslice: %#v\n\tarray: %#v", s, a) |
| 206 | + }) |
| 207 | + |
| 208 | + type Cat struct { |
| 209 | + name string |
| 210 | + age int |
| 211 | + } |
| 212 | + cats := []Cat{ |
| 213 | + {name: "Tom", age: 3}, |
| 214 | + {name: "Jonesy", age: 5}, |
| 215 | + {name: "Sylvester", age: 7}, |
| 216 | + {name: "Rita", age: 2}, |
| 217 | + } |
| 218 | + |
| 219 | + t.Run(`4 Cat slice to 4 Cat array`, func(t *testing.T) { |
| 220 | + s := cats |
| 221 | + a := [4]Cat(s) |
| 222 | + if s[0] != a[0] || s[1] != a[1] || s[2] != a[2] || s[3] != a[3] { |
| 223 | + t.Errorf("slice and array are not equal after conversion:\n\tslice: %#v\n\tarray: %#v", s, a) |
| 224 | + } |
| 225 | + }) |
| 226 | + |
| 227 | + t.Run(`4 *Cat slice to 4 *Cat array`, func(t *testing.T) { |
| 228 | + s := []*Cat{&cats[0], &cats[1], &cats[2], &cats[3]} |
| 229 | + a := [4]*Cat(s) |
| 230 | + if s[0] != a[0] || s[1] != a[1] || s[2] != a[2] || s[3] != a[3] { |
| 231 | + t.Errorf("slice and array are not equal after conversion:\n\tslice: %#v\n\tarray: %#v", s, a) |
| 232 | + } |
| 233 | + }) |
| 234 | +} |
0 commit comments