Skip to content
This repository has been archived by the owner on Jun 20, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:mbrobbel/dqcsim-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
jvanstraten committed Apr 11, 2019
2 parents 2d55a42 + 4e85ee2 commit dba8d86
Show file tree
Hide file tree
Showing 8 changed files with 1,141 additions and 7 deletions.
21 changes: 19 additions & 2 deletions dqcsim/src/common/log/tee_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl ::std::str::FromStr for TeeFileConfiguration {
let file: PathBuf = splitter
.next()
.ok_or_else(|| {
TeeFileError::ParseError("Expected a colon in tee file description.".to_string())
TeeFileError::ParseError("expected a colon in tee file description".to_string())
})?
.into();
Ok(TeeFileConfiguration { filter, file })
Expand Down Expand Up @@ -111,7 +111,17 @@ mod test {
TeeFileConfiguration::from_str("info:/tmp/hello:/there").unwrap(),
TeeFileConfiguration::new(LoglevelFilter::Info, "/tmp/hello:/there"),
);
assert!(TeeFileConfiguration::from_str("hello").is_err());

let tfc = TeeFileConfiguration::from_str("hello");
assert!(tfc.is_err());
assert_eq!(tfc.unwrap_err().to_string(), "hello is not a valid loglevel filter, valid values are off, fatal, error, warn, note, info, debug, or trace");

let tfc = TeeFileConfiguration::from_str("info");
assert!(tfc.is_err());
assert_eq!(
tfc.unwrap_err().to_string(),
"expected a colon in tee file description"
);
}

#[test]
Expand All @@ -129,6 +139,13 @@ mod test {
format!("{:?}", tf),
"TeeFileConfiguration { filter: Info, file: \"hello:/there\" }"
);

let tfc = TeeFileConfiguration::new(LoglevelFilter::Trace, "/dev/zero");
let tf = TeeFile::new(tfc);

assert!(
format!("{:?}", tf.unwrap()).starts_with("TeeFile { configuration: TeeFileConfiguration { filter: Trace, file: \"/dev/zero\" }, buffer: Some(File {")
);
}

#[test]
Expand Down
62 changes: 62 additions & 0 deletions dqcsim/src/common/types/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl Cycle {

impl Into<u64> for Cycle {
fn into(self) -> u64 {
assert!(self.0 >= 0, "Cycle count negative");
self.0 as u64
}
}
Expand All @@ -78,3 +79,64 @@ impl Into<i64> for Cycle {
self.0
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn zero() {
let c = Cycle::t_zero();
assert_eq!(c, Cycle(0i64));
}

#[test]
fn advance() {
let c = Cycle::t_zero();
let c = c.advance(1234u64);
assert_eq!(c, Cycle(1234));
let c = c.advance(1u64);
assert_eq!(c, Cycle(1235));
}

#[test]
#[should_panic]
fn overflow() {
let c = Cycle::t_zero();
c.advance(std::u64::MAX);
}

#[test]
#[should_panic]
fn convert_negative() {
let a = 18446744073709551574u64;
let c = Cycle(-42);
let c: u64 = c.into();
assert_eq!(a, c);
}

#[test]
fn convert() {
let a = 1234u64;
let c = Cycle(1234);
let c: u64 = c.into();
assert_eq!(a, c);

let a = 1234i64;
let c = Cycle(1234);
let c: i64 = c.into();
assert_eq!(a, c);
}

#[test]
fn ops() {
let a = Cycle(21);
let b: CycleDelta = 21;
let c: CycleDelta = -2;
assert_eq!(a + b, Cycle(42));
assert_eq!(a - b, Cycle(0));
assert_eq!(a + c, Cycle(19));
assert_eq!(a - c, Cycle(23));
assert_eq!(a - a, 0 as CycleDelta);
}
}
Loading

0 comments on commit dba8d86

Please sign in to comment.