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 import 'dart:isolate'; | 5 import 'dart:isolate'; |
6 import 'dart:async'; | 6 import 'dart:async'; |
7 | 7 |
8 class Expect { | 8 class Expect { |
9 static void equals(x, y) { | 9 static void equals(x, y) { |
10 if (x != y) throw new ArgumentError('not equal'); | 10 if (x != y) throw new ArgumentError('not equal'); |
11 } | 11 } |
12 } | 12 } |
13 | 13 |
14 class Fields { | 14 class Fields { |
15 Fields(int i, int j) : fld1 = i, fld2 = j, fld5 = true {} | 15 Fields(int i, int j) |
| 16 : fld1 = i, |
| 17 fld2 = j, |
| 18 fld5 = true {} |
16 int fld1; | 19 int fld1; |
17 final int fld2; | 20 final int fld2; |
18 static int fld3; | 21 static int fld3; |
19 static const int fld4 = 10; | 22 static const int fld4 = 10; |
20 bool fld5; | 23 bool fld5; |
21 } | 24 } |
| 25 |
22 class FieldsTest { | 26 class FieldsTest { |
23 static Fields testMain() { | 27 static Fields testMain() { |
24 Fields obj = new Fields(10, 20); | 28 Fields obj = new Fields(10, 20); |
25 Expect.equals(10, obj.fld1); | 29 Expect.equals(10, obj.fld1); |
26 Expect.equals(20, obj.fld2); | 30 Expect.equals(20, obj.fld2); |
27 Expect.equals(10, Fields.fld4); | 31 Expect.equals(10, Fields.fld4); |
28 Expect.equals(true, obj.fld5); | 32 Expect.equals(true, obj.fld5); |
29 return obj; | 33 return obj; |
30 } | 34 } |
31 } | 35 } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 const LoopBenchmark() : super("Loop"); | 87 const LoopBenchmark() : super("Loop"); |
84 | 88 |
85 void warmup() { | 89 void warmup() { |
86 Loop.loop(10); | 90 Loop.loop(10); |
87 } | 91 } |
88 | 92 |
89 void exercise() { | 93 void exercise() { |
90 // This value has been copied from benchpress.js, so that we can compare | 94 // This value has been copied from benchpress.js, so that we can compare |
91 // performance. | 95 // performance. |
92 var result = Loop.loop(200); | 96 var result = Loop.loop(200); |
93 if (result != 20000) | 97 if (result != 20000) Error.error("Wrong result: $result. Should be: 20000"); |
94 Error.error("Wrong result: $result. Should be: 20000"); | |
95 } | 98 } |
96 | 99 |
97 static void main() { | 100 static void main() { |
98 new LoopBenchmark().report(); | 101 new LoopBenchmark().report(); |
99 } | 102 } |
100 } | 103 } |
101 | 104 |
102 // T o w e r s | 105 // T o w e r s |
103 class TowersDisk { | 106 class TowersDisk { |
104 final int size; | 107 final int size; |
105 TowersDisk next; | 108 TowersDisk next; |
106 | 109 |
107 TowersDisk(size) : this.size = size, next = null {} | 110 TowersDisk(size) |
| 111 : this.size = size, |
| 112 next = null {} |
108 } | 113 } |
109 | 114 |
110 class Towers { | 115 class Towers { |
111 List<TowersDisk> piles; | 116 List<TowersDisk> piles; |
112 int movesDone; | 117 int movesDone; |
113 Towers(int disks) | 118 Towers(int disks) |
114 : piles = new List<TowersDisk>(3), movesDone = 0 { | 119 : piles = new List<TowersDisk>(3), |
| 120 movesDone = 0 { |
115 build(0, disks); | 121 build(0, disks); |
116 } | 122 } |
117 | 123 |
118 void build(int pile, int disks) { | 124 void build(int pile, int disks) { |
119 for (var i = disks - 1; i >= 0; i--) { | 125 for (var i = disks - 1; i >= 0; i--) { |
120 push(pile, new TowersDisk(i)); | 126 push(pile, new TowersDisk(i)); |
121 } | 127 } |
122 } | 128 } |
123 | 129 |
124 void push(int pile, TowersDisk disk) { | 130 void push(int pile, TowersDisk disk) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 class SieveBenchmark extends BenchmarkBase { | 188 class SieveBenchmark extends BenchmarkBase { |
183 const SieveBenchmark() : super("Sieve"); | 189 const SieveBenchmark() : super("Sieve"); |
184 | 190 |
185 static int sieve(int size) { | 191 static int sieve(int size) { |
186 int primeCount = 0; | 192 int primeCount = 0; |
187 List<bool> flags = new List<bool>(size + 1); | 193 List<bool> flags = new List<bool>(size + 1); |
188 for (int i = 1; i < size; i++) flags[i] = true; | 194 for (int i = 1; i < size; i++) flags[i] = true; |
189 for (int i = 2; i < size; i++) { | 195 for (int i = 2; i < size; i++) { |
190 if (flags[i]) { | 196 if (flags[i]) { |
191 primeCount++; | 197 primeCount++; |
192 for (int k = i + 1; k <= size; k += i) | 198 for (int k = i + 1; k <= size; k += i) flags[k - 1] = false; |
193 flags[k - 1] = false; | |
194 } | 199 } |
195 } | 200 } |
196 return primeCount; | 201 return primeCount; |
197 } | 202 } |
198 | 203 |
199 | |
200 void warmup() { | 204 void warmup() { |
201 sieve(100); | 205 sieve(100); |
202 } | 206 } |
203 | 207 |
204 void exercise() { | 208 void exercise() { |
205 // This value has been copied from benchpress.js, so that we can compare | 209 // This value has been copied from benchpress.js, so that we can compare |
206 // performance. | 210 // performance. |
207 int result = sieve(1000); | 211 int result = sieve(1000); |
208 if (result != 168) | 212 if (result != 168) Error.error("Wrong result: $result should be: 168"); |
209 Error.error("Wrong result: $result should be: 168"); | |
210 } | 213 } |
211 | 214 |
212 static void main() { | 215 static void main() { |
213 new SieveBenchmark().report(); | 216 new SieveBenchmark().report(); |
214 } | 217 } |
215 } | 218 } |
216 | 219 |
217 // P e r m u t e | 220 // P e r m u t e |
218 // The original benchmark uses one-based indexing. Even though arrays in JS and | 221 // The original benchmark uses one-based indexing. Even though arrays in JS and |
219 // lists in dart are zero-based, we stay with one-based indexing | 222 // lists in dart are zero-based, we stay with one-based indexing |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 const PermuteBenchmark() : super("Permute"); | 256 const PermuteBenchmark() : super("Permute"); |
254 | 257 |
255 void warmup() { | 258 void warmup() { |
256 new Permute().permute(4); | 259 new Permute().permute(4); |
257 } | 260 } |
258 | 261 |
259 void exercise() { | 262 void exercise() { |
260 // This value has been copied from benchpress.js, so that we can compare | 263 // This value has been copied from benchpress.js, so that we can compare |
261 // performance. | 264 // performance. |
262 int result = new Permute().permute(8); | 265 int result = new Permute().permute(8); |
263 if (result != 8660) | 266 if (result != 8660) Error.error("Wrong result: $result should be: 8660"); |
264 Error.error("Wrong result: $result should be: 8660"); | |
265 } | 267 } |
266 | 268 |
267 static void main() { | 269 static void main() { |
268 new PermuteBenchmark().report(); | 270 new PermuteBenchmark().report(); |
269 } | 271 } |
270 } | 272 } |
271 | 273 |
272 // Q u e e n s | 274 // Q u e e n s |
273 // The original benchmark uses one-based indexing. Even though arrays in JS and | 275 // The original benchmark uses one-based indexing. Even though arrays in JS and |
274 // lists in dart are zero-based, we stay with one-based indexing | 276 // lists in dart are zero-based, we stay with one-based indexing |
275 // (wasting one element). | 277 // (wasting one element). |
276 class Queens { | 278 class Queens { |
277 static bool tryQueens(int i, | 279 static bool tryQueens( |
278 List<bool> a, | 280 int i, List<bool> a, List<bool> b, List<bool> c, List<int> x) { |
279 List<bool> b, | |
280 List<bool> c, | |
281 List<int> x) { | |
282 int j = 0; | 281 int j = 0; |
283 bool q = false; | 282 bool q = false; |
284 while ((!q) && (j != 8)) { | 283 while ((!q) && (j != 8)) { |
285 j++; | 284 j++; |
286 q = false; | 285 q = false; |
287 if (b[j] && a[i + j] && c[i - j + 7]) { | 286 if (b[j] && a[i + j] && c[i - j + 7]) { |
288 x[i] = j; | 287 x[i] = j; |
289 b[j] = false; | 288 b[j] = false; |
290 a[i + j] = false; | 289 a[i + j] = false; |
291 c[i - j + 7] = false; | 290 c[i - j + 7] = false; |
(...skipping 17 matching lines...) Expand all Loading... |
309 List<bool> b = new List<bool>(17); | 308 List<bool> b = new List<bool>(17); |
310 List<bool> c = new List<bool>(15); | 309 List<bool> c = new List<bool>(15); |
311 List<int> x = new List<int>(9); | 310 List<int> x = new List<int>(9); |
312 b[1] = false; | 311 b[1] = false; |
313 for (int i = -7; i <= 16; i++) { | 312 for (int i = -7; i <= 16; i++) { |
314 if ((i >= 1) && (i <= 8)) a[i] = true; | 313 if ((i >= 1) && (i <= 8)) a[i] = true; |
315 if (i >= 2) b[i] = true; | 314 if (i >= 2) b[i] = true; |
316 if (i <= 7) c[i + 7] = true; | 315 if (i <= 7) c[i + 7] = true; |
317 } | 316 } |
318 | 317 |
319 if (!tryQueens(1, b, a, c, x)) | 318 if (!tryQueens(1, b, a, c, x)) Error.error("Error in queens"); |
320 Error.error("Error in queens"); | |
321 } | 319 } |
322 } | 320 } |
323 | 321 |
324 class QueensBenchmark extends BenchmarkBase { | 322 class QueensBenchmark extends BenchmarkBase { |
325 const QueensBenchmark() : super("Queens"); | 323 const QueensBenchmark() : super("Queens"); |
326 | 324 |
327 void warmup() { | 325 void warmup() { |
328 Queens.queens(); | 326 Queens.queens(); |
329 } | 327 } |
330 | 328 |
331 void exercise() { | 329 void exercise() { |
332 Queens.queens(); | 330 Queens.queens(); |
333 } | 331 } |
334 | 332 |
335 static void main() { | 333 static void main() { |
336 new QueensBenchmark().report(); | 334 new QueensBenchmark().report(); |
337 } | 335 } |
338 } | 336 } |
339 | 337 |
340 | |
341 // R e c u r s e | 338 // R e c u r s e |
342 class Recurse { | 339 class Recurse { |
343 static int recurse(int n) { | 340 static int recurse(int n) { |
344 if (n <= 0) return 1; | 341 if (n <= 0) return 1; |
345 recurse(n - 1); | 342 recurse(n - 1); |
346 return recurse(n - 1); | 343 return recurse(n - 1); |
347 } | 344 } |
348 } | 345 } |
349 | 346 |
350 class RecurseBenchmark extends BenchmarkBase { | 347 class RecurseBenchmark extends BenchmarkBase { |
351 const RecurseBenchmark() : super("Recurse"); | 348 const RecurseBenchmark() : super("Recurse"); |
352 | 349 |
353 void warmup() { | 350 void warmup() { |
354 Recurse.recurse(7); | 351 Recurse.recurse(7); |
355 } | 352 } |
356 | 353 |
357 void exercise() { | 354 void exercise() { |
358 // This value has been copied from benchpress.js, so that we can compare | 355 // This value has been copied from benchpress.js, so that we can compare |
359 // performance. | 356 // performance. |
360 Recurse.recurse(13); | 357 Recurse.recurse(13); |
361 } | 358 } |
362 | 359 |
363 static void main() { | 360 static void main() { |
364 new RecurseBenchmark().report(); | 361 new RecurseBenchmark().report(); |
365 } | 362 } |
366 } | 363 } |
367 | 364 |
368 | |
369 // S u m | 365 // S u m |
370 class SumBenchmark extends BenchmarkBase { | 366 class SumBenchmark extends BenchmarkBase { |
371 const SumBenchmark() : super("Sum"); | 367 const SumBenchmark() : super("Sum"); |
372 | 368 |
373 static int sum(int start, int end) { | 369 static int sum(int start, int end) { |
374 var sum = 0; | 370 var sum = 0; |
375 for (var i = start; i <= end; i++) sum += i; | 371 for (var i = start; i <= end; i++) sum += i; |
376 return sum; | 372 return sum; |
377 } | 373 } |
378 | 374 |
379 void warmup() { | 375 void warmup() { |
380 sum(1, 1000); | 376 sum(1, 1000); |
381 } | 377 } |
382 | 378 |
383 void exercise() { | 379 void exercise() { |
384 // This value has been copied from benchpress.js, so that we can compare | 380 // This value has been copied from benchpress.js, so that we can compare |
385 // performance. | 381 // performance. |
386 int result = sum(1, 10000); | 382 int result = sum(1, 10000); |
387 if (result != 50005000) | 383 if (result != 50005000) |
388 Error.error("Wrong result: $result should be 50005000"); | 384 Error.error("Wrong result: $result should be 50005000"); |
389 } | 385 } |
390 | 386 |
391 static void main() { | 387 static void main() { |
392 new SumBenchmark().report(); | 388 new SumBenchmark().report(); |
393 } | 389 } |
394 } | 390 } |
395 | 391 |
396 | |
397 // H e l p e r f u n c t i o n s f o r s o r t s | 392 // H e l p e r f u n c t i o n s f o r s o r t s |
398 class Random { | 393 class Random { |
399 static const int INITIAL_SEED = 74755; | 394 static const int INITIAL_SEED = 74755; |
400 int seed; | 395 int seed; |
401 Random() : seed = INITIAL_SEED {} | 396 Random() : seed = INITIAL_SEED {} |
402 | 397 |
403 int random() { | 398 int random() { |
404 seed = ((seed * 1309) + 13849) % 65536; | 399 seed = ((seed * 1309) + 13849) % 65536; |
405 return seed; | 400 return seed; |
406 } | 401 } |
(...skipping 18 matching lines...) Expand all Loading... |
425 if (e < min) min = e; | 420 if (e < min) min = e; |
426 } | 421 } |
427 | 422 |
428 this.min = min; | 423 this.min = min; |
429 this.max = max; | 424 this.max = max; |
430 } | 425 } |
431 | 426 |
432 void check() { | 427 void check() { |
433 List<int> a = list; | 428 List<int> a = list; |
434 int len = a.length; | 429 int len = a.length; |
435 if ((a[0] != min) || a[len - 1] != max) | 430 if ((a[0] != min) || a[len - 1] != max) Error.error("List is not sorted"); |
436 Error.error("List is not sorted"); | |
437 for (var i = 1; i < len; i++) { | 431 for (var i = 1; i < len; i++) { |
438 if (a[i - 1] > a[i]) Error.error("List is not sorted"); | 432 if (a[i - 1] > a[i]) Error.error("List is not sorted"); |
439 } | 433 } |
440 } | 434 } |
441 } | 435 } |
442 | 436 |
443 | |
444 // B u b b l e S o r t | 437 // B u b b l e S o r t |
445 class BubbleSort { | 438 class BubbleSort { |
446 static void sort(List<int> a) { | 439 static void sort(List<int> a) { |
447 int len = a.length; | 440 int len = a.length; |
448 for (int i = len - 2; i >= 0; i--) { | 441 for (int i = len - 2; i >= 0; i--) { |
449 for (int j = 0; j <= i; j++) { | 442 for (int j = 0; j <= i; j++) { |
450 int c = a[j]; | 443 int c = a[j]; |
451 int n = a[j + 1]; | 444 int n = a[j + 1]; |
452 if (c > n) { | 445 if (c > n) { |
453 a[j] = n; | 446 a[j] = n; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
516 SortData data = new SortData(800); | 509 SortData data = new SortData(800); |
517 QuickSort.sort(data.list, 0, data.list.length - 1); | 510 QuickSort.sort(data.list, 0, data.list.length - 1); |
518 data.check(); | 511 data.check(); |
519 } | 512 } |
520 | 513 |
521 static void main() { | 514 static void main() { |
522 new QuickSortBenchmark().report(); | 515 new QuickSortBenchmark().report(); |
523 } | 516 } |
524 } | 517 } |
525 | 518 |
526 | |
527 // T r e e S o r t | 519 // T r e e S o r t |
528 class TreeNodePress { | 520 class TreeNodePress { |
529 int value; | 521 int value; |
530 TreeNodePress left; | 522 TreeNodePress left; |
531 TreeNodePress right; | 523 TreeNodePress right; |
532 | 524 |
533 TreeNodePress(int n) : value = n {} | 525 TreeNodePress(int n) : value = n {} |
534 | 526 |
535 void insert(int n) { | 527 void insert(int n) { |
536 if (n < value) { | 528 if (n < value) { |
537 if (left == null) left = new TreeNodePress(n); | 529 if (left == null) |
538 else left.insert(n); | 530 left = new TreeNodePress(n); |
| 531 else |
| 532 left.insert(n); |
539 } else { | 533 } else { |
540 if (right == null) right = new TreeNodePress(n); | 534 if (right == null) |
541 else right.insert(n); | 535 right = new TreeNodePress(n); |
| 536 else |
| 537 right.insert(n); |
542 } | 538 } |
543 } | 539 } |
544 | 540 |
545 void check() { | 541 void check() { |
546 TreeNodePress left = this.left; | 542 TreeNodePress left = this.left; |
547 TreeNodePress right = this.right; | 543 TreeNodePress right = this.right; |
548 int value = this.value; | 544 int value = this.value; |
549 | 545 |
550 return ((left == null) || ((left.value < value) && left.check())) && | 546 return ((left == null) || ((left.value < value) && left.check())) && |
551 ((right == null) || ((right.value >= value) && right.check())); | 547 ((right == null) || ((right.value >= value) && right.check())); |
552 } | 548 } |
553 } | 549 } |
554 | 550 |
555 class TreeSort { | 551 class TreeSort { |
556 static void sort(List<int> a) { | 552 static void sort(List<int> a) { |
557 int len = a.length; | 553 int len = a.length; |
558 TreeNodePress tree = new TreeNodePress(a[0]); | 554 TreeNodePress tree = new TreeNodePress(a[0]); |
559 for (var i = 1; i < len; i++) tree.insert(a[i]); | 555 for (var i = 1; i < len; i++) tree.insert(a[i]); |
560 if (!tree.check()) Error.error("Invalid result, tree not sorted"); | 556 if (!tree.check()) Error.error("Invalid result, tree not sorted"); |
561 } | 557 } |
562 } | 558 } |
563 | 559 |
564 class TreeSortBenchmark extends BenchmarkBase { | 560 class TreeSortBenchmark extends BenchmarkBase { |
565 const TreeSortBenchmark() : super("TreeSort"); | 561 const TreeSortBenchmark() : super("TreeSort"); |
566 | 562 |
567 void warmup() { | 563 void warmup() { |
568 TreeSort.sort(new SortData(100).list); | 564 TreeSort.sort(new SortData(100).list); |
569 } | 565 } |
570 | 566 |
571 void exercise() { | 567 void exercise() { |
572 // This value has been copied from benchpress.js, so that we can compare | 568 // This value has been copied from benchpress.js, so that we can compare |
573 // performance. | 569 // performance. |
574 TreeSort.sort(new SortData(1000).list); | 570 TreeSort.sort(new SortData(1000).list); |
575 } | 571 } |
576 } | 572 } |
577 | 573 |
578 | |
579 // T a k | 574 // T a k |
580 class TakBenchmark extends BenchmarkBase { | 575 class TakBenchmark extends BenchmarkBase { |
581 const TakBenchmark() : super("Tak"); | 576 const TakBenchmark() : super("Tak"); |
582 | 577 |
583 static void tak(int x, int y, int z) { | 578 static void tak(int x, int y, int z) { |
584 if (y >= x) return z; | 579 if (y >= x) return z; |
585 return tak(tak(x - 1, y, z), tak(y - 1, z, x), tak(z - 1, x, y)); | 580 return tak(tak(x - 1, y, z), tak(y - 1, z, x), tak(z - 1, x, y)); |
586 } | 581 } |
587 | 582 |
588 void warmup() { | 583 void warmup() { |
589 tak(9, 6, 3); | 584 tak(9, 6, 3); |
590 } | 585 } |
591 | 586 |
592 void exercise() { | 587 void exercise() { |
593 // This value has been copied from benchpress.js, so that we can compare | 588 // This value has been copied from benchpress.js, so that we can compare |
594 // performance. | 589 // performance. |
595 tak(18, 12, 6); | 590 tak(18, 12, 6); |
596 } | 591 } |
597 | 592 |
598 static void main() { | 593 static void main() { |
599 new TakBenchmark().report(); | 594 new TakBenchmark().report(); |
600 } | 595 } |
601 } | 596 } |
602 | 597 |
603 | |
604 // T a k l | 598 // T a k l |
605 class ListElement { | 599 class ListElement { |
606 final int length; | 600 final int length; |
607 final ListElement next; | 601 final ListElement next; |
608 | 602 |
609 const ListElement(int length, ListElement next) | 603 const ListElement(int length, ListElement next) |
610 : this.length = length, | 604 : this.length = length, |
611 this.next = next; | 605 this.next = next; |
612 | 606 |
613 static ListElement makeList(int length) { | 607 static ListElement makeList(int length) { |
614 if (length == 0) return null; | 608 if (length == 0) return null; |
615 return new ListElement(length, makeList(length - 1)); | 609 return new ListElement(length, makeList(length - 1)); |
616 } | 610 } |
617 | 611 |
618 static bool isShorter(ListElement x, ListElement y) { | 612 static bool isShorter(ListElement x, ListElement y) { |
619 ListElement xTail = x; | 613 ListElement xTail = x; |
620 ListElement yTail = y; | 614 ListElement yTail = y; |
621 while (yTail != null) { | 615 while (yTail != null) { |
622 if (xTail == null) return true; | 616 if (xTail == null) return true; |
623 xTail = xTail.next; | 617 xTail = xTail.next; |
624 yTail = yTail.next; | 618 yTail = yTail.next; |
625 } | 619 } |
626 return false; | 620 return false; |
627 } | 621 } |
628 } | 622 } |
629 | 623 |
630 class Takl { | 624 class Takl { |
631 static ListElement takl(ListElement x, ListElement y, ListElement z) { | 625 static ListElement takl(ListElement x, ListElement y, ListElement z) { |
632 if (ListElement.isShorter(y, x)) { | 626 if (ListElement.isShorter(y, x)) { |
633 return takl(takl(x.next, y, z), | 627 return takl(takl(x.next, y, z), takl(y.next, z, x), takl(z.next, x, y)); |
634 takl(y.next, z, x), | |
635 takl(z.next, x, y)); | |
636 } else { | 628 } else { |
637 return z; | 629 return z; |
638 } | 630 } |
639 } | 631 } |
640 } | 632 } |
641 | 633 |
642 class TaklBenchmark extends BenchmarkBase { | 634 class TaklBenchmark extends BenchmarkBase { |
643 const TaklBenchmark() : super("Takl"); | 635 const TaklBenchmark() : super("Takl"); |
644 | 636 |
645 void warmup() { | 637 void warmup() { |
646 Takl.takl(ListElement.makeList(8), | 638 Takl.takl(ListElement.makeList(8), ListElement.makeList(4), |
647 ListElement.makeList(4), | 639 ListElement.makeList(3)); |
648 ListElement.makeList(3)); | |
649 } | 640 } |
650 | 641 |
651 void exercise() { | 642 void exercise() { |
652 // This value has been copied from benchpress.js, so that we can compare | 643 // This value has been copied from benchpress.js, so that we can compare |
653 // performance. | 644 // performance. |
654 ListElement result = Takl.takl(ListElement.makeList(15), | 645 ListElement result = Takl.takl(ListElement.makeList(15), |
655 ListElement.makeList(10), | 646 ListElement.makeList(10), ListElement.makeList(6)); |
656 ListElement.makeList(6)); | |
657 if (result.length != 10) { | 647 if (result.length != 10) { |
658 int len = result.length; | 648 int len = result.length; |
659 Error.error("Wrong result: $len should be: 10"); | 649 Error.error("Wrong result: $len should be: 10"); |
660 } | 650 } |
661 } | 651 } |
662 | 652 |
663 static void main() { | 653 static void main() { |
664 new TaklBenchmark().report(); | 654 new TaklBenchmark().report(); |
665 } | 655 } |
666 } | 656 } |
667 | 657 |
668 // M a i n | 658 // M a i n |
669 | 659 |
670 class BenchPress { | 660 class BenchPress { |
671 static void mainWithArgs(List<String> args) { | 661 static void mainWithArgs(List<String> args) { |
672 List<BenchmarkBase> benchmarks = [ | 662 List<BenchmarkBase> benchmarks = [ |
673 new BubbleSortBenchmark(), | 663 new BubbleSortBenchmark(), |
674 new FibBenchmark(), | 664 new FibBenchmark(), |
675 new LoopBenchmark(), | 665 new LoopBenchmark(), |
676 new PermuteBenchmark(), | 666 new PermuteBenchmark(), |
677 new QueensBenchmark(), | 667 new QueensBenchmark(), |
678 new QuickSortBenchmark(), | 668 new QuickSortBenchmark(), |
679 new RecurseBenchmark(), | 669 new RecurseBenchmark(), |
680 new SieveBenchmark(), | 670 new SieveBenchmark(), |
681 new SumBenchmark(), | 671 new SumBenchmark(), |
682 new TakBenchmark(), | 672 new TakBenchmark(), |
683 new TaklBenchmark(), | 673 new TaklBenchmark(), |
684 new TowersBenchmark(), | 674 new TowersBenchmark(), |
685 new TreeSortBenchmark(), | 675 new TreeSortBenchmark(), |
686 ]; | 676 ]; |
687 if (args.length > 0) { | 677 if (args.length > 0) { |
688 String benchName = args[0]; | 678 String benchName = args[0]; |
689 bool foundBenchmark = false; | 679 bool foundBenchmark = false; |
690 benchmarks.forEach((bench) { | 680 benchmarks.forEach((bench) { |
691 if (bench.name == benchName) { | 681 if (bench.name == benchName) { |
692 foundBenchmark = true; | 682 foundBenchmark = true; |
693 bench.report(); | 683 bench.report(); |
694 } | 684 } |
695 }); | 685 }); |
(...skipping 21 matching lines...) Expand all Loading... |
717 } | 707 } |
718 | 708 |
719 class BenchmarkBase { | 709 class BenchmarkBase { |
720 final String name; | 710 final String name; |
721 | 711 |
722 // Empty constructor. | 712 // Empty constructor. |
723 const BenchmarkBase(String name) : this.name = name; | 713 const BenchmarkBase(String name) : this.name = name; |
724 | 714 |
725 // The benchmark code. | 715 // The benchmark code. |
726 // This function is not used, if both [warmup] and [exercise] are overwritten. | 716 // This function is not used, if both [warmup] and [exercise] are overwritten. |
727 void run() { } | 717 void run() {} |
728 | 718 |
729 // Runs a short version of the benchmark. By default invokes [run] once. | 719 // Runs a short version of the benchmark. By default invokes [run] once. |
730 void warmup() { | 720 void warmup() { |
731 run(); | 721 run(); |
732 } | 722 } |
733 | 723 |
734 // Exercices the benchmark. By default invokes [run] 10 times. | 724 // Exercices the benchmark. By default invokes [run] 10 times. |
735 void exercise() { | 725 void exercise() { |
736 for (int i = 0; i < 10; i++) { | 726 for (int i = 0; i < 10; i++) { |
737 run(); | 727 run(); |
738 } | 728 } |
739 } | 729 } |
740 | 730 |
741 // Not measured setup code executed prior to the benchmark runs. | 731 // Not measured setup code executed prior to the benchmark runs. |
742 void setup() { } | 732 void setup() {} |
743 | 733 |
744 // Not measures teardown code executed after the benchark runs. | 734 // Not measures teardown code executed after the benchark runs. |
745 void teardown() { } | 735 void teardown() {} |
746 | 736 |
747 // Measures the score for this benchmark by executing it repeately until | 737 // Measures the score for this benchmark by executing it repeately until |
748 // time minimum has been reached. | 738 // time minimum has been reached. |
749 static double measureFor(Function f, int timeMinimum) { | 739 static double measureFor(Function f, int timeMinimum) { |
750 int time = 0; | 740 int time = 0; |
751 int iter = 0; | 741 int iter = 0; |
752 DateTime start = new DateTime.now(); | 742 DateTime start = new DateTime.now(); |
753 while (time < timeMinimum) { | 743 while (time < timeMinimum) { |
754 f(); | 744 f(); |
755 time = (new DateTime.now().difference(start)).inMilliseconds; | 745 time = (new DateTime.now().difference(start)).inMilliseconds; |
756 iter++; | 746 iter++; |
757 } | 747 } |
758 // Force double result by using a double constant. | 748 // Force double result by using a double constant. |
759 return (1000.0 * iter) / time; | 749 return (1000.0 * iter) / time; |
760 } | 750 } |
761 | 751 |
762 // Measures the score for the benchmark and returns it. | 752 // Measures the score for the benchmark and returns it. |
763 double measure() { | 753 double measure() { |
764 setup(); | 754 setup(); |
765 // Warmup for at least 100ms. Discard result. | 755 // Warmup for at least 100ms. Discard result. |
766 measureFor(() { this.warmup(); }, -100); | 756 measureFor(() { |
| 757 this.warmup(); |
| 758 }, -100); |
767 // Run the benchmark for at least 2000ms. | 759 // Run the benchmark for at least 2000ms. |
768 double result = measureFor(() { this.exercise(); }, -2000); | 760 double result = measureFor(() { |
| 761 this.exercise(); |
| 762 }, -2000); |
769 teardown(); | 763 teardown(); |
770 return result; | 764 return result; |
771 } | 765 } |
772 | 766 |
773 void report() { | 767 void report() { |
774 double score = measure(); | 768 double score = measure(); |
775 print("name: $score"); | 769 print("name: $score"); |
776 } | 770 } |
| 771 } |
777 | 772 |
778 } | |
779 class Logger { | 773 class Logger { |
780 static print(object) { | 774 static print(object) { |
781 printobject(object); | 775 printobject(object); |
782 } | 776 } |
783 static printobject(obj) { } | 777 |
| 778 static printobject(obj) {} |
784 } | 779 } |
785 | 780 |
786 | |
787 // | 781 // |
788 // Dromaeo ObjectString | 782 // Dromaeo ObjectString |
789 // Adapted from Mozilla JavaScript performance test suite. | 783 // Adapted from Mozilla JavaScript performance test suite. |
790 // Microtests of strings (concatenation, methods). | 784 // Microtests of strings (concatenation, methods). |
791 | 785 |
792 | |
793 class ObjectString extends BenchmarkBase { | 786 class ObjectString extends BenchmarkBase { |
794 | |
795 const ObjectString() : super("Dromaeo.ObjectString"); | 787 const ObjectString() : super("Dromaeo.ObjectString"); |
796 | 788 |
797 static void main() { | 789 static void main() { |
798 new ObjectString().report(); | 790 new ObjectString().report(); |
799 } | 791 } |
800 | 792 |
801 static void print(String str) { | 793 static void print(String str) { |
802 print(str); | 794 print(str); |
803 } | 795 } |
804 | 796 |
805 String getRandomString(int characters) { | 797 String getRandomString(int characters) { |
806 var result = ""; | 798 var result = ""; |
807 for (var i = 0; i < characters; i++) { | 799 for (var i = 0; i < characters; i++) { |
808 result += Strings. | 800 result += |
809 createFromCodePoints([(25 * Math.random()).toInt() + 97]); | 801 Strings.createFromCodePoints([(25 * Math.random()).toInt() + 97]); |
810 } | 802 } |
811 result += result; | 803 result += result; |
812 result += result; | 804 result += result; |
813 return result; | 805 return result; |
814 } | 806 } |
815 | 807 |
816 void run() { | 808 void run() { |
817 //JS Dromeaeo uses 16384 | 809 //JS Dromeaeo uses 16384 |
818 final ITERATE1 = 384; | 810 final ITERATE1 = 384; |
819 //JS Dromeaeo uses 80000 | 811 //JS Dromeaeo uses 80000 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
916 str = input[0]; | 908 str = input[0]; |
917 str = input[input.length - 1]; | 909 str = input[input.length - 1]; |
918 str = input[150]; //set it to 15000 | 910 str = input[150]; //set it to 15000 |
919 str = input[100]; //set it to 10000 | 911 str = input[100]; //set it to 10000 |
920 str = input[50]; //set it to 5000 | 912 str = input[50]; //set it to 5000 |
921 } | 913 } |
922 return str; | 914 return str; |
923 } | 915 } |
924 } | 916 } |
925 | 917 |
926 | |
927 class CodeUnitAtBenchmark { | 918 class CodeUnitAtBenchmark { |
928 CodeUnitAtBenchmark() {} | 919 CodeUnitAtBenchmark() {} |
929 | 920 |
930 static String test(String input, var iterations) { | 921 static String test(String input, var iterations) { |
931 var str; | 922 var str; |
932 for (var j = 0; j < iterations; j++) { | 923 for (var j = 0; j < iterations; j++) { |
933 str = input.codeUnitAt(0); | 924 str = input.codeUnitAt(0); |
934 str = input.codeUnitAt(input.length - 1); | 925 str = input.codeUnitAt(input.length - 1); |
935 str = input.codeUnitAt(150); //set it to 15000 | 926 str = input.codeUnitAt(150); //set it to 15000 |
936 str = input.codeUnitAt(100); //set it to 10000 | 927 str = input.codeUnitAt(100); //set it to 10000 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
974 SliceBenchmark() {} | 965 SliceBenchmark() {} |
975 | 966 |
976 static String test(String input, var iterations) { | 967 static String test(String input, var iterations) { |
977 var str; | 968 var str; |
978 for (var j = 0; j < iterations; j++) { | 969 for (var j = 0; j < iterations; j++) { |
979 str = input.substring(0, input.length - 1); | 970 str = input.substring(0, input.length - 1); |
980 str = input.substring(0, 5); | 971 str = input.substring(0, 5); |
981 str = input.substring(input.length - 1, input.length - 1); | 972 str = input.substring(input.length - 1, input.length - 1); |
982 str = input.substring(input.length - 6, input.length - 1); | 973 str = input.substring(input.length - 6, input.length - 1); |
983 str = input.substring(150, 155); //set to 15000 and 15005 | 974 str = input.substring(150, 155); //set to 15000 and 15005 |
984 str = input.substring(120, input.length-1); //set to 12000 | 975 str = input.substring(120, input.length - 1); //set to 12000 |
985 } | 976 } |
986 return str; | 977 return str; |
987 } | 978 } |
988 } | 979 } |
989 | 980 |
990 class SubstrBenchmark { | 981 class SubstrBenchmark { |
991 SubstrBenchmark() {} | 982 SubstrBenchmark() {} |
992 | 983 |
993 static String test(String input, var iterations) { | 984 static String test(String input, var iterations) { |
994 var str; | 985 var str; |
995 for (var j = 0; j < iterations; j++) { | 986 for (var j = 0; j < iterations; j++) { |
996 str = input.substring(0, input.length-1); | 987 str = input.substring(0, input.length - 1); |
997 str = input.substring(0, 4); | 988 str = input.substring(0, 4); |
998 str = input.substring(input.length - 1, input.length - 1); | 989 str = input.substring(input.length - 1, input.length - 1); |
999 str = input.substring(input.length - 6, input.length - 6); | 990 str = input.substring(input.length - 6, input.length - 6); |
1000 str = input.substring(150, 154); //set to 15000 and 15005 | 991 str = input.substring(150, 154); //set to 15000 and 15005 |
1001 str = input.substring(120, 124); //set to 12000 | 992 str = input.substring(120, 124); //set to 12000 |
1002 } | 993 } |
1003 return str; | 994 return str; |
1004 } | 995 } |
1005 } | 996 } |
1006 | 997 |
1007 class SubstringBenchmark { | 998 class SubstringBenchmark { |
1008 SubstringBenchmark() {} | 999 SubstringBenchmark() {} |
1009 | 1000 |
1010 static String test(String input, var iterations) { | 1001 static String test(String input, var iterations) { |
1011 var str; | 1002 var str; |
1012 for (var j = 0; j < iterations; j++) { | 1003 for (var j = 0; j < iterations; j++) { |
1013 str = input.substring(0, input.length - 1); | 1004 str = input.substring(0, input.length - 1); |
1014 str = input.substring(0, 4); | 1005 str = input.substring(0, 4); |
1015 str = input.substring(input.length - 1, input.length - 1); | 1006 str = input.substring(input.length - 1, input.length - 1); |
1016 str = input.substring(input.length - 6, input.length - 2); | 1007 str = input.substring(input.length - 6, input.length - 2); |
1017 str = input.substring(150, 154); //set to 15000 and 15005 | 1008 str = input.substring(150, 154); //set to 15000 and 15005 |
1018 str = input.substring(120, input.length - 2); //set to 12000 | 1009 str = input.substring(120, input.length - 2); //set to 12000 |
1019 } | 1010 } |
1020 return str; | 1011 return str; |
1021 | |
1022 } | 1012 } |
1023 } | 1013 } |
1024 | 1014 |
1025 class ToLowerCaseBenchmark { | 1015 class ToLowerCaseBenchmark { |
1026 ToLowerCaseBenchmark() {} | 1016 ToLowerCaseBenchmark() {} |
1027 | 1017 |
1028 static String test(String input, var iterations) { | 1018 static String test(String input, var iterations) { |
1029 var str; | 1019 var str; |
1030 for (var j = 0; j < (iterations / 1000); j++) { | 1020 for (var j = 0; j < (iterations / 1000); j++) { |
1031 str = Ascii.toLowerCase(input); | 1021 str = Ascii.toLowerCase(input); |
(...skipping 26 matching lines...) Expand all Loading... |
1058 res = (tmp.compareTo(tmp2) < 0); | 1048 res = (tmp.compareTo(tmp2) < 0); |
1059 res = (tmp.compareTo(tmp2) > 0); | 1049 res = (tmp.compareTo(tmp2) > 0); |
1060 } | 1050 } |
1061 return res; | 1051 return res; |
1062 } | 1052 } |
1063 } | 1053 } |
1064 | 1054 |
1065 // Benchmarks basic message communication between two isolates. | 1055 // Benchmarks basic message communication between two isolates. |
1066 | 1056 |
1067 class Benchmark1 { | 1057 class Benchmark1 { |
1068 | |
1069 static const MESSAGES = 10000; | 1058 static const MESSAGES = 10000; |
1070 static const INIT_MESSAGE = 0; | 1059 static const INIT_MESSAGE = 0; |
1071 static const TERMINATION_MESSAGE = -1; | 1060 static const TERMINATION_MESSAGE = -1; |
1072 static const WARMUP_TIME = 1000; | 1061 static const WARMUP_TIME = 1000; |
1073 static const RUN_TIME = 1000; | 1062 static const RUN_TIME = 1000; |
1074 static const RUNS = 5; | 1063 static const RUNS = 5; |
1075 | 1064 |
1076 | |
1077 static int run() { | 1065 static int run() { |
1078 return _run; | 1066 return _run; |
1079 } | 1067 } |
1080 | 1068 |
1081 static void add_result(var opsms) { | 1069 static void add_result(var opsms) { |
1082 _run++; | 1070 _run++; |
1083 _opsms += opsms; | 1071 _opsms += opsms; |
1084 } | 1072 } |
1085 | 1073 |
1086 static void get_result() { | 1074 static void get_result() { |
1087 return _opsms / _run; | 1075 return _opsms / _run; |
1088 } | 1076 } |
1089 | 1077 |
1090 static void init() { | 1078 static void init() { |
1091 _run = 0; | 1079 _run = 0; |
1092 _opsms = 0.0; | 1080 _opsms = 0.0; |
1093 } | 1081 } |
1094 | 1082 |
1095 static void main() { | 1083 static void main() { |
1096 init(); | 1084 init(); |
1097 PingPongGame pingPongGame = new PingPongGame(); | 1085 PingPongGame pingPongGame = new PingPongGame(); |
1098 } | 1086 } |
1099 | 1087 |
1100 static var _run; | 1088 static var _run; |
1101 static var _opsms; | 1089 static var _opsms; |
1102 } | 1090 } |
1103 | 1091 |
1104 class PingPongGame { | 1092 class PingPongGame { |
1105 | |
1106 PingPongGame() | 1093 PingPongGame() |
1107 : _ping = new ReceivePort(), | 1094 : _ping = new ReceivePort(), |
1108 _pingPort = _ping.toSendPort(), | 1095 _pingPort = _ping.toSendPort(), |
1109 _pong = null, | 1096 _pong = null, |
1110 _warmedup = false, | 1097 _warmedup = false, |
1111 _iterations = 0 { | 1098 _iterations = 0 { |
1112 SendPort _pong = spawnFunction(pong); | 1099 SendPort _pong = spawnFunction(pong); |
1113 play(); | 1100 play(); |
1114 } | 1101 } |
1115 | 1102 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1170 if (message == Benchmark1.INIT_MESSAGE) { | 1157 if (message == Benchmark1.INIT_MESSAGE) { |
1171 replyTo.send(message, null); | 1158 replyTo.send(message, null); |
1172 } else if (message == Benchmark1.TERMINATION_MESSAGE) { | 1159 } else if (message == Benchmark1.TERMINATION_MESSAGE) { |
1173 port.close(); | 1160 port.close(); |
1174 } else { | 1161 } else { |
1175 replyTo.send(message, null); | 1162 replyTo.send(message, null); |
1176 } | 1163 } |
1177 }); | 1164 }); |
1178 } | 1165 } |
1179 | 1166 |
1180 | |
1181 class ManyGenericInstanceofTest { | 1167 class ManyGenericInstanceofTest { |
1182 static testMain() { | 1168 static testMain() { |
1183 for (int i = 0; i < 5000; i++) { | 1169 for (int i = 0; i < 5000; i++) { |
1184 GenericInstanceof.testMain(); | 1170 GenericInstanceof.testMain(); |
1185 } | 1171 } |
1186 } | 1172 } |
1187 } | 1173 } |
1188 | 1174 |
1189 // --------------------------------------------------------------------------- | 1175 // --------------------------------------------------------------------------- |
1190 // THE REST OF THIS FILE COULD BE AUTOGENERATED | 1176 // THE REST OF THIS FILE COULD BE AUTOGENERATED |
(...skipping 27 matching lines...) Expand all Loading... |
1218 void isolate_negative_entry() { | 1204 void isolate_negative_entry() { |
1219 port.receive((ignored, replyTo) { | 1205 port.receive((ignored, replyTo) { |
1220 replyTo.send("foo", null); | 1206 replyTo.send("foo", null); |
1221 }); | 1207 }); |
1222 } | 1208 } |
1223 | 1209 |
1224 isolate_negative_test_main() { | 1210 isolate_negative_test_main() { |
1225 test("ensure isolate code is executed", () { | 1211 test("ensure isolate code is executed", () { |
1226 SendPort port = spawnFunction(isolate_negative_entry); | 1212 SendPort port = spawnFunction(isolate_negative_entry); |
1227 port.call("foo").then(expectAsync1((message) { | 1213 port.call("foo").then(expectAsync1((message) { |
1228 Expect.equals(true, "Expected fail"); // <=-------- Should fail here. | 1214 Expect.equals(true, "Expected fail"); // <=-------- Should fail here. |
1229 })); | 1215 })); |
1230 }); | 1216 }); |
1231 } | 1217 } |
1232 | 1218 |
1233 // --------------------------------------------------------------------------- | 1219 // --------------------------------------------------------------------------- |
1234 // tests/isolate/message_test.dart | 1220 // tests/isolate/message_test.dart |
1235 // --------------------------------------------------------------------------- | 1221 // --------------------------------------------------------------------------- |
1236 | 1222 |
1237 // --------------------------------------------------------------------------- | 1223 // --------------------------------------------------------------------------- |
1238 // Message passing test. | 1224 // Message passing test. |
1239 // --------------------------------------------------------------------------- | 1225 // --------------------------------------------------------------------------- |
1240 | 1226 |
1241 class MessageTest { | 1227 class MessageTest { |
1242 static const List list1 = const ["Hello", "World", "Hello", 0xfffffffffff]; | 1228 static const List list1 = const ["Hello", "World", "Hello", 0xfffffffffff]; |
1243 static const List list2 = const [null, list1, list1, list1, list1]; | 1229 static const List list2 = const [null, list1, list1, list1, list1]; |
1244 static const List list3 = const [list2, 2.0, true, false, 0xfffffffffff]; | 1230 static const List list3 = const [list2, 2.0, true, false, 0xfffffffffff]; |
1245 static const Map map1 = const { | 1231 static const Map map1 = const { |
1246 "a=1" : 1, "b=2" : 2, "c=3" : 3, | 1232 "a=1": 1, |
| 1233 "b=2": 2, |
| 1234 "c=3": 3, |
1247 }; | 1235 }; |
1248 static const Map map2 = const { | 1236 static const Map map2 = const { |
1249 "list1" : list1, "list2" : list2, "list3" : list3, | 1237 "list1": list1, |
| 1238 "list2": list2, |
| 1239 "list3": list3, |
1250 }; | 1240 }; |
1251 static const List list4 = const [map1, map2]; | 1241 static const List list4 = const [map1, map2]; |
1252 static const List elms = const [ | 1242 static const List elms = const [ |
1253 list1, list2, list3, list4, | 1243 list1, |
| 1244 list2, |
| 1245 list3, |
| 1246 list4, |
1254 ]; | 1247 ]; |
1255 | 1248 |
1256 static void VerifyMap(Map expected, Map actual) { | 1249 static void VerifyMap(Map expected, Map actual) { |
1257 Expect.equals(true, expected is Map); | 1250 Expect.equals(true, expected is Map); |
1258 Expect.equals(true, actual is Map); | 1251 Expect.equals(true, actual is Map); |
1259 Expect.equals(expected.length, actual.length); | 1252 Expect.equals(expected.length, actual.length); |
1260 testForEachMap(key, value) { | 1253 testForEachMap(key, value) { |
1261 if (value is List) { | 1254 if (value is List) { |
1262 VerifyList(value, actual[key]); | 1255 VerifyList(value, actual[key]); |
1263 } else { | 1256 } else { |
1264 Expect.equals(value, actual[key]); | 1257 Expect.equals(value, actual[key]); |
1265 } | 1258 } |
1266 } | 1259 } |
| 1260 |
1267 expected.forEach(testForEachMap); | 1261 expected.forEach(testForEachMap); |
1268 } | 1262 } |
1269 | 1263 |
1270 static void VerifyList(List expected, List actual) { | 1264 static void VerifyList(List expected, List actual) { |
1271 for (int i = 0; i < expected.length; i++) { | 1265 for (int i = 0; i < expected.length; i++) { |
1272 if (expected[i] is List) { | 1266 if (expected[i] is List) { |
1273 VerifyList(expected[i], actual[i]); | 1267 VerifyList(expected[i], actual[i]); |
1274 } else if (expected[i] is Map) { | 1268 } else if (expected[i] is Map) { |
1275 VerifyMap(expected[i], actual[i]); | 1269 VerifyMap(expected[i], actual[i]); |
1276 } else { | 1270 } else { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1313 // Send objects and receive them back. | 1307 // Send objects and receive them back. |
1314 for (int i = 0; i < MessageTest.elms.length; i++) { | 1308 for (int i = 0; i < MessageTest.elms.length; i++) { |
1315 var sentObject = MessageTest.elms[i]; | 1309 var sentObject = MessageTest.elms[i]; |
1316 remote.call(sentObject).then(expectAsync1((var receivedObject) { | 1310 remote.call(sentObject).then(expectAsync1((var receivedObject) { |
1317 MessageTest.VerifyObject(i, receivedObject); | 1311 MessageTest.VerifyObject(i, receivedObject); |
1318 })); | 1312 })); |
1319 } | 1313 } |
1320 | 1314 |
1321 // Send recursive objects and receive them back. | 1315 // Send recursive objects and receive them back. |
1322 List local_list1 = ["Hello", "World", "Hello", 0xffffffffff]; | 1316 List local_list1 = ["Hello", "World", "Hello", 0xffffffffff]; |
1323 List local_list2 = [null, local_list1, local_list1 ]; | 1317 List local_list2 = [null, local_list1, local_list1]; |
1324 List local_list3 = [local_list2, 2.0, true, false, 0xffffffffff]; | 1318 List local_list3 = [local_list2, 2.0, true, false, 0xffffffffff]; |
1325 List sendObject = new List(5); | 1319 List sendObject = new List(5); |
1326 sendObject[0] = local_list1; | 1320 sendObject[0] = local_list1; |
1327 sendObject[1] = sendObject; | 1321 sendObject[1] = sendObject; |
1328 sendObject[2] = local_list2; | 1322 sendObject[2] = local_list2; |
1329 sendObject[3] = sendObject; | 1323 sendObject[3] = sendObject; |
1330 sendObject[4] = local_list3; | 1324 sendObject[4] = local_list3; |
1331 remote.call(sendObject).then((var replyObject) { | 1325 remote.call(sendObject).then((var replyObject) { |
1332 Expect.equals(true, sendObject is List); | 1326 Expect.equals(true, sendObject is List); |
1333 Expect.equals(true, replyObject is List); | 1327 Expect.equals(true, replyObject is List); |
1334 Expect.equals(sendObject.length, replyObject.length); | 1328 Expect.equals(sendObject.length, replyObject.length); |
1335 Expect.equals(true, identical(replyObject[1], replyObject)); | 1329 Expect.equals(true, identical(replyObject[1], replyObject)); |
1336 Expect.equals(true, identical(replyObject[3], replyObject)); | 1330 Expect.equals(true, identical(replyObject[3], replyObject)); |
1337 Expect.equals(true, identical(replyObject[0], replyObject[2][1])); | 1331 Expect.equals(true, identical(replyObject[0], replyObject[2][1])); |
1338 Expect.equals(true, identical(replyObject[0], replyObject[2][2])); | 1332 Expect.equals(true, identical(replyObject[0], replyObject[2][2])); |
1339 Expect.equals(true, identical(replyObject[2], replyObject[4][0])); | 1333 Expect.equals(true, identical(replyObject[2], replyObject[4][0])); |
1340 Expect.equals(true, identical(replyObject[0][0], replyObject[0][2])); | 1334 Expect.equals(true, identical(replyObject[0][0], replyObject[0][2])); |
1341 // Bigint literals are not canonicalized so do a == check. | 1335 // Bigint literals are not canonicalized so do a == check. |
1342 Expect.equals(true, replyObject[0][3] == replyObject[4][4]); | 1336 Expect.equals(true, replyObject[0][3] == replyObject[4][4]); |
1343 }); | 1337 }); |
1344 | 1338 |
1345 // Shutdown the MessageServer. | 1339 // Shutdown the MessageServer. |
1346 remote.call(-1).then(expectAsync1((int message) { | 1340 remote.call(-1).then(expectAsync1((int message) { |
1347 Expect.equals(MessageTest.elms.length + 1, message); | 1341 Expect.equals(MessageTest.elms.length + 1, message); |
1348 })); | 1342 })); |
1349 }); | 1343 }); |
1350 } | 1344 } |
1351 | 1345 |
1352 // --------------------------------------------------------------------------- | 1346 // --------------------------------------------------------------------------- |
1353 // tests/isolate/request_reply_test.dart | 1347 // tests/isolate/request_reply_test.dart |
1354 // --------------------------------------------------------------------------- | 1348 // --------------------------------------------------------------------------- |
1355 | 1349 |
1356 void request_reply_entry() { | 1350 void request_reply_entry() { |
1357 port.receive((message, SendPort replyTo) { | 1351 port.receive((message, SendPort replyTo) { |
1358 replyTo.send(message + 87); | 1352 replyTo.send(message + 87); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1415 Expect.equals((count - 1) * 2, message); | 1409 Expect.equals((count - 1) * 2, message); |
1416 remote.send(count++, reply); | 1410 remote.send(count++, reply); |
1417 if (count == 10) { | 1411 if (count == 10) { |
1418 remote.send(-1, reply); | 1412 remote.send(-1, reply); |
1419 } | 1413 } |
1420 }, 11)); | 1414 }, 11)); |
1421 remote.send(count++, reply); | 1415 remote.send(count++, reply); |
1422 }); | 1416 }); |
1423 } | 1417 } |
1424 | 1418 |
1425 | |
1426 // --------------------------------------------------------------------------- | 1419 // --------------------------------------------------------------------------- |
1427 // tests/isolate/mandel_isolate_test.dart | 1420 // tests/isolate/mandel_isolate_test.dart |
1428 // --------------------------------------------------------------------------- | 1421 // --------------------------------------------------------------------------- |
1429 | 1422 |
1430 const TERMINATION_MESSAGE = -1; | 1423 const TERMINATION_MESSAGE = -1; |
1431 const N = 100; | 1424 const N = 100; |
1432 const ISOLATES = 20; | 1425 const ISOLATES = 20; |
1433 | 1426 |
1434 mandel_main() { | 1427 mandel_main() { |
1435 test("Render Mandelbrot in parallel", () { | 1428 test("Render Mandelbrot in parallel", () { |
1436 final state = new MandelbrotState(); | 1429 final state = new MandelbrotState(); |
1437 state._validated.future.then(expectAsync1((result) { | 1430 state._validated.future.then(expectAsync1((result) { |
1438 expect(result, isTrue); | 1431 expect(result, isTrue); |
1439 })); | 1432 })); |
1440 for (int i = 0; i < Math.min(ISOLATES, N); i++) state.startClient(i); | 1433 for (int i = 0; i < Math.min(ISOLATES, N); i++) state.startClient(i); |
1441 }); | 1434 }); |
1442 } | 1435 } |
1443 | 1436 |
1444 | |
1445 class MandelbrotState { | 1437 class MandelbrotState { |
1446 | |
1447 MandelbrotState() { | 1438 MandelbrotState() { |
1448 _result = new List<List<int>>(N); | 1439 _result = new List<List<int>>(N); |
1449 _lineProcessedBy = new List<LineProcessorClient>(N); | 1440 _lineProcessedBy = new List<LineProcessorClient>(N); |
1450 _sent = 0; | 1441 _sent = 0; |
1451 _missing = N; | 1442 _missing = N; |
1452 _validated = new Completer<bool>(); | 1443 _validated = new Completer<bool>(); |
1453 } | 1444 } |
1454 | 1445 |
1455 void startClient(int id) { | 1446 void startClient(int id) { |
1456 assert(_sent < N); | 1447 assert(_sent < N); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1494 // print(output); | 1485 // print(output); |
1495 } | 1486 } |
1496 | 1487 |
1497 List<List<int>> _result; | 1488 List<List<int>> _result; |
1498 List<LineProcessorClient> _lineProcessedBy; | 1489 List<LineProcessorClient> _lineProcessedBy; |
1499 int _sent; | 1490 int _sent; |
1500 int _missing; | 1491 int _missing; |
1501 Completer<bool> _validated; | 1492 Completer<bool> _validated; |
1502 } | 1493 } |
1503 | 1494 |
1504 | |
1505 class LineProcessorClient { | 1495 class LineProcessorClient { |
1506 | |
1507 LineProcessorClient(MandelbrotState this._state, int this._id) { | 1496 LineProcessorClient(MandelbrotState this._state, int this._id) { |
1508 _port = spawnFunction(processLines); | 1497 _port = spawnFunction(processLines); |
1509 } | 1498 } |
1510 | 1499 |
1511 void processLine(int y) { | 1500 void processLine(int y) { |
1512 _port.call(y).then((List<int> message) { | 1501 _port.call(y).then((List<int> message) { |
1513 _state.notifyProcessedLine(this, y, message); | 1502 _state.notifyProcessedLine(this, y, message); |
1514 }); | 1503 }); |
1515 } | 1504 } |
1516 | 1505 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1553 void processLines() { | 1542 void processLines() { |
1554 port.receive((message, SendPort replyTo) { | 1543 port.receive((message, SendPort replyTo) { |
1555 if (message == TERMINATION_MESSAGE) { | 1544 if (message == TERMINATION_MESSAGE) { |
1556 assert(replyTo == null); | 1545 assert(replyTo == null); |
1557 port.close(); | 1546 port.close(); |
1558 } else { | 1547 } else { |
1559 replyTo.send(processLine(message), null); | 1548 replyTo.send(processLine(message), null); |
1560 } | 1549 } |
1561 }); | 1550 }); |
1562 } | 1551 } |
OLD | NEW |