From 0ab1a6618e2b000530aad6b3e212634fdc8ce8cf Mon Sep 17 00:00:00 2001 From: Ivan Date: Sun, 19 Aug 2018 10:40:28 +0200 Subject: [PATCH] Make tests as doctests - to be run by mdbook test --- src/vec-final.md | 144 +++++++++++++++++++++++------------------------ 1 file changed, 72 insertions(+), 72 deletions(-) diff --git a/src/vec-final.md b/src/vec-final.md index 55c2988..471b4f6 100644 --- a/src/vec-final.md +++ b/src/vec-final.md @@ -304,76 +304,76 @@ impl<'a, T> Drop for Drain<'a, T> { } } -# fn main() {} - -#[cfg(test)] -mod tests { - use super::*; - #[test] - fn create_push_pop() { - let mut v = Vec::new(); - v.push(1); - assert_eq!(1, v.len()); - assert_eq!(1, v[0]); - for i in v.iter_mut() { - *i += 1; - } - v.insert(0, 1); - let x = v.pop(); - assert_eq!(Some(2), x); - assert_eq!(1, v.len()); - v.push(10); - let x = v.remove(0); - assert_eq!(1, x); - assert_eq!(1, v.len()); - } - - #[test] - fn iter_test() { - let mut v = Vec::new(); - for i in 0..10 { - v.push(Box::new(i)) - } - let mut iter = v.into_iter(); - let first = iter.next().unwrap(); - let last = iter.next_back().unwrap(); - drop(iter); - assert_eq!(0, *first); - assert_eq!(9, *last); - } - - #[test] - fn test_drain() { - let mut v = Vec::new(); - for i in 0..10 { - v.push(Box::new(i)) - } - { - let mut drain = v.drain(); - let first = drain.next().unwrap(); - let last = drain.next_back().unwrap(); - assert_eq!(0, *first); - assert_eq!(9, *last); - } - assert_eq!(0, v.len()); - v.push(Box::new(1)); - assert_eq!(1, *v.pop().unwrap()); - } - - #[test] - fn test_zst() { - let mut v = Vec::new(); - for _i in 0..10 { - v.push(()) - } - - let mut count = 0; - - for _ in v.into_iter() { - count += 1 - } - - assert_eq!(10, count); - } -} +# fn main() { +# tests::create_push_pop(); +# tests::iter_test(); +# tests::test_drain(); +# tests::test_zst(); +# } + +# mod tests { +# use super::*; +# pub fn create_push_pop() { +# let mut v = Vec::new(); +# v.push(1); +# assert_eq!(1, v.len()); +# assert_eq!(1, v[0]); +# for i in v.iter_mut() { +# *i += 1; +# } +# v.insert(0, 1); +# let x = v.pop(); +# assert_eq!(Some(2), x); +# assert_eq!(1, v.len()); +# v.push(10); +# let x = v.remove(0); +# assert_eq!(1, x); +# assert_eq!(1, v.len()); +# } +# +# pub fn iter_test() { +# let mut v = Vec::new(); +# for i in 0..10 { +# v.push(Box::new(i)) +# } +# let mut iter = v.into_iter(); +# let first = iter.next().unwrap(); +# let last = iter.next_back().unwrap(); +# drop(iter); +# assert_eq!(0, *first); +# assert_eq!(9, *last); +# } +# +# pub fn test_drain() { +# let mut v = Vec::new(); +# for i in 0..10 { +# v.push(Box::new(i)) +# } +# { +# let mut drain = v.drain(); +# let first = drain.next().unwrap(); +# let last = drain.next_back().unwrap(); +# assert_eq!(0, *first); +# assert_eq!(9, *last); +# } +# assert_eq!(0, v.len()); +# v.push(Box::new(1)); +# assert_eq!(1, *v.pop().unwrap()); +# } +# +# pub fn test_zst() { +# let mut v = Vec::new(); +# for _i in 0..10 { +# v.push(()) +# } +# +# let mut count = 0; +# +# for _ in v.into_iter() { +# count += 1 +# } +# +# assert_eq!(10, count); +# } +# } ```