Skip to content

Commit a7b82ad

Browse files
committed
Add tests
1 parent 875b31c commit a7b82ad

File tree

3 files changed

+140
-1
lines changed

3 files changed

+140
-1
lines changed

crates/core_simd/tests/masks.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ macro_rules! test_mask_api {
5959
let mask = core_simd::$name::<8>::splat(false);
6060
let _ = mask.test(8);
6161
}
62+
63+
#[test]
64+
fn any() {
65+
assert!(!core_simd::$name::<8>::splat(false).any());
66+
assert!(core_simd::$name::<8>::splat(true).any());
67+
let mut v = core_simd::$name::<8>::splat(false);
68+
v.set(2, true);
69+
assert!(v.any());
70+
}
71+
72+
#[test]
73+
fn all() {
74+
assert!(!core_simd::$name::<8>::splat(false).all());
75+
assert!(core_simd::$name::<8>::splat(true).all());
76+
let mut v = core_simd::$name::<8>::splat(false);
77+
v.set(2, true);
78+
assert!(!v.all());
79+
}
6280
}
6381
}
6482
}

crates/core_simd/tests/ops_macros.rs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,83 @@ macro_rules! impl_binary_checked_op_test {
136136
};
137137
}
138138

139+
#[macro_export]
140+
macro_rules! impl_common_integer_tests {
141+
{ $vector:ident, $scalar:ident } => {
142+
test_helpers::test_lanes! {
143+
fn wrapping_sum<const LANES: usize>() {
144+
test_helpers::test_1(&|x| {
145+
test_helpers::prop_assert_biteq! (
146+
$vector::<LANES>::from_array(x).wrapping_sum(),
147+
x.iter().copied().fold(0 as $scalar, $scalar::wrapping_add),
148+
);
149+
Ok(())
150+
});
151+
}
152+
153+
fn wrapping_product<const LANES: usize>() {
154+
test_helpers::test_1(&|x| {
155+
test_helpers::prop_assert_biteq! (
156+
$vector::<LANES>::from_array(x).wrapping_product(),
157+
x.iter().copied().fold(1 as $scalar, $scalar::wrapping_mul),
158+
);
159+
Ok(())
160+
});
161+
}
162+
163+
fn and_lanes<const LANES: usize>() {
164+
test_helpers::test_1(&|x| {
165+
test_helpers::prop_assert_biteq! (
166+
$vector::<LANES>::from_array(x).and_lanes(),
167+
x.iter().copied().fold(-1i8 as $scalar, <$scalar as core::ops::BitAnd>::bitand),
168+
);
169+
Ok(())
170+
});
171+
}
172+
173+
fn or_lanes<const LANES: usize>() {
174+
test_helpers::test_1(&|x| {
175+
test_helpers::prop_assert_biteq! (
176+
$vector::<LANES>::from_array(x).or_lanes(),
177+
x.iter().copied().fold(0 as $scalar, <$scalar as core::ops::BitOr>::bitor),
178+
);
179+
Ok(())
180+
});
181+
}
182+
183+
fn xor_lanes<const LANES: usize>() {
184+
test_helpers::test_1(&|x| {
185+
test_helpers::prop_assert_biteq! (
186+
$vector::<LANES>::from_array(x).xor_lanes(),
187+
x.iter().copied().fold(0 as $scalar, <$scalar as core::ops::BitXor>::bitxor),
188+
);
189+
Ok(())
190+
});
191+
}
192+
193+
fn max_lane<const LANES: usize>() {
194+
test_helpers::test_1(&|x| {
195+
test_helpers::prop_assert_biteq! (
196+
$vector::<LANES>::from_array(x).max_lane(),
197+
x.iter().copied().max().unwrap(),
198+
);
199+
Ok(())
200+
});
201+
}
202+
203+
fn min_lane<const LANES: usize>() {
204+
test_helpers::test_1(&|x| {
205+
test_helpers::prop_assert_biteq! (
206+
$vector::<LANES>::from_array(x).min_lane(),
207+
x.iter().copied().min().unwrap(),
208+
);
209+
Ok(())
210+
});
211+
}
212+
}
213+
}
214+
}
215+
139216
/// Implement tests for signed integers.
140217
#[macro_export]
141218
macro_rules! impl_signed_tests {
@@ -144,6 +221,8 @@ macro_rules! impl_signed_tests {
144221
type Vector<const LANES: usize> = core_simd::$vector<LANES>;
145222
type Scalar = $scalar;
146223

224+
impl_common_integer_tests! { Vector, Scalar }
225+
147226
test_helpers::test_lanes! {
148227
fn neg<const LANES: usize>() {
149228
test_helpers::test_unary_elementwise(
@@ -241,6 +320,8 @@ macro_rules! impl_unsigned_tests {
241320
type Vector<const LANES: usize> = core_simd::$vector<LANES>;
242321
type Scalar = $scalar;
243322

323+
impl_common_integer_tests! { Vector, Scalar }
324+
244325
test_helpers::test_lanes_panic! {
245326
fn rem_zero_panic<const LANES: usize>() {
246327
let a = Vector::<LANES>::splat(42);
@@ -397,6 +478,46 @@ macro_rules! impl_float_tests {
397478
},
398479
).unwrap();
399480
}
481+
482+
fn sum<const LANES: usize>() {
483+
test_helpers::test_1(&|x| {
484+
test_helpers::prop_assert_biteq! (
485+
Vector::<LANES>::from_array(x).sum(),
486+
x.iter().copied().fold(0 as Scalar, <Scalar as core::ops::Add>::add),
487+
);
488+
Ok(())
489+
});
490+
}
491+
492+
fn product<const LANES: usize>() {
493+
test_helpers::test_1(&|x| {
494+
test_helpers::prop_assert_biteq! (
495+
Vector::<LANES>::from_array(x).product(),
496+
x.iter().copied().fold(1. as Scalar, <Scalar as core::ops::Mul>::mul),
497+
);
498+
Ok(())
499+
});
500+
}
501+
502+
fn max_lane<const LANES: usize>() {
503+
test_helpers::test_1(&|x| {
504+
test_helpers::prop_assert_biteq! (
505+
Vector::<LANES>::from_array(x).max_lane(),
506+
x.iter().copied().fold(Scalar::NAN, Scalar::max),
507+
);
508+
Ok(())
509+
});
510+
}
511+
512+
fn min_lane<const LANES: usize>() {
513+
test_helpers::test_1(&|x| {
514+
test_helpers::prop_assert_biteq! (
515+
Vector::<LANES>::from_array(x).min_lane(),
516+
x.iter().copied().fold(Scalar::NAN, Scalar::min),
517+
);
518+
Ok(())
519+
});
520+
}
400521
}
401522
}
402523
}

crates/test_helpers/src/biteq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<T: BitEq> core::fmt::Debug for BitEqWrapper<'_, T> {
9595

9696
#[macro_export]
9797
macro_rules! prop_assert_biteq {
98-
{ $a:expr, $b:expr } => {
98+
{ $a:expr, $b:expr $(,)? } => {
9999
{
100100
use $crate::biteq::BitEqWrapper;
101101
let a = $a;

0 commit comments

Comments
 (0)