-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
209 lines (177 loc) · 6.02 KB
/
Copy pathmod.rs
File metadata and controls
209 lines (177 loc) · 6.02 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use crate::properties::{
AdditiveIdentityProperty, InfinityProperty, IsNanProperty, MultiplicativeIdentityProperty,
SignumProperty,
};
impl SignumProperty for f32 {
fn is_negative_property(&self) -> bool {
self.is_sign_negative()
}
fn is_positive_property(&self) -> bool {
self.is_sign_positive() && *self != 0.
}
}
impl SignumProperty for f64 {
fn is_negative_property(&self) -> bool {
self.is_sign_negative()
}
fn is_positive_property(&self) -> bool {
self.is_sign_positive() && *self != 0.
}
}
macro_rules! impl_additive_identity_property {
($type:ty) => {
impl AdditiveIdentityProperty for $type {
fn additive_identity() -> Self {
0.
}
}
impl AdditiveIdentityProperty for &$type {
fn additive_identity() -> Self {
&0.
}
}
};
}
impl_additive_identity_property!(f32);
impl_additive_identity_property!(f64);
macro_rules! impl_multiplicative_identity_property {
($type:ty) => {
impl MultiplicativeIdentityProperty for $type {
fn multiplicative_identity() -> Self {
1.
}
}
impl MultiplicativeIdentityProperty for &$type {
fn multiplicative_identity() -> Self {
&1.
}
}
};
}
impl_multiplicative_identity_property!(f32);
impl_multiplicative_identity_property!(f64);
impl InfinityProperty for f32 {
fn is_infinite_property(&self) -> bool {
self.is_infinite()
}
fn is_finite_property(&self) -> bool {
self.is_finite()
}
}
impl InfinityProperty for f64 {
fn is_infinite_property(&self) -> bool {
self.is_infinite()
}
fn is_finite_property(&self) -> bool {
self.is_finite()
}
}
impl IsNanProperty for f32 {
fn is_nan_property(&self) -> bool {
self.is_nan()
}
}
impl IsNanProperty for f64 {
fn is_nan_property(&self) -> bool {
self.is_nan()
}
}
#[cfg(feature = "float-cmp")]
mod cmp {
use crate::assertions::{AssertIsCloseToWithDefaultMargin, AssertIsCloseToWithinMargin};
use crate::colored::mark_diff;
use crate::expectations::{IsCloseTo, is_close_to, not};
use crate::spec::{DiffFormat, Expectation, Expression, FailingStrategy, Invertible, Spec};
use crate::std::{format, string::String};
use float_cmp::{ApproxEq, F32Margin, F64Margin};
impl<R> AssertIsCloseToWithDefaultMargin<f32> for Spec<'_, f32, R>
where
R: FailingStrategy,
{
fn is_close_to(self, expected: f32) -> Self {
self.expecting(is_close_to(expected).within_margin((4. * f32::EPSILON, 4)))
}
fn is_not_close_to(self, expected: f32) -> Self {
self.expecting(not(
is_close_to(expected).within_margin((4. * f32::EPSILON, 4))
))
}
}
impl<R> AssertIsCloseToWithinMargin<f32, F32Margin> for Spec<'_, f32, R>
where
R: FailingStrategy,
{
fn is_close_to_with_margin(self, expected: f32, margin: impl Into<F32Margin>) -> Self {
self.expecting(is_close_to(expected).within_margin(margin))
}
fn is_not_close_to_with_margin(self, expected: f32, margin: impl Into<F32Margin>) -> Self {
self.expecting(not(is_close_to(expected).within_margin(margin)))
}
}
impl<R> AssertIsCloseToWithDefaultMargin<f64> for Spec<'_, f64, R>
where
R: FailingStrategy,
{
fn is_close_to(self, expected: f64) -> Self {
self.expecting(is_close_to(expected).within_margin((4. * f64::EPSILON, 4)))
}
fn is_not_close_to(self, expected: f64) -> Self {
self.expecting(not(
is_close_to(expected).within_margin((4. * f64::EPSILON, 4))
))
}
}
impl<R> AssertIsCloseToWithinMargin<f64, F64Margin> for Spec<'_, f64, R>
where
R: FailingStrategy,
{
fn is_close_to_with_margin(self, expected: f64, margin: impl Into<F64Margin>) -> Self {
self.expecting(is_close_to(expected).within_margin(margin))
}
fn is_not_close_to_with_margin(self, expected: f64, margin: impl Into<F64Margin>) -> Self {
self.expecting(not(is_close_to(expected).within_margin(margin)))
}
}
impl Expectation<f32> for IsCloseTo<f32, F32Margin> {
fn test(&mut self, subject: &f32) -> bool {
subject.approx_eq(self.expected, self.margin)
}
fn message(
&self,
expression: &Expression<'_>,
actual: &f32,
inverted: bool,
format: &DiffFormat,
) -> String {
let not = if inverted { "not " } else { "" };
let (marked_actual, marked_expected) = mark_diff(actual, &self.expected, format);
format!(
"expected {expression} to be {not}close to {:?}\n within a margin of epsilon={:e} and ulps={}\n but was: {marked_actual}\n expected: {marked_expected}",
&self.expected, self.margin.epsilon, self.margin.ulps
)
}
}
impl Invertible for IsCloseTo<f32, F32Margin> {}
impl Expectation<f64> for IsCloseTo<f64, F64Margin> {
fn test(&mut self, subject: &f64) -> bool {
subject.approx_eq(self.expected, self.margin)
}
fn message(
&self,
expression: &Expression<'_>,
actual: &f64,
inverted: bool,
format: &DiffFormat,
) -> String {
let not = if inverted { "not " } else { "" };
let (marked_actual, marked_expected) = mark_diff(actual, &self.expected, format);
format!(
"expected {expression} to be {not}close to {:?}\n within a margin of epsilon={:e} and ulps={}\n but was: {marked_actual}\n expected: {marked_expected}",
&self.expected, self.margin.epsilon, self.margin.ulps
)
}
}
impl Invertible for IsCloseTo<f64, F64Margin> {}
}
#[cfg(test)]
mod tests;