OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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 | 4 |
5 library dart2js.serialization_test_helper; | 5 library dart2js.serialization_test_helper; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 import 'package:compiler/src/common/resolution.dart'; | 8 import 'package:compiler/src/common/resolution.dart'; |
9 import 'package:compiler/src/constants/expressions.dart'; | 9 import 'package:compiler/src/constants/expressions.dart'; |
| 10 import 'package:compiler/src/constants/values.dart'; |
10 import 'package:compiler/src/dart_types.dart'; | 11 import 'package:compiler/src/dart_types.dart'; |
11 import 'package:compiler/src/compiler.dart'; | 12 import 'package:compiler/src/compiler.dart'; |
12 import 'package:compiler/src/elements/elements.dart'; | 13 import 'package:compiler/src/elements/elements.dart'; |
13 import 'package:compiler/src/serialization/equivalence.dart'; | 14 import 'package:compiler/src/serialization/equivalence.dart'; |
14 import 'package:compiler/src/tree/nodes.dart'; | 15 import 'package:compiler/src/tree/nodes.dart'; |
15 import 'package:expect/expect.dart'; | 16 import 'package:expect/expect.dart'; |
16 import 'test_data.dart'; | 17 import 'test_data.dart'; |
17 | 18 |
18 Check currentCheck; | 19 Check currentCheck; |
19 | 20 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 @override | 82 @override |
82 bool testSets( | 83 bool testSets( |
83 var object1, var object2, String property, | 84 var object1, var object2, String property, |
84 Iterable set1, Iterable set2, | 85 Iterable set1, Iterable set2, |
85 [bool elementEquivalence(a, b) = equality]) { | 86 [bool elementEquivalence(a, b) = equality]) { |
86 return checkSetEquivalence( | 87 return checkSetEquivalence( |
87 object1, object2,property, set1, set2, elementEquivalence); | 88 object1, object2,property, set1, set2, elementEquivalence); |
88 } | 89 } |
89 | 90 |
90 @override | 91 @override |
| 92 bool testMaps( |
| 93 var object1, var object2, String property, Map map1, Map map2, |
| 94 [bool keyEquivalence(a, b) = equality, |
| 95 bool valueEquivalence(a, b) = equality]) { |
| 96 return checkMapEquivalence(object1, object2, property, |
| 97 map1, map2, keyEquivalence, valueEquivalence); |
| 98 } |
| 99 |
| 100 @override |
91 bool testElements( | 101 bool testElements( |
92 Object object1, Object object2, String property, | 102 Object object1, Object object2, String property, |
93 Element element1, Element element2) { | 103 Element element1, Element element2) { |
94 return checkElementIdentities( | 104 return checkElementIdentities( |
95 object1, object2, property, element1, element2); | 105 object1, object2, property, element1, element2); |
96 } | 106 } |
97 | 107 |
98 @override | 108 @override |
99 bool testTypes( | 109 bool testTypes( |
100 Object object1, Object object2, String property, | 110 Object object1, Object object2, String property, |
101 DartType type1, DartType type2) { | 111 DartType type1, DartType type2) { |
102 return checkTypes(object1, object2, property, type1, type2); | 112 return checkTypes(object1, object2, property, type1, type2); |
103 } | 113 } |
104 | 114 |
105 @override | 115 @override |
106 bool testConstants( | 116 bool testConstants( |
107 Object object1, Object object2, String property, | 117 Object object1, Object object2, String property, |
108 ConstantExpression exp1, ConstantExpression exp2) { | 118 ConstantExpression exp1, ConstantExpression exp2) { |
109 return checkConstants(object1, object2, property, exp1, exp2); | 119 return checkConstants(object1, object2, property, exp1, exp2); |
110 } | 120 } |
111 | 121 |
112 @override | 122 @override |
| 123 bool testConstantValues(Object object1, Object object2, String property, |
| 124 ConstantValue value1, ConstantValue value2) { |
| 125 return areConstantValuesEquivalent(value1, value2); |
| 126 } |
| 127 |
| 128 @override |
113 bool testTypeLists( | 129 bool testTypeLists( |
114 Object object1, Object object2, String property, | 130 Object object1, Object object2, String property, |
115 List<DartType> list1, List<DartType> list2) { | 131 List<DartType> list1, List<DartType> list2) { |
116 return checkTypeLists(object1, object2, property, list1, list2); | 132 return checkTypeLists(object1, object2, property, list1, list2); |
117 } | 133 } |
118 | 134 |
119 @override | 135 @override |
120 bool testConstantLists( | 136 bool testConstantLists( |
121 Object object1, Object object2, String property, | 137 Object object1, Object object2, String property, |
122 List<ConstantExpression> list1, | 138 List<ConstantExpression> list1, |
123 List<ConstantExpression> list2) { | 139 List<ConstantExpression> list2) { |
124 return checkConstantLists(object1, object2, property, list1, list2); | 140 return checkConstantLists(object1, object2, property, list1, list2); |
125 } | 141 } |
126 | 142 |
127 @override | 143 @override |
| 144 bool testConstantValueLists(Object object1, Object object2, String property, |
| 145 List<ConstantValue> list1, List<ConstantValue> list2) { |
| 146 return checkConstantValueLists(object1, object2, property, list1, list2); |
| 147 } |
| 148 |
| 149 @override |
128 bool testNodes(Object object1, Object object2, String property, | 150 bool testNodes(Object object1, Object object2, String property, |
129 Node node1, Node node2) { | 151 Node node1, Node node2) { |
130 return new NodeEquivalenceVisitor(this).testNodes( | 152 return new NodeEquivalenceVisitor(this).testNodes( |
131 object1, object2, property, node1, node2); | 153 object1, object2, property, node1, node2); |
132 } | 154 } |
133 } | 155 } |
134 | 156 |
135 /// Check that the values [property] of [object1] and [object2], [value1] and | 157 /// Check that the values [property] of [object1] and [object2], [value1] and |
136 /// [value2] respectively, are equal and throw otherwise. | 158 /// [value2] respectively, are equal and throw otherwise. |
137 bool check(var object1, var object2, String property, var value1, var value2, | 159 bool check(var object1, var object2, String property, var value1, var value2, |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 String message = | 262 String message = |
241 "Set mismatch for `$property` on $object1 vs $object2: \n" | 263 "Set mismatch for `$property` on $object1 vs $object2: \n" |
242 "Common:\n ${common.join('\n ')}\n" | 264 "Common:\n ${common.join('\n ')}\n" |
243 "Unfound:\n ${unfound.join('\n ')}\n" | 265 "Unfound:\n ${unfound.join('\n ')}\n" |
244 "Extra: \n ${remaining.join('\n ')}"; | 266 "Extra: \n ${remaining.join('\n ')}"; |
245 throw message; | 267 throw message; |
246 } | 268 } |
247 return true; | 269 return true; |
248 } | 270 } |
249 | 271 |
| 272 /// Check equivalence of the two iterables, [set1] and [set1], as sets using |
| 273 /// [elementEquivalence] to compute the pair-wise equivalence. |
| 274 /// |
| 275 /// Uses [object1], [object2] and [property] to provide context for failures. |
| 276 bool checkMapEquivalence( |
| 277 var object1, |
| 278 var object2, |
| 279 String property, |
| 280 Map map1, |
| 281 Map map2, |
| 282 bool sameKey(a, b), |
| 283 bool sameValue(a, b)) { |
| 284 List<List> common = <List>[]; |
| 285 List unfound = []; |
| 286 Set remaining = |
| 287 computeSetDifference(map1.keys, map2.keys, common, unfound, |
| 288 sameElement: sameKey); |
| 289 if (unfound.isNotEmpty || remaining.isNotEmpty) { |
| 290 String message = |
| 291 "Map key mismatch for `$property` on $object1 vs $object2: \n" |
| 292 "Common:\n ${common.join('\n ')}\n" |
| 293 "Unfound:\n ${unfound.join('\n ')}\n" |
| 294 "Extra: \n ${remaining.join('\n ')}"; |
| 295 throw message; |
| 296 } |
| 297 for (List pair in common) { |
| 298 check(object1, object2, 'Map value for `$property`', |
| 299 map1[pair[0]], map2[pair[1]], sameValue); |
| 300 } |
| 301 return true; |
| 302 } |
| 303 |
250 /// Checks the equivalence of the identity (but not properties) of [element1] | 304 /// Checks the equivalence of the identity (but not properties) of [element1] |
251 /// and [element2]. | 305 /// and [element2]. |
252 /// | 306 /// |
253 /// Uses [object1], [object2] and [property] to provide context for failures. | 307 /// Uses [object1], [object2] and [property] to provide context for failures. |
254 bool checkElementIdentities( | 308 bool checkElementIdentities( |
255 Object object1, Object object2, String property, | 309 Object object1, Object object2, String property, |
256 Element element1, Element element2) { | 310 Element element1, Element element2) { |
257 if (identical(element1, element2)) return true; | 311 if (identical(element1, element2)) return true; |
258 return check(object1, object2, | 312 return check(object1, object2, |
259 property, element1, element2, areElementsEquivalent); | 313 property, element1, element2, areElementsEquivalent); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 ConstantExpression exp1, ConstantExpression exp2) { | 358 ConstantExpression exp1, ConstantExpression exp2) { |
305 if (identical(exp1, exp2)) return true; | 359 if (identical(exp1, exp2)) return true; |
306 if (exp1 == null || exp2 == null) { | 360 if (exp1 == null || exp2 == null) { |
307 return check(object1, object2, property, exp1, exp2); | 361 return check(object1, object2, property, exp1, exp2); |
308 } else { | 362 } else { |
309 return check(object1, object2, property, exp1, exp2, | 363 return check(object1, object2, property, exp1, exp2, |
310 (a, b) => const ConstantEquivalence(const CheckStrategy()).visit(a, b)); | 364 (a, b) => const ConstantEquivalence(const CheckStrategy()).visit(a, b)); |
311 } | 365 } |
312 } | 366 } |
313 | 367 |
314 /// Checks the pair-wise equivalence of the contants in [list1] and [list2]. | 368 /// Checks the equivalence of [value1] and [value2]. |
| 369 /// |
| 370 /// Uses [object1], [object2] and [property] to provide context for failures. |
| 371 bool checkConstantValues( |
| 372 Object object1, Object object2, String property, |
| 373 ConstantValue value1, ConstantValue value2) { |
| 374 if (identical(value1, value2)) return true; |
| 375 if (value1 == null || value2 == null) { |
| 376 return check(object1, object2, property, value1, value2); |
| 377 } else { |
| 378 return check(object1, object2, property, value1, value2, |
| 379 (a, b) => const ConstantValueEquivalence( |
| 380 const CheckStrategy()).visit(a, b)); |
| 381 } |
| 382 } |
| 383 |
| 384 /// Checks the pair-wise equivalence of the constants in [list1] and [list2]. |
315 /// | 385 /// |
316 /// Uses [object1], [object2] and [property] to provide context for failures. | 386 /// Uses [object1], [object2] and [property] to provide context for failures. |
317 bool checkConstantLists( | 387 bool checkConstantLists( |
318 Object object1, Object object2, String property, | 388 Object object1, Object object2, String property, |
319 List<ConstantExpression> list1, | 389 List<ConstantExpression> list1, |
320 List<ConstantExpression> list2) { | 390 List<ConstantExpression> list2) { |
321 return checkListEquivalence( | 391 return checkListEquivalence( |
322 object1, object2, property, | 392 object1, object2, property, |
323 list1, list2, checkConstants); | 393 list1, list2, checkConstants); |
324 } | 394 } |
325 | 395 |
| 396 /// Checks the pair-wise equivalence of the constants values in [list1] and |
| 397 /// [list2]. |
| 398 /// |
| 399 /// Uses [object1], [object2] and [property] to provide context for failures. |
| 400 bool checkConstantValueLists( |
| 401 Object object1, Object object2, String property, |
| 402 List<ConstantValue> list1, |
| 403 List<ConstantValue> list2) { |
| 404 return checkListEquivalence( |
| 405 object1, object2, property, |
| 406 list1, list2, checkConstantValues); |
| 407 } |
326 | 408 |
327 /// Check member property equivalence between all members common to [compiler1] | 409 /// Check member property equivalence between all members common to [compiler1] |
328 /// and [compiler2]. | 410 /// and [compiler2]. |
329 void checkLoadedLibraryMembers( | 411 void checkLoadedLibraryMembers( |
330 Compiler compiler1, | 412 Compiler compiler1, |
331 Compiler compiler2, | 413 Compiler compiler2, |
332 bool hasProperty(Element member1), | 414 bool hasProperty(Element member1), |
333 void checkMemberProperties(Compiler compiler1, Element member1, | 415 void checkMemberProperties(Compiler compiler1, Element member1, |
334 Compiler compiler2, Element member2, | 416 Compiler compiler2, Element member2, |
335 {bool verbose}), | 417 {bool verbose}), |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 } | 514 } |
433 | 515 |
434 void checkSets( | 516 void checkSets( |
435 Iterable set1, | 517 Iterable set1, |
436 Iterable set2, | 518 Iterable set2, |
437 String messagePrefix, | 519 String messagePrefix, |
438 bool sameElement(a, b), | 520 bool sameElement(a, b), |
439 {bool failOnUnfound: true, | 521 {bool failOnUnfound: true, |
440 bool failOnExtra: true, | 522 bool failOnExtra: true, |
441 bool verbose: false, | 523 bool verbose: false, |
442 void onSameElement(a, b)}) { | 524 void onSameElement(a, b), |
| 525 void onUnfoundElement(a), |
| 526 void onExtraElement(b), |
| 527 String elementToString(key): defaultToString}) { |
443 List<List> common = <List>[]; | 528 List<List> common = <List>[]; |
444 List unfound = []; | 529 List unfound = []; |
445 Set remaining = computeSetDifference( | 530 Set remaining = computeSetDifference( |
446 set1, set2, common, unfound, | 531 set1, set2, common, unfound, |
447 sameElement: sameElement, | 532 sameElement: sameElement, |
448 checkElements: onSameElement); | 533 checkElements: onSameElement); |
| 534 if (onUnfoundElement != null) { |
| 535 unfound.forEach(onUnfoundElement); |
| 536 } |
| 537 if (onExtraElement != null) { |
| 538 remaining.forEach(onExtraElement); |
| 539 } |
449 StringBuffer sb = new StringBuffer(); | 540 StringBuffer sb = new StringBuffer(); |
450 sb.write("$messagePrefix:"); | 541 sb.write("$messagePrefix:"); |
451 if (verbose) { | 542 if (verbose) { |
452 sb.write("\n Common:\n ${common.join('\n ')}"); | 543 sb.write("\n Common: \n"); |
| 544 for (List pair in common) { |
| 545 var element1 = pair[0]; |
| 546 var element2 = pair[1]; |
| 547 sb.write(" [${elementToString(element1)}," |
| 548 "${elementToString(element2)}]\n"); |
| 549 } |
453 } | 550 } |
454 if (unfound.isNotEmpty || verbose) { | 551 if (unfound.isNotEmpty || verbose) { |
455 sb.write("\n Unfound:\n ${unfound.join('\n ')}"); | 552 sb.write("\n Unfound:\n ${unfound.map(elementToString).join('\n ')}"); |
456 } | 553 } |
457 if (remaining.isNotEmpty || verbose) { | 554 if (remaining.isNotEmpty || verbose) { |
458 sb.write("\n Extra: \n ${remaining.join('\n ')}"); | 555 sb.write("\n Extra: \n ${remaining.map(elementToString).join('\n ')}"); |
459 } | 556 } |
460 String message = sb.toString(); | 557 String message = sb.toString(); |
461 if (unfound.isNotEmpty || remaining.isNotEmpty) { | 558 if (unfound.isNotEmpty || remaining.isNotEmpty) { |
462 | |
463 if ((failOnUnfound && unfound.isNotEmpty) || | 559 if ((failOnUnfound && unfound.isNotEmpty) || |
464 (failOnExtra && remaining.isNotEmpty)) { | 560 (failOnExtra && remaining.isNotEmpty)) { |
465 Expect.fail(message); | 561 Expect.fail(message); |
466 } else { | 562 } else { |
467 print(message); | 563 print(message); |
468 } | 564 } |
469 } else if (verbose) { | 565 } else if (verbose) { |
470 print(message); | 566 print(message); |
471 } | 567 } |
472 } | 568 } |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
613 } | 709 } |
614 | 710 |
615 if (index == 1 && skip != 0) { | 711 if (index == 1 && skip != 0) { |
616 return ['${skip}', segmentNumber(index)]; | 712 return ['${skip}', segmentNumber(index)]; |
617 } else if (index == count) { | 713 } else if (index == count) { |
618 return [segmentNumber(index - 1)]; | 714 return [segmentNumber(index - 1)]; |
619 } else { | 715 } else { |
620 return [segmentNumber(index - 1), segmentNumber(index)]; | 716 return [segmentNumber(index - 1), segmentNumber(index)]; |
621 } | 717 } |
622 } | 718 } |
OLD | NEW |