Source file test/initialize.go

     1  // run
     2  
     3  // Copyright 2009 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Test initialization of package-level variables.
     8  
     9  package main
    10  
    11  import (
    12  	"fmt"
    13  	"reflect"
    14  )
    15  
    16  type S struct {
    17  	A, B, C, X, Y, Z int
    18  }
    19  
    20  type T struct {
    21  	S
    22  }
    23  
    24  var a1 = S{0, 0, 0, 1, 2, 3}
    25  var b1 = S{X: 1, Z: 3, Y: 2}
    26  
    27  var a2 = S{0, 0, 0, 0, 0, 0}
    28  var b2 = S{}
    29  
    30  var a3 = T{S{1, 2, 3, 0, 0, 0}}
    31  var b3 = T{S: S{A: 1, B: 2, C: 3}}
    32  
    33  var a4 = &[16]byte{0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0}
    34  var b4 = &[16]byte{4: 1, 1, 1, 1, 12: 1, 1}
    35  
    36  var a5 = &[16]byte{1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0}
    37  var b5 = &[16]byte{1, 4: 1, 1, 1, 1, 12: 1, 1}
    38  
    39  var a6 = &[16]byte{1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0}
    40  var b6 = &[...]byte{1, 4: 1, 1, 1, 1, 12: 1, 1, 0, 0}
    41  
    42  func f7(ch chan int) [2]chan int { return [2]chan int{ch, ch} }
    43  
    44  var a7 = f7(make(chan int))
    45  
    46  func f8(m map[string]string) [2]map[string]string { return [2]map[string]string{m, m} }
    47  func m8(m [2]map[string]string) string {
    48  	m[0]["def"] = "ghi"
    49  	return m[1]["def"]
    50  }
    51  
    52  var a8 = f8(make(map[string]string))
    53  var a9 = f8(map[string]string{"abc": "def"})
    54  
    55  func f10(s *S) [2]*S { return [2]*S{s, s} }
    56  
    57  var a10 = f10(new(S))
    58  var a11 = f10(&S{X: 1})
    59  
    60  func f12(b []byte) [2][]byte { return [2][]byte{b, b} }
    61  
    62  var a12 = f12([]byte("hello"))
    63  var a13 = f12([]byte{1, 2, 3})
    64  var a14 = f12(make([]byte, 1))
    65  
    66  func f15(b []rune) [2][]rune { return [2][]rune{b, b} }
    67  
    68  var a15 = f15([]rune("hello"))
    69  var a16 = f15([]rune{1, 2, 3})
    70  
    71  type Same struct {
    72  	a, b interface{}
    73  }
    74  
    75  var 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  
    94  func main() {
    95  	ok := true
    96  	for i, s := range same {
    97  		if !reflect.DeepEqual(s.a, s.b) {
    98  			ok = false
    99  			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  

View as plain text