1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

/// Panics if the provided value is almost equal to zero.
///
/// Uses the [`almost` crate](https://crates.io/crates/almost).
/// See [the `almost::zero` documentation](https://docs.rs/almost/%2a/almost/fn.zero.html)
/// for more details.
///
/// A debug-only version is available as `chek::debug_not_almost_zero!`.
///
/// Optionally may take an additional message to display on failure, which is
/// formatted using standard format syntax.
///
/// # Example
///
/// ```rust
/// chek::not_almost_zero!(100.0);
/// chek::not_almost_zero!(0.1, "Some message goes here");
/// ```
#[macro_export]
macro_rules! not_almost_zero {
    ($value:expr $(,)?) => ({
        let value = $value;
        if $crate::almost::zero(value) {
            $crate::__value_assert_fail!("not_almost_zero", &value, stringify!($value));
        }
    });
    ($value:expr, $($msg_args:tt)+) => ({
        let value = $value;
        if $crate::almost::zero(value) {
            $crate::__value_assert_fail!("not_almost_zero", &value, stringify!($value), format_args!($($msg_args)+));
        }
    });
}

/// Same as `chek::not_almost_zero!` in debug builds or release builds where the `-C
/// debug-assertions` was provided to the compiler. For all other builds,
/// vanishes without a trace.
///
/// Optionally may take an additional message to display on failure, which is
/// formatted using standard format syntax.
///
/// # Example
///
/// ```rust
/// // These are compiled to nothing if debug_assertions are off!
/// chek::debug_not_almost_zero!(100.0);
/// chek::debug_not_almost_zero!(0.1, "Some message goes here");
/// ```
#[macro_export]
macro_rules! debug_not_almost_zero {
    ($value:expr $(,)?) => ({
        if cfg!(debug_assertions) {
            let value = $value;
            if $crate::almost::zero(value) {
                $crate::__value_assert_fail!("debug_not_almost_zero", &value, stringify!($value));
            }
        }
    });
    ($value:expr, $($msg_args:tt)+) => ({
        if cfg!(debug_assertions) {
            let value = $value;
            if $crate::almost::zero(value) {
                $crate::__value_assert_fail!("debug_not_almost_zero", &value, stringify!($value), format_args!($($msg_args)+));
            }
        }
    });
}

/// Panics if the provided value is not almost equal to zero.
///
/// Uses the [`almost` crate](https://crates.io/crates/almost). See the
/// [`almost::zero_with_tolerance`](https://docs.rs/almost/%2a/almost/fn.zero_with_tolerance.html)
/// documentation for more details.
///
/// A debug-only version is available as `chek::debug_not_almost_zero_with_tolerance!`.
///
/// Optionally may take an additional message to display on failure, which is
/// formatted using standard format syntax.
///
/// # Example
///
/// ```rust
/// chek::not_almost_zero_with_tolerance!(0.1, 0.01);
/// chek::not_almost_zero_with_tolerance!(std::f32::EPSILON, std::f32::EPSILON / 2.0, "Should not be almost zero!");
/// ```
#[macro_export]
macro_rules! not_almost_zero_with_tolerance {
    ($value:expr, $tolerance:expr $(,)?) => ({
        let (value, tolerance) = ($value, $tolerance);
        if $crate::almost::zero_with_tolerance(value, tolerance) {
            $crate::__value_assert_fail!("not_almost_zero_with_tolerance", &value, stringify!($value));
        }
    });
    ($value:expr, $tolerance:expr, $($msg_args:tt)+) => ({
        let (value, tolerance) = ($value, $tolerance);
        if $crate::almost::zero_with_tolerance(value, tolerance) {
            $crate::__value_assert_fail!("not_almost_zero_with_tolerance", &value, stringify!($value), format_args!($($msg_args)+));
        }
    });
}

/// Same as `chek::not_almost_zero_with_tolerance!` in debug builds or release
/// builds where the `-C debug-assertions` was provided to the compiler. For all
/// other builds, vanishes without a trace.
///
/// Optionally may take an additional message to display on failure, which is
/// formatted using standard format syntax.
///
/// # Example
///
/// ```rust
/// // These are compiled to nothing if debug_assertions are off!
/// chek::debug_not_almost_zero_with_tolerance!(0.1, 0.01);
/// chek::debug_not_almost_zero_with_tolerance!(std::f32::EPSILON, std::f32::EPSILON / 2.0, "Should not be almost zero!");
/// ```
#[macro_export]
macro_rules! debug_not_almost_zero_with_tolerance {
    ($value:expr, $tolerance:expr $(,)?) => ({
        let (value, tolerance) = ($value, $tolerance);
        if $crate::almost::zero_with_tolerance(value, tolerance) {
            $crate::__value_assert_fail!("debug_not_almost_zero_with_tolerance", &value, stringify!($value));
        }
    });
    ($value:expr, $tolerance:expr, $($msg_args:tt)+) => ({
        let (value, tolerance) = ($value, $tolerance);
        if $crate::almost::zero_with_tolerance(value, tolerance) {
            $crate::__value_assert_fail!("debug_not_almost_zero_with_tolerance", &value, stringify!($value), format_args!($($msg_args)+));
        }
    });
}