Source file test/bigalg.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 the internal "algorithms" for objects larger than a word: hashing, equality etc.
     8  
     9  package main
    10  
    11  type T struct {
    12  	a float64
    13  	b int64
    14  	c string
    15  	d byte
    16  }
    17  
    18  var a = []int{1, 2, 3}
    19  var NIL []int
    20  
    21  func arraycmptest() {
    22  	if NIL != nil {
    23  		println("fail1:", NIL, "!= nil")
    24  		panic("bigalg")
    25  	}
    26  	if nil != NIL {
    27  		println("fail2: nil !=", NIL)
    28  		panic("bigalg")
    29  	}
    30  	if a == nil || nil == a {
    31  		println("fail3:", a, "== nil")
    32  		panic("bigalg")
    33  	}
    34  }
    35  
    36  func SameArray(a, b []int) bool {
    37  	if len(a) != len(b) || cap(a) != cap(b) {
    38  		return false
    39  	}
    40  	if len(a) > 0 && &a[0] != &b[0] {
    41  		return false
    42  	}
    43  	return true
    44  }
    45  
    46  var t = T{1.5, 123, "hello", 255}
    47  var mt = make(map[int]T)
    48  var ma = make(map[int][]int)
    49  
    50  func maptest() {
    51  	mt[0] = t
    52  	t1 := mt[0]
    53  	if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d {
    54  		println("fail: map val struct", t1.a, t1.b, t1.c, t1.d)
    55  		panic("bigalg")
    56  	}
    57  
    58  	ma[1] = a
    59  	a1 := ma[1]
    60  	if !SameArray(a, a1) {
    61  		println("fail: map val array", a, a1)
    62  		panic("bigalg")
    63  	}
    64  }
    65  
    66  var ct = make(chan T)
    67  var ca = make(chan []int)
    68  
    69  func send() {
    70  	ct <- t
    71  	ca <- a
    72  }
    73  
    74  func chantest() {
    75  	go send()
    76  
    77  	t1 := <-ct
    78  	if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d {
    79  		println("fail: map val struct", t1.a, t1.b, t1.c, t1.d)
    80  		panic("bigalg")
    81  	}
    82  
    83  	a1 := <-ca
    84  	if !SameArray(a, a1) {
    85  		println("fail: map val array", a, a1)
    86  		panic("bigalg")
    87  	}
    88  }
    89  
    90  type E struct{}
    91  
    92  var e E
    93  
    94  func interfacetest() {
    95  	var i interface{}
    96  
    97  	i = a
    98  	a1 := i.([]int)
    99  	if !SameArray(a, a1) {
   100  		println("interface <-> []int", a, a1)
   101  		panic("bigalg")
   102  	}
   103  	pa := new([]int)
   104  	*pa = a
   105  	i = pa
   106  	a1 = *i.(*[]int)
   107  	if !SameArray(a, a1) {
   108  		println("interface <-> *[]int", a, a1)
   109  		panic("bigalg")
   110  	}
   111  
   112  	i = t
   113  	t1 := i.(T)
   114  	if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d {
   115  		println("interface <-> struct", t1.a, t1.b, t1.c, t1.d)
   116  		panic("bigalg")
   117  	}
   118  
   119  	i = e
   120  	e1 := i.(E)
   121  	// nothing to check; just verify it doesn't crash
   122  	_ = e1
   123  }
   124  
   125  func main() {
   126  	arraycmptest()
   127  	maptest()
   128  	chantest()
   129  	interfacetest()
   130  }
   131  

View as plain text