]> Cypherpunks.ru repositories - gostls13.git/blob - test/map.go
convert composite literals from { } to ( ).
[gostls13.git] / test / map.go
1 // $G $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         "fmt";
11         "strconv";
12 )
13
14 const arraylen = 2; // BUG: shouldn't need this
15
16 func P(a []string) string {
17         s := "{";
18         for i := 0; i < len(a); i++ {
19                 if i > 0 {
20                         s += ","
21                 }
22                 s += `"` + a[i] + `"`;
23         }
24         s +="}";
25         return s;
26 }
27
28 func main() {
29         // Test a map literal.
30         mlit := map[string] int ( "0":0, "1":1, "2":2, "3":3, "4":4 );
31         for i := 0; i < len(mlit); i++ {
32                 s := string([]byte(byte(i)+'0'));
33                 if mlit[s] != i {
34                         fmt.Printf("mlit[%s] = %d\n", s, mlit[s])
35                 }
36         }
37
38         mib := make(map[int] bool);
39         mii := make(map[int] int);
40         mfi := make(map[float] int);
41         mif := make(map[int] float);
42         msi := make(map[string] int);
43         mis := make(map[int] string);
44         mss := make(map[string] string);
45         mspa := make(map[string] []string);
46         // BUG need an interface map both ways too
47
48         type T struct {
49                 i int64;        // can't use string here; struct values are only compared at the top level
50                 f float;
51         };
52         mipT := make(map[int] *T);
53         mpTi := make(map[*T] int);
54         mit := make(map[int] T);
55 //      mti := make(map[T] int);
56
57         type M map[int] int;
58         mipM := make(map[int] M);
59
60         const count = 1000;
61         var apT [2*count]*T;
62
63         for i := 0; i < count; i++ {
64                 s := strconv.Itoa(i);
65                 s10 := strconv.Itoa(i*10);
66                 f := float(i);
67                 t := T(int64(i),f);
68                 apT[i] = new(T);
69                 apT[i].i = int64(i);
70                 apT[i].f = f;
71                 apT[2*i] = new(T);      // need twice as many entries as we use, for the nonexistence check
72                 apT[2*i].i = int64(i);
73                 apT[2*i].f = f;
74                 m := M(i: i+1);
75                 mib[i] = (i != 0);
76                 mii[i] = 10*i;
77                 mfi[float(i)] = 10*i;
78                 mif[i] = 10.0*f;
79                 mis[i] = s;
80                 msi[s] = i;
81                 mss[s] = s10;
82                 mss[s] = s10;
83                 as := make([]string, arraylen);
84                         as[0] = s10;
85                         as[1] = s10;
86                 mspa[s] = as;
87                 mipT[i] = apT[i];
88                 mpTi[apT[i]] = i;
89                 mipM[i] = m;
90                 mit[i] = t;
91         //      mti[t] = i;
92         }
93
94         // test len
95         if len(mib) != count {
96                 fmt.Printf("len(mib) = %d\n", len(mib));
97         }
98         if len(mii) != count {
99                 fmt.Printf("len(mii) = %d\n", len(mii));
100         }
101         if len(mfi) != count {
102                 fmt.Printf("len(mfi) = %d\n", len(mfi));
103         }
104         if len(mif) != count {
105                 fmt.Printf("len(mif) = %d\n", len(mif));
106         }
107         if len(msi) != count {
108                 fmt.Printf("len(msi) = %d\n", len(msi));
109         }
110         if len(mis) != count {
111                 fmt.Printf("len(mis) = %d\n", len(mis));
112         }
113         if len(mss) != count {
114                 fmt.Printf("len(mss) = %d\n", len(mss));
115         }
116         if len(mspa) != count {
117                 fmt.Printf("len(mspa) = %d\n", len(mspa));
118         }
119         if len(mipT) != count {
120                 fmt.Printf("len(mipT) = %d\n", len(mipT));
121         }
122         if len(mpTi) != count {
123                 fmt.Printf("len(mpTi) = %d\n", len(mpTi));
124         }
125 //      if len(mti) != count {
126 //              fmt.Printf("len(mti) = %d\n", len(mti));
127 //      }
128         if len(mipM) != count {
129                 fmt.Printf("len(mipM) = %d\n", len(mipM));
130         }
131 //      if len(mti) != count {
132 //              fmt.Printf("len(mti) = %d\n", len(mti));
133 //      }
134         if len(mit) != count {
135                 fmt.Printf("len(mit) = %d\n", len(mit));
136         }
137
138         // test construction directly
139         for i := 0; i < count; i++ {
140                 s := strconv.Itoa(i);
141                 s10 := strconv.Itoa(i*10);
142                 f := float(i);
143                 t := T(int64(i), f);
144                 // BUG m := M(i, i+1);
145                 if mib[i] != (i != 0) {
146                         fmt.Printf("mib[%d] = %t\n", i, mib[i]);
147                 }
148                 if(mii[i] != 10*i) {
149                         fmt.Printf("mii[%d] = %d\n", i, mii[i]);
150                 }
151                 if(mfi[f] != 10*i) {
152                         fmt.Printf("mfi[%d] = %d\n", i, mfi[f]);
153                 }
154                 if(mif[i] != 10.0*f) {
155                         fmt.Printf("mif[%d] = %g\n", i, mif[i]);
156                 }
157                 if(mis[i] != s) {
158                         fmt.Printf("mis[%d] = %s\n", i, mis[i]);
159                 }
160                 if(msi[s] != i) {
161                         fmt.Printf("msi[%s] = %d\n", s, msi[s]);
162                 }
163                 if mss[s] != s10 {
164                         fmt.Printf("mss[%s] = %g\n", s, mss[s]);
165                 }
166                 for j := 0; j < arraylen; j++ {
167                         if mspa[s][j] != s10 {
168                                 fmt.Printf("mspa[%s][%d] = %s\n", s, j, mspa[s][j]);
169                         }
170                 }
171                 if(mipT[i].i != int64(i) || mipT[i].f != f) {
172                         fmt.Printf("mipT[%d] = %v\n", i, mipT[i]);
173                 }
174                 if(mpTi[apT[i]] != i) {
175                         fmt.Printf("mpTi[apT[%d]] = %d\n", i, mpTi[apT[i]]);
176                 }
177         //      if(mti[t] != i) {
178         //              fmt.Printf("mti[%s] = %s\n", s, mti[t]);
179         //      }
180                 if (mipM[i][i] != i + 1) {
181                         fmt.Printf("mipM[%d][%d] = %d\n", i, i, mipM[i][i]);
182                 }
183         //      if(mti[t] != i) {
184         //              fmt.Printf("mti[%v] = %d\n", t, mti[t]);
185         //      }
186                 if(mit[i].i != int64(i) || mit[i].f != f) {
187                         fmt.Printf("mit[%d] = {%d %g}\n", i, mit[i].i, mit[i].f);
188                 }
189         }
190
191         // test existence with tuple check
192         // failed lookups yield a false value for the boolean.
193         for i := 0; i < count; i++ {
194                 s := strconv.Itoa(i);
195                 f := float(i);
196                 t := T(int64(i), f);
197                 {
198                         a, b := mib[i];
199                         if !b {
200                                 fmt.Printf("tuple existence decl: mib[%d]\n", i);
201                         }
202                         a, b = mib[i];
203                         if !b {
204                                 fmt.Printf("tuple existence assign: mib[%d]\n", i);
205                         }
206                 }
207                 {
208                         a, b := mii[i];
209                         if !b {
210                                 fmt.Printf("tuple existence decl: mii[%d]\n", i);
211                         }
212                         a, b = mii[i];
213                         if !b {
214                                 fmt.Printf("tuple existence assign: mii[%d]\n", i);
215                         }
216                 }
217                 {
218                         a, b := mfi[f];
219                         if !b {
220                                 fmt.Printf("tuple existence decl: mfi[%d]\n", i);
221                         }
222                         a, b = mfi[f];
223                         if !b {
224                                 fmt.Printf("tuple existence assign: mfi[%d]\n", i);
225                         }
226                 }
227                 {
228                         a, b := mif[i];
229                         if !b {
230                                 fmt.Printf("tuple existence decl: mif[%d]\n", i);
231                         }
232                         a, b = mif[i];
233                         if !b {
234                                 fmt.Printf("tuple existence assign: mif[%d]\n", i);
235                         }
236                 }
237                 {
238                         a, b := mis[i];
239                         if !b {
240                                 fmt.Printf("tuple existence decl: mis[%d]\n", i);
241                         }
242                         a, b = mis[i];
243                         if !b {
244                                 fmt.Printf("tuple existence assign: mis[%d]\n", i);
245                         }
246                 }
247                 {
248                         a, b := msi[s];
249                         if !b {
250                                 fmt.Printf("tuple existence decl: msi[%d]\n", i);
251                         }
252                         a, b = msi[s];
253                         if !b {
254                                 fmt.Printf("tuple existence assign: msi[%d]\n", i);
255                         }
256                 }
257                 {
258                         a, b := mss[s];
259                         if !b {
260                                 fmt.Printf("tuple existence decl: mss[%d]\n", i);
261                         }
262                         a, b = mss[s];
263                         if !b {
264                                 fmt.Printf("tuple existence assign: mss[%d]\n", i);
265                         }
266                 }
267                 {
268                         a, b := mspa[s];
269                         if !b {
270                                 fmt.Printf("tuple existence decl: mspa[%d]\n", i);
271                         }
272                         a, b = mspa[s];
273                         if !b {
274                                 fmt.Printf("tuple existence assign: mspa[%d]\n", i);
275                         }
276                 }
277                 {
278                         a, b := mipT[i];
279                         if !b {
280                                 fmt.Printf("tuple existence decl: mipT[%d]\n", i);
281                         }
282                         a, b = mipT[i];
283                         if !b {
284                                 fmt.Printf("tuple existence assign: mipT[%d]\n", i);
285                         }
286                 }
287                 {
288                         a, b := mpTi[apT[i]];
289                         if !b {
290                                 fmt.Printf("tuple existence decl: mpTi[apT[%d]]\n", i);
291                         }
292                         a, b = mpTi[apT[i]];
293                         if !b {
294                                 fmt.Printf("tuple existence assign: mpTi[apT[%d]]\n", i);
295                         }
296                 }
297                 {
298                         a, b := mipM[i];
299                         if !b {
300                                 fmt.Printf("tuple existence decl: mipM[%d]\n", i);
301                         }
302                         a, b = mipM[i];
303                         if !b {
304                                 fmt.Printf("tuple existence assign: mipM[%d]\n", i);
305                         }
306                 }
307                 {
308                         a, b := mit[i];
309                         if !b {
310                                 fmt.Printf("tuple existence decl: mit[%d]\n", i);
311                         }
312                         a, b = mit[i];
313                         if !b {
314                                 fmt.Printf("tuple existence assign: mit[%d]\n", i);
315                         }
316                 }
317 //              {
318 //                      a, b := mti[t];
319 //                      if !b {
320 //                              fmt.Printf("tuple existence decl: mti[%d]\n", i);
321 //                      }
322 //                      a, b = mti[t];
323 //                      if !b {
324 //                              fmt.Printf("tuple existence assign: mti[%d]\n", i);
325 //                      }
326 //              }
327         }
328
329         // test nonexistence with tuple check
330         // failed lookups yield a false value for the boolean.
331         for i := count; i < 2*count; i++ {
332                 s := strconv.Itoa(i);
333                 f := float(i);
334                 t := T(int64(i),f);
335                 {
336                         a, b := mib[i];
337                         if b {
338                                 fmt.Printf("tuple nonexistence decl: mib[%d]", i);
339                         }
340                         a, b = mib[i];
341                         if b {
342                                 fmt.Printf("tuple nonexistence assign: mib[%d]", i);
343                         }
344                 }
345                 {
346                         a, b := mii[i];
347                         if b {
348                                 fmt.Printf("tuple nonexistence decl: mii[%d]", i);
349                         }
350                         a, b = mii[i];
351                         if b {
352                                 fmt.Printf("tuple nonexistence assign: mii[%d]", i);
353                         }
354                 }
355                 {
356                         a, b := mfi[f];
357                         if b {
358                                 fmt.Printf("tuple nonexistence decl: mfi[%d]", i);
359                         }
360                         a, b = mfi[f];
361                         if b {
362                                 fmt.Printf("tuple nonexistence assign: mfi[%d]", i);
363                         }
364                 }
365                 {
366                         a, b := mif[i];
367                         if b {
368                                 fmt.Printf("tuple nonexistence decl: mif[%d]", i);
369                         }
370                         a, b = mif[i];
371                         if b {
372                                 fmt.Printf("tuple nonexistence assign: mif[%d]", i);
373                         }
374                 }
375                 {
376                         a, b := mis[i];
377                         if b {
378                                 fmt.Printf("tuple nonexistence decl: mis[%d]", i);
379                         }
380                         a, b = mis[i];
381                         if b {
382                                 fmt.Printf("tuple nonexistence assign: mis[%d]", i);
383                         }
384                 }
385                 {
386                         a, b := msi[s];
387                         if b {
388                                 fmt.Printf("tuple nonexistence decl: msi[%d]", i);
389                         }
390                         a, b = msi[s];
391                         if b {
392                                 fmt.Printf("tuple nonexistence assign: msi[%d]", i);
393                         }
394                 }
395                 {
396                         a, b := mss[s];
397                         if b {
398                                 fmt.Printf("tuple nonexistence decl: mss[%d]", i);
399                         }
400                         a, b = mss[s];
401                         if b {
402                                 fmt.Printf("tuple nonexistence assign: mss[%d]", i);
403                         }
404                 }
405                 {
406                         a, b := mspa[s];
407                         if b {
408                                 fmt.Printf("tuple nonexistence decl: mspa[%d]", i);
409                         }
410                         a, b = mspa[s];
411                         if b {
412                                 fmt.Printf("tuple nonexistence assign: mspa[%d]", i);
413                         }
414                 }
415                 {
416                         a, b := mipT[i];
417                         if b {
418                                 fmt.Printf("tuple nonexistence decl: mipT[%d]", i);
419                         }
420                         a, b = mipT[i];
421                         if b {
422                                 fmt.Printf("tuple nonexistence assign: mipT[%d]", i);
423                         }
424                 }
425                 {
426                         a, b := mpTi[apT[i]];
427                         if b {
428                                 fmt.Printf("tuple nonexistence decl: mpTi[apt[%d]]", i);
429                         }
430                         a, b = mpTi[apT[i]];
431                         if b {
432                                 fmt.Printf("tuple nonexistence assign: mpTi[apT[%d]]", i);
433                         }
434                 }
435                 {
436                         a, b := mipM[i];
437                         if b {
438                                 fmt.Printf("tuple nonexistence decl: mipM[%d]", i);
439                         }
440                         a, b = mipM[i];
441                         if b {
442                                 fmt.Printf("tuple nonexistence assign: mipM[%d]", i);
443                         }
444                 }
445 //              {
446 //                      a, b := mti[t];
447 //                      if b {
448 //                              fmt.Printf("tuple nonexistence decl: mti[%d]", i);
449 //                      }
450 //                      a, b = mti[t];
451 //                      if b {
452 //                              fmt.Printf("tuple nonexistence assign: mti[%d]", i);
453 //                      }
454 //              }
455                 {
456                         a, b := mit[i];
457                         if b {
458                                 fmt.Printf("tuple nonexistence decl: mit[%d]", i);
459                         }
460                         a, b = mit[i];
461                         if b {
462                                 fmt.Printf("tuple nonexistence assign: mit[%d]", i);
463                         }
464                 }
465         }
466
467
468         // tests for structured map element updates
469         for i := 0; i < count; i++ {
470                 s := strconv.Itoa(i);
471                 mspa[s][i % 2] = "deleted";
472                 if mspa[s][i % 2] != "deleted" {
473                         fmt.Printf("update mspa[%s][%d] = %s\n", s, i %2, mspa[s][i % 2]);
474                 }
475
476                 mipT[i].i += 1;
477                 if mipT[i].i != int64(i)+1 {
478                         fmt.Printf("update mipT[%d].i = %d\n", i, mipT[i].i);
479                 }
480                 mipT[i].f = float(i + 1);
481                 if (mipT[i].f != float(i + 1)) {
482                         fmt.Printf("update mipT[%d].f = %g\n", i, mipT[i].f);
483                 }
484
485                 mipM[i][i]++;
486                 if mipM[i][i] != (i + 1) + 1 {
487                         fmt.Printf("update mipM[%d][%d] = %i\n", i, i, mipM[i][i]);
488                 }
489         }
490 }