OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, 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 // Check that arrays from final array literals are immutable. | |
5 | |
6 class ListLiteral3Test { | |
7 | |
8 static final List<String> canonicalJoke = const ["knock", "knock"]; | |
9 | |
10 static testMain() { | |
11 | |
12 List<String> joke = const ["knock", "knock"]; | |
13 // Elements of canonical lists are canonicalized. | |
14 Expect.equals(true, joke === canonicalJoke); | |
15 Expect.equals(true, joke[0] === joke[1]); | |
16 Expect.equals(true, joke[0] === canonicalJoke[0]); | |
17 | |
18 // Lists from literals are immutable. | |
19 bool caughtException = false; | |
20 try { | |
21 joke[0] = "sock"; | |
22 } catch (UnsupportedOperationException e) { | |
23 caughtException = true; | |
24 } | |
25 Expect.equals(true, caughtException); | |
26 Expect.equals(true, joke[0] === joke[1]); | |
27 | |
28 // Make sure lists allocated at runtime are mutable and are | |
29 // not canonicalized. | |
30 List<String> lame_joke = ["knock", "knock"]; // Invokes operator new. | |
31 Expect.equals(true, joke[1] === lame_joke[1]); | |
32 // Operator new creates a mutable list. | |
33 Expect.equals(false, joke === lame_joke); | |
34 lame_joke[1] = "who"; | |
35 Expect.equals(true, "who" === lame_joke[1]); | |
36 | |
37 // Elements of canonical lists are canonicalized. | |
38 List<List<int>> a = const <List<int>>[ const [1, 2], const [1, 2]]; | |
39 Expect.equals(true, a[0] === a[1]); | |
40 Expect.equals(true, a[0][0] === a[1][0]); | |
41 try { | |
42 caughtException = false; | |
43 a[0][0] = 42; | |
44 } catch (UnsupportedOperationException e) { | |
45 caughtException = true; | |
46 } | |
47 Expect.equals(true, caughtException); | |
48 | |
49 List<List<double>> b = const [ const [1.0, 2.0], const [1.0, 2.0]]; | |
50 Expect.equals(true, b[0] === b[1]); | |
51 Expect.equals(true, b[0][0] === 1.0); | |
52 Expect.equals(true, b[0][0] === b[1][0]); | |
53 try { | |
54 caughtException = false; | |
55 b[0][0] = 42.0; | |
56 } catch (UnsupportedOperationException e) { | |
57 caughtException = true; | |
58 } | |
59 Expect.equals(true, caughtException); | |
60 } | |
61 } | |
62 | |
63 main() { | |
64 ListLiteral3Test.testMain(); | |
65 } | |
OLD | NEW |