| 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 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 * a list of collections currently under construction, i.e., candidates for | 202 * a list of collections currently under construction, i.e., candidates for |
| 203 * recursive references. | 203 * recursive references. |
| 204 * | 204 * |
| 205 * If exact is true, the elements of the returned collections will not be, | 205 * If exact is true, the elements of the returned collections will not be, |
| 206 * and will not contain a collection with ill-defined iteration order | 206 * and will not contain a collection with ill-defined iteration order |
| 207 * (i.e., a HashSet or HashMap). | 207 * (i.e., a HashSet or HashMap). |
| 208 */ | 208 */ |
| 209 Collection populateRandomCollection(int size, bool exact, | 209 Collection populateRandomCollection(int size, bool exact, |
| 210 StringBuffer stringRep, List beingMade, Collection coll) { | 210 StringBuffer stringRep, List beingMade, Collection coll) { |
| 211 beingMade.add(coll); | 211 beingMade.add(coll); |
| 212 stringRep.add(coll is List ? '[' : '{'); | 212 stringRep.write(coll is List ? '[' : '{'); |
| 213 | 213 |
| 214 for (int i = 0; i < size; i++) { | 214 for (int i = 0; i < size; i++) { |
| 215 if (i != 0) stringRep.add(', '); | 215 if (i != 0) stringRep.write(', '); |
| 216 coll.add(randomElement(random(size), exact, stringRep, beingMade)); | 216 coll.add(randomElement(random(size), exact, stringRep, beingMade)); |
| 217 } | 217 } |
| 218 | 218 |
| 219 stringRep.add(coll is List ? ']' : '}'); | 219 stringRep.write(coll is List ? ']' : '}'); |
| 220 beingMade.removeLast(); | 220 beingMade.removeLast(); |
| 221 return coll; | 221 return coll; |
| 222 } | 222 } |
| 223 | 223 |
| 224 /** Like populateRandomCollection, but for sets (elements must be hashable) */ | 224 /** Like populateRandomCollection, but for sets (elements must be hashable) */ |
| 225 Set populateRandomSet(int size, bool exact, StringBuffer stringRep, | 225 Set populateRandomSet(int size, bool exact, StringBuffer stringRep, |
| 226 List beingMade, Set set) { | 226 List beingMade, Set set) { |
| 227 stringRep.add('{'); | 227 stringRep.write('{'); |
| 228 | 228 |
| 229 for (int i = 0; i < size; i++) { | 229 for (int i = 0; i < size; i++) { |
| 230 if (i != 0) stringRep.add(', '); | 230 if (i != 0) stringRep.write(', '); |
| 231 set.add(i); | 231 set.add(i); |
| 232 stringRep.add(i); | 232 stringRep.write(i); |
| 233 } | 233 } |
| 234 | 234 |
| 235 stringRep.add('}'); | 235 stringRep.write('}'); |
| 236 return set; | 236 return set; |
| 237 } | 237 } |
| 238 | 238 |
| 239 | 239 |
| 240 /** Like populateRandomCollection, but for maps. */ | 240 /** Like populateRandomCollection, but for maps. */ |
| 241 Map populateRandomMap(int size, bool exact, StringBuffer stringRep, | 241 Map populateRandomMap(int size, bool exact, StringBuffer stringRep, |
| 242 List beingMade, Map map) { | 242 List beingMade, Map map) { |
| 243 beingMade.add(map); | 243 beingMade.add(map); |
| 244 stringRep.add('{'); | 244 stringRep.write('{'); |
| 245 | 245 |
| 246 for (int i = 0; i < size; i++) { | 246 for (int i = 0; i < size; i++) { |
| 247 if (i != 0) stringRep.add(', '); | 247 if (i != 0) stringRep.write(', '); |
| 248 | 248 |
| 249 int key = i; // Ensures no duplicates | 249 int key = i; // Ensures no duplicates |
| 250 stringRep.add(key); | 250 stringRep.write(key); |
| 251 stringRep.add(': '); | 251 stringRep.write(': '); |
| 252 Object val = randomElement(random(size), exact, stringRep, beingMade); | 252 Object val = randomElement(random(size), exact, stringRep, beingMade); |
| 253 map[key] = val; | 253 map[key] = val; |
| 254 } | 254 } |
| 255 | 255 |
| 256 stringRep.add('}'); | 256 stringRep.write('}'); |
| 257 beingMade.removeLast(); | 257 beingMade.removeLast(); |
| 258 return map; | 258 return map; |
| 259 } | 259 } |
| 260 | 260 |
| 261 /** | 261 /** |
| 262 * Generates a random element which can be an int, a collection, or a map, | 262 * Generates a random element which can be an int, a collection, or a map, |
| 263 * and emits it to StringRep. The beingMade parameter is a list of collections | 263 * and emits it to StringRep. The beingMade parameter is a list of collections |
| 264 * currently under construction, i.e., candidates for recursive references. | 264 * currently under construction, i.e., candidates for recursive references. |
| 265 * | 265 * |
| 266 * If exact is true, the returned element will not be, and will not contain | 266 * If exact is true, the returned element will not be, and will not contain |
| 267 * a collection with ill-defined iteration order (i.e., a HashSet or HashMap). | 267 * a collection with ill-defined iteration order (i.e., a HashSet or HashMap). |
| 268 */ | 268 */ |
| 269 Object randomElement(int size, bool exact, StringBuffer stringRep, | 269 Object randomElement(int size, bool exact, StringBuffer stringRep, |
| 270 List beingMade) { | 270 List beingMade) { |
| 271 Object result; | 271 Object result; |
| 272 double elementTypeFrac = rand.nextDouble(); | 272 double elementTypeFrac = rand.nextDouble(); |
| 273 if (elementTypeFrac < 1/3) { | 273 if (elementTypeFrac < 1/3) { |
| 274 result = random(1000); | 274 result = random(1000); |
| 275 stringRep.add(result); | 275 stringRep.write(result); |
| 276 } else if (elementTypeFrac < 2/3) { | 276 } else if (elementTypeFrac < 2/3) { |
| 277 // Element Is a random (new) collection | 277 // Element Is a random (new) collection |
| 278 result = randomCollectionHelper(size, exact, stringRep, beingMade); | 278 result = randomCollectionHelper(size, exact, stringRep, beingMade); |
| 279 } else { | 279 } else { |
| 280 // Element Is a random recursive ref | 280 // Element Is a random recursive ref |
| 281 result = beingMade[random(beingMade.length)]; | 281 result = beingMade[random(beingMade.length)]; |
| 282 if (result is List) | 282 if (result is List) |
| 283 stringRep.add('[...]'); | 283 stringRep.write('[...]'); |
| 284 else | 284 else |
| 285 stringRep.add('{...}'); | 285 stringRep.write('{...}'); |
| 286 } | 286 } |
| 287 return result; | 287 return result; |
| 288 } | 288 } |
| 289 | 289 |
| 290 /** Returns a random int on [0, max) */ | 290 /** Returns a random int on [0, max) */ |
| 291 int random(int max) { | 291 int random(int max) { |
| 292 return rand.nextInt(max); | 292 return rand.nextInt(max); |
| 293 } | 293 } |
| 294 | 294 |
| 295 /** Returns a random boolean value. */ | 295 /** Returns a random boolean value. */ |
| 296 bool randomBool() { | 296 bool randomBool() { |
| 297 return rand.nextBool(); | 297 return rand.nextBool(); |
| 298 } | 298 } |
| 299 | 299 |
| 300 /** Returns the alphabetized characters in a string. */ | 300 /** Returns the alphabetized characters in a string. */ |
| 301 String alphagram(String s) { | 301 String alphagram(String s) { |
| 302 // Calling [toList] to convert unmodifiable list to normal list. | 302 // Calling [toList] to convert unmodifiable list to normal list. |
| 303 List<int> chars = s.codeUnits.toList(); | 303 List<int> chars = s.codeUnits.toList(); |
| 304 chars.sort((int a, int b) => a - b); | 304 chars.sort((int a, int b) => a - b); |
| 305 return new String.fromCharCodes(chars); | 305 return new String.fromCharCodes(chars); |
| 306 } | 306 } |
| OLD | NEW |