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