| 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) { |
| 60 break; |
| 61 } |
| 50 } | 62 } |
| 51 sw.stop(); | 63 sw.stop(); |
| 52 Expect.isTrue(sw.elapsed() >= initial); | 64 Expect.isTrue(sw.elapsed() > initial); |
| 65 } |
| 66 |
| 67 static checkReset() { |
| 68 Stopwatch sw = new Stopwatch(); |
| 69 sw.start(); |
| 70 for (int i = 0; i < 100000; i++) { |
| 71 Math.parseInt(i.toString()); |
| 72 if (sw.elapsed() > 0) { |
| 73 break; |
| 74 } |
| 75 } |
| 76 sw.stop(); |
| 77 sw.reset(); |
| 78 Expect.equals(0, sw.elapsed()); |
| 79 sw.start(); |
| 80 for (int i = 0; i < 100000; i++) { |
| 81 Math.parseInt(i.toString()); |
| 82 if (sw.elapsed() > 0) { |
| 83 break; |
| 84 } |
| 85 } |
| 86 sw.reset(); |
| 87 for (int i = 0; i < 100000; i++) { |
| 88 Math.parseInt(i.toString()); |
| 89 if (sw.elapsed() > 0) { |
| 90 break; |
| 91 } |
| 92 } |
| 93 sw.stop(); |
| 94 Expect.isTrue(sw.elapsed() > 0); |
| 53 } | 95 } |
| 54 | 96 |
| 55 static testMain() { | 97 static testMain() { |
| 56 StopWatch sw = new StopWatch(); | 98 Stopwatch sw = new Stopwatch(); |
| 57 Expect.isTrue(checkTicking(sw)); | 99 Expect.isTrue(checkTicking(sw)); |
| 58 Expect.isTrue(checkStopping(sw)); | 100 Expect.isTrue(checkStopping(sw)); |
| 59 checkRestart(); | 101 checkRestart(); |
| 102 checkReset(); |
| 60 } | 103 } |
| 61 } | 104 } |
| 62 | 105 |
| 63 main() { | 106 main() { |
| 64 StopWatchTest.testMain(); | 107 StopwatchTest.testMain(); |
| 65 } | 108 } |
| OLD | NEW |