1 // Copyright 2022 Google LLC
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 use crate::{
16 description::Description,
17 matcher::{Matcher, MatcherBase, MatcherResult},
18 };
19 use num_traits::float::Float;
20 use std::fmt::Debug;
21
22 /// Matches a floating point value which is NaN.
is_nan() -> IsNanMatcher23 pub fn is_nan() -> IsNanMatcher {
24 IsNanMatcher
25 }
26
27 #[derive(MatcherBase)]
28 pub struct IsNanMatcher;
29
30 impl<T: Float + Debug + Copy> Matcher<T> for IsNanMatcher {
matches(&self, actual: T) -> MatcherResult31 fn matches(&self, actual: T) -> MatcherResult {
32 actual.is_nan().into()
33 }
34
describe(&self, matcher_result: MatcherResult) -> Description35 fn describe(&self, matcher_result: MatcherResult) -> Description {
36 if matcher_result.into() { "is NaN" } else { "isn't NaN" }.into()
37 }
38 }
39
40 #[cfg(test)]
41 mod tests {
42 use crate::prelude::*;
43
44 #[test]
matches_f32_nan() -> Result<()>45 fn matches_f32_nan() -> Result<()> {
46 verify_that!(f32::NAN, is_nan())
47 }
48
49 #[test]
does_not_match_f32_number() -> Result<()>50 fn does_not_match_f32_number() -> Result<()> {
51 verify_that!(0.0f32, not(is_nan()))
52 }
53
54 #[test]
matches_f64_nan() -> Result<()>55 fn matches_f64_nan() -> Result<()> {
56 verify_that!(f64::NAN, is_nan())
57 }
58
59 #[test]
does_not_match_f64_number() -> Result<()>60 fn does_not_match_f64_number() -> Result<()> {
61 verify_that!(0.0f64, not(is_nan()))
62 }
63 }
64