OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // Test program for map literals. | 4 // Test program for map literals. |
5 | 5 |
6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 | 7 |
8 class MapLiteralTest { | 8 class MapLiteralTest { |
9 | 9 |
10 static testMain() { | 10 static testMain() { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 m["14"] = 14; | 56 m["14"] = 14; |
57 m["15"] = 15; | 57 m["15"] = 15; |
58 m["16"] = 16; | 58 m["16"] = 16; |
59 Expect.equals(16, m.length); | 59 Expect.equals(16, m.length); |
60 m.remove("1"); | 60 m.remove("1"); |
61 m.remove("1"); // Remove element twice. | 61 m.remove("1"); // Remove element twice. |
62 m.remove("16"); | 62 m.remove("16"); |
63 Expect.equals(14, m.length); | 63 Expect.equals(14, m.length); |
64 | 64 |
65 // Check that last value of duplicate key wins for const maps. | 65 // Check that last value of duplicate key wins for const maps. |
66 final cmap = const <String, num>{"a": 10, "b": 100, "a": 1000}; /// static t
ype warning | 66 final cmap = const <String, num>{"a": 10, "b": 100, "a": 1000}; //# static t
ype warning |
67 Expect.equals(2, cmap.length); | 67 Expect.equals(2, cmap.length); |
68 Expect.equals(1000, cmap["a"]); | 68 Expect.equals(1000, cmap["a"]); |
69 Expect.equals(100, cmap["b"]); | 69 Expect.equals(100, cmap["b"]); |
70 | 70 |
71 final cmap2 = const <String, num>{"a": 10, "a": 100, "a": 1000}; /// static
type warning | 71 final cmap2 = const <String, num>{"a": 10, "a": 100, "a": 1000}; //# static
type warning |
72 Expect.equals(1, cmap2.length); | 72 Expect.equals(1, cmap2.length); |
73 Expect.equals(1000, cmap["a"]); | 73 Expect.equals(1000, cmap["a"]); |
74 | 74 |
75 // Check that last value of duplicate key wins for mutable maps. | 75 // Check that last value of duplicate key wins for mutable maps. |
76 var mmap = <String, num>{"a": 10, "b": 100, "a": 1000}; /// static type warn
ing | 76 var mmap = <String, num>{"a": 10, "b": 100, "a": 1000}; //# static type warn
ing |
77 | 77 |
78 Expect.equals(2, mmap.length); | 78 Expect.equals(2, mmap.length); |
79 Expect.equals(1000, mmap["a"]); | 79 Expect.equals(1000, mmap["a"]); |
80 Expect.equals(100, mmap["b"]); | 80 Expect.equals(100, mmap["b"]); |
81 | 81 |
82 // Check that even if a key gets eliminated (the first "a"), all values | 82 // Check that even if a key gets eliminated (the first "a"), all values |
83 // are still evaluated, including side effects. | 83 // are still evaluated, including side effects. |
84 int counter = 0; | 84 int counter = 0; |
85 int ctr() { counter += 10; return counter; } | 85 int ctr() { counter += 10; return counter; } |
86 mmap = <String, num>{"a": ctr(), "b": ctr(), "a": ctr()}; /// static type wa
rning | 86 mmap = <String, num>{"a": ctr(), "b": ctr(), "a": ctr()}; //# static type wa
rning |
87 Expect.equals(2, mmap.length); | 87 Expect.equals(2, mmap.length); |
88 Expect.equals(40, ctr()); | 88 Expect.equals(40, ctr()); |
89 Expect.equals(30, mmap["a"]); | 89 Expect.equals(30, mmap["a"]); |
90 Expect.equals(20, mmap["b"]); | 90 Expect.equals(20, mmap["b"]); |
91 | 91 |
92 Expect.equals(10, { "beta": 100, "alpha": 9 + 1 }["alpha"]); | 92 Expect.equals(10, { "beta": 100, "alpha": 9 + 1 }["alpha"]); |
93 Expect.equals(10, <String, Map>{ | 93 Expect.equals(10, <String, Map>{ |
94 "beta": {"delta": 10}, | 94 "beta": {"delta": 10}, |
95 "alpha": {"gamma": 10} }["alpha"]["gamma"]); | 95 "alpha": {"gamma": 10} }["alpha"]["gamma"]); |
96 | 96 |
97 // Map literals at beginning of statement. | 97 // Map literals at beginning of statement. |
98 <String, num>{"pink": 100}; | 98 <String, num>{"pink": 100}; |
99 const <String, num>{"floyd": 100}; | 99 const <String, num>{"floyd": 100}; |
100 } | 100 } |
101 } | 101 } |
102 | 102 |
103 | 103 |
104 main() { | 104 main() { |
105 MapLiteralTest.testMain(); | 105 MapLiteralTest.testMain(); |
106 } | 106 } |
OLD | NEW |