Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: tests/corelib/stopwatch_test.dart

Issue 12221073: Add Stopwatch.isRunning. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/core/stopwatch.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 8
9 class StopwatchTest { 9 class StopwatchTest {
10 static bool checkTicking(Stopwatch sw) { 10 static bool checkTicking(Stopwatch sw) {
11 Expect.isFalse(sw.isRunning);
11 sw.start(); 12 sw.start();
13 Expect.isTrue(sw.isRunning);
12 for (int i = 0; i < 10000; i++) { 14 for (int i = 0; i < 10000; i++) {
13 int.parse(i.toString()); 15 int.parse(i.toString());
14 if (sw.elapsedTicks > 0) { 16 if (sw.elapsedTicks > 0) {
15 break; 17 break;
16 } 18 }
17 } 19 }
18 return sw.elapsedTicks > 0; 20 return sw.elapsedTicks > 0;
19 } 21 }
20 22
21 static bool checkStopping(Stopwatch sw) { 23 static bool checkStopping(Stopwatch sw) {
22 sw.stop(); 24 sw.stop();
25 Expect.isFalse(sw.isRunning);
23 int v1 = sw.elapsedTicks; 26 int v1 = sw.elapsedTicks;
24 Expect.isTrue(v1 > 0); // Expect a non-zero elapsed time. 27 Expect.isTrue(v1 > 0); // Expect a non-zero elapsed time.
25 Stopwatch sw2 = new Stopwatch(); // Used for verification. 28 Stopwatch sw2 = new Stopwatch(); // Used for verification.
26 sw2.start(); 29 sw2.start();
30 Expect.isTrue(sw2.isRunning);
27 int sw2LastElapsed = 0; 31 int sw2LastElapsed = 0;
28 for (int i = 0; i < 100000; i++) { 32 for (int i = 0; i < 100000; i++) {
29 int.parse(i.toString()); 33 int.parse(i.toString());
30 int v2 = sw.elapsedTicks; 34 int v2 = sw.elapsedTicks;
31 if (v1 != v2) { 35 if (v1 != v2) {
32 return false; 36 return false;
33 } 37 }
34 // If sw2 elapsed twice then sw must have advanced too if it wasn't 38 // If sw2 elapsed twice then sw must have advanced too if it wasn't
35 // stopped. 39 // stopped.
36 if (sw2LastElapsed > 0 && sw2.elapsedTicks > sw2LastElapsed) { 40 if (sw2LastElapsed > 0 && sw2.elapsedTicks > sw2LastElapsed) {
37 break; 41 break;
38 } 42 }
39 sw2LastElapsed = sw2.elapsedTicks; 43 sw2LastElapsed = sw2.elapsedTicks;
40 } 44 }
41 // The test only makes sense if measureable time elapsed and elapsed time 45 // The test only makes sense if measureable time elapsed and elapsed time
42 // on the stopped Stopwatch did not increase. 46 // on the stopped Stopwatch did not increase.
43 Expect.isTrue(sw2.elapsedTicks > 0); 47 Expect.isTrue(sw2.elapsedTicks > 0);
44 return true; 48 return true;
45 } 49 }
46 50
47 static checkRestart() { 51 static checkRestart() {
48 Stopwatch sw = new Stopwatch(); 52 Stopwatch sw = new Stopwatch();
53 Expect.isFalse(sw.isRunning);
49 sw.start(); 54 sw.start();
55 Expect.isTrue(sw.isRunning);
50 for (int i = 0; i < 100000; i++) { 56 for (int i = 0; i < 100000; i++) {
51 int.parse(i.toString()); 57 int.parse(i.toString());
52 if (sw.elapsedTicks > 0) { 58 if (sw.elapsedTicks > 0) {
53 break; 59 break;
54 } 60 }
55 } 61 }
56 sw.stop(); 62 sw.stop();
63 Expect.isFalse(sw.isRunning);
57 int initial = sw.elapsedTicks; 64 int initial = sw.elapsedTicks;
58 sw.start(); 65 sw.start();
66 Expect.isTrue(sw.isRunning);
59 for (int i = 0; i < 100000; i++) { 67 for (int i = 0; i < 100000; i++) {
60 int.parse(i.toString()); 68 int.parse(i.toString());
61 if (sw.elapsedTicks > initial) { 69 if (sw.elapsedTicks > initial) {
62 break; 70 break;
63 } 71 }
64 } 72 }
65 sw.stop(); 73 sw.stop();
74 Expect.isFalse(sw.isRunning);
66 Expect.isTrue(sw.elapsedTicks > initial); 75 Expect.isTrue(sw.elapsedTicks > initial);
67 } 76 }
68 77
69 static checkReset() { 78 static checkReset() {
70 Stopwatch sw = new Stopwatch(); 79 Stopwatch sw = new Stopwatch();
80 Expect.isFalse(sw.isRunning);
71 sw.start(); 81 sw.start();
82 Expect.isTrue(sw.isRunning);
72 for (int i = 0; i < 100000; i++) { 83 for (int i = 0; i < 100000; i++) {
73 int.parse(i.toString()); 84 int.parse(i.toString());
74 if (sw.elapsedTicks > 0) { 85 if (sw.elapsedTicks > 0) {
75 break; 86 break;
76 } 87 }
77 } 88 }
78 sw.stop(); 89 sw.stop();
90 Expect.isFalse(sw.isRunning);
79 sw.reset(); 91 sw.reset();
92 Expect.isFalse(sw.isRunning);
80 Expect.equals(0, sw.elapsedTicks); 93 Expect.equals(0, sw.elapsedTicks);
81 sw.start(); 94 sw.start();
95 Expect.isTrue(sw.isRunning);
82 for (int i = 0; i < 100000; i++) { 96 for (int i = 0; i < 100000; i++) {
83 int.parse(i.toString()); 97 int.parse(i.toString());
84 if (sw.elapsedTicks > 0) { 98 if (sw.elapsedTicks > 0) {
85 break; 99 break;
86 } 100 }
87 } 101 }
88 sw.reset(); 102 sw.reset();
103 Expect.isTrue(sw.isRunning);
89 for (int i = 0; i < 100000; i++) { 104 for (int i = 0; i < 100000; i++) {
90 int.parse(i.toString()); 105 int.parse(i.toString());
91 if (sw.elapsedTicks > 0) { 106 if (sw.elapsedTicks > 0) {
92 break; 107 break;
93 } 108 }
94 } 109 }
95 sw.stop(); 110 sw.stop();
111 Expect.isFalse(sw.isRunning);
96 Expect.isTrue(sw.elapsedTicks > 0); 112 Expect.isTrue(sw.elapsedTicks > 0);
97 } 113 }
98 114
99 static testMain() { 115 static testMain() {
100 Stopwatch sw = new Stopwatch(); 116 Stopwatch sw = new Stopwatch();
101 Expect.isTrue(checkTicking(sw)); 117 Expect.isTrue(checkTicking(sw));
102 Expect.isTrue(checkStopping(sw)); 118 Expect.isTrue(checkStopping(sw));
103 checkRestart(); 119 checkRestart();
104 checkReset(); 120 checkReset();
105 } 121 }
106 } 122 }
107 123
108 main() { 124 main() {
109 StopwatchTest.testMain(); 125 StopwatchTest.testMain();
110 } 126 }
OLDNEW
« no previous file with comments | « sdk/lib/core/stopwatch.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698