OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 // Test program for map literals. | |
5 | |
6 class MapLiteralTest { | |
7 | |
8 static testMain() { | |
9 var map = { "a": 1, "b": 2, "c": 3 }; | |
10 | |
11 Expect.equals(map.length, 3); | |
12 Expect.equals(map["a"], 1); | |
13 Expect.equals(map["z"], null); | |
14 Expect.equals(map["c"], 3); | |
15 | |
16 map["foo"] = 42; | |
17 Expect.equals(map.length, 4); | |
18 Expect.equals(map["foo"], 42); | |
19 map["foo"] = 55; | |
20 Expect.equals(map.length, 4); | |
21 Expect.equals(map["foo"], 55); | |
22 | |
23 map.remove("foo"); | |
24 Expect.equals(map.length, 3); | |
25 Expect.equals(map["foo"], null); | |
26 | |
27 map["foo"] = "bar"; | |
28 Expect.equals(map.length, 4); | |
29 Expect.equals(map["foo"], "bar"); | |
30 | |
31 map.clear(); | |
32 Expect.equals(map.length, 0); | |
33 | |
34 var b = 22; | |
35 Expect.equals(22, {"a": 11, "b": b, }["b"]); | |
36 | |
37 // Make map grow. We currently don't have a way to construct | |
38 // strings from an integer value, so we can't use a loop here. | |
39 var m = new Map(); | |
40 Expect.equals(m.length, 0); | |
41 m["1"] = 1; | |
42 m["2"] = 2; | |
43 m["3"] = 3; | |
44 m["4"] = 4; | |
45 m["5"] = 5; | |
46 m["6"] = 6; | |
47 m["7"] = 7; | |
48 m["8"] = 8; | |
49 m["9"] = 9; | |
50 m["10"] = 10; | |
51 m["11"] = 11; | |
52 m["12"] = 12; | |
53 m["13"] = 13; | |
54 m["14"] = 14; | |
55 m["15"] = 15; | |
56 m["16"] = 16; | |
57 Expect.equals(16, m.length); | |
58 m.remove("1"); | |
59 m.remove("1"); // Remove element twice. | |
60 m.remove("16"); | |
61 Expect.equals(14, m.length); | |
62 | |
63 // Check that last value of duplicate key wins for const maps. | |
64 final cmap = const <num>{"a": 10, "b": 100, "a": 1000}; /// static type warn
ing | |
65 Expect.equals(2, cmap.length); | |
66 Expect.equals(1000, cmap["a"]); | |
67 Expect.equals(100, cmap["b"]); | |
68 | |
69 final cmap2 = const <num>{"a": 10, "a": 100, "a": 1000}; /// static type war
ning | |
70 Expect.equals(1, cmap2.length); | |
71 Expect.equals(1000, cmap["a"]); | |
72 | |
73 // Check that last value of duplicate key wins for mutable maps. | |
74 var mmap = <num>{"a": 10, "b": 100, "a": 1000}; /// static type warning | |
75 | |
76 Expect.equals(2, mmap.length); | |
77 Expect.equals(1000, mmap["a"]); | |
78 Expect.equals(100, mmap["b"]); | |
79 | |
80 // Check that even if a key gets eliminated (the first "a"), all values | |
81 // are still evaluated, including side effects. | |
82 int counter = 0; | |
83 int ctr() { counter += 10; return counter; } | |
84 mmap = <num>{"a": ctr(), "b": ctr(), "a": ctr()}; /// static type warning | |
85 Expect.equals(2, mmap.length); | |
86 Expect.equals(40, ctr()); | |
87 Expect.equals(30, mmap["a"]); | |
88 Expect.equals(20, mmap["b"]); | |
89 | |
90 Expect.equals(10, { "beta": 100, "alpha": 9 + 1 }["alpha"]); | |
91 Expect.equals(10, <Map>{ | |
92 "beta": {"delta": 10}, | |
93 "alpha": {"gamma": 10} }["alpha"]["gamma"]); | |
94 | |
95 // Map literals at beginning of statement. | |
96 <num>{"pink": 100}; | |
97 const <num>{"floyd": 100}; | |
98 } | |
99 } | |
100 | |
101 | |
102 main() { | |
103 MapLiteralTest.testMain(); | |
104 } | |
OLD | NEW |