| 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:io'; | 7 import 'dart:io'; |
| 8 import '../memory_compiler.dart'; | 8 import '../memory_compiler.dart'; |
| 9 import 'package:async_helper/async_helper.dart'; | 9 import 'package:async_helper/async_helper.dart'; |
| 10 import 'package:compiler/src/common/resolution.dart'; | 10 import 'package:compiler/src/common/resolution.dart'; |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 /// [elementEquivalence] to determine element equivalence. | 156 /// [elementEquivalence] to determine element equivalence. |
| 157 /// | 157 /// |
| 158 /// Elements both in [set1] and [set2] are added to [common], elements in [set1] | 158 /// Elements both in [set1] and [set2] are added to [common], elements in [set1] |
| 159 /// but not in [set2] are added to [unfound], and the set of elements in [set2] | 159 /// but not in [set2] are added to [unfound], and the set of elements in [set2] |
| 160 /// but not in [set1] are returned. | 160 /// but not in [set1] are returned. |
| 161 Set computeSetDifference( | 161 Set computeSetDifference( |
| 162 Iterable set1, | 162 Iterable set1, |
| 163 Iterable set2, | 163 Iterable set2, |
| 164 List common, | 164 List common, |
| 165 List unfound, | 165 List unfound, |
| 166 [bool sameElement(a, b) = equality]) { | 166 {bool sameElement(a, b): equality, |
| 167 void checkElements(a, b)}) { |
| 167 // TODO(johnniwinther): Avoid the quadratic cost here. Some ideas: | 168 // TODO(johnniwinther): Avoid the quadratic cost here. Some ideas: |
| 168 // - convert each set to a list and sort it first, then compare by walking | 169 // - convert each set to a list and sort it first, then compare by walking |
| 169 // both lists in parallel | 170 // both lists in parallel |
| 170 // - map each element to a canonical object, create a map containing those | 171 // - map each element to a canonical object, create a map containing those |
| 171 // mappings, use the mapped sets to compare (then operations like | 172 // mappings, use the mapped sets to compare (then operations like |
| 172 // set.difference would work) | 173 // set.difference would work) |
| 173 Set remaining = set2.toSet(); | 174 Set remaining = set2.toSet(); |
| 174 for (var element1 in set1) { | 175 for (var element1 in set1) { |
| 175 bool found = false; | 176 bool found = false; |
| 176 for (var element2 in remaining) { | 177 for (var element2 in remaining) { |
| 177 if (sameElement(element1, element2)) { | 178 if (sameElement(element1, element2)) { |
| 179 if (checkElements != null) { |
| 180 checkElements(element1, element2); |
| 181 } |
| 178 found = true; | 182 found = true; |
| 179 remaining.remove(element2); | 183 remaining.remove(element2); |
| 180 break; | 184 break; |
| 181 } | 185 } |
| 182 } | 186 } |
| 183 if (found) { | 187 if (found) { |
| 184 common.add(element1); | 188 common.add(element1); |
| 185 } else { | 189 } else { |
| 186 unfound.add(element1); | 190 unfound.add(element1); |
| 187 } | 191 } |
| 188 } | 192 } |
| 189 return remaining; | 193 return remaining; |
| 190 } | 194 } |
| 191 | 195 |
| 192 /// Check equivalence of the two iterables, [set1] and [set1], as sets using | 196 /// Check equivalence of the two iterables, [set1] and [set1], as sets using |
| 193 /// [elementEquivalence] to compute the pair-wise equivalence. | 197 /// [elementEquivalence] to compute the pair-wise equivalence. |
| 194 /// | 198 /// |
| 195 /// Uses [object1], [object2] and [property] to provide context for failures. | 199 /// Uses [object1], [object2] and [property] to provide context for failures. |
| 196 bool checkSetEquivalence( | 200 bool checkSetEquivalence( |
| 197 var object1, | 201 var object1, |
| 198 var object2, | 202 var object2, |
| 199 String property, | 203 String property, |
| 200 Iterable set1, | 204 Iterable set1, |
| 201 Iterable set2, | 205 Iterable set2, |
| 202 bool sameElement(a, b)) { | 206 bool sameElement(a, b), |
| 207 {void onSameElement(a, b)}) { |
| 203 List common = []; | 208 List common = []; |
| 204 List unfound = []; | 209 List unfound = []; |
| 205 Set remaining = | 210 Set remaining = |
| 206 computeSetDifference(set1, set2, common, unfound, sameElement); | 211 computeSetDifference(set1, set2, common, unfound, |
| 212 sameElement: sameElement, checkElements: onSameElement); |
| 207 if (unfound.isNotEmpty || remaining.isNotEmpty) { | 213 if (unfound.isNotEmpty || remaining.isNotEmpty) { |
| 208 String message = | 214 String message = |
| 209 "Set mismatch for `$property` on $object1 vs $object2: \n" | 215 "Set mismatch for `$property` on $object1 vs $object2: \n" |
| 210 "Common:\n ${common.join('\n ')}\n" | 216 "Common:\n ${common.join('\n ')}\n" |
| 211 "Unfound:\n ${unfound.join('\n ')}\n" | 217 "Unfound:\n ${unfound.join('\n ')}\n" |
| 212 "Extra: \n ${remaining.join('\n ')}"; | 218 "Extra: \n ${remaining.join('\n ')}"; |
| 213 throw message; | 219 throw message; |
| 214 } | 220 } |
| 215 return true; | 221 return true; |
| 216 } | 222 } |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 | 386 |
| 381 if (impact1 == null) { | 387 if (impact1 == null) { |
| 382 throw 'Missing impact for $member1. $member2 has $impact2'; | 388 throw 'Missing impact for $member1. $member2 has $impact2'; |
| 383 } | 389 } |
| 384 if (impact2 == null) { | 390 if (impact2 == null) { |
| 385 throw 'Missing impact for $member2. $member1 has $impact1'; | 391 throw 'Missing impact for $member2. $member1 has $impact1'; |
| 386 } | 392 } |
| 387 | 393 |
| 388 testResolutionImpactEquivalence(impact1, impact2, const CheckStrategy()); | 394 testResolutionImpactEquivalence(impact1, impact2, const CheckStrategy()); |
| 389 } | 395 } |
| OLD | NEW |