1+ use crate :: { caller_name:: get_caller_name, expected:: Expected , ShouldBeApproximatelyEqual } ;
2+
3+ impl ShouldBeApproximatelyEqual < f32 > for f32 {
4+ fn should_be_approx ( & self , expected : impl Expected < f32 > , tolerance : impl Expected < f32 > ) {
5+ let other = expected. value ( ) ;
6+ let tolerance = tolerance. value ( ) ;
7+ let high = other + tolerance;
8+ let low = other - tolerance;
9+ let caller_name = get_caller_name ( ) . unwrap_or ( "UNKNOWN" . to_string ( ) ) ;
10+
11+ if * self > high || * self < low {
12+ panic ! ( "{} should be between {:?} and {:?} but was {:?}" , caller_name, low, high, self )
13+ }
14+ }
15+
16+ fn should_not_be_approx ( & self , expected : impl Expected < f32 > , tolerance : impl Expected < f32 > ) {
17+ let other = expected. value ( ) ;
18+ let tolerance = tolerance. value ( ) ;
19+ let high = other + tolerance;
20+ let low = other - tolerance;
21+ let caller_name = get_caller_name ( ) . unwrap_or ( "UNKNOWN" . to_string ( ) ) ;
22+
23+ if * self <= high && * self >= low {
24+ panic ! ( "{} should not be between {:?} and {:?} but was {:?}" , caller_name, low, high, self )
25+ }
26+ }
27+ }
28+
29+ impl ShouldBeApproximatelyEqual < f64 > for f64 {
30+ fn should_be_approx ( & self , expected : impl Expected < f64 > , tolerance : impl Expected < f64 > ) {
31+ let other = expected. value ( ) ;
32+ let tolerance = tolerance. value ( ) ;
33+ let high = other + tolerance;
34+ let low = other - tolerance;
35+ let caller_name = get_caller_name ( ) . unwrap_or ( "UNKNOWN" . to_string ( ) ) ;
36+
37+ if * self > high || * self < low {
38+ panic ! ( "{} should be between {:?} and {:?} but was {:?}" , caller_name, low, high, self )
39+ }
40+ }
41+
42+ fn should_not_be_approx ( & self , expected : impl Expected < f64 > , tolerance : impl Expected < f64 > ) {
43+ let other = expected. value ( ) ;
44+ let tolerance = tolerance. value ( ) ;
45+ let high = other + tolerance;
46+ let low = other - tolerance;
47+ let caller_name = get_caller_name ( ) . unwrap_or ( "UNKNOWN" . to_string ( ) ) ;
48+
49+ if * self <= high && * self >= low {
50+ panic ! ( "{} should not be between {:?} and {:?} but was {:?}" , caller_name, low, high, self )
51+ }
52+ }
53+ }
0 commit comments