]> Cypherpunks.ru repositories - gostls13.git/blob - test/bigalg.go
chan and map of [] and struct
[gostls13.git] / test / bigalg.go
1 // $G $D/$F.go && $L $F.$A && ./$A.out
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 package main
8
9 import (
10         "os";
11         "fmt";
12 )
13
14 type T struct {
15         a float64;
16         b int64;
17         c string;
18         d byte;
19 }
20
21 var a = []int{ 1, 2, 3 }
22 var NIL []int;
23
24 func arraycmptest() {
25         a1 := a;
26         if NIL != nil {
27                 println("fail1:", NIL, "!= nil");
28         }
29         if nil != NIL {
30                 println("fail2: nil !=", NIL);
31         }
32         if a == nil || nil == a {
33                 println("fail3:", a, "== nil");
34         }
35         if a == NIL || NIL == a {
36                 println("fail4:", a, "==", NIL);
37         }
38         if a != a {
39                 println("fail5:", a, "!=", a);
40         }
41         if a1 != a {
42                 println("fail6:", a1, "!=", a);
43         }
44 }
45
46 var t = T{1.5, 123, "hello", 255}
47 var mt = new(map[int]T)
48 var ma = new(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         }
56
57         ma[1] = a;
58         a1 := ma[1];
59         if a1 != a {
60                 println("fail: map val array", a, a1);
61         }
62 }
63
64 var mt1 = new(map[T]int)
65 var ma1 = new(map[[]int] int)
66
67 func maptest2() {
68         mt1[t] = 123;
69         t1 := t;
70         val, ok := mt1[t1];
71         if val != 123 || !ok {
72                 println("fail: map key struct", val, ok);
73         }
74
75         ma1[a] = 345;
76         a1 := a;
77         val, ok = ma1[a1];
78         if val != 345 || !ok {
79                 panic("map key array", val, ok);
80         }
81 }
82
83 var ct = new(chan T)
84 var ca = new(chan []int)
85
86 func send() {
87         ct <- t;
88         ca <- a;
89 }
90
91 func chantest() {
92         go send();
93
94         t1 := <-ct;
95         if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d {
96                 println("fail: chan struct", t1.a, t1.b, t1.c, t1.d);
97         }
98
99         a1 := <-ca;
100         if a1 != a {
101                 println("fail: chan array", a, a1);
102         }
103 }
104
105 func main() {
106         arraycmptest();
107         maptest();
108         maptest2();
109         chantest();
110 }