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