| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** | 5 /** |
| 6 * Tests for the toString methods on collections and maps. | 6 * Tests for the toString methods on collections and maps. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 library collection_to_string; | 9 library collection_to_string; |
| 10 | 10 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 /** | 165 /** |
| 166 * Return a random List of the specified size, placing its string | 166 * Return a random List of the specified size, placing its string |
| 167 * representation into the given string buffer. The beingMade | 167 * representation into the given string buffer. The beingMade |
| 168 * parameter is a list of collections currently under construction, i.e., | 168 * parameter is a list of collections currently under construction, i.e., |
| 169 * candidates for recursive references. | 169 * candidates for recursive references. |
| 170 * | 170 * |
| 171 * If exact is true, the returned collections will not be, and will not contain | 171 * If exact is true, the returned collections will not be, and will not contain |
| 172 * a collection with ill-defined iteration order (i.e., a HashSet or HashMap). | 172 * a collection with ill-defined iteration order (i.e., a HashSet or HashMap). |
| 173 */ | 173 */ |
| 174 List randomList(int size, bool exact, StringBuffer stringRep, List beingMade) { | 174 List randomList(int size, bool exact, StringBuffer stringRep, List beingMade) { |
| 175 return populateRandomCollection(size, exact, stringRep, beingMade, []); | 175 return populateRandomCollection(size, exact, stringRep, beingMade, [], "[]"); |
| 176 } | 176 } |
| 177 | 177 |
| 178 /** | 178 /** |
| 179 * Like randomList, but returns a queue. | 179 * Like randomList, but returns a queue. |
| 180 */ | 180 */ |
| 181 Queue randomQueue(int size, bool exact, StringBuffer stringRep, List beingMade){ | 181 Queue randomQueue(int size, bool exact, StringBuffer stringRep, List beingMade){ |
| 182 return populateRandomCollection(size, exact, stringRep, beingMade, new Queue()
); | 182 return populateRandomCollection( |
| 183 size, exact, stringRep, beingMade, new Queue(), "{}"); |
| 183 } | 184 } |
| 184 | 185 |
| 185 /** | 186 /** |
| 186 * Like randomList, but returns a Set. | 187 * Like randomList, but returns a Set. |
| 187 */ | 188 */ |
| 188 Set randomSet(int size, bool exact, StringBuffer stringRep, List beingMade) { | 189 Set randomSet(int size, bool exact, StringBuffer stringRep, List beingMade) { |
| 189 // Until we have LinkedHashSet, method will only be called with exact==true | 190 // Until we have LinkedHashSet, method will only be called with exact==true |
| 190 return populateRandomSet(size, exact, stringRep, beingMade, new Set()); | 191 return populateRandomSet(size, exact, stringRep, beingMade, new Set()); |
| 191 } | 192 } |
| 192 | 193 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 207 * Populates the given empty collection with elements, emitting the string | 208 * Populates the given empty collection with elements, emitting the string |
| 208 * representation of the collection to stringRep. The beingMade parameter is | 209 * representation of the collection to stringRep. The beingMade parameter is |
| 209 * a list of collections currently under construction, i.e., candidates for | 210 * a list of collections currently under construction, i.e., candidates for |
| 210 * recursive references. | 211 * recursive references. |
| 211 * | 212 * |
| 212 * If exact is true, the elements of the returned collections will not be, | 213 * If exact is true, the elements of the returned collections will not be, |
| 213 * and will not contain a collection with ill-defined iteration order | 214 * and will not contain a collection with ill-defined iteration order |
| 214 * (i.e., a HashSet or HashMap). | 215 * (i.e., a HashSet or HashMap). |
| 215 */ | 216 */ |
| 216 populateRandomCollection(int size, bool exact, | 217 populateRandomCollection(int size, bool exact, |
| 217 StringBuffer stringRep, List beingMade, var coll) { | 218 StringBuffer stringRep, List beingMade, var coll, String delimiters) { |
| 218 beingMade.add(coll); | 219 beingMade.add(coll); |
| 219 String delimiters = "()"; // Default for iterables. | |
| 220 if (coll is List) { | |
| 221 delimiters = "[]"; | |
| 222 } else if (coll is Set) { | |
| 223 delimiters = "{}"; | |
| 224 } | |
| 225 int start = stringRep.length; | 220 int start = stringRep.length; |
| 226 | 221 |
| 227 stringRep.write(delimiters[0]); | 222 stringRep.write(delimiters[0]); |
| 228 | 223 |
| 229 List indices = []; | 224 List indices = []; |
| 230 for (int i = 0; i < size; i++) { | 225 for (int i = 0; i < size; i++) { |
| 231 indices.add(stringRep.length); | 226 indices.add(stringRep.length); |
| 232 if (i != 0) stringRep.write(', '); | 227 if (i != 0) stringRep.write(', '); |
| 233 coll.add(randomElement(random(size), exact, stringRep, beingMade)); | 228 coll.add(randomElement(random(size), exact, stringRep, beingMade)); |
| 234 } | 229 } |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 result = random(1000); | 330 result = random(1000); |
| 336 stringRep.write(result); | 331 stringRep.write(result); |
| 337 } else if (elementTypeFrac < 2/3) { | 332 } else if (elementTypeFrac < 2/3) { |
| 338 // Element Is a random (new) collection | 333 // Element Is a random (new) collection |
| 339 result = randomCollectionHelper(size, exact, stringRep, beingMade); | 334 result = randomCollectionHelper(size, exact, stringRep, beingMade); |
| 340 } else { | 335 } else { |
| 341 // Element Is a random recursive ref | 336 // Element Is a random recursive ref |
| 342 result = beingMade[random(beingMade.length)]; | 337 result = beingMade[random(beingMade.length)]; |
| 343 if (result is List) { | 338 if (result is List) { |
| 344 stringRep.write('[...]'); | 339 stringRep.write('[...]'); |
| 345 } else if (result is Set || result is Map) { | 340 } else if (result is Set || result is Map || result is Queue) { |
| 346 stringRep.write('{...}'); | 341 stringRep.write('{...}'); |
| 347 } else { | 342 } else { |
| 348 stringRep.write('(...)'); | 343 stringRep.write('(...)'); |
| 349 } | 344 } |
| 350 } | 345 } |
| 351 return result; | 346 return result; |
| 352 } | 347 } |
| 353 | 348 |
| 354 /** Returns a random int on [0, max) */ | 349 /** Returns a random int on [0, max) */ |
| 355 int random(int max) { | 350 int random(int max) { |
| 356 return rand.nextInt(max); | 351 return rand.nextInt(max); |
| 357 } | 352 } |
| 358 | 353 |
| 359 /** Returns a random boolean value. */ | 354 /** Returns a random boolean value. */ |
| 360 bool randomBool() { | 355 bool randomBool() { |
| 361 return rand.nextBool(); | 356 return rand.nextBool(); |
| 362 } | 357 } |
| 363 | 358 |
| 364 /** Returns the alphabetized characters in a string. */ | 359 /** Returns the alphabetized characters in a string. */ |
| 365 String alphagram(String s) { | 360 String alphagram(String s) { |
| 366 // Calling [toList] to convert unmodifiable list to normal list. | 361 // Calling [toList] to convert unmodifiable list to normal list. |
| 367 List<int> chars = s.codeUnits.toList(); | 362 List<int> chars = s.codeUnits.toList(); |
| 368 chars.sort((int a, int b) => a - b); | 363 chars.sort((int a, int b) => a - b); |
| 369 return new String.fromCharCodes(chars); | 364 return new String.fromCharCodes(chars); |
| 370 } | 365 } |
| OLD | NEW |