Debian Codegraph @

debian/rust-vendor/approx/src/macros.rs

raw
1 // Copyright 2015 Brendan Zabarauskas
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /// Approximate equality of using the absolute difference.
16 #[macro_export]
17 macro_rules! abs_diff_eq {
18 ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*) => {
19 $crate::AbsDiff::default()$(.$opt($val))*.eq(&$lhs, &$rhs)
20 };
21 ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*,) => {
22 $crate::AbsDiff::default()$(.$opt($val))*.eq(&$lhs, &$rhs)
23 };
24 }
25
26 /// Approximate inequality of using the absolute difference.
27 #[macro_export]
28 macro_rules! abs_diff_ne {
29 ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*) => {
30 $crate::AbsDiff::default()$(.$opt($val))*.ne(&$lhs, &$rhs)
31 };
32 ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*,) => {
33 $crate::AbsDiff::default()$(.$opt($val))*.ne(&$lhs, &$rhs)
34 };
35 }
36
37 /// Approximate equality using both the absolute difference and relative based comparisons.
38 #[macro_export]
39 macro_rules! relative_eq {
40 ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*) => {
41 $crate::Relative::default()$(.$opt($val))*.eq(&$lhs, &$rhs)
42 };
43 ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*,) => {
44 $crate::Relative::default()$(.$opt($val))*.eq(&$lhs, &$rhs)
45 };
46 }
47
48 /// Approximate inequality using both the absolute difference and relative based comparisons.
49 #[macro_export]
50 macro_rules! relative_ne {
51 ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*) => {
52 $crate::Relative::default()$(.$opt($val))*.ne(&$lhs, &$rhs)
53 };
54 ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*,) => {
55 $crate::Relative::default()$(.$opt($val))*.ne(&$lhs, &$rhs)
56 };
57 }
58
59 /// Approximate equality using both the absolute difference and ULPs (Units in Last Place).
60 #[macro_export]
61 macro_rules! ulps_eq {
62 ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*) => {
63 $crate::Ulps::default()$(.$opt($val))*.eq(&$lhs, &$rhs)
64 };
65 ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*,) => {
66 $crate::Ulps::default()$(.$opt($val))*.eq(&$lhs, &$rhs)
67 };
68 }
69
70 /// Approximate inequality using both the absolute difference and ULPs (Units in Last Place).
71 #[macro_export]
72 macro_rules! ulps_ne {
73 ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*) => {
74 $crate::Ulps::default()$(.$opt($val))*.ne(&$lhs, &$rhs)
75 };
76 ($lhs:expr, $rhs:expr $(, $opt:ident = $val:expr)*,) => {
77 $crate::Ulps::default()$(.$opt($val))*.ne(&$lhs, &$rhs)
78 };
79 }
80
81 #[doc(hidden)]
82 #[macro_export]
83 macro_rules! __assert_approx {
84 ($eq:ident, $given:expr, $expected:expr) => {{
85 match (&($given), &($expected)) {
86 (given, expected) => assert!(
87 $eq!(*given, *expected),
88 "assert_{}!({}, {})
89
90 left = {:?}
91 right = {:?}
92
93 ",
94 stringify!($eq),
95 stringify!($given),
96 stringify!($expected),
97 given, expected,
98 ),
99 }
100 }};
101 ($eq:ident, $given:expr, $expected:expr, $($opt:ident = $val:expr),+) => {{
102 match (&($given), &($expected)) {
103 (given, expected) => assert!(
104 $eq!(*given, *expected, $($opt = $val),+),
105 "assert_{}!({}, {}, {})
106
107 left = {:?}
108 right = {:?}
109
110 ",
111 stringify!($eq),
112 stringify!($given),
113 stringify!($expected),
114 stringify!($($opt = $val),+),
115 given, expected,
116 ),
117 }
118 }};
119 }
120
121 /// An assertion that delegates to [`abs_diff_eq!`], and panics with a helpful error on failure.
122 #[macro_export(local_inner_macros)]
123 macro_rules! assert_abs_diff_eq {
124 ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*) => {
125 __assert_approx!(abs_diff_eq, $given, $expected $(, $opt = $val)*)
126 };
127 ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*,) => {
128 __assert_approx!(abs_diff_eq, $given, $expected $(, $opt = $val)*)
129 };
130 }
131
132 /// An assertion that delegates to [`abs_diff_ne!`], and panics with a helpful error on failure.
133 #[macro_export(local_inner_macros)]
134 macro_rules! assert_abs_diff_ne {
135 ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*) => {
136 __assert_approx!(abs_diff_ne, $given, $expected $(, $opt = $val)*)
137 };
138 ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*,) => {
139 __assert_approx!(abs_diff_ne, $given, $expected $(, $opt = $val)*)
140 };
141 }
142
143 /// An assertion that delegates to [`relative_eq!`], and panics with a helpful error on failure.
144 #[macro_export(local_inner_macros)]
145 macro_rules! assert_relative_eq {
146 ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*) => {
147 __assert_approx!(relative_eq, $given, $expected $(, $opt = $val)*)
148 };
149 ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*,) => {
150 __assert_approx!(relative_eq, $given, $expected $(, $opt = $val)*)
151 };
152 }
153
154 /// An assertion that delegates to [`relative_ne!`], and panics with a helpful error on failure.
155 #[macro_export(local_inner_macros)]
156 macro_rules! assert_relative_ne {
157 ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*) => {
158 __assert_approx!(relative_ne, $given, $expected $(, $opt = $val)*)
159 };
160 ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*,) => {
161 __assert_approx!(relative_ne, $given, $expected $(, $opt = $val)*)
162 };
163 }
164
165 /// An assertion that delegates to [`ulps_eq!`], and panics with a helpful error on failure.
166 #[macro_export(local_inner_macros)]
167 macro_rules! assert_ulps_eq {
168 ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*) => {
169 __assert_approx!(ulps_eq, $given, $expected $(, $opt = $val)*)
170 };
171 ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*,) => {
172 __assert_approx!(ulps_eq, $given, $expected $(, $opt = $val)*)
173 };
174 }
175
176 /// An assertion that delegates to [`ulps_ne!`], and panics with a helpful error on failure.
177 #[macro_export(local_inner_macros)]
178 macro_rules! assert_ulps_ne {
179 ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*) => {
180 __assert_approx!(ulps_ne, $given, $expected $(, $opt = $val)*)
181 };
182 ($given:expr, $expected:expr $(, $opt:ident = $val:expr)*,) => {
183 __assert_approx!(ulps_ne, $given, $expected $(, $opt = $val)*)
184 };
185 }