OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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 dart2js.serialization_test_helper; |
| 6 |
| 7 import 'dart:io'; |
| 8 import 'memory_compiler.dart'; |
| 9 import 'package:async_helper/async_helper.dart'; |
| 10 import 'package:compiler/src/common/resolution.dart'; |
| 11 import 'package:compiler/src/commandline_options.dart'; |
| 12 import 'package:compiler/src/constants/constructors.dart'; |
| 13 import 'package:compiler/src/constants/expressions.dart'; |
| 14 import 'package:compiler/src/dart_types.dart'; |
| 15 import 'package:compiler/src/compiler.dart'; |
| 16 import 'package:compiler/src/diagnostics/invariant.dart'; |
| 17 import 'package:compiler/src/elements/elements.dart'; |
| 18 import 'package:compiler/src/elements/visitor.dart'; |
| 19 import 'package:compiler/src/ordered_typeset.dart'; |
| 20 import 'package:compiler/src/serialization/element_serialization.dart'; |
| 21 import 'package:compiler/src/serialization/equivalence.dart'; |
| 22 import 'package:compiler/src/serialization/json_serializer.dart'; |
| 23 import 'package:compiler/src/serialization/serialization.dart'; |
| 24 |
| 25 |
| 26 /// Strategy for checking equivalence. |
| 27 /// |
| 28 /// Use this strategy to fail early with contextual information in the event of |
| 29 /// inequivalence. |
| 30 class CheckStrategy implements TestStrategy { |
| 31 const CheckStrategy(); |
| 32 |
| 33 @override |
| 34 bool test(var object1, var object2, String property, var value1, var value2, |
| 35 [bool equivalence(a, b) = equality]) { |
| 36 return check(object1, object2, property, value1, value2, equivalence); |
| 37 } |
| 38 |
| 39 @override |
| 40 bool testLists( |
| 41 Object object1, Object object2, String property, |
| 42 List list1, List list2, |
| 43 [bool elementEquivalence(a, b) = equality]) { |
| 44 return checkListEquivalence( |
| 45 object1, object2, property, list1, list2, |
| 46 (o1, o2, p, v1, v2) { |
| 47 if (!elementEquivalence(v1, v2)) { |
| 48 throw "$o1.$p = '${v1}' <> " |
| 49 "$o2.$p = '${v2}'"; |
| 50 } |
| 51 return true; |
| 52 }); |
| 53 } |
| 54 |
| 55 @override |
| 56 bool testSets( |
| 57 var object1, var object2, String property, |
| 58 Iterable set1, Iterable set2, |
| 59 [bool elementEquivalence(a, b) = equality]) { |
| 60 return checkSetEquivalence( |
| 61 object1, object2,property, set1, set2, elementEquivalence); |
| 62 } |
| 63 |
| 64 @override |
| 65 bool testElements( |
| 66 Object object1, Object object2, String property, |
| 67 Element element1, Element element2) { |
| 68 return checkElementIdentities( |
| 69 object1, object2, property, element1, element2); |
| 70 } |
| 71 |
| 72 @override |
| 73 bool testTypes( |
| 74 Object object1, Object object2, String property, |
| 75 DartType type1, DartType type2) { |
| 76 return checkTypes(object1, object2, property, type1, type2); |
| 77 } |
| 78 |
| 79 @override |
| 80 bool testConstants( |
| 81 Object object1, Object object2, String property, |
| 82 ConstantExpression exp1, ConstantExpression exp2) { |
| 83 return checkConstants(object1, object2, property, exp1, exp2); |
| 84 } |
| 85 |
| 86 @override |
| 87 bool testTypeLists( |
| 88 Object object1, Object object2, String property, |
| 89 List<DartType> list1, List<DartType> list2) { |
| 90 return checkTypeLists(object1, object2, property, list1, list2); |
| 91 } |
| 92 |
| 93 @override |
| 94 bool testConstantLists( |
| 95 Object object1, Object object2, String property, |
| 96 List<ConstantExpression> list1, |
| 97 List<ConstantExpression> list2) { |
| 98 return checkConstantLists(object1, object2, property, list1, list2); |
| 99 } |
| 100 } |
| 101 |
| 102 /// Check that the values [property] of [object1] and [object2], [value1] and |
| 103 /// [value2] respectively, are equal and throw otherwise. |
| 104 bool check(var object1, var object2, String property, var value1, var value2, |
| 105 [bool equivalence(a, b) = equality]) { |
| 106 if (!equivalence(value1, value2)) { |
| 107 throw "property='$property' " |
| 108 "object1=$object1 (${object1.runtimeType}), value='${value1}' <> " |
| 109 "object2=$object2 (${object2.runtimeType}), value='${value2}'"; |
| 110 } |
| 111 return true; |
| 112 } |
| 113 |
| 114 /// Check equivalence of the two lists, [list1] and [list2], using |
| 115 /// [checkEquivalence] to check the pair-wise equivalence. |
| 116 /// |
| 117 /// Uses [object1], [object2] and [property] to provide context for failures. |
| 118 bool checkListEquivalence( |
| 119 Object object1, Object object2, String property, |
| 120 Iterable list1, Iterable list2, |
| 121 void checkEquivalence(o1, o2, property, a, b)) { |
| 122 for (int i = 0; i < list1.length && i < list2.length; i++) { |
| 123 checkEquivalence( |
| 124 object1, object2, property, |
| 125 list1.elementAt(i), list2.elementAt(i)); |
| 126 } |
| 127 for (int i = list1.length; i < list2.length; i++) { |
| 128 throw |
| 129 'Missing equivalent for element ' |
| 130 '#$i ${list2.elementAt(i)} in `${property}` on $object2.\n' |
| 131 '`${property}` on $object1:\n ${list1.join('\n ')}\n' |
| 132 '`${property}` on $object2:\n ${list2.join('\n ')}'; |
| 133 } |
| 134 for (int i = list2.length; i < list1.length; i++) { |
| 135 throw |
| 136 'Missing equivalent for element ' |
| 137 '#$i ${list1.elementAt(i)} in `${property}` on $object1.\n' |
| 138 '`${property}` on $object1:\n ${list1.join('\n ')}\n' |
| 139 '`${property}` on $object2:\n ${list2.join('\n ')}'; |
| 140 } |
| 141 return true; |
| 142 } |
| 143 |
| 144 /// Computes the set difference between [set1] and [set2] using |
| 145 /// [elementEquivalence] to determine element equivalence. |
| 146 /// |
| 147 /// Elements both in [set1] and [set2] are added to [common], elements in [set1] |
| 148 /// but not in [set2] are added to [unfound], and the set of elements in [set2] |
| 149 /// but not in [set1] are returned. |
| 150 Set computeSetDifference( |
| 151 Iterable set1, |
| 152 Iterable set2, |
| 153 List common, |
| 154 List unfound, |
| 155 [bool sameElement(a, b) = equality]) { |
| 156 // TODO(johnniwinther): Avoid the quadratic cost here. Some ideas: |
| 157 // - convert each set to a list and sort it first, then compare by walking |
| 158 // both lists in parallel |
| 159 // - map each element to a canonical object, create a map containing those |
| 160 // mappings, use the mapped sets to compare (then operations like |
| 161 // set.difference would work) |
| 162 Set remaining = set2.toSet(); |
| 163 for (var element1 in set1) { |
| 164 bool found = false; |
| 165 for (var element2 in remaining) { |
| 166 if (sameElement(element1, element2)) { |
| 167 found = true; |
| 168 remaining.remove(element2); |
| 169 break; |
| 170 } |
| 171 } |
| 172 if (found) { |
| 173 common.add(element1); |
| 174 } else { |
| 175 unfound.add(element1); |
| 176 } |
| 177 } |
| 178 return remaining; |
| 179 } |
| 180 |
| 181 /// Check equivalence of the two iterables, [set1] and [set1], as sets using |
| 182 /// [elementEquivalence] to compute the pair-wise equivalence. |
| 183 /// |
| 184 /// Uses [object1], [object2] and [property] to provide context for failures. |
| 185 bool checkSetEquivalence( |
| 186 var object1, |
| 187 var object2, |
| 188 String property, |
| 189 Iterable set1, |
| 190 Iterable set2, |
| 191 bool sameElement(a, b)) { |
| 192 List common = []; |
| 193 List unfound = []; |
| 194 Set remaining = |
| 195 computeSetDifference(set1, set2, common, unfound, sameElement); |
| 196 if (unfound.isNotEmpty || remaining.isNotEmpty) { |
| 197 String message = |
| 198 "Set mismatch for `$property` on $object1 vs $object2: \n" |
| 199 "Common:\n ${common.join('\n ')}\n" |
| 200 "Unfound:\n ${unfound.join('\n ')}\n" |
| 201 "Extra: \n ${remaining.join('\n ')}"; |
| 202 throw message; |
| 203 } |
| 204 return true; |
| 205 } |
| 206 |
| 207 /// Checks the equivalence of the identity (but not properties) of [element1] |
| 208 /// and [element2]. |
| 209 /// |
| 210 /// Uses [object1], [object2] and [property] to provide context for failures. |
| 211 bool checkElementIdentities( |
| 212 Object object1, Object object2, String property, |
| 213 Element element1, Element element2) { |
| 214 if (identical(element1, element2)) return true; |
| 215 if (element1 == null || element2 == null) { |
| 216 return check(object1, object2, property, element1, element2); |
| 217 } else { |
| 218 return const ElementIdentityEquivalence(const CheckStrategy()) |
| 219 .visit(element1, element2); |
| 220 } |
| 221 } |
| 222 |
| 223 /// Checks the pair-wise equivalence of the identity (but not properties) of the |
| 224 /// elements in [list] and [list2]. |
| 225 /// |
| 226 /// Uses [object1], [object2] and [property] to provide context for failures. |
| 227 bool checkElementListIdentities( |
| 228 Object object1, Object object2, String property, |
| 229 Iterable<Element> list1, Iterable<Element> list2) { |
| 230 return checkListEquivalence( |
| 231 object1, object2, property, |
| 232 list1, list2, checkElementIdentities); |
| 233 } |
| 234 |
| 235 /// Checks the equivalence of [type1] and [type2]. |
| 236 /// |
| 237 /// Uses [object1], [object2] and [property] to provide context for failures. |
| 238 bool checkTypes( |
| 239 Object object1, Object object2, String property, |
| 240 DartType type1, DartType type2) { |
| 241 if (identical(type1, type2)) return true; |
| 242 if (type1 == null || type2 == null) { |
| 243 return check(object1, object2, property, type1, type2); |
| 244 } else { |
| 245 return const TypeEquivalence(const CheckStrategy()).visit(type1, type2); |
| 246 } |
| 247 } |
| 248 |
| 249 /// Checks the pair-wise equivalence of the types in [list1] and [list2]. |
| 250 /// |
| 251 /// Uses [object1], [object2] and [property] to provide context for failures. |
| 252 bool checkTypeLists( |
| 253 Object object1, Object object2, String property, |
| 254 List<DartType> list1, List<DartType> list2) { |
| 255 return checkListEquivalence( |
| 256 object1, object2, property, list1, list2, checkTypes); |
| 257 } |
| 258 |
| 259 /// Checks the equivalence of [exp1] and [exp2]. |
| 260 /// |
| 261 /// Uses [object1], [object2] and [property] to provide context for failures. |
| 262 bool checkConstants( |
| 263 Object object1, Object object2, String property, |
| 264 ConstantExpression exp1, ConstantExpression exp2) { |
| 265 if (identical(exp1, exp2)) return true; |
| 266 if (exp1 == null || exp2 == null) { |
| 267 return check(object1, object2, property, exp1, exp2); |
| 268 } else { |
| 269 return const ConstantEquivalence(const CheckStrategy()).visit(exp1, exp2); |
| 270 } |
| 271 } |
| 272 |
| 273 /// Checks the pair-wise equivalence of the contants in [list1] and [list2]. |
| 274 /// |
| 275 /// Uses [object1], [object2] and [property] to provide context for failures. |
| 276 bool checkConstantLists( |
| 277 Object object1, Object object2, String property, |
| 278 List<ConstantExpression> list1, |
| 279 List<ConstantExpression> list2) { |
| 280 return checkListEquivalence( |
| 281 object1, object2, property, |
| 282 list1, list2, checkConstants); |
| 283 } |
| 284 |
| 285 |
| 286 /// Check member property equivalence between all members common to [compiler1] |
| 287 /// and [compiler2]. |
| 288 void checkLoadedLibraryMembers( |
| 289 Compiler compiler1, |
| 290 Compiler compiler2, |
| 291 bool hasProperty(Element member1), |
| 292 void checkMemberProperties(Compiler compiler1, Element member1, |
| 293 Compiler compiler2, Element member2, |
| 294 {bool verbose}), |
| 295 {bool verbose: false}) { |
| 296 |
| 297 void checkMembers(Element member1, Element member2) { |
| 298 if (member1.isClass && member2.isClass) { |
| 299 ClassElement class1 = member1; |
| 300 ClassElement class2 = member2; |
| 301 class1.forEachLocalMember((m1) { |
| 302 checkMembers(m1, class2.lookupLocalMember(m1.name)); |
| 303 }); |
| 304 return; |
| 305 } |
| 306 |
| 307 if (!hasProperty(member1)) { |
| 308 return; |
| 309 } |
| 310 |
| 311 if (member2 == null) { |
| 312 return; |
| 313 } |
| 314 |
| 315 if (areElementsEquivalent(member1, member2)) { |
| 316 checkMemberProperties( |
| 317 compiler1, member1, |
| 318 compiler2, member2, |
| 319 verbose: verbose); |
| 320 } |
| 321 } |
| 322 |
| 323 for (LibraryElement library1 in compiler1.libraryLoader.libraries) { |
| 324 LibraryElement library2 = |
| 325 compiler2.libraryLoader.lookupLibrary(library1.canonicalUri); |
| 326 if (library2 != null) { |
| 327 library1.forEachLocalMember((Element member1) { |
| 328 checkMembers(member1, library2.localLookup(member1.name)); |
| 329 }); |
| 330 |
| 331 } |
| 332 } |
| 333 } |
| 334 |
| 335 /// Check equivalence of all resolution impacts. |
| 336 void checkAllImpacts( |
| 337 Compiler compiler1, |
| 338 Compiler compiler2, |
| 339 {bool verbose: false}) { |
| 340 checkLoadedLibraryMembers( |
| 341 compiler1, |
| 342 compiler2, |
| 343 (Element member1) { |
| 344 return compiler1.resolution.hasResolutionImpact(member1); |
| 345 }, |
| 346 checkImpacts, |
| 347 verbose: true); |
| 348 } |
| 349 |
| 350 /// Check equivalence of resolution impact for [member1] and [member2]. |
| 351 void checkImpacts(Compiler compiler1, Element member1, |
| 352 Compiler compiler2, Element member2, |
| 353 {bool verbose: false}) { |
| 354 ResolutionImpact impact1 = compiler1.resolution.getResolutionImpact(member1); |
| 355 ResolutionImpact impact2 = |
| 356 compiler2.serialization.deserializer.getResolutionImpact(member2); |
| 357 |
| 358 if (impact1 == null || impact2 == null) return; |
| 359 |
| 360 if (verbose) { |
| 361 print('Checking impacts for $member1 vs $member2'); |
| 362 } |
| 363 |
| 364 testResolutionImpactEquivalence(impact1, impact2, const CheckStrategy()); |
| 365 } |
OLD | NEW |