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