diff --git a/tests/compile-fail/test_const_generic_array_zero_length.rs b/tests/compile-fail/test_const_generic_array_zero_length.rs new file mode 100644 index 0000000..3b69f1a --- /dev/null +++ b/tests/compile-fail/test_const_generic_array_zero_length.rs @@ -0,0 +1,9 @@ +extern crate ringbuffer; + +use ringbuffer::ConstGenericRingBuffer; + +fn main() { + let _ = ConstGenericRingBuffer::::new(); + //~^ note: the above error was encountered while instantiating `fn ringbuffer::ConstGenericRingBuffer::::new::<0>` + // ringbuffer can't be zero length +} diff --git a/tests/compile-fail/test_const_generic_array_zero_length_new.rs b/tests/compile-fail/test_const_generic_array_zero_length_new.rs new file mode 100644 index 0000000..b080de1 --- /dev/null +++ b/tests/compile-fail/test_const_generic_array_zero_length_new.rs @@ -0,0 +1,10 @@ +extern crate ringbuffer; + +use ringbuffer::{ConstGenericRingBuffer, RingBuffer}; + +fn main() { + let mut buf = ConstGenericRingBuffer::new::<0>(); + //~^ note: the above error was encountered while instantiating `fn ringbuffer::ConstGenericRingBuffer::::new::<0>` + // ringbuffer can't be zero length + buf.push(5); +} diff --git a/tests/compiletests.rs b/tests/compiletests.rs new file mode 100644 index 0000000..f48163e --- /dev/null +++ b/tests/compiletests.rs @@ -0,0 +1,23 @@ +extern crate compiletest_rs as compiletest; + +use std::path::PathBuf; + +#[cfg(test)] +mod conversions; + +fn run_mode(mode: &'static str) { + let mut config = compiletest::Config::default(); + + config.mode = mode.parse().expect("Invalid mode"); + config.src_base = PathBuf::from(format!("tests/{}", mode)); + config.link_deps(); // Populate config.target_rustcflags with dependencies on the path + config.clean_rmeta(); // If your tests import the parent crate, this helps with E0464 + + compiletest::run_tests(&config); +} + +#[test] +#[cfg_attr(miri, ignore)] +fn compile_test() { + run_mode("compile-fail"); +} diff --git a/tests/conversions.rs b/tests/conversions.rs new file mode 100644 index 0000000..e3c13a2 --- /dev/null +++ b/tests/conversions.rs @@ -0,0 +1,135 @@ +extern crate alloc; + +use alloc::collections::{LinkedList, VecDeque}; +use alloc::string::ToString; +use core::ops::Deref; +use ringbuffer::RingBuffer; +use ringbuffer::{AllocRingBuffer, ConstGenericRingBuffer, GrowableAllocRingBuffer}; +use std::vec; + +macro_rules! convert_test { + ($name: ident: $from: expr => $to: ty) => { + #[test] + fn $name() { + let a = $from; + + let mut b: $to = a.into(); + assert_eq!(b.to_vec(), vec!['1', '2']); + b.push('3'); + assert_eq!(b, b); + } + }; +} + +macro_rules! convert_tests { + ( + [$($name: ident: $from: expr),* $(,)?] + => $to: ty + ) => { + $( + convert_test!($name: $from => $to); + )* + }; +} + +convert_tests!( + [ + alloc_from_vec: vec!['1', '2'], + alloc_from_ll: {let mut l = LinkedList::new(); l.push_back('1'); l.push_back('2'); l}, + alloc_from_vd: {let mut l = VecDeque::new(); l.push_back('1'); l.push_back('2'); l}, + alloc_from_str: "12".to_string(), + alloc_from_str_slice: "12", + alloc_from_slice: {let a: &[char] = &['1', '2']; a}, + alloc_from_const_slice: {let a: &[char; 2] = &['1', '2']; a}, + alloc_from_arr: {let a: [char; 2] = ['1', '2']; a}, + + alloc_from_cgrb: {let a = ConstGenericRingBuffer::from(['1', '2']); a}, + alloc_from_garb: {let a = GrowableAllocRingBuffer::from(['1', '2']); a}, + ] => AllocRingBuffer::<_> +); + +convert_tests!( + [ + growable_alloc_from_vec: vec!['1', '2'], + growable_alloc_from_ll: {let mut l = LinkedList::new(); l.push_back('1'); l.push_back('2'); l}, + growable_alloc_from_vd: {let mut l = VecDeque::new(); l.push_back('1'); l.push_back('2'); l}, + growable_alloc_from_str: "12".to_string(), + growable_alloc_from_str_slice: "12", + growable_alloc_from_slice: {let a: &[char] = &['1', '2']; a}, + growable_alloc_from_const_slice: {let a: &[char; 2] = &['1', '2']; a}, + growable_alloc_from_arr: {let a: [char; 2] = ['1', '2']; a}, + + growable_alloc_from_cgrb: {let a = ConstGenericRingBuffer::from(['1', '2']); a}, + growable_alloc_from_arb: {let a = AllocRingBuffer::from(['1', '2']); a}, + ] => GrowableAllocRingBuffer::<_> +); + +convert_tests!( + [ + const_from_vec: vec!['1', '2'], + const_from_ll: {let mut l = LinkedList::new(); l.push_back('1'); l.push_back('2'); l}, + const_from_vd: {let mut l = VecDeque::new(); l.push_back('1'); l.push_back('2'); l}, + const_from_str: "12".to_string(), + const_from_str_slice: "12", + const_from_slice: {let a: &[char] = &['1', '2']; a}, + const_from_const_slice: {let a: &[char; 2] = &['1', '2']; a}, + const_from_arr: {let a: [char; 2] = ['1', '2']; a}, + + const_from_garb: {let a = GrowableAllocRingBuffer::from(['1', '2']); a}, + const_from_arb: {let a = AllocRingBuffer::from(['1', '2']); a}, + ] => ConstGenericRingBuffer::<_, 2> +); + +#[test] +fn test_extra_conversions_growable() { + let a: &mut [i32; 2] = &mut [1, 2]; + let a = GrowableAllocRingBuffer::from(a); + assert_eq!(a.to_vec(), vec![1, 2]); + + let a: &mut [i32] = &mut [1, 2]; + let a = GrowableAllocRingBuffer::from(a); + assert_eq!(a.to_vec(), vec![1, 2]); + + let mut b = VecDeque::::new(); + b.push_back(1); + b.push_back(2); + assert_eq!(a.deref(), &b); + assert_eq!(a.as_ref(), &b); +} + +#[test] +fn test_extra_conversions_alloc() { + let a: &mut [i32; 2] = &mut [1, 2]; + let a = AllocRingBuffer::from(a); + assert_eq!(a.to_vec(), vec![1, 2]); + + let a: &mut [i32] = &mut [1, 2]; + let a = AllocRingBuffer::from(a); + assert_eq!(a.to_vec(), vec![1, 2]); +} + +#[test] +fn test_extra_conversions_const() { + let a: &mut [i32; 2] = &mut [1, 2]; + let a = ConstGenericRingBuffer::<_, 2>::from(a); + assert_eq!(a.to_vec(), vec![1, 2]); + + let a: &mut [i32] = &mut [1, 2]; + let a = ConstGenericRingBuffer::<_, 2>::from(a); + assert_eq!(a.to_vec(), vec![1, 2]); +} + +#[test] +fn test_const_generic_new_parameter() { + // Can we specify size only on the method? + let mut a = ConstGenericRingBuffer::new::<2>(); + a.push(5); + + // Can we specify size in both positions? + let mut a = ConstGenericRingBuffer::::new::<50>(); + a.push(5); + + // Can we specify size only on the struct? + let mut a = ConstGenericRingBuffer::::new(); + a.push(5); +}