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