Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive Clone trait for Stopwatch #3

Merged
merged 2 commits into from
Apr 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::default::Default;
use std::fmt;
use std::num::ToPrimitive;

#[derive(Copy)]
#[derive(Clone, Copy)]
pub struct Stopwatch {
start_time: Option<u64>,
elapsed: Duration,
Expand Down
34 changes: 17 additions & 17 deletions tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
#![feature(core)]
#![feature(old_io)]
#![feature(std_misc)]

extern crate time;
extern crate stopwatch;

use stopwatch::{Stopwatch};
use std::num::SignedInt;
use std::old_io::timer;
use std::time::Duration;

static SLEEP_MS: i64 = 50;
static TOLERANCE_PERCENTAGE: f64 = 0.3;
Expand All @@ -32,44 +27,44 @@ fn elapsed_none() {
#[test]
fn elapsed_ms() {
let sw = Stopwatch::start_new();
timer::sleep(Duration::milliseconds(SLEEP_MS));
sleep_ms(SLEEP_MS);
assert_sw_near(sw, SLEEP_MS);
}

#[test]
fn stop() {
let mut sw = Stopwatch::start_new();
timer::sleep(Duration::milliseconds(SLEEP_MS));
sleep_ms(SLEEP_MS);
sw.stop();
assert_sw_near(sw, SLEEP_MS);

timer::sleep(Duration::milliseconds(SLEEP_MS));
sleep_ms(SLEEP_MS);
assert_sw_near(sw, SLEEP_MS);
}

#[test]
fn resume_once() {
let mut sw = Stopwatch::start_new();
timer::sleep(Duration::milliseconds(SLEEP_MS));
sleep_ms(SLEEP_MS);
sw.stop();
assert_sw_near(sw, SLEEP_MS);
sw.start();
timer::sleep(Duration::milliseconds(SLEEP_MS));
sleep_ms(SLEEP_MS);
assert_sw_near(sw, 2 * SLEEP_MS);
}

#[test]
fn resume_twice() {
let mut sw = Stopwatch::start_new();
timer::sleep(Duration::milliseconds(SLEEP_MS));
sleep_ms(SLEEP_MS);
sw.stop();
assert_sw_near(sw, SLEEP_MS);
sw.start();
timer::sleep(Duration::milliseconds(SLEEP_MS));
sleep_ms(SLEEP_MS);
sw.stop();
assert_sw_near(sw, 2 * SLEEP_MS);
sw.start();
timer::sleep(Duration::milliseconds(SLEEP_MS));
sleep_ms(SLEEP_MS);
assert_sw_near(sw, 3 * SLEEP_MS);
}

Expand All @@ -86,16 +81,16 @@ fn is_running() {
#[test]
fn restart() {
let mut sw = Stopwatch::start_new();
timer::sleep(Duration::milliseconds(SLEEP_MS));
sleep_ms(SLEEP_MS);
sw.restart();
timer::sleep(Duration::milliseconds(SLEEP_MS));
sleep_ms(SLEEP_MS);
assert_sw_near(sw, SLEEP_MS);
}

#[test]
fn reset() {
let mut sw = Stopwatch::start_new();
timer::sleep(Duration::milliseconds(SLEEP_MS));
sleep_ms(SLEEP_MS);
sw.reset();
assert!(!sw.is_running());
assert_eq!(sw.elapsed_ms(), 0);
Expand All @@ -105,6 +100,11 @@ fn reset() {

/////////////// helpers

fn sleep_ms(ms: i64) {
use std::num::ToPrimitive;
std::thread::sleep_ms(ms.to_u32().unwrap())
}

fn assert_near(x: i64, y: i64, tolerance: i64) {
let diff = (x - y).abs();
if diff > tolerance {
Expand All @@ -115,4 +115,4 @@ fn assert_near(x: i64, y: i64, tolerance: i64) {
fn assert_sw_near(sw: Stopwatch, elapsed: i64) {
let tolerance_value = (TOLERANCE_PERCENTAGE * elapsed as f64) as i64;
assert_near(elapsed, sw.elapsed_ms(), tolerance_value);
}
}