OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013, 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 | |
5 library test.class_equality_test; | |
6 | |
7 import 'dart:mirrors'; | |
8 | |
9 import 'package:expect/expect.dart'; | |
10 | |
11 class A<T> {} | |
12 class B extends A<int> {} | |
13 | |
14 class BadEqualityHash { | |
15 int count = 0; | |
16 bool operator ==(other) => true; | |
17 int get hashCode => count++; | |
18 } | |
19 | |
20 checkEquality(List<Map> equivalenceClasses) { | |
21 for (var equivalenceClass in equivalenceClasses) { | |
22 equivalenceClass.forEach((name, member) { | |
23 equivalenceClass.forEach((otherName, otherMember) { | |
24 // Reflexivity, symmetry and transitivity. | |
25 Expect.equals(member, | |
26 otherMember, | |
27 "$name == $otherName"); | |
28 Expect.equals(member.hashCode, | |
29 otherMember.hashCode, | |
30 "$name.hashCode == $otherName.hashCode"); | |
31 }); | |
32 for (var otherEquivalenceClass in equivalenceClasses) { | |
33 if (otherEquivalenceClass == equivalenceClass) continue; | |
34 otherEquivalenceClass.forEach((otherName, otherMember) { | |
35 Expect.notEquals(member, | |
36 otherMember, | |
37 "$name != $otherName"); // Exclusion. | |
38 // Hash codes may or may not be equal. | |
39 }); | |
40 } | |
41 }); | |
42 } | |
43 } | |
44 | |
45 void subroutine() { | |
46 } | |
47 | |
48 main() { | |
49 LibraryMirror thisLibrary = | |
50 currentMirrorSystem() | |
51 .findLibrary(const Symbol('test.class_equality_test')) | |
52 .single; | |
53 | |
54 var o1 = new Object(); | |
55 var o2 = new Object(); | |
56 | |
57 var badEqualityHash1 = new BadEqualityHash(); | |
58 var badEqualityHash2 = new BadEqualityHash(); | |
59 | |
60 checkEquality([ | |
ahe
2013/08/30 20:40:14
Could you avoid passing in a list, and instead cal
rmacnak
2013/08/30 21:08:29
The number of cases is quadratic. Notice I am pass
ahe
2013/08/30 21:11:13
OK
| |
61 {'reflect(o1)' : reflect(o1), | |
62 'reflect(o1), again' : reflect(o1)}, | |
63 | |
64 {'reflect(o2)' : reflect(o2), | |
65 'reflect(o2), again' : reflect(o2)}, | |
66 | |
67 {'reflect(badEqualityHash1)' : reflect(badEqualityHash1), | |
68 'reflect(badEqualityHash1), again' : reflect(badEqualityHash1)}, | |
69 | |
70 {'reflect(badEqualityHash2)' : reflect(badEqualityHash2), | |
71 'reflect(badEqualityHash2), again' : reflect(badEqualityHash2)}, | |
72 | |
73 {'reflect(true)' : reflect(true), | |
74 'reflect(true), again' : reflect(true)}, | |
75 | |
76 {'reflect(false)' : reflect(false), | |
77 'reflect(false), again' : reflect(false)}, | |
78 | |
79 {'reflect(null)' : reflect(null), | |
80 'reflect(null), again' : reflect(null)}, | |
81 | |
82 {'reflect(3.5+4.5)' : reflect(3.5+4.5), | |
83 'reflect(6.5+1.5)' : reflect(6.5+1.5)}, | |
84 | |
85 {'reflect(3+4)' : reflect(3+4), | |
86 'reflect(6+1)' : reflect(6+1)}, | |
87 | |
88 {'reflect("foo")' : reflect("foo"), | |
89 'reflect("foo"), again' : reflect("foo")}, | |
90 | |
91 // {'currentMirrorSystem().voidType' : currentMirrorSystem().voidType, | |
92 // 'thisLibrary.functions[#subroutine].returnType' : | |
93 // thisLibrary.functions[const Symbol('subroutine')].returnType}, | |
94 | |
95 {'currentMirrorSystem().dynamicType' : currentMirrorSystem().dynamicType, | |
96 'thisLibrary.functions[#main].returnType' : | |
97 thisLibrary.functions[const Symbol('main')].returnType}, | |
98 | |
99 // {'reflectClass(A)' : reflectClass(A), | |
100 // 'thisLibrary.classes[#A]' : thisLibrary.classes[const Symbol('A')], | |
101 // 'reflect(new A<int>()).type.originalDeclaration' : | |
102 // reflect(new A<int>()).type.originalDeclaration}, | |
103 | |
104 {'reflectClass(B).superclass' : reflectClass(B).superclass, | |
105 'reflect(new A<int>()).type' : reflect(new A<int>()).type}, | |
106 | |
107 {'reflectClass(B)' : reflectClass(B), | |
108 'thisLibrary.classes[#B]' : thisLibrary.classes[const Symbol('B')], | |
109 'reflect(new B()).type' : reflect(new B()).type}, | |
110 | |
111 {'thisLibrary' : thisLibrary, | |
112 'reflectClass(A).owner' : reflectClass(A).owner, | |
113 'reflectClass(B).owner' : reflectClass(B).owner, | |
114 'reflect(new A()).type.owner' : reflect(new A()).type.owner, | |
115 'reflect(new A()).type.owner' : reflect(new A()).type.owner}, | |
116 ]); | |
117 } | |
OLD | NEW |