Skip to content

Commit f4e044d

Browse files
authored
Merge pull request #126 from NULLx76/index-with-usizes
add compile tests
2 parents 5283276 + f7bc336 commit f4e044d

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extern crate ringbuffer;
2+
3+
use ringbuffer::ConstGenericRingBuffer;
4+
5+
fn main() {
6+
let _ = ConstGenericRingBuffer::<i32, 0>::new();
7+
//~^ note: the above error was encountered while instantiating `fn ringbuffer::ConstGenericRingBuffer::<i32, 0>::new::<0>`
8+
// ringbuffer can't be zero length
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extern crate ringbuffer;
2+
3+
use ringbuffer::{ConstGenericRingBuffer, RingBuffer};
4+
5+
fn main() {
6+
let mut buf = ConstGenericRingBuffer::new::<0>();
7+
//~^ note: the above error was encountered while instantiating `fn ringbuffer::ConstGenericRingBuffer::<i32, 0>::new::<0>`
8+
// ringbuffer can't be zero length
9+
buf.push(5);
10+
}

tests/compiletests.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
extern crate compiletest_rs as compiletest;
2+
3+
use std::path::PathBuf;
4+
5+
#[cfg(test)]
6+
mod conversions;
7+
8+
fn run_mode(mode: &'static str) {
9+
let mut config = compiletest::Config::default();
10+
11+
config.mode = mode.parse().expect("Invalid mode");
12+
config.src_base = PathBuf::from(format!("tests/{}", mode));
13+
config.link_deps(); // Populate config.target_rustcflags with dependencies on the path
14+
config.clean_rmeta(); // If your tests import the parent crate, this helps with E0464
15+
16+
compiletest::run_tests(&config);
17+
}
18+
19+
#[test]
20+
#[cfg_attr(miri, ignore)]
21+
fn compile_test() {
22+
run_mode("compile-fail");
23+
}

tests/conversions.rs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
extern crate alloc;
2+
3+
use alloc::collections::{LinkedList, VecDeque};
4+
use alloc::string::ToString;
5+
use core::ops::Deref;
6+
use ringbuffer::RingBuffer;
7+
use ringbuffer::{AllocRingBuffer, ConstGenericRingBuffer, GrowableAllocRingBuffer};
8+
use std::vec;
9+
10+
macro_rules! convert_test {
11+
($name: ident: $from: expr => $to: ty) => {
12+
#[test]
13+
fn $name() {
14+
let a = $from;
15+
16+
let mut b: $to = a.into();
17+
assert_eq!(b.to_vec(), vec!['1', '2']);
18+
b.push('3');
19+
assert_eq!(b, b);
20+
}
21+
};
22+
}
23+
24+
macro_rules! convert_tests {
25+
(
26+
[$($name: ident: $from: expr),* $(,)?]
27+
=> $to: ty
28+
) => {
29+
$(
30+
convert_test!($name: $from => $to);
31+
)*
32+
};
33+
}
34+
35+
convert_tests!(
36+
[
37+
alloc_from_vec: vec!['1', '2'],
38+
alloc_from_ll: {let mut l = LinkedList::new(); l.push_back('1'); l.push_back('2'); l},
39+
alloc_from_vd: {let mut l = VecDeque::new(); l.push_back('1'); l.push_back('2'); l},
40+
alloc_from_str: "12".to_string(),
41+
alloc_from_str_slice: "12",
42+
alloc_from_slice: {let a: &[char] = &['1', '2']; a},
43+
alloc_from_const_slice: {let a: &[char; 2] = &['1', '2']; a},
44+
alloc_from_arr: {let a: [char; 2] = ['1', '2']; a},
45+
46+
alloc_from_cgrb: {let a = ConstGenericRingBuffer::from(['1', '2']); a},
47+
alloc_from_garb: {let a = GrowableAllocRingBuffer::from(['1', '2']); a},
48+
] => AllocRingBuffer::<_>
49+
);
50+
51+
convert_tests!(
52+
[
53+
growable_alloc_from_vec: vec!['1', '2'],
54+
growable_alloc_from_ll: {let mut l = LinkedList::new(); l.push_back('1'); l.push_back('2'); l},
55+
growable_alloc_from_vd: {let mut l = VecDeque::new(); l.push_back('1'); l.push_back('2'); l},
56+
growable_alloc_from_str: "12".to_string(),
57+
growable_alloc_from_str_slice: "12",
58+
growable_alloc_from_slice: {let a: &[char] = &['1', '2']; a},
59+
growable_alloc_from_const_slice: {let a: &[char; 2] = &['1', '2']; a},
60+
growable_alloc_from_arr: {let a: [char; 2] = ['1', '2']; a},
61+
62+
growable_alloc_from_cgrb: {let a = ConstGenericRingBuffer::from(['1', '2']); a},
63+
growable_alloc_from_arb: {let a = AllocRingBuffer::from(['1', '2']); a},
64+
] => GrowableAllocRingBuffer::<_>
65+
);
66+
67+
convert_tests!(
68+
[
69+
const_from_vec: vec!['1', '2'],
70+
const_from_ll: {let mut l = LinkedList::new(); l.push_back('1'); l.push_back('2'); l},
71+
const_from_vd: {let mut l = VecDeque::new(); l.push_back('1'); l.push_back('2'); l},
72+
const_from_str: "12".to_string(),
73+
const_from_str_slice: "12",
74+
const_from_slice: {let a: &[char] = &['1', '2']; a},
75+
const_from_const_slice: {let a: &[char; 2] = &['1', '2']; a},
76+
const_from_arr: {let a: [char; 2] = ['1', '2']; a},
77+
78+
const_from_garb: {let a = GrowableAllocRingBuffer::from(['1', '2']); a},
79+
const_from_arb: {let a = AllocRingBuffer::from(['1', '2']); a},
80+
] => ConstGenericRingBuffer::<_, 2>
81+
);
82+
83+
#[test]
84+
fn test_extra_conversions_growable() {
85+
let a: &mut [i32; 2] = &mut [1, 2];
86+
let a = GrowableAllocRingBuffer::from(a);
87+
assert_eq!(a.to_vec(), vec![1, 2]);
88+
89+
let a: &mut [i32] = &mut [1, 2];
90+
let a = GrowableAllocRingBuffer::from(a);
91+
assert_eq!(a.to_vec(), vec![1, 2]);
92+
93+
let mut b = VecDeque::<i32>::new();
94+
b.push_back(1);
95+
b.push_back(2);
96+
assert_eq!(a.deref(), &b);
97+
assert_eq!(a.as_ref(), &b);
98+
}
99+
100+
#[test]
101+
fn test_extra_conversions_alloc() {
102+
let a: &mut [i32; 2] = &mut [1, 2];
103+
let a = AllocRingBuffer::from(a);
104+
assert_eq!(a.to_vec(), vec![1, 2]);
105+
106+
let a: &mut [i32] = &mut [1, 2];
107+
let a = AllocRingBuffer::from(a);
108+
assert_eq!(a.to_vec(), vec![1, 2]);
109+
}
110+
111+
#[test]
112+
fn test_extra_conversions_const() {
113+
let a: &mut [i32; 2] = &mut [1, 2];
114+
let a = ConstGenericRingBuffer::<_, 2>::from(a);
115+
assert_eq!(a.to_vec(), vec![1, 2]);
116+
117+
let a: &mut [i32] = &mut [1, 2];
118+
let a = ConstGenericRingBuffer::<_, 2>::from(a);
119+
assert_eq!(a.to_vec(), vec![1, 2]);
120+
}
121+
122+
#[test]
123+
fn test_const_generic_new_parameter() {
124+
// Can we specify size only on the method?
125+
let mut a = ConstGenericRingBuffer::new::<2>();
126+
a.push(5);
127+
128+
// Can we specify size in both positions?
129+
let mut a = ConstGenericRingBuffer::<i32, 50>::new::<50>();
130+
a.push(5);
131+
132+
// Can we specify size only on the struct?
133+
let mut a = ConstGenericRingBuffer::<i32, 50>::new();
134+
a.push(5);
135+
}

0 commit comments

Comments
 (0)