| 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 static testMain() { | 10 static testMain() { |
| 10 var map = {"a": 1, "b": 2, "c": 3}; | 11 var map = { "a": 1, "b": 2, "c": 3 }; |
| 11 | 12 |
| 12 Expect.equals(map.length, 3); | 13 Expect.equals(map.length, 3); |
| 13 Expect.equals(map["a"], 1); | 14 Expect.equals(map["a"], 1); |
| 14 Expect.equals(map["z"], null); | 15 Expect.equals(map["z"], null); |
| 15 Expect.equals(map["c"], 3); | 16 Expect.equals(map["c"], 3); |
| 16 | 17 |
| 17 map["foo"] = 42; | 18 map["foo"] = 42; |
| 18 Expect.equals(map.length, 4); | 19 Expect.equals(map.length, 4); |
| 19 Expect.equals(map["foo"], 42); | 20 Expect.equals(map["foo"], 42); |
| 20 map["foo"] = 55; | 21 map["foo"] = 55; |
| 21 Expect.equals(map.length, 4); | 22 Expect.equals(map.length, 4); |
| 22 Expect.equals(map["foo"], 55); | 23 Expect.equals(map["foo"], 55); |
| 23 | 24 |
| 24 map.remove("foo"); | 25 map.remove("foo"); |
| 25 Expect.equals(map.length, 3); | 26 Expect.equals(map.length, 3); |
| 26 Expect.equals(map["foo"], null); | 27 Expect.equals(map["foo"], null); |
| 27 | 28 |
| 28 map["foo"] = "bar"; | 29 map["foo"] = "bar"; |
| 29 Expect.equals(map.length, 4); | 30 Expect.equals(map.length, 4); |
| 30 Expect.equals(map["foo"], "bar"); | 31 Expect.equals(map["foo"], "bar"); |
| 31 | 32 |
| 32 map.clear(); | 33 map.clear(); |
| 33 Expect.equals(map.length, 0); | 34 Expect.equals(map.length, 0); |
| 34 | 35 |
| 35 var b = 22; | 36 var b = 22; |
| 36 Expect.equals( | 37 Expect.equals(22, {"a": 11, "b": b, }["b"]); |
| 37 22, | |
| 38 { | |
| 39 "a": 11, | |
| 40 "b": b, | |
| 41 }["b"]); | |
| 42 | 38 |
| 43 // Make map grow. We currently don't have a way to construct | 39 // Make map grow. We currently don't have a way to construct |
| 44 // strings from an integer value, so we can't use a loop here. | 40 // strings from an integer value, so we can't use a loop here. |
| 45 var m = new Map(); | 41 var m = new Map(); |
| 46 Expect.equals(m.length, 0); | 42 Expect.equals(m.length, 0); |
| 47 m["1"] = 1; | 43 m["1"] = 1; |
| 48 m["2"] = 2; | 44 m["2"] = 2; |
| 49 m["3"] = 3; | 45 m["3"] = 3; |
| 50 m["4"] = 4; | 46 m["4"] = 4; |
| 51 m["5"] = 5; | 47 m["5"] = 5; |
| 52 m["6"] = 6; | 48 m["6"] = 6; |
| 53 m["7"] = 7; | 49 m["7"] = 7; |
| 54 m["8"] = 8; | 50 m["8"] = 8; |
| 55 m["9"] = 9; | 51 m["9"] = 9; |
| 56 m["10"] = 10; | 52 m["10"] = 10; |
| 57 m["11"] = 11; | 53 m["11"] = 11; |
| 58 m["12"] = 12; | 54 m["12"] = 12; |
| 59 m["13"] = 13; | 55 m["13"] = 13; |
| 60 m["14"] = 14; | 56 m["14"] = 14; |
| 61 m["15"] = 15; | 57 m["15"] = 15; |
| 62 m["16"] = 16; | 58 m["16"] = 16; |
| 63 Expect.equals(16, m.length); | 59 Expect.equals(16, m.length); |
| 64 m.remove("1"); | 60 m.remove("1"); |
| 65 m.remove("1"); // Remove element twice. | 61 m.remove("1"); // Remove element twice. |
| 66 m.remove("16"); | 62 m.remove("16"); |
| 67 Expect.equals(14, m.length); | 63 Expect.equals(14, m.length); |
| 68 | 64 |
| 69 // Check that last value of duplicate key wins for const maps. | 65 // Check that last value of duplicate key wins for const maps. |
| 70 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 |
| 71 Expect.equals(2, cmap.length); | 67 Expect.equals(2, cmap.length); |
| 72 Expect.equals(1000, cmap["a"]); | 68 Expect.equals(1000, cmap["a"]); |
| 73 Expect.equals(100, cmap["b"]); | 69 Expect.equals(100, cmap["b"]); |
| 74 | 70 |
| 75 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 |
| 76 Expect.equals(1, cmap2.length); | 72 Expect.equals(1, cmap2.length); |
| 77 Expect.equals(1000, cmap["a"]); | 73 Expect.equals(1000, cmap["a"]); |
| 78 | 74 |
| 79 // Check that last value of duplicate key wins for mutable maps. | 75 // Check that last value of duplicate key wins for mutable maps. |
| 80 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 |
| 81 | 77 |
| 82 Expect.equals(2, mmap.length); | 78 Expect.equals(2, mmap.length); |
| 83 Expect.equals(1000, mmap["a"]); | 79 Expect.equals(1000, mmap["a"]); |
| 84 Expect.equals(100, mmap["b"]); | 80 Expect.equals(100, mmap["b"]); |
| 85 | 81 |
| 86 // 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 |
| 87 // are still evaluated, including side effects. | 83 // are still evaluated, including side effects. |
| 88 int counter = 0; | 84 int counter = 0; |
| 89 int ctr() { | 85 int ctr() { counter += 10; return counter; } |
| 90 counter += 10; | |
| 91 return counter; | |
| 92 } | |
| 93 | |
| 94 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 |
| 95 Expect.equals(2, mmap.length); | 87 Expect.equals(2, mmap.length); |
| 96 Expect.equals(40, ctr()); | 88 Expect.equals(40, ctr()); |
| 97 Expect.equals(30, mmap["a"]); | 89 Expect.equals(30, mmap["a"]); |
| 98 Expect.equals(20, mmap["b"]); | 90 Expect.equals(20, mmap["b"]); |
| 99 | 91 |
| 100 Expect.equals(10, {"beta": 100, "alpha": 9 + 1}["alpha"]); | 92 Expect.equals(10, { "beta": 100, "alpha": 9 + 1 }["alpha"]); |
| 101 Expect.equals( | 93 Expect.equals(10, <String, Map>{ |
| 102 10, | 94 "beta": {"delta": 10}, |
| 103 <String, Map>{ | 95 "alpha": {"gamma": 10} }["alpha"]["gamma"]); |
| 104 "beta": {"delta": 10}, | |
| 105 "alpha": {"gamma": 10} | |
| 106 }["alpha"]["gamma"]); | |
| 107 | 96 |
| 108 // Map literals at beginning of statement. | 97 // Map literals at beginning of statement. |
| 109 < | 98 <String, num>{"pink": 100}; |
| 110 String, | |
| 111 num>{"pink": 100}; | |
| 112 const <String, num>{"floyd": 100}; | 99 const <String, num>{"floyd": 100}; |
| 113 } | 100 } |
| 114 } | 101 } |
| 115 | 102 |
| 103 |
| 116 main() { | 104 main() { |
| 117 MapLiteralTest.testMain(); | 105 MapLiteralTest.testMain(); |
| 118 } | 106 } |
| OLD | NEW |