Skip to content

Commit

Permalink
Merge pull request #126 from NULLx76/index-with-usizes
Browse files Browse the repository at this point in the history
add compile tests
  • Loading branch information
jdonszelmann committed Sep 15, 2023
2 parents 5283276 + f7bc336 commit f4e044d
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/compile-fail/test_const_generic_array_zero_length.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
extern crate ringbuffer;

use ringbuffer::ConstGenericRingBuffer;

fn main() {
let _ = ConstGenericRingBuffer::<i32, 0>::new();
//~^ note: the above error was encountered while instantiating `fn ringbuffer::ConstGenericRingBuffer::<i32, 0>::new::<0>`
// ringbuffer can't be zero length
}
10 changes: 10 additions & 0 deletions tests/compile-fail/test_const_generic_array_zero_length_new.rs
Original file line number Diff line number Diff line change
@@ -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::<i32, 0>::new::<0>`
// ringbuffer can't be zero length
buf.push(5);
}
23 changes: 23 additions & 0 deletions tests/compiletests.rs
Original file line number Diff line number Diff line change
@@ -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");
}
135 changes: 135 additions & 0 deletions tests/conversions.rs
Original file line number Diff line number Diff line change
@@ -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::<i32>::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::<i32, 50>::new::<50>();
a.push(5);

// Can we specify size only on the struct?
let mut a = ConstGenericRingBuffer::<i32, 50>::new();
a.push(5);
}

1 comment on commit f4e044d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.