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 // TODO(jjb): seed random number generator when API allows it | 9 // TODO(jjb): seed random number generator when API allows it |
10 | 10 |
11 final int NUM_TESTS = 3000; | 11 final int NUM_TESTS = 3000; |
12 final int MAX_COLLECTION_SIZE = 6; | 12 final int MAX_COLLECTION_SIZE = 7; |
13 | 13 |
14 main() { | 14 main() { |
15 smokeTest(); | 15 smokeTest(); |
16 exactTest(); | 16 exactTest(); |
17 inexactTest(); | 17 inexactTest(); |
18 } | 18 } |
19 | 19 |
20 | 20 |
21 /** | 21 /** |
22 * Test a few simple examples. | 22 * Test a few simple examples. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 * Generate a bunch of random collections (including Maps), and test that | 61 * Generate a bunch of random collections (including Maps), and test that |
62 * there string form is as expected. The collections include collections | 62 * there string form is as expected. The collections include collections |
63 * as elements, keys, and values, and include recursive references. | 63 * as elements, keys, and values, and include recursive references. |
64 * | 64 * |
65 * This test restricts itself to collections with well-defined iteration | 65 * This test restricts itself to collections with well-defined iteration |
66 * orders (i.e., no HashSet, HashMap). | 66 * orders (i.e., no HashSet, HashMap). |
67 */ | 67 */ |
68 void exactTest() { | 68 void exactTest() { |
69 for (int i = 0; i < NUM_TESTS; i++) { | 69 for (int i = 0; i < NUM_TESTS; i++) { |
70 // Choose a size from 0 to MAX_COLLECTION_SIZE, favoring larger sizes | 70 // Choose a size from 0 to MAX_COLLECTION_SIZE, favoring larger sizes |
71 float sqrtSize = Math.sqrt(Math.random() * (MAX_COLLECTION_SIZE + 1)); | 71 int size = Math.sqrt(random(MAX_COLLECTION_SIZE * MAX_COLLECTION_SIZE)).toIn
t(); |
72 int size = (sqrtSize * sqrtSize).toInt(); | 72 |
73 | |
74 StringBuffer stringRep = new StringBuffer(); | 73 StringBuffer stringRep = new StringBuffer(); |
75 Object o = randomCollection(size, stringRep, exact:true); | 74 Object o = randomCollection(size, stringRep, exact:true); |
76 Expect.equals(o.toString(), stringRep.toString()); | 75 Expect.equals(o.toString(), stringRep.toString()); |
77 } | 76 } |
78 } | 77 } |
79 | 78 |
80 /** | 79 /** |
81 * Generate a bunch of random collections (including Maps), and test that | 80 * Generate a bunch of random collections (including Maps), and test that |
82 * there string form is as expected. The collections include collections | 81 * there string form is as expected. The collections include collections |
83 * as elements, keys, and values, and include recursive references. | 82 * as elements, keys, and values, and include recursive references. |
84 * | 83 * |
85 * This test includes collections with ill-defined iteration orders (i.e., | 84 * This test includes collections with ill-defined iteration orders (i.e., |
86 * HashSet, HashMap). As a consequence, it can't use equality tests on the | 85 * HashSet, HashMap). As a consequence, it can't use equality tests on the |
87 * string form. Instead, it performs equality tests on their "alphagrams." | 86 * string form. Instead, it performs equality tests on their "alphagrams." |
88 * This might allow false positives, but it does give a fair amount of | 87 * This might allow false positives, but it does give a fair amount of |
89 * confidence. | 88 * confidence. |
90 */ | 89 */ |
91 void inexactTest() { | 90 void inexactTest() { |
92 for (int i = 0; i < NUM_TESTS; i++) { | 91 for (int i = 0; i < NUM_TESTS; i++) { |
93 // Choose a size from 0 to MAX_COLLECTION_SIZE, favoring larger sizes | 92 // Choose a size from 0 to MAX_COLLECTION_SIZE, favoring larger sizes |
94 float sqrtSize = Math.sqrt(Math.random() * (MAX_COLLECTION_SIZE + 1)); | 93 int size = Math.sqrt(random(MAX_COLLECTION_SIZE * MAX_COLLECTION_SIZE)).toIn
t(); |
95 int size = (sqrtSize * sqrtSize).toInt(); | 94 |
96 | |
97 StringBuffer stringRep = new StringBuffer(); | 95 StringBuffer stringRep = new StringBuffer(); |
98 Object o = randomCollection(size, stringRep, exact:false); | 96 Object o = randomCollection(size, stringRep, exact:false); |
99 Expect.equals(alphagram(o.toString()), alphagram(stringRep.toString())); | 97 Expect.equals(alphagram(o.toString()), alphagram(stringRep.toString())); |
100 } | 98 } |
101 } | 99 } |
102 | 100 |
103 /** | 101 /** |
104 * Return a random collection (or Map) of the specified size, placing its | 102 * Return a random collection (or Map) of the specified size, placing its |
105 * string representation into the given string buffer. | 103 * string representation into the given string buffer. |
106 * | 104 * |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 bool randomBool() { | 284 bool randomBool() { |
287 return Math.random() < .5; | 285 return Math.random() < .5; |
288 } | 286 } |
289 | 287 |
290 /** Returns the alphabetized characters in a string. */ | 288 /** Returns the alphabetized characters in a string. */ |
291 String alphagram(String s) { | 289 String alphagram(String s) { |
292 List<int> chars = s.charCodes(); | 290 List<int> chars = s.charCodes(); |
293 chars.sort((int a, int b) => a - b); | 291 chars.sort((int a, int b) => a - b); |
294 return new String.fromCharCodes(chars); | 292 return new String.fromCharCodes(chars); |
295 } | 293 } |
OLD | NEW |