assertables/assert_f64/assert_f64_le.rs
1//! Assert a floating point 64-bit number is equal to another within f64::EPSILON.
2//!
3//! Pseudocode:<br>
4//! a ≤ b
5//!
6//! # Example
7//!
8//! ```rust
9//! use assertables::*;
10//!
11//! let a: f64 = 1.0 / 3.0;
12//! let b: f64 = 0.3333333333333339;
13//! assert_f64_le!(a, b);
14//! ```
15//!
16//! # Module macros
17//!
18//! * [`assert_f64_le`](macro@crate::assert_f64_le)
19//! * [`assert_f64_le_as_result`](macro@crate::assert_f64_le_as_result)
20//! * [`debug_assert_f64_le`](macro@crate::debug_assert_f64_le)
21
22/// Assert two floating point numbers are equal within f64::EPSILON.
23///
24/// Pseudocode:<br>
25/// a ≤ b
26///
27/// * If true, return Result `Ok(())`.
28///
29/// * Otherwise, return Result `Err(message)`.
30///
31/// This macro is useful for runtime checks, such as checking parameters,
32/// or sanitizing inputs, or handling different results in different ways.
33///
34/// # Module macros
35///
36/// * [`assert_f64_le`](macro@crate::assert_f64_le)
37/// * [`assert_f64_le_as_result`](macro@crate::assert_f64_le_as_result)
38/// * [`debug_assert_f64_le`](macro@crate::debug_assert_f64_le)
39///
40#[macro_export]
41macro_rules! assert_f64_le_as_result {
42 ($a:expr, $b:expr $(,)?) => {
43 match (&$a, &$b) {
44 (a, b) => {
45 if (a + f64::EPSILON <= *b) || (a >= b && a - b < f64::EPSILON) || (a <= b && b - a < f64::EPSILON){
46 Ok(())
47 } else {
48 Err(format!(
49 concat!(
50 "assertion failed: `assert_f64_le!(a, b)`\n",
51 "https://docs.rs/assertables/9.8.3/assertables/macro.assert_f64_le.html\n",
52 " a label: `{}`,\n",
53 " a debug: `{}`,\n",
54 " b label: `{}`,\n",
55 " b debug: `{:?}`,\n",
56 " diff: `{}`,\n",
57 " ε: `{}`",
58 ),
59 stringify!($a),
60 a,
61 stringify!($b),
62 b,
63 a - b,
64 f64::EPSILON,
65 ))
66 }
67 }
68 }
69 };
70}
71
72#[cfg(test)]
73mod test_assert_f64_le_as_result {
74 use crate::assert_f64::{EQ, EQ_GT, EQ_LT, GT, LT};
75 use std::sync::Once;
76
77 #[test]
78 fn lt() {
79 let a: f64 = EQ;
80 let b: f64 = GT;
81 for _ in 0..1 {
82 let actual = assert_f64_le_as_result!(a, b);
83 assert_eq!(actual.unwrap(), ());
84 }
85 }
86
87 #[test]
88 fn lt_once() {
89 static A: Once = Once::new();
90 fn a() -> f64 {
91 if A.is_completed() {
92 panic!("A.is_completed()")
93 } else {
94 A.call_once(|| {})
95 }
96 EQ
97 }
98
99 static B: Once = Once::new();
100 fn b() -> f64 {
101 if B.is_completed() {
102 panic!("B.is_completed()")
103 } else {
104 B.call_once(|| {})
105 }
106 GT
107 }
108
109 assert_eq!(A.is_completed(), false);
110 assert_eq!(B.is_completed(), false);
111 let result = assert_f64_le_as_result!(a(), b());
112 assert!(result.is_ok());
113 assert_eq!(A.is_completed(), true);
114 assert_eq!(B.is_completed(), true);
115 }
116
117 #[test]
118 fn eq() {
119 let a: f64 = EQ;
120 let b: f64 = EQ;
121 for _ in 0..1 {
122 let actual = assert_f64_le_as_result!(a, b);
123 assert_eq!(actual.unwrap(), ());
124 }
125 }
126
127 #[test]
128 fn eq_once() {
129 static A: Once = Once::new();
130 fn a() -> f64 {
131 if A.is_completed() {
132 panic!("A.is_completed()")
133 } else {
134 A.call_once(|| {})
135 }
136 EQ
137 }
138
139 static B: Once = Once::new();
140 fn b() -> f64 {
141 if B.is_completed() {
142 panic!("B.is_completed()")
143 } else {
144 B.call_once(|| {})
145 }
146 EQ
147 }
148
149 assert_eq!(A.is_completed(), false);
150 assert_eq!(B.is_completed(), false);
151 let result = assert_f64_le_as_result!(a(), b());
152 assert!(result.is_ok());
153 assert_eq!(A.is_completed(), true);
154 assert_eq!(B.is_completed(), true);
155 }
156
157 #[test]
158 fn eq_lt() {
159 let a: f64 = EQ;
160 let b: f64 = EQ_GT;
161 for _ in 0..1 {
162 let actual = assert_f64_le_as_result!(a, b);
163 assert_eq!(actual.unwrap(), ());
164 }
165 }
166
167 #[test]
168 fn eq_lt_once() {
169 static A: Once = Once::new();
170 fn a() -> f64 {
171 if A.is_completed() {
172 panic!("A.is_completed()")
173 } else {
174 A.call_once(|| {})
175 }
176 EQ
177 }
178
179 static B: Once = Once::new();
180 fn b() -> f64 {
181 if B.is_completed() {
182 panic!("B.is_completed()")
183 } else {
184 B.call_once(|| {})
185 }
186 EQ_GT
187 }
188
189 assert_eq!(A.is_completed(), false);
190 assert_eq!(B.is_completed(), false);
191 let result = assert_f64_le_as_result!(a(), b());
192 assert!(result.is_ok());
193 assert_eq!(A.is_completed(), true);
194 assert_eq!(B.is_completed(), true);
195 }
196
197 #[test]
198 fn eq_gt() {
199 let a: f64 = EQ;
200 let b: f64 = EQ_LT;
201 for _ in 0..1 {
202 let actual = assert_f64_le_as_result!(a, b);
203 assert_eq!(actual.unwrap(), ());
204 }
205 }
206
207 #[test]
208 fn eq_gt_once() {
209 static A: Once = Once::new();
210 fn a() -> f64 {
211 if A.is_completed() {
212 panic!("A.is_completed()")
213 } else {
214 A.call_once(|| {})
215 }
216 EQ
217 }
218
219 static B: Once = Once::new();
220 fn b() -> f64 {
221 if B.is_completed() {
222 panic!("B.is_completed()")
223 } else {
224 B.call_once(|| {})
225 }
226 EQ_LT
227 }
228
229 assert_eq!(A.is_completed(), false);
230 assert_eq!(B.is_completed(), false);
231 let result = assert_f64_le_as_result!(a(), b());
232 assert!(result.is_ok());
233 assert_eq!(A.is_completed(), true);
234 assert_eq!(B.is_completed(), true);
235 }
236
237 #[test]
238 fn gt() {
239 let a: f64 = EQ;
240 let b: f64 = LT;
241 let actual = assert_f64_le_as_result!(a, b);
242 let message = concat!(
243 "assertion failed: `assert_f64_le!(a, b)`\n",
244 "https://docs.rs/assertables/9.8.3/assertables/macro.assert_f64_le.html\n",
245 " a label: `a`,\n",
246 " a debug: `0.3333333333333333`,\n",
247 " b label: `b`,\n",
248 " b debug: `0.3333333333333329`,\n",
249 " diff: `0.0000000000000003885780586188048`,\n",
250 " ε: `0.0000000000000002220446049250313`",
251 );
252 assert_eq!(actual.unwrap_err(), message);
253 }
254}
255
256/// Assert a floating point 64-bit number is equal to another within f64::EPSILON.
257///
258/// Pseudocode:<br>
259/// a ≤ b
260///
261/// * If true, return `()`.
262///
263/// * Otherwise, call [`panic!`] with a message and the values of the
264/// expressions with their debug representations.
265///
266/// # Examples
267///
268/// ```rust
269/// use assertables::*;
270/// # use std::panic;
271///
272/// # fn main() {
273/// let a: f64 = 1.0 / 3.0;
274/// let b: f64 = 0.3333333333333335;
275/// assert_f64_le!(a, b);
276///
277/// # let result = panic::catch_unwind(|| {
278/// // This will panic
279/// let a: f64 = 1.0 / 3.0;
280/// let b: f64 = 0.3333333333333329;
281/// assert_f64_le!(a, b);
282/// # });
283/// // assertion failed: `assert_f64_le!(a, b)`
284/// // https://docs.rs/assertables/…/assertables/macro.assert_f64_le.html
285/// // a label: `a`,
286/// // a debug: `0.3333333333333333`,
287/// // b label: `b`,
288/// // b debug: `0.3333333333333329`,`
289/// // diff: `0.0000000000000004996003610813204`,
290/// // ε: `0.0000000000000002220446049250313`
291/// # let actual = result.unwrap_err().downcast::<String>().unwrap().to_string();
292/// # let message = concat!(
293/// # "assertion failed: `assert_f64_le!(a, b)`\n",
294/// # "https://docs.rs/assertables/9.8.3/assertables/macro.assert_f64_le.html\n",
295/// # " a label: `a`,\n",
296/// # " a debug: `0.3333333333333333`,\n",
297/// # " b label: `b`,\n",
298/// # " b debug: `0.3333333333333329`,\n",
299/// # " diff: `0.0000000000000003885780586188048`,\n",
300/// # " ε: `0.0000000000000002220446049250313`",
301/// # );
302/// # assert_eq!(actual, message);
303/// # }
304/// ```
305///
306/// # Module macros
307///
308/// * [`assert_f64_le`](macro@crate::assert_f64_le)
309/// * [`assert_f64_le_as_result`](macro@crate::assert_f64_le_as_result)
310/// * [`debug_assert_f64_le`](macro@crate::debug_assert_f64_le)
311///
312#[macro_export]
313macro_rules! assert_f64_le {
314 ($a:expr, $b:expr $(,)?) => {
315 match $crate::assert_f64_le_as_result!($a, $b) {
316 Ok(()) => (),
317 Err(err) => panic!("{}", err),
318 }
319 };
320 ($a:expr, $b:expr, $($message:tt)+) => {
321 match $crate::assert_f64_le_as_result!($a, $b) {
322 Ok(()) => (),
323 Err(err) => panic!("{}\n{}", format_args!($($message)+), err),
324 }
325 };
326}
327
328#[cfg(test)]
329mod test_assert_f64_le {
330 use crate::assert_f64::{EQ, GT};
331 use std::panic;
332
333 #[test]
334 fn lt() {
335 let a: f64 = EQ;
336 let b: f64 = GT;
337 for _ in 0..1 {
338 let actual = assert_f64_le!(a, b);
339 assert_eq!(actual, ());
340 }
341 }
342}
343
344/// Assert a floating point 64-bit number is equal to another within f64::EPSILON.
345///
346/// Pseudocode:<br>
347/// a ≤ b
348///
349/// This macro provides the same statements as [`assert_f64_le`](macro.assert_f64_le.html),
350/// except this macro's statements are only enabled in non-optimized
351/// builds by default. An optimized build will not execute this macro's
352/// statements unless `-C debug-assertions` is passed to the compiler.
353///
354/// This macro is useful for checks that are too expensive to be present
355/// in a release build but may be helpful during development.
356///
357/// The result of expanding this macro is always type checked.
358///
359/// An unchecked assertion allows a program in an inconsistent state to
360/// keep running, which might have unexpected consequences but does not
361/// introduce unsafety as long as this only happens in safe code. The
362/// performance cost of assertions, however, is not measurable in general.
363/// Replacing `assert*!` with `debug_assert*!` is thus only encouraged
364/// after thorough profiling, and more importantly, only in safe code!
365///
366/// This macro is intended to work in a similar way to
367/// [`::std::debug_assert`](https://doc.rust-lang.org/std/macro.debug_assert.html).
368///
369/// # Module macros
370///
371/// * [`assert_f64_le`](macro@crate::assert_f64_le)
372/// * [`assert_f64_le`](macro@crate::assert_f64_le)
373/// * [`debug_assert_f64_le`](macro@crate::debug_assert_f64_le)
374///
375#[macro_export]
376macro_rules! debug_assert_f64_le {
377 ($($arg:tt)*) => {
378 if $crate::cfg!(debug_assertions) {
379 $crate::assert_f64_le!($($arg)*);
380 }
381 };
382}