OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // Dart test program for testing stopwatch support. | |
6 | |
7 class StopwatchTest { | |
8 static bool checkTicking(Stopwatch sw) { | |
9 sw.start(); | |
10 for (int i = 0; i < 10000; i++) { | |
11 Math.parseInt(i.toString()); | |
12 if (sw.elapsed() > 0) { | |
13 break; | |
14 } | |
15 } | |
16 return sw.elapsed() > 0; | |
17 } | |
18 | |
19 static bool checkStopping(Stopwatch sw) { | |
20 sw.stop(); | |
21 int v1 = sw.elapsed(); | |
22 Expect.isTrue(v1 > 0); // Expect a non-zero elapsed time. | |
23 Stopwatch sw2 = new Stopwatch(); // Used for verification. | |
24 sw2.start(); | |
25 int sw2LastElapsed = 0; | |
26 for (int i = 0; i < 100000; i++) { | |
27 Math.parseInt(i.toString()); | |
28 int v2 = sw.elapsed(); | |
29 if (v1 != v2) { | |
30 return false; | |
31 } | |
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(); | |
38 } | |
39 // The test only makes sense if measureable time elapsed and elapsed time | |
40 // on the stopped Stopwatch did not increase. | |
41 Expect.isTrue(sw2.elapsed() > 0); | |
42 return true; | |
43 } | |
44 | |
45 static checkRestart() { | |
46 Stopwatch sw = new Stopwatch(); | |
47 sw.start(); | |
48 for (int i = 0; i < 100000; i++) { | |
49 Math.parseInt(i.toString()); | |
50 if (sw.elapsed() > 0) { | |
51 break; | |
52 } | |
53 } | |
54 sw.stop(); | |
55 int initial = sw.elapsed(); | |
56 sw.start(); | |
57 for (int i = 0; i < 100000; i++) { | |
58 Math.parseInt(i.toString()); | |
59 if (sw.elapsed() > initial) { | |
60 break; | |
61 } | |
62 } | |
63 sw.stop(); | |
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); | |
95 } | |
96 | |
97 static testMain() { | |
98 Stopwatch sw = new Stopwatch(); | |
99 Expect.isTrue(checkTicking(sw)); | |
100 Expect.isTrue(checkStopping(sw)); | |
101 checkRestart(); | |
102 checkReset(); | |
103 } | |
104 } | |
105 | |
106 main() { | |
107 StopwatchTest.testMain(); | |
108 } | |
OLD | NEW |