diff options
| author | troido <troido@protonmail.com> | 2020-02-04 10:07:57 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-04 10:07:57 +0100 |
| commit | 057f38d6933207fb2e39c32f94269db83e7f1db2 (patch) | |
| tree | 930531b3ef7e0443d6a84448840a155317c9a7b9 /src/util.rs | |
| parent | 33576fe7a5f241dbd884315c3552ad8186562e74 (diff) | |
added some unit tests; added hashmap! macro
Diffstat (limited to 'src/util.rs')
| -rw-r--r-- | src/util.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs index fed0400..7f3c7cd 100644 --- a/src/util.rs +++ b/src/util.rs @@ -10,3 +10,31 @@ pub fn clamp<T: Ord>(val: T, lower: T, upper: T) -> T{ pub trait ToJson { fn to_json(&self) -> Value; } + +#[macro_export] +macro_rules! hashmap { + ( $($key:expr => $value:expr ),* ) => {{ + #[allow(unused_mut)] + let mut h = std::collections::HashMap::new(); + $( + h.insert($key, $value); + )* + h + }} +} + +#[cfg(test)] +mod tests { + use std::collections::HashMap; + #[test] + fn test_hashmap_macro() { + let mut h = hashmap!("hello" => 1, "world" => 2); + assert_eq!(h.remove("hello"), Some(1)); + assert_eq!(h.remove("world"), Some(2)); + assert!(h.is_empty()); + let h2: HashMap<i32, usize> = hashmap!(); + assert!(h2.is_empty()); + assert_eq!(h2, HashMap::new()); + + } +} |
