codekingpro/portable-devtools
114k
1// run2 3// Copyright 2009 The Go Authors. All rights reserved.4// Use of this source code is governed by a BSD-style5// license that can be found in the LICENSE file.6 7// Test initialization of package-level variables.8 9package main10 11import (12 "fmt"13 "reflect"14)15 16type S struct {17 A, B, C, X, Y, Z int18}19 20type T struct {21 S22}23 24var a1 = S{0, 0, 0, 1, 2, 3}25var b1 = S{X: 1, Z: 3, Y: 2}26 27var a2 = S{0, 0, 0, 0, 0, 0}28var b2 = S{}29 30var a3 = T{S{1, 2, 3, 0, 0, 0}}31var b3 = T{S: S{A: 1, B: 2, C: 3}}32 33var a4 = &[16]byte{0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0}34var b4 = &[16]byte{4: 1, 1, 1, 1, 12: 1, 1}35 36var a5 = &[16]byte{1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0}37var b5 = &[16]byte{1, 4: 1, 1, 1, 1, 12: 1, 1}38 39var a6 = &[16]byte{1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0}40var b6 = &[...]byte{1, 4: 1, 1, 1, 1, 12: 1, 1, 0, 0}41 42func f7(ch chan int) [2]chan int { return [2]chan int{ch, ch} }43 44var a7 = f7(make(chan int))45 46func f8(m map[string]string) [2]map[string]string { return [2]map[string]string{m, m} }47func m8(m [2]map[string]string) string {48 m[0]["def"] = "ghi"49 return m[1]["def"]50}51 52var a8 = f8(make(map[string]string))53var a9 = f8(map[string]string{"abc": "def"})54 55func f10(s *S) [2]*S { return [2]*S{s, s} }56 57var a10 = f10(new(S))58var a11 = f10(&S{X: 1})59 60func f12(b []byte) [2][]byte { return [2][]byte{b, b} }61 62var a12 = f12([]byte("hello"))63var a13 = f12([]byte{1, 2, 3})64var a14 = f12(make([]byte, 1))65 66func f15(b []rune) [2][]rune { return [2][]rune{b, b} }67 68var a15 = f15([]rune("hello"))69var a16 = f15([]rune{1, 2, 3})70 71type Same struct {72 a, b interface{}73}74 75var same = []Same{76 {a1, b1},77 {a2, b2},78 {a3, b3},79 {a4, b4},80 {a5, b5},81 {a6, b6},82 {a7[0] == a7[1], true},83 {m8(a8) == "ghi", true},84 {m8(a9) == "ghi", true},85 {a10[0] == a10[1], true},86 {a11[0] == a11[1], true},87 {&a12[0][0] == &a12[1][0], true},88 {&a13[0][0] == &a13[1][0], true},89 {&a14[0][0] == &a14[1][0], true},90 {&a15[0][0] == &a15[1][0], true},91 {&a16[0][0] == &a16[1][0], true},92}93 94func main() {95 ok := true96 for i, s := range same {97 if !reflect.DeepEqual(s.a, s.b) {98 ok = false99 fmt.Printf("#%d not same: %v and %v\n", i+1, s.a, s.b)100 }101 }102 if !ok {103 fmt.Println("BUG: test/initialize")104 }105}106 