mlx_rs/macros/assert.rs
1/// Asserts that two arrays have the same shape and sufficiently close elements.
2///
3/// This macro ignores dtype. Its optional third argument is passed as both `rtol`
4/// and `atol` to [`Array::all_close`](crate::Array::all_close); omitting it uses
5/// that method's default tolerances. It panics if the shapes differ, the values
6/// are not sufficiently close, or the comparison returns an error.
7///
8/// Use [`Array::all_close`](crate::Array::all_close) with separate `rtol` and
9/// `atol` arguments, plus explicit shape and dtype checks:
10///
11/// ```
12/// use mlx_rs::array;
13///
14/// let actual = array!([1.0_f32, 2.0]);
15/// let expected = array!([1.0_f32, 2.0]);
16/// assert_eq!(actual.shape(), expected.shape());
17/// assert_eq!(actual.dtype(), expected.dtype());
18/// assert!(actual.all_close(&expected, Some(1e-5), Some(1e-8), None).unwrap());
19/// ```
20#[macro_export]
21#[deprecated(
22 since = "0.33.0",
23 note = "use `Array::all_close` with separate `rtol` and `atol` arguments and an explicit dtype check"
24)]
25macro_rules! assert_array_eq {
26 ($value:expr, $expected:expr) => {
27 #[allow(deprecated)]
28 {
29 assert_array_eq!($value, $expected, None);
30 }
31 };
32 ($value:expr, $expected:expr, $atol:expr) => {
33 assert_eq!($value.shape(), $expected.shape(), "Shapes are not equal");
34 let assert = $value.all_close(&$expected, $atol, $atol, None);
35 assert!(assert.unwrap(), "Values are not sufficiently close");
36 };
37}