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