Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 // Dart test program for testing stopwatch support. | 5 // Dart test program for testing stopwatch support. |
| 6 | 6 |
| 7 class StopWatchTest { | 7 class StopwatchTest { |
| 8 static bool checkTicking(StopWatch sw) { | 8 static bool checkTicking(Stopwatch sw) { |
| 9 sw.start(); | 9 sw.start(); |
| 10 for (int i = 0; i < 10000; i++) { | 10 for (int i = 0; i < 10000; i++) { |
| 11 Math.parseInt(i.toString()); | 11 Math.parseInt(i.toString()); |
| 12 if (sw.elapsed() > 0) { | 12 if (sw.elapsed() > 0) { |
| 13 break; | 13 break; |
| 14 } | 14 } |
| 15 } | 15 } |
| 16 return sw.elapsed() > 0; | 16 return sw.elapsed() > 0; |
| 17 } | 17 } |
| 18 | 18 |
| 19 static bool checkStopping(StopWatch sw) { | 19 static bool checkStopping(Stopwatch sw) { |
| 20 sw.stop(); | 20 sw.stop(); |
| 21 int v1 = sw.elapsed(); | 21 int v1 = sw.elapsed(); |
| 22 Expect.isTrue(v1 > 0); // Expect a non-zero elapsed time. | 22 Expect.isTrue(v1 > 0); // Expect a non-zero elapsed time. |
| 23 StopWatch sw2 = new StopWatch(); // Used for verification. | 23 Stopwatch sw2 = new Stopwatch(); // Used for verification. |
| 24 sw2.start(); | 24 sw2.start(); |
| 25 for (int i = 0; i < 10000; i++) { | 25 int sw2LastElapsed = 0; |
| 26 for (int i = 0; i < 100000; i++) { | |
| 26 Math.parseInt(i.toString()); | 27 Math.parseInt(i.toString()); |
| 27 int v2 = sw.elapsed(); | 28 int v2 = sw.elapsed(); |
| 28 if (v1 != v2) { | 29 if (v1 != v2) { |
| 29 return false; | 30 return false; |
| 30 } | 31 } |
| 31 v1 = v2; | 32 // If sw2 elapsed twice then sw must have advanced too if it wasn't |
| 33 // stopped. | |
| 34 if (sw2LastElapsed > 0 && sw2.elapsed() > sw2LastElapsed) { | |
| 35 break; | |
| 36 } | |
| 37 sw2LastElapsed = sw2.elapsed(); | |
| 32 } | 38 } |
| 33 // The test only makes sense if measureable time elapsed and elapsed time | 39 // The test only makes sense if measureable time elapsed and elapsed time |
| 34 // on the stopped StopWatch did not increase. | 40 // on the stopped Stopwatch did not increase. |
| 35 Expect.isTrue(sw2.elapsed() > 0); | 41 Expect.isTrue(sw2.elapsed() > 0); |
| 36 return true; | 42 return true; |
| 37 } | 43 } |
| 38 | 44 |
| 39 static checkRestart() { | 45 static checkRestart() { |
| 40 StopWatch sw = new StopWatch(); | 46 Stopwatch sw = new Stopwatch(); |
| 41 sw.start(); | 47 sw.start(); |
| 42 for (int i = 0; i < 1000; i++) { | 48 for (int i = 0; i < 100000; i++) { |
| 43 Math.parseInt(i.toString()); | 49 Math.parseInt(i.toString()); |
| 50 if (sw.elapsed() > 0) { | |
| 51 break; | |
| 52 } | |
| 44 } | 53 } |
| 45 sw.stop(); | 54 sw.stop(); |
| 46 int initial = sw.elapsed(); | 55 int initial = sw.elapsed(); |
| 47 sw.start(); | 56 sw.start(); |
| 48 for (int i = 0; i < 10; i++) { | 57 for (int i = 0; i < 100000; i++) { |
| 49 Math.parseInt(i.toString()); | 58 Math.parseInt(i.toString()); |
| 59 if (sw.elapsed() > initial) break; | |
|
floitsch
2011/11/11 13:57:49
In the very latest revision I have moved the break
| |
| 50 } | 60 } |
| 51 sw.stop(); | 61 sw.stop(); |
| 52 Expect.isTrue(sw.elapsed() >= initial); | 62 Expect.isTrue(sw.elapsed() > initial); |
| 63 } | |
| 64 | |
| 65 static checkReset() { | |
| 66 Stopwatch sw = new Stopwatch(); | |
| 67 sw.start(); | |
| 68 for (int i = 0; i < 100000; i++) { | |
| 69 Math.parseInt(i.toString()); | |
| 70 if (sw.elapsed() > 0) { | |
| 71 break; | |
| 72 } | |
| 73 } | |
| 74 sw.stop(); | |
| 75 sw.reset(); | |
| 76 Expect.equals(0, sw.elapsed()); | |
| 77 sw.start(); | |
| 78 for (int i = 0; i < 100000; i++) { | |
| 79 Math.parseInt(i.toString()); | |
| 80 if (sw.elapsed() > 0) { | |
| 81 break; | |
| 82 } | |
| 83 } | |
| 84 sw.reset(); | |
| 85 for (int i = 0; i < 100000; i++) { | |
| 86 Math.parseInt(i.toString()); | |
| 87 if (sw.elapsed() > 0) { | |
| 88 break; | |
| 89 } | |
| 90 } | |
| 91 sw.stop(); | |
| 92 Expect.isTrue(sw.elapsed() > 0); | |
| 53 } | 93 } |
| 54 | 94 |
| 55 static testMain() { | 95 static testMain() { |
| 56 StopWatch sw = new StopWatch(); | 96 Stopwatch sw = new Stopwatch(); |
| 57 Expect.isTrue(checkTicking(sw)); | 97 Expect.isTrue(checkTicking(sw)); |
| 58 Expect.isTrue(checkStopping(sw)); | 98 Expect.isTrue(checkStopping(sw)); |
| 59 checkRestart(); | 99 checkRestart(); |
| 100 checkReset(); | |
| 60 } | 101 } |
| 61 } | 102 } |
| 62 | 103 |
| 63 main() { | 104 main() { |
| 64 StopWatchTest.testMain(); | 105 StopwatchTest.testMain(); |
| 65 } | 106 } |
| OLD | NEW |