Add Stopwatch.isRunning.

BUG= http://dartbug.com/8393

Review URL: https://codereview.chromium.org//12221073

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@18241 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
floitsch@google.com
2013-02-08 10:45:16 +00:00
parent 47bf8ce91d
commit caa7fe236d
2 changed files with 26 additions and 7 deletions
+10 -7
View File
@@ -63,6 +63,11 @@ abstract class Stopwatch {
* Returns the frequency of the elapsed counter in Hz.
*/
int get frequency;
/**
* Returns wether the [StopWatch] is currently running.
*/
bool get isRunning;
}
class _StopwatchImpl implements Stopwatch {
@@ -77,14 +82,12 @@ class _StopwatchImpl implements Stopwatch {
_StopwatchImpl() : _start = null, _stop = null {}
void start() {
if (isRunning) return;
if (_start == null) {
// This stopwatch has never been started.
_start = _now();
} else {
if (_stop == null) {
return;
}
// Restarting this stopwatch. Prepend the elapsed time to the current
// Restart this stopwatch. Prepend the elapsed time to the current
// start time.
_start = _now() - (_stop - _start);
_stop = null;
@@ -92,9 +95,7 @@ class _StopwatchImpl implements Stopwatch {
}
void stop() {
if (_start == null || _stop != null) {
return;
}
if (!isRunning) return;
_stop = _now();
}
@@ -127,6 +128,8 @@ class _StopwatchImpl implements Stopwatch {
int get frequency => _frequency();
bool get isRunning => _start != null && _stop == null;
external static int _frequency();
external static int _now();
}
+16
View File
@@ -8,7 +8,9 @@ library stopwatch_test;
class StopwatchTest {
static bool checkTicking(Stopwatch sw) {
Expect.isFalse(sw.isRunning);
sw.start();
Expect.isTrue(sw.isRunning);
for (int i = 0; i < 10000; i++) {
int.parse(i.toString());
if (sw.elapsedTicks > 0) {
@@ -20,10 +22,12 @@ class StopwatchTest {
static bool checkStopping(Stopwatch sw) {
sw.stop();
Expect.isFalse(sw.isRunning);
int v1 = sw.elapsedTicks;
Expect.isTrue(v1 > 0); // Expect a non-zero elapsed time.
Stopwatch sw2 = new Stopwatch(); // Used for verification.
sw2.start();
Expect.isTrue(sw2.isRunning);
int sw2LastElapsed = 0;
for (int i = 0; i < 100000; i++) {
int.parse(i.toString());
@@ -46,7 +50,9 @@ class StopwatchTest {
static checkRestart() {
Stopwatch sw = new Stopwatch();
Expect.isFalse(sw.isRunning);
sw.start();
Expect.isTrue(sw.isRunning);
for (int i = 0; i < 100000; i++) {
int.parse(i.toString());
if (sw.elapsedTicks > 0) {
@@ -54,8 +60,10 @@ class StopwatchTest {
}
}
sw.stop();
Expect.isFalse(sw.isRunning);
int initial = sw.elapsedTicks;
sw.start();
Expect.isTrue(sw.isRunning);
for (int i = 0; i < 100000; i++) {
int.parse(i.toString());
if (sw.elapsedTicks > initial) {
@@ -63,12 +71,15 @@ class StopwatchTest {
}
}
sw.stop();
Expect.isFalse(sw.isRunning);
Expect.isTrue(sw.elapsedTicks > initial);
}
static checkReset() {
Stopwatch sw = new Stopwatch();
Expect.isFalse(sw.isRunning);
sw.start();
Expect.isTrue(sw.isRunning);
for (int i = 0; i < 100000; i++) {
int.parse(i.toString());
if (sw.elapsedTicks > 0) {
@@ -76,9 +87,12 @@ class StopwatchTest {
}
}
sw.stop();
Expect.isFalse(sw.isRunning);
sw.reset();
Expect.isFalse(sw.isRunning);
Expect.equals(0, sw.elapsedTicks);
sw.start();
Expect.isTrue(sw.isRunning);
for (int i = 0; i < 100000; i++) {
int.parse(i.toString());
if (sw.elapsedTicks > 0) {
@@ -86,6 +100,7 @@ class StopwatchTest {
}
}
sw.reset();
Expect.isTrue(sw.isRunning);
for (int i = 0; i < 100000; i++) {
int.parse(i.toString());
if (sw.elapsedTicks > 0) {
@@ -93,6 +108,7 @@ class StopwatchTest {
}
}
sw.stop();
Expect.isFalse(sw.isRunning);
Expect.isTrue(sw.elapsedTicks > 0);
}