diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/generic-array-0.12.4/tests | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | vendor/generic-array-0.12.4/tests/arr.rs | 27 | ||||
-rw-r--r-- | vendor/generic-array-0.12.4/tests/generics.rs | 98 | ||||
-rw-r--r-- | vendor/generic-array-0.12.4/tests/hex.rs | 61 | ||||
-rw-r--r-- | vendor/generic-array-0.12.4/tests/import_name.rs | 10 | ||||
-rw-r--r-- | vendor/generic-array-0.12.4/tests/iter.rs | 164 | ||||
-rw-r--r-- | vendor/generic-array-0.12.4/tests/mod.rs | 287 |
6 files changed, 647 insertions, 0 deletions
diff --git a/vendor/generic-array-0.12.4/tests/arr.rs b/vendor/generic-array-0.12.4/tests/arr.rs new file mode 100644 index 000000000..c37b5d50f --- /dev/null +++ b/vendor/generic-array-0.12.4/tests/arr.rs @@ -0,0 +1,27 @@ +#[macro_use] +extern crate generic_array; +extern crate typenum; + +#[test] +fn empty_without_trailing_comma() { + let ar = arr![u8; ]; + assert_eq!(format!("{:x}", ar), ""); +} + +#[test] +fn empty_with_trailing_comma() { + let ar = arr![u8; , ]; + assert_eq!(format!("{:x}", ar), ""); +} + +#[test] +fn without_trailing_comma() { + let ar = arr![u8; 10, 20, 30]; + assert_eq!(format!("{:x}", ar), "0a141e"); +} + +#[test] +fn with_trailing_comma() { + let ar = arr![u8; 10, 20, 30, ]; + assert_eq!(format!("{:x}", ar), "0a141e"); +} diff --git a/vendor/generic-array-0.12.4/tests/generics.rs b/vendor/generic-array-0.12.4/tests/generics.rs new file mode 100644 index 000000000..d48fe08c0 --- /dev/null +++ b/vendor/generic-array-0.12.4/tests/generics.rs @@ -0,0 +1,98 @@ +#![recursion_limit = "128"] + +#[macro_use] +extern crate generic_array; + +use generic_array::typenum::consts::U4; + +use std::fmt::Debug; +use std::ops::Add; + +use generic_array::{GenericArray, ArrayLength}; +use generic_array::sequence::*; +use generic_array::functional::*; + +/// Example function using generics to pass N-length sequences and map them +pub fn generic_map<S>(s: S) +where + S: FunctionalSequence<i32>, // `.map` + S::Item: Add<i32, Output = i32>, // `x + 1` + S: MappedGenericSequence<i32, i32>, // `i32` -> `i32` + MappedSequence<S, i32, i32>: Debug, // println! +{ + let a = s.map(|x| x + 1); + + println!("{:?}", a); +} + +/// Complex example function using generics to pass N-length sequences, zip them, and then map that result. +/// +/// If used with `GenericArray` specifically this isn't necessary +pub fn generic_sequence_zip_sum<A, B>(a: A, b: B) -> i32 +where + A: FunctionalSequence<i32>, // `.zip` + B: FunctionalSequence<i32, Length = A::Length>, // `.zip` + A: MappedGenericSequence<i32, i32>, // `i32` -> `i32` + B: MappedGenericSequence<i32, i32, Mapped = MappedSequence<A, i32, i32>>, // `i32` -> `i32`, prove A and B can map to the same output + A::Item: Add<B::Item, Output = i32>, // `l + r` + MappedSequence<A, i32, i32>: MappedGenericSequence<i32, i32> + FunctionalSequence<i32>, // `.map` + SequenceItem<MappedSequence<A, i32, i32>>: Add<i32, Output=i32>, // `x + 1` + MappedSequence<MappedSequence<A, i32, i32>, i32, i32>: Debug, // `println!` + MappedSequence<MappedSequence<A, i32, i32>, i32, i32>: FunctionalSequence<i32>, // `.fold` + SequenceItem<MappedSequence<MappedSequence<A, i32, i32>, i32, i32>>: Add<i32, Output=i32> // `x + a`, note the order +{ + let c = a.zip(b, |l, r| l + r).map(|x| x + 1); + + println!("{:?}", c); + + c.fold(0, |a, x| x + a) +} + +/// Super-simple fixed-length i32 `GenericArray`s +pub fn generic_array_plain_zip_sum(a: GenericArray<i32, U4>, b: GenericArray<i32, U4>) -> i32 { + a.zip(b, |l, r| l + r).map(|x| x + 1).fold(0, |a, x| x + a) +} + +pub fn generic_array_variable_length_zip_sum<N>(a: GenericArray<i32, N>, b: GenericArray<i32, N>) -> i32 +where + N: ArrayLength<i32>, +{ + a.zip(b, |l, r| l + r).map(|x| x + 1).fold(0, |a, x| x + a) +} + +pub fn generic_array_same_type_variable_length_zip_sum<T, N>(a: GenericArray<T, N>, b: GenericArray<T, N>) -> i32 +where + N: ArrayLength<T> + ArrayLength<<T as Add<T>>::Output>, + T: Add<T, Output=i32>, +{ + a.zip(b, |l, r| l + r).map(|x| x + 1).fold(0, |a, x| x + a) +} + +/// Complex example using fully generic `GenericArray`s with the same length. +/// +/// It's mostly just the repeated `Add` traits, which would be present in other systems anyway. +pub fn generic_array_zip_sum<A, B, N: ArrayLength<A> + ArrayLength<B>>(a: GenericArray<A, N>, b: GenericArray<B, N>) -> i32 +where + A: Add<B>, + N: ArrayLength<<A as Add<B>>::Output> + + ArrayLength<<<A as Add<B>>::Output as Add<i32>>::Output>, + <A as Add<B>>::Output: Add<i32>, + <<A as Add<B>>::Output as Add<i32>>::Output: Add<i32, Output=i32>, +{ + a.zip(b, |l, r| l + r).map(|x| x + 1).fold(0, |a, x| x + a) +} + +#[test] +fn test_generics() { + generic_map(arr![i32; 1, 2, 3, 4]); + + assert_eq!(generic_sequence_zip_sum(arr![i32; 1, 2, 3, 4], arr![i32; 2, 3, 4, 5]), 28); + + assert_eq!(generic_array_plain_zip_sum(arr![i32; 1, 2, 3, 4], arr![i32; 2, 3, 4, 5]), 28); + + assert_eq!(generic_array_variable_length_zip_sum(arr![i32; 1, 2, 3, 4], arr![i32; 2, 3, 4, 5]), 28); + + assert_eq!(generic_array_same_type_variable_length_zip_sum(arr![i32; 1, 2, 3, 4], arr![i32; 2, 3, 4, 5]), 28); + + assert_eq!(generic_array_zip_sum(arr![i32; 1, 2, 3, 4], arr![i32; 2, 3, 4, 5]), 28); +}
\ No newline at end of file diff --git a/vendor/generic-array-0.12.4/tests/hex.rs b/vendor/generic-array-0.12.4/tests/hex.rs new file mode 100644 index 000000000..0882e9bb3 --- /dev/null +++ b/vendor/generic-array-0.12.4/tests/hex.rs @@ -0,0 +1,61 @@ +#[macro_use] +extern crate generic_array; +extern crate typenum; + +use generic_array::GenericArray; +use std::str::from_utf8; +use typenum::U2048; + +#[test] +fn short_lower_hex() { + let ar = arr![u8; 10, 20, 30]; + assert_eq!(format!("{:x}", ar), "0a141e"); +} + +#[test] +fn short_upper_hex() { + let ar = arr![u8; 30, 20, 10]; + assert_eq!(format!("{:X}", ar), "1E140A"); +} + +#[test] +fn long_lower_hex() { + let ar = GenericArray::<u8, U2048>::default(); + assert_eq!(format!("{:x}", ar), from_utf8(&[b'0'; 4096]).unwrap()); +} + +#[test] +fn long_lower_hex_truncated() { + let ar = GenericArray::<u8, U2048>::default(); + assert_eq!(format!("{:.3001x}", ar), from_utf8(&[b'0'; 3001]).unwrap()); +} + +#[test] +fn long_upper_hex() { + let ar = GenericArray::<u8, U2048>::default(); + assert_eq!(format!("{:X}", ar), from_utf8(&[b'0'; 4096]).unwrap()); +} + +#[test] +fn long_upper_hex_truncated() { + let ar = GenericArray::<u8, U2048>::default(); + assert_eq!(format!("{:.2777X}", ar), from_utf8(&[b'0'; 2777]).unwrap()); +} + +#[test] +fn truncated_lower_hex() { + let ar = arr![u8; 10, 20, 30, 40, 50]; + assert_eq!(format!("{:.2x}", ar), "0a"); + assert_eq!(format!("{:.3x}", ar), "0a1"); + assert_eq!(format!("{:.4x}", ar), "0a14"); +} + +#[test] +fn truncated_upper_hex() { + let ar = arr![u8; 30, 20, 10, 17, 0]; + assert_eq!(format!("{:.4X}", ar), "1E14"); + assert_eq!(format!("{:.5X}", ar), "1E140"); + assert_eq!(format!("{:.6X}", ar), "1E140A"); + assert_eq!(format!("{:.7X}", ar), "1E140A1"); + assert_eq!(format!("{:.8X}", ar), "1E140A11"); +} diff --git a/vendor/generic-array-0.12.4/tests/import_name.rs b/vendor/generic-array-0.12.4/tests/import_name.rs new file mode 100644 index 000000000..27653c9a8 --- /dev/null +++ b/vendor/generic-array-0.12.4/tests/import_name.rs @@ -0,0 +1,10 @@ +#[macro_use] +extern crate generic_array as gen_arr; + +use gen_arr::typenum; + +#[test] +fn test_different_crate_name() { + let _: gen_arr::GenericArray<u32, typenum::U4> = arr![u32; 0, 1, 2, 3]; + let _: gen_arr::GenericArray<u32, typenum::U0> = arr![u32;]; +} diff --git a/vendor/generic-array-0.12.4/tests/iter.rs b/vendor/generic-array-0.12.4/tests/iter.rs new file mode 100644 index 000000000..47860d728 --- /dev/null +++ b/vendor/generic-array-0.12.4/tests/iter.rs @@ -0,0 +1,164 @@ +#[macro_use] +extern crate generic_array; + +use std::cell::Cell; +use std::ops::Drop; + +use generic_array::GenericArray; +use generic_array::typenum::consts::U5; + +#[test] +fn test_into_iter_as_slice() { + let array = arr![char; 'a', 'b', 'c']; + let mut into_iter = array.into_iter(); + assert_eq!(into_iter.as_slice(), &['a', 'b', 'c']); + let _ = into_iter.next().unwrap(); + assert_eq!(into_iter.as_slice(), &['b', 'c']); + let _ = into_iter.next().unwrap(); + let _ = into_iter.next().unwrap(); + assert_eq!(into_iter.as_slice(), &[]); +} + +#[test] +fn test_into_iter_as_mut_slice() { + let array = arr![char; 'a', 'b', 'c']; + let mut into_iter = array.into_iter(); + assert_eq!(into_iter.as_slice(), &['a', 'b', 'c']); + into_iter.as_mut_slice()[0] = 'x'; + into_iter.as_mut_slice()[1] = 'y'; + assert_eq!(into_iter.next().unwrap(), 'x'); + assert_eq!(into_iter.as_slice(), &['y', 'c']); +} + +#[test] +fn test_into_iter_debug() { + let array = arr![char; 'a', 'b', 'c']; + let into_iter = array.into_iter(); + let debug = format!("{:?}", into_iter); + assert_eq!(debug, "GenericArrayIter(['a', 'b', 'c'])"); +} + +#[test] +fn test_into_iter_clone() { + fn iter_equal<I: Iterator<Item = i32>>(it: I, slice: &[i32]) { + let v: Vec<i32> = it.collect(); + assert_eq!(&v[..], slice); + } + let mut it = arr![i32; 1, 2, 3].into_iter(); + iter_equal(it.clone(), &[1, 2, 3]); + assert_eq!(it.next(), Some(1)); + let mut it = it.rev(); + iter_equal(it.clone(), &[3, 2]); + assert_eq!(it.next(), Some(3)); + iter_equal(it.clone(), &[2]); + assert_eq!(it.next(), Some(2)); + iter_equal(it.clone(), &[]); + assert_eq!(it.next(), None); +} + +#[test] +fn test_into_iter_nth() { + let v = arr![i32; 0, 1, 2, 3, 4]; + for i in 0..v.len() { + assert_eq!(v.clone().into_iter().nth(i).unwrap(), v[i]); + } + assert_eq!(v.clone().into_iter().nth(v.len()), None); + + let mut iter = v.into_iter(); + assert_eq!(iter.nth(2).unwrap(), v[2]); + assert_eq!(iter.nth(1).unwrap(), v[4]); +} + +#[test] +fn test_into_iter_last() { + let v = arr![i32; 0, 1, 2, 3, 4]; + assert_eq!(v.into_iter().last().unwrap(), 4); + assert_eq!(arr![i32; 0].into_iter().last().unwrap(), 0); +} + +#[test] +fn test_into_iter_count() { + let v = arr![i32; 0, 1, 2, 3, 4]; + assert_eq!(v.clone().into_iter().count(), 5); + + let mut iter2 = v.into_iter(); + iter2.next(); + iter2.next(); + assert_eq!(iter2.count(), 3); +} + +#[test] +fn test_into_iter_flat_map() { + assert!((0..5).flat_map(|i| arr![i32; 2 * i, 2 * i + 1]).eq(0..10)); +} + +#[test] +fn test_into_iter_drops() { + struct R<'a> { + i: &'a Cell<usize>, + } + + impl<'a> Drop for R<'a> { + fn drop(&mut self) { + self.i.set(self.i.get() + 1); + } + } + + fn r(i: &Cell<usize>) -> R { + R { + i: i + } + } + + fn v(i: &Cell<usize>) -> GenericArray<R, U5> { + arr![R; r(i), r(i), r(i), r(i), r(i)] + } + + let i = Cell::new(0); + { + v(&i).into_iter(); + } + assert_eq!(i.get(), 5); + + let i = Cell::new(0); + { + let mut iter = v(&i).into_iter(); + let _x = iter.next(); + assert_eq!(i.get(), 0); + assert_eq!(iter.count(), 4); + assert_eq!(i.get(), 4); + } + assert_eq!(i.get(), 5); + + let i = Cell::new(0); + { + let mut iter = v(&i).into_iter(); + let _x = iter.nth(2); + assert_eq!(i.get(), 2); + let _y = iter.last(); + assert_eq!(i.get(), 3); + } + assert_eq!(i.get(), 5); + + let i = Cell::new(0); + for (index, _x) in v(&i).into_iter().enumerate() { + assert_eq!(i.get(), index); + } + assert_eq!(i.get(), 5); + + let i = Cell::new(0); + for (index, _x) in v(&i).into_iter().rev().enumerate() { + assert_eq!(i.get(), index); + } + assert_eq!(i.get(), 5); +} + +/* +//TODO: Cover this +#[allow(dead_code)] +fn assert_covariance() { + fn into_iter<'new>(i: GenericArrayIter<&'static str, U10>) -> GenericArrayIter<&'new str, U10> { + i + } +} +*/
\ No newline at end of file diff --git a/vendor/generic-array-0.12.4/tests/mod.rs b/vendor/generic-array-0.12.4/tests/mod.rs new file mode 100644 index 000000000..6c6d9eb49 --- /dev/null +++ b/vendor/generic-array-0.12.4/tests/mod.rs @@ -0,0 +1,287 @@ +#![recursion_limit = "128"] +#![no_std] +#[macro_use] +extern crate generic_array; +use core::cell::Cell; +use core::ops::{Add, Drop}; +use generic_array::GenericArray; +use generic_array::functional::*; +use generic_array::sequence::*; +use generic_array::typenum::{U1, U3, U4, U97}; + +#[test] +fn test() { + let mut list97 = [0; 97]; + for i in 0..97 { + list97[i] = i as i32; + } + let l: GenericArray<i32, U97> = GenericArray::clone_from_slice(&list97); + assert_eq!(l[0], 0); + assert_eq!(l[1], 1); + assert_eq!(l[32], 32); + assert_eq!(l[56], 56); +} + +#[test] +fn test_drop() { + #[derive(Clone)] + struct TestDrop<'a>(&'a Cell<u32>); + + impl<'a> Drop for TestDrop<'a> { + fn drop(&mut self) { + self.0.set(self.0.get() + 1); + } + } + + let drop_counter = Cell::new(0); + { + let _: GenericArray<TestDrop, U3> = arr![TestDrop; TestDrop(&drop_counter), + TestDrop(&drop_counter), + TestDrop(&drop_counter)]; + } + assert_eq!(drop_counter.get(), 3); +} + +#[test] +fn test_arr() { + let test: GenericArray<u32, U3> = arr![u32; 1, 2, 3]; + assert_eq!(test[1], 2); +} + +#[test] +fn test_copy() { + let test = arr![u32; 1, 2, 3]; + let test2 = test; + // if GenericArray is not copy, this should fail as a use of a moved value + assert_eq!(test[1], 2); + assert_eq!(test2[0], 1); +} + +#[derive(Debug, PartialEq, Eq)] +struct NoClone<T>(T); + +#[test] +fn test_from_slice() { + let arr = [1, 2, 3, 4]; + let gen_arr = GenericArray::<_, U3>::from_slice(&arr[..3]); + assert_eq!(&arr[..3], gen_arr.as_slice()); + let arr = [NoClone(1u32), NoClone(2), NoClone(3), NoClone(4)]; + let gen_arr = GenericArray::<_, U3>::from_slice(&arr[..3]); + assert_eq!(&arr[..3], gen_arr.as_slice()); +} + +#[test] +fn test_from_mut_slice() { + let mut arr = [1, 2, 3, 4]; + { + let gen_arr = GenericArray::<_, U3>::from_mut_slice(&mut arr[..3]); + gen_arr[2] = 10; + } + assert_eq!(arr, [1, 2, 10, 4]); + let mut arr = [NoClone(1u32), NoClone(2), NoClone(3), NoClone(4)]; + { + let gen_arr = GenericArray::<_, U3>::from_mut_slice(&mut arr[..3]); + gen_arr[2] = NoClone(10); + } + assert_eq!(arr, [NoClone(1), NoClone(2), NoClone(10), NoClone(4)]); +} + +#[test] +fn test_default() { + let arr = GenericArray::<u8, U1>::default(); + assert_eq!(arr[0], 0); +} + +#[test] +fn test_from() { + let data = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]; + let garray: GenericArray<(usize, usize, usize), U3> = data.into(); + assert_eq!(&data, garray.as_slice()); +} + +#[test] +fn test_unit_macro() { + let arr = arr![f32; 3.14]; + assert_eq!(arr[0], 3.14); +} + +#[test] +fn test_empty_macro() { + let _arr = arr![f32;]; +} + +#[test] +fn test_cmp() { + arr![u8; 0x00].cmp(&arr![u8; 0x00]); +} + +/// This test should cause a helpful compile error if uncommented. +// #[test] +// fn test_empty_macro2(){ +// let arr = arr![]; +// } +#[cfg(feature = "serde")] +mod impl_serde { + extern crate serde_json; + + use generic_array::GenericArray; + use generic_array::typenum::U6; + + #[test] + fn test_serde_implementation() { + let array: GenericArray<f64, U6> = arr![f64; 0.0, 5.0, 3.0, 7.07192, 76.0, -9.0]; + let string = serde_json::to_string(&array).unwrap(); + assert_eq!(string, "[0.0,5.0,3.0,7.07192,76.0,-9.0]"); + + let test_array: GenericArray<f64, U6> = serde_json::from_str(&string).unwrap(); + assert_eq!(test_array, array); + } +} + +#[test] +fn test_map() { + let b: GenericArray<i32, U4> = GenericArray::generate(|i| i as i32 * 4).map(|x| x - 3); + + assert_eq!(b, arr![i32; -3, 1, 5, 9]); +} + +#[test] +fn test_zip() { + let a: GenericArray<_, U4> = GenericArray::generate(|i| i + 1); + let b: GenericArray<_, U4> = GenericArray::generate(|i| i as i32 * 4); + + // Uses reference and non-reference arguments + let c = (&a).zip(b, |r, l| *r as i32 + l); + + assert_eq!(c, arr![i32; 1, 6, 11, 16]); +} + +#[test] +#[should_panic] +fn test_from_iter_short() { + use core::iter::repeat; + + let a: GenericArray<_, U4> = repeat(11).take(3).collect(); + + assert_eq!(a, arr![i32; 11, 11, 11, 0]); +} + +#[test] +fn test_from_iter() { + use core::iter::{once, repeat}; + + let a: GenericArray<_, U4> = repeat(11).take(3).chain(once(0)).collect(); + + assert_eq!(a, arr![i32; 11, 11, 11, 0]); +} + +#[test] +fn test_sizes() { + #![allow(dead_code)] + use core::mem::{size_of, size_of_val}; + + #[derive(Debug, Copy, Clone)] + #[repr(C)] + #[repr(packed)] + struct Test { + t: u16, + s: u32, + r: u16, + f: u16, + o: u32, + } + + assert_eq!(size_of::<Test>(), 14); + + assert_eq!(size_of_val(&arr![u8; 1, 2, 3]), size_of::<u8>() * 3); + assert_eq!(size_of_val(&arr![u32; 1]), size_of::<u32>() * 1); + assert_eq!(size_of_val(&arr![u64; 1, 2, 3, 4]), size_of::<u64>() * 4); + + assert_eq!(size_of::<GenericArray<Test, U97>>(), size_of::<Test>() * 97); +} + +#[test] +fn test_append() { + let a = arr![i32; 1, 2, 3]; + + let b = a.append(4); + + assert_eq!(b, arr![i32; 1, 2, 3, 4]); +} + +#[test] +fn test_prepend() { + let a = arr![i32; 1, 2, 3]; + + let b = a.prepend(4); + + assert_eq!(b, arr![i32; 4, 1, 2, 3]); +} + +#[test] +fn test_pop() { + let a = arr![i32; 1, 2, 3, 4]; + + let (init, last) = a.pop_back(); + + assert_eq!(init, arr![i32; 1, 2, 3]); + assert_eq!(last, 4); + + let (head, tail) = a.pop_front(); + + assert_eq!(head, 1); + assert_eq!(tail, arr![i32; 2, 3, 4]); +} + +#[test] +fn test_split() { + let a = arr![i32; 1, 2, 3, 4]; + + let (b, c) = a.split(); + + assert_eq!(b, arr![i32; 1]); + assert_eq!(c, arr![i32; 2, 3, 4]); + + let (e, f) = a.split(); + + assert_eq!(e, arr![i32; 1, 2]); + assert_eq!(f, arr![i32; 3, 4]); +} + +#[test] +fn test_concat() { + let a = arr![i32; 1, 2]; + let b = arr![i32; 3, 4]; + + let c = a.concat(b); + + assert_eq!(c, arr![i32; 1, 2, 3, 4]); + + let (d, e) = c.split(); + + assert_eq!(d, arr![i32; 1]); + assert_eq!(e, arr![i32; 2, 3, 4]); +} + +#[test] +fn test_fold() { + let a = arr![i32; 1, 2, 3, 4]; + + assert_eq!(10, a.fold(0, |a, x| a + x)); +} + +fn sum_generic<S>(s: S) -> i32 +where + S: FunctionalSequence<i32>, + S::Item: Add<i32, Output = i32>, // `+` + i32: Add<S::Item, Output = i32>, // reflexive +{ + s.fold(0, |a, x| a + x) +} + +#[test] +fn test_sum() { + let a = sum_generic(arr![i32; 1, 2, 3, 4]); + + assert_eq!(a, 10); +} |