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 import 'dart:math' as Math; | 10 import 'dart:math' as Math; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 * Generate a bunch of random collections (including Maps), and test that | 71 * Generate a bunch of random collections (including Maps), and test that |
72 * there string form is as expected. The collections include collections | 72 * there string form is as expected. The collections include collections |
73 * as elements, keys, and values, and include recursive references. | 73 * as elements, keys, and values, and include recursive references. |
74 * | 74 * |
75 * This test restricts itself to collections with well-defined iteration | 75 * This test restricts itself to collections with well-defined iteration |
76 * orders (i.e., no HashSet, HashMap). | 76 * orders (i.e., no HashSet, HashMap). |
77 */ | 77 */ |
78 void exactTest() { | 78 void exactTest() { |
79 for (int i = 0; i < NUM_TESTS; i++) { | 79 for (int i = 0; i < NUM_TESTS; i++) { |
80 // Choose a size from 0 to MAX_COLLECTION_SIZE, favoring larger sizes | 80 // Choose a size from 0 to MAX_COLLECTION_SIZE, favoring larger sizes |
81 int size = Math.sqrt(random(MAX_COLLECTION_SIZE * MAX_COLLECTION_SIZE)).toIn
t(); | 81 int size = Math.sqrt(random(MAX_COLLECTION_SIZE * MAX_COLLECTION_SIZE)).trun
cate(); |
82 | 82 |
83 StringBuffer stringRep = new StringBuffer(); | 83 StringBuffer stringRep = new StringBuffer(); |
84 Object o = randomCollection(size, stringRep, exact:true); | 84 Object o = randomCollection(size, stringRep, exact:true); |
85 Expect.equals(o.toString(), stringRep.toString()); | 85 Expect.equals(o.toString(), stringRep.toString()); |
86 } | 86 } |
87 } | 87 } |
88 | 88 |
89 /** | 89 /** |
90 * Generate a bunch of random collections (including Maps), and test that | 90 * Generate a bunch of random collections (including Maps), and test that |
91 * there string form is as expected. The collections include collections | 91 * there string form is as expected. The collections include collections |
92 * as elements, keys, and values, and include recursive references. | 92 * as elements, keys, and values, and include recursive references. |
93 * | 93 * |
94 * This test includes collections with ill-defined iteration orders (i.e., | 94 * This test includes collections with ill-defined iteration orders (i.e., |
95 * HashSet, HashMap). As a consequence, it can't use equality tests on the | 95 * HashSet, HashMap). As a consequence, it can't use equality tests on the |
96 * string form. Instead, it performs equality tests on their "alphagrams." | 96 * string form. Instead, it performs equality tests on their "alphagrams." |
97 * This might allow false positives, but it does give a fair amount of | 97 * This might allow false positives, but it does give a fair amount of |
98 * confidence. | 98 * confidence. |
99 */ | 99 */ |
100 void inexactTest() { | 100 void inexactTest() { |
101 for (int i = 0; i < NUM_TESTS; i++) { | 101 for (int i = 0; i < NUM_TESTS; i++) { |
102 // Choose a size from 0 to MAX_COLLECTION_SIZE, favoring larger sizes | 102 // Choose a size from 0 to MAX_COLLECTION_SIZE, favoring larger sizes |
103 int size = Math.sqrt(random(MAX_COLLECTION_SIZE * MAX_COLLECTION_SIZE)).toIn
t(); | 103 int size = Math.sqrt(random(MAX_COLLECTION_SIZE * MAX_COLLECTION_SIZE)).trun
cate(); |
104 | 104 |
105 StringBuffer stringRep = new StringBuffer(); | 105 StringBuffer stringRep = new StringBuffer(); |
106 Object o = randomCollection(size, stringRep, exact:false); | 106 Object o = randomCollection(size, stringRep, exact:false); |
107 Expect.equals(alphagram(o.toString()), alphagram(stringRep.toString())); | 107 Expect.equals(alphagram(o.toString()), alphagram(stringRep.toString())); |
108 } | 108 } |
109 } | 109 } |
110 | 110 |
111 /** | 111 /** |
112 * Return a random collection (or Map) of the specified size, placing its | 112 * Return a random collection (or Map) of the specified size, placing its |
113 * string representation into the given string buffer. | 113 * string representation into the given string buffer. |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 bool randomBool() { | 294 bool randomBool() { |
295 return rand.nextBool(); | 295 return rand.nextBool(); |
296 } | 296 } |
297 | 297 |
298 /** Returns the alphabetized characters in a string. */ | 298 /** Returns the alphabetized characters in a string. */ |
299 String alphagram(String s) { | 299 String alphagram(String s) { |
300 List<int> chars = s.charCodes; | 300 List<int> chars = s.charCodes; |
301 chars.sort((int a, int b) => a - b); | 301 chars.sort((int a, int b) => a - b); |
302 return new String.fromCharCodes(chars); | 302 return new String.fromCharCodes(chars); |
303 } | 303 } |
OLD | NEW |