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