diff options
Diffstat (limited to 'vendor/tinyvec/tests')
-rw-r--r-- | vendor/tinyvec/tests/arrayvec.rs | 477 | ||||
-rw-r--r-- | vendor/tinyvec/tests/tinyvec.rs | 468 |
2 files changed, 945 insertions, 0 deletions
diff --git a/vendor/tinyvec/tests/arrayvec.rs b/vendor/tinyvec/tests/arrayvec.rs new file mode 100644 index 000000000..e400317c3 --- /dev/null +++ b/vendor/tinyvec/tests/arrayvec.rs @@ -0,0 +1,477 @@ +#![allow(bad_style)]
+
+#[cfg(feature = "serde")]
+use serde_test::{assert_tokens, Token};
+use std::iter::FromIterator;
+use tinyvec::*;
+
+#[test]
+fn test_a_vec() {
+ let mut expected: ArrayVec<[i32; 4]> = Default::default();
+ expected.push(1);
+ expected.push(2);
+ expected.push(3);
+
+ let actual = array_vec!(1, 2, 3);
+
+ assert_eq!(expected, actual);
+
+ assert_eq!(array_vec![0u8; 4], array_vec!(0u8, 0u8, 0u8, 0u8));
+ assert_eq!(array_vec![0u8; 4], array_vec!([u8; 4] => 0, 0, 0, 0));
+ assert_eq!(array_vec![0; 4], array_vec!(0, 0, 0, 0));
+ assert_eq!(array_vec![0; 4], array_vec!([u8; 4] => 0, 0, 0, 0));
+
+ let expected2 = array_vec![1.1; 3];
+ let actual2 = array_vec!([f32; 3] => 1.1, 1.1, 1.1);
+ assert_eq!(expected2, actual2);
+}
+
+#[test]
+fn ArrayVec_push_pop() {
+ let mut av: ArrayVec<[i32; 4]> = Default::default();
+ assert_eq!(av.len(), 0);
+ assert_eq!(av.pop(), None);
+
+ av.push(10_i32);
+ assert_eq!(av.len(), 1);
+ assert_eq!(av[0], 10);
+ assert_eq!(av.pop(), Some(10));
+ assert_eq!(av.len(), 0);
+ assert_eq!(av.pop(), None);
+
+ av.push(10);
+ av.push(11);
+ av.push(12);
+ av.push(13);
+ assert_eq!(av[0], 10);
+ assert_eq!(av[1], 11);
+ assert_eq!(av[2], 12);
+ assert_eq!(av[3], 13);
+ assert_eq!(av.len(), 4);
+ assert_eq!(av.pop(), Some(13));
+ assert_eq!(av.len(), 3);
+ assert_eq!(av.pop(), Some(12));
+ assert_eq!(av.len(), 2);
+ assert_eq!(av.pop(), Some(11));
+ assert_eq!(av.len(), 1);
+ assert_eq!(av.pop(), Some(10));
+ assert_eq!(av.len(), 0);
+ assert_eq!(av.pop(), None);
+}
+
+#[test]
+#[should_panic]
+fn ArrayVec_push_overflow() {
+ let mut av: ArrayVec<[i32; 0]> = Default::default();
+ av.push(7);
+}
+
+#[test]
+fn ArrayVec_formatting() {
+ // check that we get the comma placement correct
+
+ let mut av: ArrayVec<[i32; 4]> = Default::default();
+ assert_eq!(format!("{:?}", av), "[]");
+ av.push(10);
+ assert_eq!(format!("{:?}", av), "[10]");
+ av.push(11);
+ assert_eq!(format!("{:?}", av), "[10, 11]");
+ av.push(12);
+ assert_eq!(format!("{:?}", av), "[10, 11, 12]");
+
+ // below here just asserts that the impls exist.
+
+ //
+ let av: ArrayVec<[i32; 4]> = Default::default();
+ assert_eq!(format!("{:b}", av), "[]");
+ assert_eq!(format!("{:o}", av), "[]");
+ assert_eq!(format!("{:x}", av), "[]");
+ assert_eq!(format!("{:X}", av), "[]");
+ assert_eq!(format!("{}", av), "[]");
+ //
+ let av: ArrayVec<[f32; 4]> = Default::default();
+ assert_eq!(format!("{:e}", av), "[]");
+ assert_eq!(format!("{:E}", av), "[]");
+ //
+ let av: ArrayVec<[&'static str; 4]> = Default::default();
+ assert_eq!(format!("{:p}", av), "[]");
+}
+
+#[test]
+fn ArrayVec_iteration() {
+ let av = array_vec!([i32; 4] => 10, 11, 12, 13);
+
+ let mut i = av.into_iter();
+ assert_eq!(i.next(), Some(10));
+ assert_eq!(i.next(), Some(11));
+ assert_eq!(i.next(), Some(12));
+ assert_eq!(i.next(), Some(13));
+ assert_eq!(i.next(), None);
+
+ let av = array_vec!([i32; 4] => 10, 11, 12, 13);
+
+ let mut av2: ArrayVec<[i32; 4]> = av.clone().into_iter().collect();
+ assert_eq!(av, av2);
+
+ // IntoIterator for &mut ArrayVec
+ for x in &mut av2 {
+ *x = -*x;
+ }
+
+ // IntoIterator for &ArrayVec
+ assert!(av.iter().zip(&av2).all(|(&a, &b)| a == -b));
+}
+
+#[test]
+fn ArrayVec_append() {
+ let mut av = array_vec!([i32; 8] => 1, 2, 3);
+ let mut av2 = array_vec!([i32; 8] => 4, 5, 6);
+ //
+ av.append(&mut av2);
+ assert_eq!(av.as_slice(), &[1_i32, 2, 3, 4, 5, 6]);
+ assert_eq!(av2.as_slice(), &[]);
+}
+
+#[test]
+fn ArrayVec_remove() {
+ let mut av: ArrayVec<[i32; 10]> = Default::default();
+ av.push(1);
+ av.push(2);
+ av.push(3);
+ assert_eq!(av.remove(1), 2);
+ assert_eq!(&av[..], &[1, 3][..]);
+}
+
+#[test]
+#[should_panic]
+fn ArrayVec_remove_invalid() {
+ let mut av: ArrayVec<[i32; 1]> = Default::default();
+ av.push(1);
+ av.remove(1);
+}
+
+#[test]
+fn ArrayVec_swap_remove() {
+ let mut av: ArrayVec<[i32; 10]> = Default::default();
+ av.push(1);
+ av.push(2);
+ av.push(3);
+ av.push(4);
+ assert_eq!(av.swap_remove(3), 4);
+ assert_eq!(&av[..], &[1, 2, 3][..]);
+ assert_eq!(av.swap_remove(0), 1);
+ assert_eq!(&av[..], &[3, 2][..]);
+ assert_eq!(av.swap_remove(0), 3);
+ assert_eq!(&av[..], &[2][..]);
+ assert_eq!(av.swap_remove(0), 2);
+ assert_eq!(&av[..], &[][..]);
+}
+
+#[test]
+fn ArrayVec_drain() {
+ let mut av: ArrayVec<[i32; 10]> = Default::default();
+ av.push(1);
+ av.push(2);
+ av.push(3);
+
+ assert_eq!(Vec::from_iter(av.clone().drain(..)), vec![1, 2, 3]);
+
+ assert_eq!(Vec::from_iter(av.clone().drain(..2)), vec![1, 2]);
+ assert_eq!(Vec::from_iter(av.clone().drain(..3)), vec![1, 2, 3]);
+
+ assert_eq!(Vec::from_iter(av.clone().drain(..=1)), vec![1, 2]);
+ assert_eq!(Vec::from_iter(av.clone().drain(..=2)), vec![1, 2, 3]);
+
+ assert_eq!(Vec::from_iter(av.clone().drain(0..)), vec![1, 2, 3]);
+ assert_eq!(Vec::from_iter(av.clone().drain(1..)), vec![2, 3]);
+
+ assert_eq!(Vec::from_iter(av.clone().drain(0..2)), vec![1, 2]);
+ assert_eq!(Vec::from_iter(av.clone().drain(0..3)), vec![1, 2, 3]);
+ assert_eq!(Vec::from_iter(av.clone().drain(1..2)), vec![2]);
+ assert_eq!(Vec::from_iter(av.clone().drain(1..3)), vec![2, 3]);
+
+ assert_eq!(Vec::from_iter(av.clone().drain(0..=1)), vec![1, 2]);
+ assert_eq!(Vec::from_iter(av.clone().drain(0..=2)), vec![1, 2, 3]);
+ assert_eq!(Vec::from_iter(av.clone().drain(1..=1)), vec![2]);
+ assert_eq!(Vec::from_iter(av.clone().drain(1..=2)), vec![2, 3]);
+}
+
+#[test]
+fn ArrayVec_splice() {
+ let mut av: ArrayVec<[i32; 10]> = Default::default();
+ av.push(1);
+ av.push(2);
+ av.push(3);
+
+ // splice returns the same things as drain
+ assert_eq!(Vec::from_iter(av.clone().splice(.., None)), vec![1, 2, 3]);
+
+ assert_eq!(Vec::from_iter(av.clone().splice(..2, None)), vec![1, 2]);
+ assert_eq!(Vec::from_iter(av.clone().splice(..3, None)), vec![1, 2, 3]);
+
+ assert_eq!(Vec::from_iter(av.clone().splice(..=1, None)), vec![1, 2]);
+ assert_eq!(Vec::from_iter(av.clone().splice(..=2, None)), vec![1, 2, 3]);
+
+ assert_eq!(Vec::from_iter(av.clone().splice(0.., None)), vec![1, 2, 3]);
+ assert_eq!(Vec::from_iter(av.clone().splice(1.., None)), vec![2, 3]);
+
+ assert_eq!(Vec::from_iter(av.clone().splice(0..2, None)), vec![1, 2]);
+ assert_eq!(Vec::from_iter(av.clone().splice(0..3, None)), vec![1, 2, 3]);
+ assert_eq!(Vec::from_iter(av.clone().splice(1..2, None)), vec![2]);
+ assert_eq!(Vec::from_iter(av.clone().splice(1..3, None)), vec![2, 3]);
+
+ assert_eq!(Vec::from_iter(av.clone().splice(0..=1, None)), vec![1, 2]);
+ assert_eq!(Vec::from_iter(av.clone().splice(0..=2, None)), vec![1, 2, 3]);
+ assert_eq!(Vec::from_iter(av.clone().splice(1..=1, None)), vec![2]);
+ assert_eq!(Vec::from_iter(av.clone().splice(1..=2, None)), vec![2, 3]);
+
+ // splice removes the same things as drain
+ let mut av2 = av.clone();
+ av2.splice(.., None);
+ assert_eq!(av2, array_vec![]);
+
+ let mut av2 = av.clone();
+ av2.splice(..2, None);
+ assert_eq!(av2, array_vec![3]);
+
+ let mut av2 = av.clone();
+ av2.splice(..3, None);
+ assert_eq!(av2, array_vec![]);
+
+ let mut av2 = av.clone();
+ av2.splice(..=1, None);
+ assert_eq!(av2, array_vec![3]);
+ let mut av2 = av.clone();
+ av2.splice(..=2, None);
+ assert_eq!(av2, array_vec![]);
+
+ let mut av2 = av.clone();
+ av2.splice(0.., None);
+ assert_eq!(av2, array_vec![]);
+ let mut av2 = av.clone();
+ av2.splice(1.., None);
+ assert_eq!(av2, array_vec![1]);
+
+ let mut av2 = av.clone();
+ av2.splice(0..2, None);
+ assert_eq!(av2, array_vec![3]);
+
+ let mut av2 = av.clone();
+ av2.splice(0..3, None);
+ assert_eq!(av2, array_vec![]);
+ let mut av2 = av.clone();
+ av2.splice(1..2, None);
+ assert_eq!(av2, array_vec![1, 3]);
+
+ let mut av2 = av.clone();
+ av2.splice(1..3, None);
+ assert_eq!(av2, array_vec![1]);
+
+ let mut av2 = av.clone();
+ av2.splice(0..=1, None);
+ assert_eq!(av2, array_vec![3]);
+
+ let mut av2 = av.clone();
+ av2.splice(0..=2, None);
+ assert_eq!(av2, array_vec![]);
+
+ let mut av2 = av.clone();
+ av2.splice(1..=1, None);
+ assert_eq!(av2, array_vec![1, 3]);
+
+ let mut av2 = av.clone();
+ av2.splice(1..=2, None);
+ assert_eq!(av2, array_vec![1]);
+
+ // splice adds the elements correctly
+ let mut av2 = av.clone();
+ av2.splice(.., 4..=6);
+ assert_eq!(av2, array_vec![4, 5, 6]);
+
+ let mut av2 = av.clone();
+ av2.splice(..2, 4..=6);
+ assert_eq!(av2, array_vec![4, 5, 6, 3]);
+
+ let mut av2 = av.clone();
+ av2.splice(..3, 4..=6);
+ assert_eq!(av2, array_vec![4, 5, 6]);
+
+ let mut av2 = av.clone();
+ av2.splice(..=1, 4..=6);
+ assert_eq!(av2, array_vec![4, 5, 6, 3]);
+
+ let mut av2 = av.clone();
+ av2.splice(..=2, 4..=6);
+ assert_eq!(av2, array_vec![4, 5, 6]);
+
+ let mut av2 = av.clone();
+ av2.splice(0.., 4..=6);
+ assert_eq!(av2, array_vec![4, 5, 6]);
+
+ let mut av2 = av.clone();
+ av2.splice(1.., 4..=6);
+ assert_eq!(av2, array_vec![1, 4, 5, 6]);
+
+ let mut av2 = av.clone();
+ av2.splice(0..2, 4..=6);
+ assert_eq!(av2, array_vec![4, 5, 6, 3]);
+
+ let mut av2 = av.clone();
+ av2.splice(0..3, 4..=6);
+ assert_eq!(av2, array_vec![4, 5, 6]);
+
+ let mut av2 = av.clone();
+ av2.splice(1..2, 4..=6);
+ assert_eq!(av2, array_vec![1, 4, 5, 6, 3]);
+
+ let mut av2 = av.clone();
+ av2.splice(1..3, 4..=6);
+ assert_eq!(av2, array_vec![1, 4, 5, 6]);
+
+ let mut av2 = av.clone();
+ av2.splice(0..=1, 4..=6);
+ assert_eq!(av2, array_vec![4, 5, 6, 3]);
+
+ let mut av2 = av.clone();
+ av2.splice(0..=2, 4..=6);
+ assert_eq!(av2, array_vec![4, 5, 6]);
+
+ let mut av2 = av.clone();
+ av2.splice(1..=1, 4..=6);
+ assert_eq!(av2, array_vec![1, 4, 5, 6, 3]);
+
+ let mut av2 = av.clone();
+ av2.splice(1..=2, 4..=6);
+ assert_eq!(av2, array_vec![1, 4, 5, 6]);
+
+ // splice adds the elements correctly when the replacement is smaller
+ let mut av2 = av.clone();
+ av2.splice(.., Some(4));
+ assert_eq!(av2, array_vec![4]);
+
+ let mut av2 = av.clone();
+ av2.splice(..2, Some(4));
+ assert_eq!(av2, array_vec![4, 3]);
+
+ let mut av2 = av.clone();
+ av2.splice(1.., Some(4));
+ assert_eq!(av2, array_vec![1, 4]);
+
+ let mut av2 = av.clone();
+ av2.splice(1..=1, Some(4));
+ assert_eq!(av2, array_vec![1, 4, 3]);
+}
+
+#[test]
+fn iter_last_nth() {
+ let mut av: ArrayVec<[i32; 10]> = Default::default();
+ av.push(1);
+ av.push(2);
+ av.push(3);
+ av.push(4);
+ assert_eq!(av.len(), 4);
+ let mut iter = av.into_iter();
+ assert_eq!(iter.next(), Some(1));
+ assert_eq!(iter.next(), Some(2));
+ assert_eq!(iter.next(), Some(3));
+ assert_eq!(iter.next(), Some(4));
+ assert_eq!(iter.next(), None);
+ assert_eq!(iter.last(), None);
+
+ let mut av: ArrayVec<[i32; 10]> = Default::default();
+ av.push(1);
+ av.push(2);
+ av.push(3);
+
+ assert_eq!(av.into_iter().nth(0), Some(1));
+}
+
+#[test]
+#[cfg(feature = "rustc_1_40")]
+fn reviter() {
+ let mut av: ArrayVec<[i32; 10]> = Default::default();
+ av.push(1);
+ av.push(2);
+ av.push(3);
+ av.push(4);
+
+ let mut iter = av.into_iter();
+
+ assert_eq!(iter.next(), Some(1));
+ assert_eq!(iter.next_back(), Some(4));
+ assert_eq!(iter.next(), Some(2));
+ assert_eq!(iter.next_back(), Some(3));
+ assert_eq!(iter.next(), None);
+ assert_eq!(iter.next_back(), None);
+
+ let mut av: ArrayVec<[i32; 32]> = Default::default();
+ av.extend(0..32);
+
+ let mut iter = av.into_iter();
+
+ assert_eq!(iter.nth_back(0), Some(31));
+ assert_eq!(iter.nth_back(2), Some(28));
+ assert_eq!(iter.nth_back(0), Some(27));
+ assert_eq!(iter.nth_back(99), None);
+ assert_eq!(iter.nth_back(99), None);
+}
+
+#[cfg(feature = "serde")]
+#[test]
+fn ArrayVec_ser_de_empty() {
+ let tv: ArrayVec<[i32; 0]> = Default::default();
+
+ assert_tokens(&tv, &[Token::Seq { len: Some(0) }, Token::SeqEnd]);
+}
+
+#[cfg(feature = "serde")]
+#[test]
+fn ArrayVec_ser_de() {
+ let mut tv: ArrayVec<[i32; 4]> = Default::default();
+ tv.push(1);
+ tv.push(2);
+ tv.push(3);
+ tv.push(4);
+
+ assert_tokens(
+ &tv,
+ &[
+ Token::Seq { len: Some(4) },
+ Token::I32(1),
+ Token::I32(2),
+ Token::I32(3),
+ Token::I32(4),
+ Token::SeqEnd,
+ ],
+ );
+}
+
+#[test]
+fn ArrayVec_try_from_slice() {
+ use std::convert::TryFrom;
+
+ let nums = [1, 2, 3, 4];
+
+ let empty: Result<ArrayVec<[i32; 2]>, _> = ArrayVec::try_from(&nums[..0]);
+ assert!(empty.is_ok());
+ assert_eq!(empty.unwrap().as_slice(), &[]);
+
+ let fits: Result<ArrayVec<[i32; 2]>, _> = ArrayVec::try_from(&nums[..2]);
+ assert!(fits.is_ok());
+ assert_eq!(fits.unwrap().as_slice(), &[1, 2]);
+
+ let doesnt_fit: Result<ArrayVec<[i32; 2]>, _> =
+ ArrayVec::try_from(&nums[..4]);
+ assert!(doesnt_fit.is_err());
+}
+
+#[test]
+fn ArrayVec_pretty_debug() {
+ let arr: [i32; 3] = [1, 2, 3];
+ let expect = format!("{:#?}", arr);
+
+ let arr: ArrayVec<[i32; 3]> = array_vec![1, 2, 3];
+ let got = format!("{:#?}", arr);
+
+ assert_eq!(got, expect);
+}
diff --git a/vendor/tinyvec/tests/tinyvec.rs b/vendor/tinyvec/tests/tinyvec.rs new file mode 100644 index 000000000..658462ae5 --- /dev/null +++ b/vendor/tinyvec/tests/tinyvec.rs @@ -0,0 +1,468 @@ +#![cfg(feature = "alloc")]
+#![allow(bad_style)]
+#![allow(clippy::redundant_clone)]
+
+#[cfg(feature = "serde")]
+use serde_test::{assert_tokens, Token};
+use std::iter::FromIterator;
+use tinyvec::*;
+
+#[test]
+fn TinyVec_swap_remove() {
+ let mut tv: TinyVec<[i32; 10]> = Default::default();
+ tv.push(1);
+ tv.push(2);
+ tv.push(3);
+ tv.push(4);
+ assert_eq!(tv.swap_remove(3), 4);
+ assert_eq!(&tv[..], &[1, 2, 3][..]);
+ assert_eq!(tv.swap_remove(0), 1);
+ assert_eq!(&tv[..], &[3, 2][..]);
+ assert_eq!(tv.swap_remove(0), 3);
+ assert_eq!(&tv[..], &[2][..]);
+ assert_eq!(tv.swap_remove(0), 2);
+ assert_eq!(&tv[..], &[][..]);
+}
+
+#[test]
+fn TinyVec_capacity() {
+ let mut tv: TinyVec<[i32; 1]> = Default::default();
+ assert_eq!(tv.capacity(), 1);
+ tv.move_to_the_heap();
+ tv.extend_from_slice(&[1, 2, 3, 4]);
+ assert_eq!(tv.capacity(), 4);
+}
+
+#[test]
+fn TinyVec_drain() {
+ let mut tv: TinyVec<[i32; 10]> = Default::default();
+ tv.push(1);
+ tv.push(2);
+ tv.push(3);
+
+ assert_eq!(Vec::from_iter(tv.clone().drain(..)), vec![1, 2, 3]);
+
+ assert_eq!(Vec::from_iter(tv.clone().drain(..2)), vec![1, 2]);
+ assert_eq!(Vec::from_iter(tv.clone().drain(..3)), vec![1, 2, 3]);
+
+ assert_eq!(Vec::from_iter(tv.clone().drain(..=1)), vec![1, 2]);
+ assert_eq!(Vec::from_iter(tv.clone().drain(..=2)), vec![1, 2, 3]);
+
+ assert_eq!(Vec::from_iter(tv.clone().drain(0..)), vec![1, 2, 3]);
+ assert_eq!(Vec::from_iter(tv.clone().drain(1..)), vec![2, 3]);
+
+ assert_eq!(Vec::from_iter(tv.clone().drain(0..2)), vec![1, 2]);
+ assert_eq!(Vec::from_iter(tv.clone().drain(0..3)), vec![1, 2, 3]);
+ assert_eq!(Vec::from_iter(tv.clone().drain(1..2)), vec![2]);
+ assert_eq!(Vec::from_iter(tv.clone().drain(1..3)), vec![2, 3]);
+
+ assert_eq!(Vec::from_iter(tv.clone().drain(0..=1)), vec![1, 2]);
+ assert_eq!(Vec::from_iter(tv.clone().drain(0..=2)), vec![1, 2, 3]);
+ assert_eq!(Vec::from_iter(tv.clone().drain(1..=1)), vec![2]);
+ assert_eq!(Vec::from_iter(tv.clone().drain(1..=2)), vec![2, 3]);
+}
+
+#[test]
+fn TinyVec_splice() {
+ let mut tv: TinyVec<[i32; 10]> = Default::default();
+ tv.push(1);
+ tv.push(2);
+ tv.push(3);
+
+ // splice returns the same things as drain
+ assert_eq!(Vec::from_iter(tv.clone().splice(.., None)), vec![1, 2, 3]);
+
+ assert_eq!(Vec::from_iter(tv.clone().splice(..2, None)), vec![1, 2]);
+ assert_eq!(Vec::from_iter(tv.clone().splice(..3, None)), vec![1, 2, 3]);
+
+ assert_eq!(Vec::from_iter(tv.clone().splice(..=1, None)), vec![1, 2]);
+ assert_eq!(Vec::from_iter(tv.clone().splice(..=2, None)), vec![1, 2, 3]);
+
+ assert_eq!(Vec::from_iter(tv.clone().splice(0.., None)), vec![1, 2, 3]);
+ assert_eq!(Vec::from_iter(tv.clone().splice(1.., None)), vec![2, 3]);
+
+ assert_eq!(Vec::from_iter(tv.clone().splice(0..2, None)), vec![1, 2]);
+ assert_eq!(Vec::from_iter(tv.clone().splice(0..3, None)), vec![1, 2, 3]);
+ assert_eq!(Vec::from_iter(tv.clone().splice(1..2, None)), vec![2]);
+ assert_eq!(Vec::from_iter(tv.clone().splice(1..3, None)), vec![2, 3]);
+
+ assert_eq!(Vec::from_iter(tv.clone().splice(0..=1, None)), vec![1, 2]);
+ assert_eq!(Vec::from_iter(tv.clone().splice(0..=2, None)), vec![1, 2, 3]);
+ assert_eq!(Vec::from_iter(tv.clone().splice(1..=1, None)), vec![2]);
+ assert_eq!(Vec::from_iter(tv.clone().splice(1..=2, None)), vec![2, 3]);
+
+ // splice removes the same things as drain
+ let mut tv2 = tv.clone();
+ tv2.splice(.., None);
+ assert_eq!(tv2, tiny_vec![]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(..2, None);
+ assert_eq!(tv2, tiny_vec![3]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(..3, None);
+ assert_eq!(tv2, tiny_vec![]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(..=1, None);
+ assert_eq!(tv2, tiny_vec![3]);
+ let mut tv2 = tv.clone();
+ tv2.splice(..=2, None);
+ assert_eq!(tv2, tiny_vec![]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(0.., None);
+ assert_eq!(tv2, tiny_vec![]);
+ let mut tv2 = tv.clone();
+ tv2.splice(1.., None);
+ assert_eq!(tv2, tiny_vec![1]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(0..2, None);
+ assert_eq!(tv2, tiny_vec![3]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(0..3, None);
+ assert_eq!(tv2, tiny_vec![]);
+ let mut tv2 = tv.clone();
+ tv2.splice(1..2, None);
+ assert_eq!(tv2, tiny_vec![1, 3]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(1..3, None);
+ assert_eq!(tv2, tiny_vec![1]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(0..=1, None);
+ assert_eq!(tv2, tiny_vec![3]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(0..=2, None);
+ assert_eq!(tv2, tiny_vec![]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(1..=1, None);
+ assert_eq!(tv2, tiny_vec![1, 3]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(1..=2, None);
+ assert_eq!(tv2, tiny_vec![1]);
+
+ // splice adds the elements correctly
+ let mut tv2 = tv.clone();
+ tv2.splice(.., 4..=6);
+ assert_eq!(tv2, tiny_vec![4, 5, 6]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(..2, 4..=6);
+ assert_eq!(tv2, tiny_vec![4, 5, 6, 3]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(..3, 4..=6);
+ assert_eq!(tv2, tiny_vec![4, 5, 6]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(..=1, 4..=6);
+ assert_eq!(tv2, tiny_vec![4, 5, 6, 3]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(..=2, 4..=6);
+ assert_eq!(tv2, tiny_vec![4, 5, 6]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(0.., 4..=6);
+ assert_eq!(tv2, tiny_vec![4, 5, 6]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(1.., 4..=6);
+ assert_eq!(tv2, tiny_vec![1, 4, 5, 6]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(0..2, 4..=6);
+ assert_eq!(tv2, tiny_vec![4, 5, 6, 3]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(0..3, 4..=6);
+ assert_eq!(tv2, tiny_vec![4, 5, 6]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(1..2, 4..=6);
+ assert_eq!(tv2, tiny_vec![1, 4, 5, 6, 3]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(1..3, 4..=6);
+ assert_eq!(tv2, tiny_vec![1, 4, 5, 6]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(0..=1, 4..=6);
+ assert_eq!(tv2, tiny_vec![4, 5, 6, 3]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(0..=2, 4..=6);
+ assert_eq!(tv2, tiny_vec![4, 5, 6]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(1..=1, 4..=6);
+ assert_eq!(tv2, tiny_vec![1, 4, 5, 6, 3]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(1..=2, 4..=6);
+ assert_eq!(tv2, tiny_vec![1, 4, 5, 6]);
+
+ // splice adds the elements correctly when the replacement is smaller
+ let mut tv2 = tv.clone();
+ tv2.splice(.., Some(4));
+ assert_eq!(tv2, tiny_vec![4]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(..2, Some(4));
+ assert_eq!(tv2, tiny_vec![4, 3]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(1.., Some(4));
+ assert_eq!(tv2, tiny_vec![1, 4]);
+
+ let mut tv2 = tv.clone();
+ tv2.splice(1..=1, Some(4));
+ assert_eq!(tv2, tiny_vec![1, 4, 3]);
+}
+
+#[test]
+fn TinyVec_resize() {
+ let mut tv: TinyVec<[i32; 10]> = Default::default();
+ tv.resize(20, 5);
+ assert_eq!(&tv[..], &[5; 20]);
+}
+
+#[test]
+fn TinyVec_from_slice_impl() {
+ let bigger_slice: [u8; 11] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
+ let tinyvec: TinyVec<[u8; 10]> = TinyVec::Heap((&bigger_slice[..]).into());
+ assert_eq!(TinyVec::from(&bigger_slice[..]), tinyvec);
+
+ let smaller_slice: [u8; 5] = [0, 1, 2, 3, 4];
+ let tinyvec: TinyVec<[u8; 10]> = TinyVec::Inline(ArrayVec::from_array_len(
+ [0, 1, 2, 3, 4, 0, 0, 0, 0, 0],
+ 5,
+ ));
+ assert_eq!(TinyVec::from(&smaller_slice[..]), tinyvec);
+
+ let same_size: [u8; 4] = [0, 1, 2, 3];
+ let tinyvec: TinyVec<[u8; 4]> =
+ TinyVec::Inline(ArrayVec::from_array_len(same_size, 4));
+ assert_eq!(TinyVec::from(&same_size[..]), tinyvec);
+}
+
+#[test]
+fn TinyVec_from_array() {
+ let array = [9, 8, 7, 6, 5, 4, 3, 2, 1];
+ let tv = TinyVec::from(array);
+ assert_eq!(&array, &tv[..]);
+}
+
+#[test]
+fn TinyVec_macro() {
+ let mut expected: TinyVec<[i32; 4]> = Default::default();
+ expected.push(1);
+ expected.push(2);
+ expected.push(3);
+
+ let actual = tiny_vec!(1, 2, 3);
+
+ assert_eq!(expected, actual);
+
+ assert_eq!(tiny_vec![0u8; 4], tiny_vec!(0u8, 0u8, 0u8, 0u8));
+ assert_eq!(tiny_vec![0u8; 4], tiny_vec!([u8; 4] => 0, 0, 0, 0));
+ assert_eq!(tiny_vec![0; 4], tiny_vec!(0, 0, 0, 0));
+ assert_eq!(tiny_vec![0; 4], tiny_vec!([u8; 4] => 0, 0, 0, 0));
+
+ let expected2 = tiny_vec![1.1; 3];
+ let actual2 = tiny_vec!([f32; 3] => 1.1, 1.1, 1.1);
+ assert_eq!(expected2, actual2);
+}
+
+#[test]
+fn TinyVec_macro_non_copy() {
+ // must use a variable here to avoid macro shenanigans
+ let s = String::new();
+ let _: TinyVec<[String; 10]> = tiny_vec!([String; 10] => s);
+}
+
+#[test]
+fn TinyVec_reserve() {
+ let mut tv: TinyVec<[i32; 4]> = Default::default();
+ assert_eq!(tv.capacity(), 4);
+ tv.extend_from_slice(&[1, 2]);
+ assert_eq!(tv.capacity(), 4);
+ tv.reserve(2);
+ assert_eq!(tv.capacity(), 4);
+ tv.reserve(4);
+ assert!(tv.capacity() >= 6);
+ tv.extend_from_slice(&[3, 4, 5, 6]);
+ tv.reserve(4);
+ assert!(tv.capacity() >= 10);
+}
+
+#[cfg(feature = "rustc_1_57")]
+#[test]
+fn TinyVec_try_reserve() {
+ let mut tv: TinyVec<[i32; 4]> = Default::default();
+ assert_eq!(tv.capacity(), 4);
+ tv.extend_from_slice(&[1, 2]);
+ assert_eq!(tv.capacity(), 4);
+ assert!(tv.try_reserve(2).is_ok());
+ assert_eq!(tv.capacity(), 4);
+ assert!(tv.try_reserve(4).is_ok());
+ assert!(tv.capacity() >= 6);
+ tv.extend_from_slice(&[3, 4, 5, 6]);
+ assert!(tv.try_reserve(4).is_ok());
+ assert!(tv.capacity() >= 10);
+}
+
+#[test]
+fn TinyVec_reserve_exact() {
+ let mut tv: TinyVec<[i32; 4]> = Default::default();
+ assert_eq!(tv.capacity(), 4);
+
+ tv.extend_from_slice(&[1, 2]);
+ assert_eq!(tv.capacity(), 4);
+ tv.reserve_exact(2);
+ assert_eq!(tv.capacity(), 4);
+ tv.reserve_exact(4);
+ assert!(tv.capacity() >= 6);
+ tv.extend_from_slice(&[3, 4, 5, 6]);
+ tv.reserve_exact(4);
+ assert!(tv.capacity() >= 10);
+}
+
+#[cfg(feature = "rustc_1_57")]
+#[test]
+fn TinyVec_try_reserve_exact() {
+ let mut tv: TinyVec<[i32; 4]> = Default::default();
+ assert_eq!(tv.capacity(), 4);
+
+ tv.extend_from_slice(&[1, 2]);
+ assert_eq!(tv.capacity(), 4);
+ assert!(tv.try_reserve_exact(2).is_ok());
+ assert_eq!(tv.capacity(), 4);
+ assert!(tv.try_reserve_exact(4).is_ok());
+ assert!(tv.capacity() >= 6);
+ tv.extend_from_slice(&[3, 4, 5, 6]);
+ assert!(tv.try_reserve_exact(4).is_ok());
+ assert!(tv.capacity() >= 10);
+}
+
+#[test]
+fn TinyVec_move_to_heap_and_shrink() {
+ let mut tv: TinyVec<[i32; 4]> = Default::default();
+ assert!(tv.is_inline());
+ tv.move_to_the_heap();
+ assert!(tv.is_heap());
+ assert_eq!(tv.capacity(), 0);
+
+ tv.push(1);
+ tv.shrink_to_fit();
+ assert!(tv.is_inline());
+ assert_eq!(tv.capacity(), 4);
+
+ tv.move_to_the_heap_and_reserve(3);
+ assert!(tv.is_heap());
+ assert_eq!(tv.capacity(), 4);
+ tv.extend(2..=4);
+ assert_eq!(tv.capacity(), 4);
+ assert_eq!(tv.as_slice(), [1, 2, 3, 4]);
+}
+
+#[cfg(feature = "rustc_1_57")]
+#[test]
+fn TinyVec_try_move_to_heap_and_shrink() {
+ let mut tv: TinyVec<[i32; 4]> = Default::default();
+ assert!(tv.is_inline());
+ assert!(tv.try_move_to_the_heap().is_ok());
+ assert!(tv.is_heap());
+ assert_eq!(tv.capacity(), 0);
+
+ assert!(tv.try_reserve_exact(1).is_ok());
+ assert_eq!(tv.capacity(), 1);
+ tv.push(1);
+ tv.shrink_to_fit();
+ assert!(tv.is_inline());
+ assert_eq!(tv.capacity(), 4);
+
+ assert!(tv.try_move_to_the_heap_and_reserve(3).is_ok());
+ assert!(tv.is_heap());
+ assert_eq!(tv.capacity(), 4);
+ tv.extend(2..=4);
+ assert_eq!(tv.capacity(), 4);
+ assert_eq!(tv.as_slice(), [1, 2, 3, 4]);
+}
+
+#[cfg(feature = "serde")]
+#[test]
+fn TinyVec_ser_de_empty() {
+ let tv: TinyVec<[i32; 0]> = tiny_vec![];
+
+ assert_tokens(&tv, &[Token::Seq { len: Some(0) }, Token::SeqEnd]);
+}
+
+#[cfg(feature = "serde")]
+#[test]
+fn TinyVec_ser_de() {
+ let tv: TinyVec<[i32; 4]> = tiny_vec![1, 2, 3, 4];
+
+ assert_tokens(
+ &tv,
+ &[
+ Token::Seq { len: Some(4) },
+ Token::I32(1),
+ Token::I32(2),
+ Token::I32(3),
+ Token::I32(4),
+ Token::SeqEnd,
+ ],
+ );
+}
+
+#[cfg(feature = "serde")]
+#[test]
+fn TinyVec_ser_de_heap() {
+ let mut tv: TinyVec<[i32; 4]> = tiny_vec![1, 2, 3, 4];
+ tv.move_to_the_heap();
+
+ assert_tokens(
+ &tv,
+ &[
+ Token::Seq { len: Some(4) },
+ Token::I32(1),
+ Token::I32(2),
+ Token::I32(3),
+ Token::I32(4),
+ Token::SeqEnd,
+ ],
+ );
+}
+
+#[test]
+fn TinyVec_pretty_debug() {
+ let tv: TinyVec<[i32; 6]> = tiny_vec![1, 2, 3];
+ let s = format!("{:#?}", tv);
+ let expected = format!("{:#?}", tv.as_slice());
+
+ assert_eq!(s, expected);
+}
+
+#[cfg(feature = "std")]
+#[test]
+fn TinyVec_std_io_write() {
+ use std::io::Write;
+ let mut tv: TinyVec<[u8; 3]> = TinyVec::new();
+
+ tv.write_all(b"foo").ok();
+ assert!(tv.is_inline());
+ assert_eq!(tv, tiny_vec![b'f', b'o', b'o']);
+
+ tv.write_all(b"bar").ok();
+ assert!(tv.is_heap());
+ assert_eq!(tv, tiny_vec![b'f', b'o', b'o', b'b', b'a', b'r']);
+}
|