feat: add helpers module
This commit is contained in:
parent
75aa23dcd1
commit
00407542e4
2 changed files with 21 additions and 14 deletions
2
src/helpers.rs
Normal file
2
src/helpers.rs
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
// Use this file if you want to extract sections of your solutions.
|
||||||
|
// Example import: `use aoc::helpers::example_fn;`
|
33
src/lib.rs
33
src/lib.rs
|
@ -1,6 +1,10 @@
|
||||||
|
// This file contains template helpers.
|
||||||
|
// Prefer `./helpers.rs` if you want to extract code from your solutions.
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
|
pub mod helpers;
|
||||||
|
|
||||||
pub const ANSI_ITALIC: &str = "\x1b[3m";
|
pub const ANSI_ITALIC: &str = "\x1b[3m";
|
||||||
pub const ANSI_BOLD: &str = "\x1b[1m";
|
pub const ANSI_BOLD: &str = "\x1b[1m";
|
||||||
pub const ANSI_RESET: &str = "\x1b[0m";
|
pub const ANSI_RESET: &str = "\x1b[0m";
|
||||||
|
@ -52,7 +56,7 @@ pub fn parse_exec_time(output: &str) -> f64 {
|
||||||
} else {
|
} else {
|
||||||
let timing = l.split("(elapsed: ").last().unwrap();
|
let timing = l.split("(elapsed: ").last().unwrap();
|
||||||
// use `contains` istd. of `ends_with`: string may contain ANSI escape sequences.
|
// use `contains` istd. of `ends_with`: string may contain ANSI escape sequences.
|
||||||
// possible time formats: see [rust/library/core/src/time.rs](https://github.com/rust-lang/rust/blob/1.57.0/library/core/src/time.rs#L1225-L1249).
|
// for possible time formats, see: https://github.com/rust-lang/rust/blob/1.64.0/library/core/src/time.rs#L1176-L1200
|
||||||
if timing.contains("ns)") {
|
if timing.contains("ns)") {
|
||||||
acc // range below rounding precision.
|
acc // range below rounding precision.
|
||||||
} else if timing.contains("µs)") {
|
} else if timing.contains("µs)") {
|
||||||
|
@ -68,23 +72,24 @@ pub fn parse_exec_time(output: &str) -> f64 {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// copied from: https://github.com/rust-lang/rust/blob/1.64.0/library/std/src/macros.rs#L328-L333
|
||||||
|
#[cfg(test)]
|
||||||
|
macro_rules! assert_approx_eq {
|
||||||
|
($a:expr, $b:expr) => {{
|
||||||
|
let (a, b) = (&$a, &$b);
|
||||||
|
assert!(
|
||||||
|
(*a - *b).abs() < 1.0e-6,
|
||||||
|
"{} is not approximately equal to {}",
|
||||||
|
*a,
|
||||||
|
*b
|
||||||
|
);
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
/// copied from: [rust/library/std/src/macros.rs](https://github.com/rust-lang/rust/blob/1.57.0/library/std/src/macros.rs#L311-L316)
|
|
||||||
macro_rules! assert_approx_eq {
|
|
||||||
($a:expr, $b:expr) => {{
|
|
||||||
let (a, b) = (&$a, &$b);
|
|
||||||
assert!(
|
|
||||||
(*a - *b).abs() < 1.0e-6,
|
|
||||||
"{} is not approximately equal to {}",
|
|
||||||
*a,
|
|
||||||
*b
|
|
||||||
);
|
|
||||||
}};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_exec_time() {
|
fn test_parse_exec_time() {
|
||||||
assert_approx_eq!(
|
assert_approx_eq!(
|
||||||
|
|
Loading…
Add table
Reference in a new issue