dyn_vec/src/test.rs
2022-05-11 13:27:15 -05:00

123 lines
3.2 KiB
Rust

use super::prelude::{Vec, vec};
use std::{fmt::Debug, sync::atomic::{AtomicBool, Ordering}};
trait DebugExt: Debug {
fn debug(&self) -> String {
format!("{:?}", self)
}
}
impl<T: Debug> DebugExt for T {}
#[test]
fn basic_push() {
let mut vec: Vec<i32> = Vec::new();
vec.push(3);
vec.push(5);
vec.push(7);
assert_eq!(vec, [3, 5, 7]);
}
#[test]
fn box_push() {
let mut vec: Vec<dyn Debug> = Vec::new();
vec.push_box(Box::new(1));
vec.push_box(Box::new(String::from("foo")));
vec.push_box(Box::new(true));
assert_eq!(vec.debug(), "[1, \"foo\", true]");
}
#[test]
fn unsize_push() {
let mut vec: Vec<dyn Debug> = Vec::new();
vec.push_unsize(1);
vec.push_unsize(String::from("foo"));
vec.push_unsize(true);
assert_eq!(vec.debug(), "[1, \"foo\", true]");
}
#[test]
fn all_macro() {
let vec: Vec<i32> = vec![3, 5, 7];
assert_eq!(vec, [3, 5, 7]);
let vec2: Vec<dyn Debug> = vec![box:
Box::new(1) as _,
Box::new(String::from("foo")) as _,
Box::new(true) as _,
];
let vec3: Vec<dyn Debug> = vec![unsized: 1, String::from("foo"), true];
// assert_eq!(vec2, vec3); // doesnt compile, but would theoretically work
assert_eq!(vec2.debug(), vec3.debug());
}
#[test]
fn dropped() {
static DROPPED: AtomicBool = AtomicBool::new(false);
#[derive(Debug)] // for dyn Debug
struct FunkyDrop;
impl Drop for FunkyDrop {
fn drop(&mut self) {
DROPPED.store(true, Ordering::SeqCst);
}
}
let vec: Vec<dyn Debug> = vec![unsized: 1, FunkyDrop, true];
assert_eq!(DROPPED.load(Ordering::SeqCst), false);
drop(vec);
assert_eq!(DROPPED.load(Ordering::SeqCst), true);
}
#[test]
fn get() {
let vec: Vec<i32> = vec![3, 5, 7];
assert_eq!(vec.get(0).copied(), Some(3));
assert_eq!(vec.get(1).copied(), Some(5));
assert_eq!(vec.get(2).copied(), Some(7));
assert_eq!(vec.get(3).copied(), None);
}
#[test]
#[should_panic = "index out of bounds: the len is 3 but the index is 3"]
fn index() {
let vec: Vec<i32> = vec![3, 5, 7];
assert_eq!(vec[0], 3);
assert_eq!(vec[1], 5);
assert_eq!(vec[2], 7);
vec[3];
}
#[test]
fn slice_flatten() {
let mut vec: Vec<[i32]> = vec![unsized: [1, 2, 3], [4, 5], [6, 7, 8, 9]];
assert_eq!(vec.as_slice_flatten(), [1, 2, 3, 4, 5, 6, 7, 8, 9]);
vec.as_mut_slice_flatten()[4] = 10;
assert_eq!(vec[1], [4, 10]);
}
#[test]
fn iteration() {
let mut vec: Vec<dyn Debug> = vec![unsized: 1, String::from("foo"), true];
let mut iter = vec.iter();
assert_eq!(iter.next().unwrap().debug(), "1");
assert_eq!(iter.next().unwrap().debug(), "\"foo\"");
assert_eq!(iter.next().unwrap().debug(), "true");
assert_eq!(iter.next().map(|_|()), None);
let mut iter = vec.iter_mut(); // TODO: create a trait to properly test this
assert_eq!(iter.next().unwrap().debug(), "1");
assert_eq!(iter.next().unwrap().debug(), "\"foo\"");
assert_eq!(iter.next().unwrap().debug(), "true");
assert_eq!(iter.next().map(|_|()), None);
let mut debugs = Vec::new(); // using custom vec instead of std vec >:)
for item in vec {
debugs.push(item.debug());
}
assert_eq!(debugs, ["1", "\"foo\"", "true"]);
}