diff --git a/tests/language/map/literal10_test.dart b/tests/language/map/literal10_test.dart new file mode 100644 index 00000000000..d1d388604d0 --- /dev/null +++ b/tests/language/map/literal10_test.dart @@ -0,0 +1,39 @@ +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// Test the use of '__proto__' keys in maps. + +library map_literal10_test; + +import "package:expect/expect.dart"; + +void main() { + var m1 = const {"__proto__": 0, 1: 1}; + Expect.isTrue(m1.containsKey("__proto__")); + Expect.equals(0, m1["__proto__"]); + Expect.isTrue(m1.containsKey(1)); + Expect.equals(1, m1[1]); + Expect.listEquals(["__proto__", 1], m1.keys.toList()); + + var m2 = const {1: 0, "__proto__": 1}; + Expect.isTrue(m2.containsKey(1)); + Expect.equals(0, m2[1]); + Expect.isTrue(m2.containsKey("__proto__")); + Expect.equals(1, m2["__proto__"]); + Expect.listEquals([1, "__proto__"], m2.keys.toList()); + + var m3 = const {"1": 0, "__proto__": 1}; + Expect.isTrue(m3.containsKey("1")); + Expect.equals(0, m3["1"]); + Expect.isTrue(m3.containsKey("__proto__")); + Expect.equals(1, m3["__proto__"]); + Expect.listEquals(["1", "__proto__"], m3.keys.toList()); + + var m4 = const {"__proto__": 1, "1": 2}; + Expect.isTrue(m4.containsKey("1")); + Expect.equals(2, m4["1"]); + Expect.isTrue(m4.containsKey("__proto__")); + Expect.equals(1, m4["__proto__"]); + Expect.listEquals(["__proto__", "1"], m4.keys.toList()); +} diff --git a/tests/language/map/literal11_test.dart b/tests/language/map/literal11_test.dart new file mode 100644 index 00000000000..93643eb9878 --- /dev/null +++ b/tests/language/map/literal11_test.dart @@ -0,0 +1,18 @@ +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// Test the use type arguments on constant maps. + +library map_literal11_test; + +import "package:expect/expect.dart"; + +void foo(Map m) { + m[23] = 23; //# none: runtime error +} + +void main() { + Map map = {}; + foo(map); +} diff --git a/tests/language/map/literal12_test.dart b/tests/language/map/literal12_test.dart new file mode 100644 index 00000000000..1cd42d89a9c --- /dev/null +++ b/tests/language/map/literal12_test.dart @@ -0,0 +1,18 @@ +// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// Test relative order of key and value evaluation. + +library map_literal12_test; + +import "package:expect/expect.dart"; + +int x = 0; +void main() { + var map1 = {++x: ++x}; + Expect.equals('{1: 2}', map1.toString()); + + var map2 = {++x: ++x, ++x: ++x}; + Expect.equals('{3: 4, 5: 6}', map2.toString()); +} diff --git a/tests/language/map/literal13_test.dart b/tests/language/map/literal13_test.dart new file mode 100644 index 00000000000..ec5e0bef927 --- /dev/null +++ b/tests/language/map/literal13_test.dart @@ -0,0 +1,81 @@ +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// Legacy compound literal syntax that should go away. + +main() { + var map = new Map{ "a": 1, "b": 2, "c": 3 }; + // ^^^^^^^^ + // [analyzer] STATIC_TYPE_WARNING.WRONG_NUMBER_OF_TYPE_ARGUMENTS + // [cfe] Expected 2 type arguments. + // ^ + // [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN + // [cfe] Expected '(' after this. + // ^ + // [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN + // [cfe] Expected ';' after this. + // ^^^ + // [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN + // [cfe] Expected ';' after this. + // ^ + // [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN + // [cfe] Expected ';' after this. + // ^ + // [analyzer] SYNTACTIC_ERROR.MISSING_IDENTIFIER + // [cfe] Expected an identifier, but got ':'. + // ^ + // [analyzer] SYNTACTIC_ERROR.UNEXPECTED_TOKEN + // [cfe] Unexpected token ':'. + // ^ + // [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN + // [cfe] Expected ';' after this. + // ^ + // [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN + // [cfe] Expected ';' after this. + // ^ + // [analyzer] SYNTACTIC_ERROR.MISSING_IDENTIFIER + // [cfe] Expected an identifier, but got ','. + // ^ + // [analyzer] SYNTACTIC_ERROR.UNEXPECTED_TOKEN + // [cfe] Unexpected token ','. + // ^^^ + // [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN + // [cfe] Expected ';' after this. + // ^ + // [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN + // [cfe] Expected ';' after this. + // ^ + // [analyzer] SYNTACTIC_ERROR.MISSING_IDENTIFIER + // [cfe] Expected an identifier, but got ':'. + // ^ + // [analyzer] SYNTACTIC_ERROR.UNEXPECTED_TOKEN + // [cfe] Unexpected token ':'. + // ^ + // [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN + // [cfe] Expected ';' after this. + // ^ + // [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN + // [cfe] Expected ';' after this. + // ^ + // [analyzer] SYNTACTIC_ERROR.MISSING_IDENTIFIER + // [cfe] Expected an identifier, but got ','. + // ^ + // [analyzer] SYNTACTIC_ERROR.UNEXPECTED_TOKEN + // [cfe] Unexpected token ','. + // ^^^ + // [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN + // [cfe] Expected ';' after this. + // ^ + // [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN + // [cfe] Expected ';' after this. + // ^ + // [analyzer] SYNTACTIC_ERROR.MISSING_IDENTIFIER + // [cfe] Expected an identifier, but got ':'. + // ^ + // [analyzer] SYNTACTIC_ERROR.UNEXPECTED_TOKEN + // [cfe] Unexpected token ':'. + // ^ + // [analyzer] SYNTACTIC_ERROR.EXPECTED_TOKEN + // [cfe] Expected ';' after this. +} diff --git a/tests/language/map/literal14_test.dart b/tests/language/map/literal14_test.dart new file mode 100644 index 00000000000..4351eb9eb5d --- /dev/null +++ b/tests/language/map/literal14_test.dart @@ -0,0 +1,18 @@ +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// Dart test program const map literals. + +class MapLiteral2NegativeTest { + test() { + var m = const {}; + // ^ + // [analyzer] COMPILE_TIME_ERROR.INVALID_TYPE_ARGUMENT_IN_CONST_MAP + // [cfe] Type variables can't be used as constants. + } +} + +main() { + MapLiteral2NegativeTest().test(); +} diff --git a/tests/language/map/literal1_runtime_test.dart b/tests/language/map/literal1_runtime_test.dart new file mode 100644 index 00000000000..093f893c9fc --- /dev/null +++ b/tests/language/map/literal1_runtime_test.dart @@ -0,0 +1,14 @@ +// TODO(multitest): This was automatically migrated from a multitest and may +// contain strange or dead code. + +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. +// +// A type mismatch in a constant map literal is a compile-time error. + +main() { + var m = const + + {"a": 0}; +} diff --git a/tests/language/map/literal1_test.dart b/tests/language/map/literal1_test.dart new file mode 100644 index 00000000000..71c4e8e88a4 --- /dev/null +++ b/tests/language/map/literal1_test.dart @@ -0,0 +1,14 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. +// +// A type mismatch in a constant map literal is a compile-time error. + +main() { + var m = const + + {"a": 0}; + // ^ + // [analyzer] STATIC_WARNING.MAP_VALUE_TYPE_NOT_ASSIGNABLE + // [cfe] A value of type 'int' can't be assigned to a variable of type 'String'. +} diff --git a/tests/language/map/literal2_test.dart b/tests/language/map/literal2_test.dart new file mode 100644 index 00000000000..083fe75ebfe --- /dev/null +++ b/tests/language/map/literal2_test.dart @@ -0,0 +1,21 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. +// Test program for map literals. + +import "package:expect/expect.dart"; + +int nextValCtr = 0; + +get nextVal { + return nextValCtr++; +} + +main() { + // Map literals with string interpolation in keys. + var map = {"a$nextVal": "Grey", "a$nextVal": "Poupon"}; + Expect.equals(true, map.containsKey("a0")); + Expect.equals(true, map.containsKey("a1")); + Expect.equals("Grey", map["a0"]); + Expect.equals("Poupon", map["a1"]); +} diff --git a/tests/language/map/literal3_test.dart b/tests/language/map/literal3_test.dart new file mode 100644 index 00000000000..517ef904fed --- /dev/null +++ b/tests/language/map/literal3_test.dart @@ -0,0 +1,75 @@ +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. +// Test program for map literals. + +import "package:expect/expect.dart"; + +class MapLiteralTest { + static testMain() { + var map = {"a": 1, "b": 2, "c": 3}; + + Expect.equals(map.length, 3); + Expect.equals(map["a"], 1); + Expect.equals(map["z"], null); + Expect.equals(map["c"], 3); + + map["foo"] = 42; + Expect.equals(map.length, 4); + Expect.equals(map["foo"], 42); + map["foo"] = 55; + Expect.equals(map.length, 4); + Expect.equals(map["foo"], 55); + + map.remove("foo"); + Expect.equals(map.length, 3); + Expect.equals(map["foo"], null); + + map["foo"] = "bar"; + Expect.equals(map.length, 4); + Expect.equals(map["foo"], "bar"); + + map.clear(); + Expect.equals(map.length, 0); + + var b = 22; + Expect.equals( + 22, + { + "a": 11, + "b": b, + }["b"]); + + var m = new Map(); + Expect.equals(m.length, 0); + for (var i = 1; i <= 16; i++) { + m[i.toString()] = i; + } + Expect.equals(16, m.length); + Expect.equals(1, m.remove("1")); + Expect.isNull(m.remove("1")); // Remove element twice. + Expect.equals(16, m.remove("16")); + Expect.equals(14, m.length); + + final cmap = const {"a": 10, "b": 100, "a": 1000}; //# 01: compile-time error + final cmap2 = const {"a": 10, "a": 100, "a": 1000}; //# 02: compile-time error + var mmap = {"a": 10, "b": 100, "a": 1000}; //# 03: ok + var mmap = {"a": ctr(), "b": ctr(), "a": ctr()}; //# 04: compile-time error + + Expect.equals(10, {"beta": 100, "alpha": 9 + 1}["alpha"]); + Expect.equals( + 10, + { + "beta": {"delta": 10}, + "alpha": {"gamma": 10} + }["alpha"]!["gamma"]); + + // Map literals at beginning of statement. + {"pink": 100}; + const {"floyd": 100}; + } +} + +main() { + MapLiteralTest.testMain(); +} diff --git a/tests/language/map/literal4_test.dart b/tests/language/map/literal4_test.dart new file mode 100644 index 00000000000..9358e59f6c4 --- /dev/null +++ b/tests/language/map/literal4_test.dart @@ -0,0 +1,27 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. +// VMOptions=--enable_type_checks +// +// Dart test program testing type checks in map literals. + +import "package:expect/expect.dart"; + +class MapLiteral4Test { + test() { + int result = 0; + var m = {"a": 0}; //# 01: compile-time error + var m = {"a": 0}; //# 02: ok + m[2] = 1; //# 02: compile-time error + var m = {"a": "b"}; //# 03: compile-time error + var m = {"a": 0}; //# 04: compile-time error + var m = {"a": 0}; //# 05: continued + m[2] = 1; //# 05: compile-time error + var m = const {"a": 0}; //# 06: continued + m[2] = 1; //# 06: compile-time error + } +} + +main() { + var t = new MapLiteral4Test(); +} diff --git a/tests/language/map/literal5_test.dart b/tests/language/map/literal5_test.dart new file mode 100644 index 00000000000..94f298d13ae --- /dev/null +++ b/tests/language/map/literal5_test.dart @@ -0,0 +1,44 @@ +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// Test the use of general expression as keys in map literals. + +library map_literal5_test; + +import "package:expect/expect.dart"; + +void main() { + test(true); + test(false); +} + +void test(bool b) { + var m = create(b); + Expect.equals(b, m.containsKey(true)); + Expect.equals(b, m.containsKey(2)); + Expect.equals(b, m.containsKey(1)); + Expect.equals(!b, m.containsKey(false)); + Expect.equals(!b, m.containsKey("bar")); + Expect.equals(!b, m.containsKey("foo")); + if (b) { + Expect.equals(0, m[true]); + Expect.equals(3, m[2]); + Expect.equals(2, m[1]); + } else { + Expect.equals(0, m[false]); + Expect.equals("baz", m["bar"]); + Expect.equals(2, m["foo"]); + } +} + +Map create(bool b) { + return { + b: 0, + m(b): n(b), + b ? 1 : "foo": 2, + }; +} + +Object m(bool b) => b ? 2 : "bar"; +Object n(bool b) => b ? 3 : "baz"; diff --git a/tests/language/map/literal6_test.dart b/tests/language/map/literal6_test.dart new file mode 100644 index 00000000000..5454f487f5c --- /dev/null +++ b/tests/language/map/literal6_test.dart @@ -0,0 +1,35 @@ +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// Test the use of general expression as keys in const map literals. + +library map_literal6_test; + +import "package:expect/expect.dart"; + +class A { + const A(); +} + +class B { + final a; + const B(this.a); +} + +void main() { + var m1 = const { + const A(): 0, + const B(1): 2, + const B(const A()): 3, + const B(0): 4, + }; + Expect.isTrue(m1.containsKey(const A())); + Expect.isTrue(m1.containsKey(const B(0))); + Expect.isTrue(m1.containsKey(const B(1))); + Expect.isTrue(m1.containsKey(const B(const A()))); + Expect.equals(0, m1[const A()]); + Expect.equals(4, m1[const B(0)]); + Expect.equals(2, m1[const B(1)]); + Expect.equals(3, m1[const B(const A())]); +} diff --git a/tests/language/map/literal7_test.dart b/tests/language/map/literal7_test.dart new file mode 100644 index 00000000000..06c2e39eac4 --- /dev/null +++ b/tests/language/map/literal7_test.dart @@ -0,0 +1,23 @@ +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// Test the use type arguments on constant maps. + +library map_literal7_test; + +import "package:expect/expect.dart"; + +void main() { + Map m1 = const {"0": 0, "1": 1}; + Expect.isTrue(m1 is Map); + Expect.isFalse(m1 is Map); + Expect.isFalse(m1 is Map); + Expect.isFalse(m1 is Map); + + Map m2 = const {"0": 0, "1": 1}; + Expect.isTrue(m2 is Map); + Expect.isTrue(m2 is Map); + Expect.isFalse(m2 is Map); + Expect.isFalse(m2 is Map); +} diff --git a/tests/language/map/literal8_test.dart b/tests/language/map/literal8_test.dart new file mode 100644 index 00000000000..bf793205170 --- /dev/null +++ b/tests/language/map/literal8_test.dart @@ -0,0 +1,43 @@ +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// Test the use of type arguments on const map literals using general expression +// as keys. + +library map_literal8_test; + +import "package:expect/expect.dart"; + +class A { + const A(); +} + +class B extends A { + final a; + const B(this.a); +} + +void main() { + var m1 = const { + const A(): 0, + const B(0): 1, + const B(1): 2, + const B(const A()): 3, + }; + Expect.isTrue(m1 is Map); + Expect.isTrue(m1 is Map); + Expect.isFalse(m1 is Map); + Expect.isFalse(m1 is Map); + + var m2 = const { + const A(): 0, + const B(0): 1, + const B(1): 2, + const B(const A()): 3, + }; + Expect.isTrue(m2 is Map); + Expect.isTrue(m2 is Map); + Expect.isFalse(m2 is Map); + Expect.isFalse(m2 is Map); +} diff --git a/tests/language/map/literal9_test.dart b/tests/language/map/literal9_test.dart new file mode 100644 index 00000000000..cc12411a2f0 --- /dev/null +++ b/tests/language/map/literal9_test.dart @@ -0,0 +1,23 @@ +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// Test the use type arguments on constant maps. + +library map_literal9_test; + +import "package:expect/expect.dart"; + +void main() { + var m1 = const {"[object Object]": 0, "1": 1}; + Expect.isFalse(m1.containsKey(new Object())); + Expect.isNull(m1[new Object()]); + Expect.isFalse(m1.containsKey(1)); + Expect.isNull(m1[1]); + + var m2 = const {"[object Object]": 0, "1": 1, "__proto__": 2}; + Expect.isFalse(m2.containsKey(new Object())); + Expect.isNull(m2[new Object()]); + Expect.isFalse(m2.containsKey(1)); + Expect.isNull(m2[1]); +} diff --git a/tests/language/map/literal_syntax_test.dart b/tests/language/map/literal_syntax_test.dart new file mode 100644 index 00000000000..953730a1315 --- /dev/null +++ b/tests/language/map/literal_syntax_test.dart @@ -0,0 +1,24 @@ +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import "package:expect/expect.dart"; + +class Foo { + var x; + var y; + var z; + var v; + Foo() + : x = {}, + y = {}, + z = const {}, + v = const {}; +} + +main() { + Expect.equals("{}", new Foo().x.toString()); + Expect.equals("{}", new Foo().y.toString()); + Expect.equals("{}", new Foo().z.toString()); + Expect.equals("{}", new Foo().v.toString()); +} diff --git a/tests/language/map/literal_test.dart b/tests/language/map/literal_test.dart new file mode 100644 index 00000000000..93f1c3d563e --- /dev/null +++ b/tests/language/map/literal_test.dart @@ -0,0 +1,78 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import "package:expect/expect.dart"; + +// Tests map literals. + +class MapLiteralTest { + MapLiteralTest() {} + + static testMain() { + var test = new MapLiteralTest(); + test.testStaticInit(); + test.testConstInit(); + } + + testStaticInit() { + var testClass = new StaticInit(); + testClass.test(); + } + + testConstInit() { + var testClass = new ConstInit(); + testClass.test(); + } + + testLocalInit() { + // Test construction of static const map literals + var map1 = {"a": 1, "b": 2}; + // Test construction of static const map literals, with numbers + var map2 = {"1": 1, "2": 2}; + + Expect.equals(1, map1["a"]); + Expect.equals(2, map1["b"]); + + Expect.equals(1, map2["1"]); + Expect.equals(2, map2["2"]); + } +} + +class StaticInit { + StaticInit() {} + + // Test construction of static const map literals + static const map1 = const {"a": 1, "b": 2}; + // Test construction of static const map literals, with numbers + static const map2 = const {"1": 1, "2": 2}; + + test() { + Expect.equals(1, map1["a"]); + Expect.equals(2, map1["b"]); + + Expect.equals(1, map2["1"]); + Expect.equals(2, map2["2"]); + } +} + +class ConstInit { + final map1; + final map2; + + ConstInit() + : this.map1 = {"a": 1, "b": 2}, + this.map2 = {"1": 1, "2": 2} {} + + test() { + Expect.equals(1, map1["a"]); + Expect.equals(2, map1["b"]); + + Expect.equals(1, map2["1"]); + Expect.equals(2, map2["2"]); + } +} + +main() { + MapLiteralTest.testMain(); +} diff --git a/tests/language/map/map_test.dart b/tests/language/map/map_test.dart new file mode 100644 index 00000000000..1ccee3c5e5f --- /dev/null +++ b/tests/language/map/map_test.dart @@ -0,0 +1,222 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. +// A subtest of the larger MapTest. Will eliminate once the full +// test is running. + +import "package:expect/expect.dart"; + +class MapTest { + static void testDeletedElement(Map map) { + map.clear(); + for (int i = 0; i < 100; i++) { + map[1] = 2; + Expect.equals(1, map.length); + int x = map.remove(1); + Expect.equals(2, x); + Expect.equals(0, map.length); + } + Expect.equals(0, map.length); + for (int i = 0; i < 100; i++) { + map[i] = 2; + Expect.equals(1, map.length); + int? x = map.remove(105); + Expect.equals(null, x); + Expect.equals(1, map.length); + x = map.remove(i); + Expect.equals(2, x); + Expect.equals(0, map.length); + } + Expect.equals(0, map.length); + map.remove(105); + } + + static void test(Map map) { + testDeletedElement(map); + testMap(map, 1, 2, 3, 4, 5, 6, 7, 8); + map.clear(); + testMap(map, "value1", "value2", "value3", "value4", "value5", "value6", + "value7", "value8"); + } + + static void testMap(Map map, key1, key2, key3, key4, key5, key6, key7, key8) { + int value1 = 10; + int value2 = 20; + int value3 = 30; + int value4 = 40; + int value5 = 50; + int value6 = 60; + int value7 = 70; + int value8 = 80; + + Expect.equals(0, map.length); + + map[key1] = value1; + Expect.equals(value1, map[key1]); + map[key1] = value2; + Expect.equals(false, map.containsKey(key2)); + Expect.equals(1, map.length); + + map[key1] = value1; + Expect.equals(value1, map[key1]); + // Add enough entries to make sure the table grows. + map[key2] = value2; + Expect.equals(value2, map[key2]); + Expect.equals(2, map.length); + map[key3] = value3; + Expect.equals(value2, map[key2]); + Expect.equals(value3, map[key3]); + map[key4] = value4; + Expect.equals(value3, map[key3]); + Expect.equals(value4, map[key4]); + map[key5] = value5; + Expect.equals(value4, map[key4]); + Expect.equals(value5, map[key5]); + map[key6] = value6; + Expect.equals(value5, map[key5]); + Expect.equals(value6, map[key6]); + map[key7] = value7; + Expect.equals(value6, map[key6]); + Expect.equals(value7, map[key7]); + map[key8] = value8; + Expect.equals(value1, map[key1]); + Expect.equals(value2, map[key2]); + Expect.equals(value3, map[key3]); + Expect.equals(value4, map[key4]); + Expect.equals(value5, map[key5]); + Expect.equals(value6, map[key6]); + Expect.equals(value7, map[key7]); + Expect.equals(value8, map[key8]); + Expect.equals(8, map.length); + + map.remove(key4); + Expect.equals(false, map.containsKey(key4)); + Expect.equals(7, map.length); + + // Test clearing the table. + map.clear(); + Expect.equals(0, map.length); + Expect.equals(false, map.containsKey(key1)); + Expect.equals(false, map.containsKey(key2)); + Expect.equals(false, map.containsKey(key3)); + Expect.equals(false, map.containsKey(key4)); + Expect.equals(false, map.containsKey(key5)); + Expect.equals(false, map.containsKey(key6)); + Expect.equals(false, map.containsKey(key7)); + Expect.equals(false, map.containsKey(key8)); + + // Test adding and removing again. + map[key1] = value1; + Expect.equals(value1, map[key1]); + Expect.equals(1, map.length); + map[key2] = value2; + Expect.equals(value2, map[key2]); + Expect.equals(2, map.length); + map[key3] = value3; + Expect.equals(value3, map[key3]); + map.remove(key3); + Expect.equals(2, map.length); + map[key4] = value4; + Expect.equals(value4, map[key4]); + map.remove(key4); + Expect.equals(2, map.length); + map[key5] = value5; + Expect.equals(value5, map[key5]); + map.remove(key5); + Expect.equals(2, map.length); + map[key6] = value6; + Expect.equals(value6, map[key6]); + map.remove(key6); + Expect.equals(2, map.length); + map[key7] = value7; + Expect.equals(value7, map[key7]); + map.remove(key7); + Expect.equals(2, map.length); + map[key8] = value8; + Expect.equals(value8, map[key8]); + map.remove(key8); + Expect.equals(2, map.length); + + Expect.equals(true, map.containsKey(key1)); + Expect.equals(true, map.containsValue(value1)); + + // Test Map.forEach. + Map other_map = new Map(); + void testForEachMap(key, value) { + other_map[key] = value; + } + + map.forEach(testForEachMap); + Expect.equals(true, other_map.containsKey(key1)); + Expect.equals(true, other_map.containsKey(key2)); + Expect.equals(true, other_map.containsValue(value1)); + Expect.equals(true, other_map.containsValue(value2)); + Expect.equals(2, other_map.length); + + other_map.clear(); + Expect.equals(0, other_map.length); + + // Test Collection.keys. + void testForEachCollection(value) { + other_map[value] = value; + } + + Iterable keys = map.keys; + keys.forEach(testForEachCollection); + Expect.equals(true, other_map.containsKey(key1)); + Expect.equals(true, other_map.containsKey(key2)); + Expect.equals(true, other_map.containsValue(key1)); + Expect.equals(true, other_map.containsValue(key2)); + Expect.equals(true, !other_map.containsKey(value1)); + Expect.equals(true, !other_map.containsKey(value2)); + Expect.equals(true, !other_map.containsValue(value1)); + Expect.equals(true, !other_map.containsValue(value2)); + Expect.equals(2, other_map.length); + other_map.clear(); + Expect.equals(0, other_map.length); + + // Test Collection.values. + Iterable values = map.values; + values.forEach(testForEachCollection); + Expect.equals(true, !other_map.containsKey(key1)); + Expect.equals(true, !other_map.containsKey(key2)); + Expect.equals(true, !other_map.containsValue(key1)); + Expect.equals(true, !other_map.containsValue(key2)); + Expect.equals(true, other_map.containsKey(value1)); + Expect.equals(true, other_map.containsKey(value2)); + Expect.equals(true, other_map.containsValue(value1)); + Expect.equals(true, other_map.containsValue(value2)); + Expect.equals(2, other_map.length); + other_map.clear(); + Expect.equals(0, other_map.length); + + // Test Map.putIfAbsent. + map.clear(); + Expect.equals(false, map.containsKey(key1)); + map.putIfAbsent(key1, () => 10); + Expect.equals(true, map.containsKey(key1)); + Expect.equals(10, map[key1]); + Expect.equals(10, map.putIfAbsent(key1, () => 11)); + } + + static testKeys(Map map) { + map[1] = 101; + map[2] = 102; + Iterable k = map.keys; + Expect.equals(2, k.length); + Iterable v = map.values; + Expect.equals(2, v.length); + Expect.equals(true, map.containsValue(101)); + Expect.equals(true, map.containsValue(102)); + Expect.equals(false, map.containsValue(103)); + } + + static testMain() { + test(new Map()); + testKeys(new Map()); + } +} + +main() { + MapTest.testMain(); +} diff --git a/tests/language/map/null_key_foreach_test.dart b/tests/language/map/null_key_foreach_test.dart new file mode 100644 index 00000000000..67e99a3ae3e --- /dev/null +++ b/tests/language/map/null_key_foreach_test.dart @@ -0,0 +1,19 @@ +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import "package:expect/expect.dart"; + +// Regression test for using `null` as a key with `forEach`. + +main() { + var x = new Map(); + x[1] = 2; + x[null] = 1; + int c = 0; + x.forEach((int? i, int j) { + c++; + Expect.isTrue(i == null || i is int, 'int or null expected'); + }); + Expect.equals(2, c); +} diff --git a/tests/language/map/ordered_test.dart b/tests/language/map/ordered_test.dart new file mode 100644 index 00000000000..b074ecb5b67 --- /dev/null +++ b/tests/language/map/ordered_test.dart @@ -0,0 +1,80 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import "package:expect/expect.dart"; + +// Tests that map literals are ordered. + +class OrderedMapsTest { + static testMain() { + testMaps(const {"a": 1, "c": 2}, const {"c": 2, "a": 1}, true); + testMaps({"a": 1, "c": 2}, {"c": 2, "a": 1}, false); + } + + static void testMaps(map1, map2, bool isConst) { + Expect.isFalse(identical(map1, map2)); + + var keys = map1.keys.toList(); + Expect.equals(2, keys.length); + Expect.equals("a", keys[0]); + Expect.equals("c", keys[1]); + + keys = map2.keys.toList(); + Expect.equals(2, keys.length); + Expect.equals("c", keys[0]); + Expect.equals("a", keys[1]); + + var values = map1.values.toList(); + Expect.equals(2, values.length); + Expect.equals(1, values[0]); + Expect.equals(2, values[1]); + + values = map2.values.toList(); + Expect.equals(2, values.length); + Expect.equals(2, values[0]); + Expect.equals(1, values[1]); + + if (isConst) return; + + map1["b"] = 3; + map2["b"] = 3; + + keys = map1.keys.toList(); + Expect.equals(3, keys.length); + Expect.equals("a", keys[0]); + Expect.equals("c", keys[1]); + Expect.equals("b", keys[2]); + + keys = map2.keys.toList(); + Expect.equals(3, keys.length); + Expect.equals("c", keys[0]); + Expect.equals("a", keys[1]); + Expect.equals("b", keys[2]); + + values = map1.values.toList(); + Expect.equals(3, values.length); + Expect.equals(1, values[0]); + Expect.equals(2, values[1]); + Expect.equals(3, values[2]); + + values = map2.values.toList(); + Expect.equals(3, values.length); + Expect.equals(2, values[0]); + Expect.equals(1, values[1]); + Expect.equals(3, values[2]); + + map1["a"] = 4; + keys = map1.keys.toList(); + Expect.equals(3, keys.length); + Expect.equals("a", keys[0]); + + values = map1.values.toList(); + Expect.equals(3, values.length); + Expect.equals(4, values[0]); + } +} + +main() { + OrderedMapsTest.testMain(); +}