summaryrefslogtreecommitdiffstats
path: root/test/bigalg.go
blob: 60e822942e2826367093350db4346f6556224c34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// run

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Test the internal "algorithms" for objects larger than a word: hashing, equality etc.

package main

type T struct {
	a float64
	b int64
	c string
	d byte
}

var a = []int{1, 2, 3}
var NIL []int

func arraycmptest() {
	if NIL != nil {
		println("fail1:", NIL, "!= nil")
		panic("bigalg")
	}
	if nil != NIL {
		println("fail2: nil !=", NIL)
		panic("bigalg")
	}
	if a == nil || nil == a {
		println("fail3:", a, "== nil")
		panic("bigalg")
	}
}

func SameArray(a, b []int) bool {
	if len(a) != len(b) || cap(a) != cap(b) {
		return false
	}
	if len(a) > 0 && &a[0] != &b[0] {
		return false
	}
	return true
}

var t = T{1.5, 123, "hello", 255}
var mt = make(map[int]T)
var ma = make(map[int][]int)

func maptest() {
	mt[0] = t
	t1 := mt[0]
	if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d {
		println("fail: map val struct", t1.a, t1.b, t1.c, t1.d)
		panic("bigalg")
	}

	ma[1] = a
	a1 := ma[1]
	if !SameArray(a, a1) {
		println("fail: map val array", a, a1)
		panic("bigalg")
	}
}

var ct = make(chan T)
var ca = make(chan []int)

func send() {
	ct <- t
	ca <- a
}

func chantest() {
	go send()

	t1 := <-ct
	if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d {
		println("fail: map val struct", t1.a, t1.b, t1.c, t1.d)
		panic("bigalg")
	}

	a1 := <-ca
	if !SameArray(a, a1) {
		println("fail: map val array", a, a1)
		panic("bigalg")
	}
}

type E struct{}

var e E

func interfacetest() {
	var i interface{}

	i = a
	a1 := i.([]int)
	if !SameArray(a, a1) {
		println("interface <-> []int", a, a1)
		panic("bigalg")
	}
	pa := new([]int)
	*pa = a
	i = pa
	a1 = *i.(*[]int)
	if !SameArray(a, a1) {
		println("interface <-> *[]int", a, a1)
		panic("bigalg")
	}

	i = t
	t1 := i.(T)
	if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d {
		println("interface <-> struct", t1.a, t1.b, t1.c, t1.d)
		panic("bigalg")
	}

	i = e
	e1 := i.(E)
	// nothing to check; just verify it doesn't crash
	_ = e1
}

func main() {
	arraycmptest()
	maptest()
	chantest()
	interfacetest()
}