| 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 (including maps). | 6 * Tests for the toString methods on collections (including maps). |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #library('collection_to_string'); |
| 10 #import('dart:math', prefix: 'Math'); |
| 11 |
| 9 // TODO(jjb): seed random number generator when API allows it | 12 // TODO(jjb): seed random number generator when API allows it |
| 10 | 13 |
| 11 final int NUM_TESTS = 300; | 14 final int NUM_TESTS = 300; |
| 12 final int MAX_COLLECTION_SIZE = 7; | 15 final int MAX_COLLECTION_SIZE = 7; |
| 13 | 16 |
| 17 Math.Random rand; |
| 18 |
| 14 main() { | 19 main() { |
| 20 rand = new Math.Random(); |
| 15 smokeTest(); | 21 smokeTest(); |
| 16 exactTest(); | 22 exactTest(); |
| 17 inexactTest(); | 23 inexactTest(); |
| 18 } | 24 } |
| 19 | 25 |
| 20 | 26 |
| 21 /** | 27 /** |
| 22 * Test a few simple examples. | 28 * Test a few simple examples. |
| 23 */ | 29 */ |
| 24 void smokeTest() { | 30 void smokeTest() { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 * Return a random collection (or map) of the specified size, placing its | 123 * Return a random collection (or map) of the specified size, placing its |
| 118 * string representation into the given string buffer. The beingMade | 124 * string representation into the given string buffer. The beingMade |
| 119 * parameter is a list of collections currently under construction, i.e., | 125 * parameter is a list of collections currently under construction, i.e., |
| 120 * candidates for recursive references. | 126 * candidates for recursive references. |
| 121 * | 127 * |
| 122 * If exact is true, the returned collections will not be, and will not contain | 128 * If exact is true, the returned collections will not be, and will not contain |
| 123 * a collection with ill-defined iteration order (i.e., a HashSet or HashMap). | 129 * a collection with ill-defined iteration order (i.e., a HashSet or HashMap). |
| 124 */ | 130 */ |
| 125 Object randomCollectionHelper(int size, bool exact, StringBuffer stringRep, | 131 Object randomCollectionHelper(int size, bool exact, StringBuffer stringRep, |
| 126 List beingMade) { | 132 List beingMade) { |
| 127 double interfaceFrac = Math.random(); | 133 double interfaceFrac = rand.nextDouble(); |
| 128 | 134 |
| 129 if (exact) { | 135 if (exact) { |
| 130 if (interfaceFrac < 1/3) { | 136 if (interfaceFrac < 1/3) { |
| 131 return randomList(size, exact, stringRep, beingMade); | 137 return randomList(size, exact, stringRep, beingMade); |
| 132 } else if (interfaceFrac < 2/3) { | 138 } else if (interfaceFrac < 2/3) { |
| 133 return randomQueue(size, exact, stringRep, beingMade); | 139 return randomQueue(size, exact, stringRep, beingMade); |
| 134 } else { | 140 } else { |
| 135 return randomMap(size, exact, stringRep, beingMade); | 141 return randomMap(size, exact, stringRep, beingMade); |
| 136 } | 142 } |
| 137 } else { | 143 } else { |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 * Generates a random element which can be an int, a collection, or a map, | 260 * Generates a random element which can be an int, a collection, or a map, |
| 255 * and emits it to StringRep. The beingMade parameter is a list of collections | 261 * and emits it to StringRep. The beingMade parameter is a list of collections |
| 256 * currently under construction, i.e., candidates for recursive references. | 262 * currently under construction, i.e., candidates for recursive references. |
| 257 * | 263 * |
| 258 * If exact is true, the returned element will not be, and will not contain | 264 * If exact is true, the returned element will not be, and will not contain |
| 259 * a collection with ill-defined iteration order (i.e., a HashSet or HashMap). | 265 * a collection with ill-defined iteration order (i.e., a HashSet or HashMap). |
| 260 */ | 266 */ |
| 261 Object randomElement(int size, bool exact, StringBuffer stringRep, | 267 Object randomElement(int size, bool exact, StringBuffer stringRep, |
| 262 List beingMade) { | 268 List beingMade) { |
| 263 Object result; | 269 Object result; |
| 264 double elementTypeFrac = Math.random(); | 270 double elementTypeFrac = rand.nextDouble(); |
| 265 if (elementTypeFrac < 1/3) { | 271 if (elementTypeFrac < 1/3) { |
| 266 result = random(1000); | 272 result = random(1000); |
| 267 stringRep.add(result); | 273 stringRep.add(result); |
| 268 } else if (elementTypeFrac < 2/3) { | 274 } else if (elementTypeFrac < 2/3) { |
| 269 // Element Is a random (new) collection | 275 // Element Is a random (new) collection |
| 270 result = randomCollectionHelper(size, exact, stringRep, beingMade); | 276 result = randomCollectionHelper(size, exact, stringRep, beingMade); |
| 271 } else { | 277 } else { |
| 272 // Element Is a random recursive ref | 278 // Element Is a random recursive ref |
| 273 result = beingMade[random(beingMade.length)]; | 279 result = beingMade[random(beingMade.length)]; |
| 274 if (result is List) | 280 if (result is List) |
| 275 stringRep.add('[...]'); | 281 stringRep.add('[...]'); |
| 276 else | 282 else |
| 277 stringRep.add('{...}'); | 283 stringRep.add('{...}'); |
| 278 } | 284 } |
| 279 return result; | 285 return result; |
| 280 } | 286 } |
| 281 | 287 |
| 282 /** Returns a random int on [0, max) */ | 288 /** Returns a random int on [0, max) */ |
| 283 int random(int max) { | 289 int random(int max) { |
| 284 return (Math.random() * max).toInt(); | 290 return rand.nextInt(max); |
| 285 } | 291 } |
| 286 | 292 |
| 287 /** Returns a random boolean value. */ | 293 /** Returns a random boolean value. */ |
| 288 bool randomBool() { | 294 bool randomBool() { |
| 289 return Math.random() < .5; | 295 return rand.nextBool(); |
| 290 } | 296 } |
| 291 | 297 |
| 292 /** Returns the alphabetized characters in a string. */ | 298 /** Returns the alphabetized characters in a string. */ |
| 293 String alphagram(String s) { | 299 String alphagram(String s) { |
| 294 List<int> chars = s.charCodes(); | 300 List<int> chars = s.charCodes(); |
| 295 chars.sort((int a, int b) => a - b); | 301 chars.sort((int a, int b) => a - b); |
| 296 return new String.fromCharCodes(chars); | 302 return new String.fromCharCodes(chars); |
| 297 } | 303 } |
| OLD | NEW |