...
Run Format

Source file test/complit.go

Documentation: test

     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 composite literals.
     8  
     9  package main
    10  
    11  type T struct {
    12  	i    int
    13  	f    float64
    14  	s    string
    15  	next *T
    16  }
    17  
    18  type R struct {
    19  	num int
    20  }
    21  
    22  func itor(a int) *R {
    23  	r := new(R)
    24  	r.num = a
    25  	return r
    26  }
    27  
    28  func eq(a []*R) {
    29  	for i := 0; i < len(a); i++ {
    30  		if a[i].num != i {
    31  			panic("bad")
    32  		}
    33  	}
    34  }
    35  
    36  func teq(t *T, n int) {
    37  	for i := 0; i < n; i++ {
    38  		if t == nil || t.i != i {
    39  			panic("bad")
    40  		}
    41  		t = t.next
    42  	}
    43  	if t != nil {
    44  		panic("bad")
    45  	}
    46  }
    47  
    48  type P struct {
    49  	a, b int
    50  }
    51  
    52  func NewP(a, b int) *P {
    53  	return &P{a, b}
    54  }
    55  
    56  func main() {
    57  	var t T
    58  	t = T{0, 7.2, "hi", &t}
    59  
    60  	var tp *T
    61  	tp = &T{0, 7.2, "hi", &t}
    62  
    63  	tl := &T{i: 0, next: &T{i: 1, next: &T{i: 2, next: &T{i: 3, next: &T{i: 4}}}}}
    64  	teq(tl, 5)
    65  
    66  	a1 := []int{1, 2, 3}
    67  	if len(a1) != 3 {
    68  		panic("a1")
    69  	}
    70  	a2 := [10]int{1, 2, 3}
    71  	if len(a2) != 10 || cap(a2) != 10 {
    72  		panic("a2")
    73  	}
    74  
    75  	a3 := [10]int{1, 2, 3}
    76  	if len(a3) != 10 || a2[3] != 0 {
    77  		panic("a3")
    78  	}
    79  
    80  	var oai []int
    81  	oai = []int{1, 2, 3}
    82  	if len(oai) != 3 {
    83  		panic("oai")
    84  	}
    85  
    86  	at := [...]*T{&t, tp, &t}
    87  	if len(at) != 3 {
    88  		panic("at")
    89  	}
    90  
    91  	c := make(chan int)
    92  	ac := []chan int{c, c, c}
    93  	if len(ac) != 3 {
    94  		panic("ac")
    95  	}
    96  
    97  	aat := [][len(at)]*T{at, at}
    98  	if len(aat) != 2 || len(aat[1]) != 3 {
    99  		panic("aat")
   100  	}
   101  
   102  	s := string([]byte{'h', 'e', 'l', 'l', 'o'})
   103  	if s != "hello" {
   104  		panic("s")
   105  	}
   106  
   107  	m := map[string]float64{"one": 1.0, "two": 2.0, "pi": 22. / 7.}
   108  	if len(m) != 3 {
   109  		panic("m")
   110  	}
   111  
   112  	eq([]*R{itor(0), itor(1), itor(2), itor(3), itor(4), itor(5)})
   113  	eq([]*R{{0}, {1}, {2}, {3}, {4}, {5}})
   114  
   115  	p1 := NewP(1, 2)
   116  	p2 := NewP(1, 2)
   117  	if p1 == p2 {
   118  		panic("NewP")
   119  	}
   120  }
   121  

View as plain text