| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 Google Inc. All Rights Reserved. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 | |
| 15 part of quiver.testing.time; | |
| 16 | |
| 17 /** | |
| 18 * Returns the current test time in microseconds. | |
| 19 */ | |
| 20 typedef int Now(); | |
| 21 | |
| 22 /** | |
| 23 * A [Stopwatch] implementation that gets the current time in microseconds | |
| 24 * via a user-supplied function. | |
| 25 */ | |
| 26 class FakeStopwatch implements Stopwatch { | |
| 27 Now _now; | |
| 28 int frequency; | |
| 29 int _start; | |
| 30 int _stop; | |
| 31 | |
| 32 FakeStopwatch(int now(), int this.frequency) | |
| 33 : _now = now, | |
| 34 _start = null, | |
| 35 _stop = null; | |
| 36 | |
| 37 void start() { | |
| 38 if (isRunning) return; | |
| 39 if (_start == null) { | |
| 40 _start = _now(); | |
| 41 } else { | |
| 42 _start = _now() - (_stop - _start); | |
| 43 _stop = null; | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 void stop() { | |
| 48 if (!isRunning) return; | |
| 49 _stop = _now(); | |
| 50 } | |
| 51 | |
| 52 void reset() { | |
| 53 if (_start == null) return; | |
| 54 _start = _now(); | |
| 55 if (_stop != null) { | |
| 56 _stop = _start; | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 int get elapsedTicks { | |
| 61 if (_start == null) { | |
| 62 return 0; | |
| 63 } | |
| 64 return (_stop == null) ? (_now() - _start) : (_stop - _start); | |
| 65 } | |
| 66 | |
| 67 Duration get elapsed => new Duration(microseconds: elapsedMicroseconds); | |
| 68 | |
| 69 int get elapsedMicroseconds => (elapsedTicks * 1000000) ~/ frequency; | |
| 70 | |
| 71 int get elapsedMilliseconds => (elapsedTicks * 1000) ~/ frequency; | |
| 72 | |
| 73 bool get isRunning => _start != null && _stop == null; | |
| 74 } | |
| OLD | NEW |