googletest/matchers/
is_finite_matcher.rs1use crate::{
16 description::Description,
17 matcher::{Matcher, MatcherBase, MatcherResult},
18};
19use num_traits::float::Float;
20use std::fmt::Debug;
21
22pub fn is_finite() -> IsFiniteMatcher {
24 IsFiniteMatcher
25}
26
27#[derive(MatcherBase)]
28pub struct IsFiniteMatcher;
29
30impl<T: Float + Debug + Copy> Matcher<T> for IsFiniteMatcher {
31 fn matches(&self, actual: T) -> MatcherResult {
32 actual.is_finite().into()
33 }
34
35 fn describe(&self, matcher_result: MatcherResult) -> Description {
36 if matcher_result.into() { "is Finite" } else { "isn't Finite" }.into()
37 }
38}
39
40#[cfg(test)]
41mod tests {
42 use crate::prelude::*;
43 use crate::Result;
44
45 #[test]
46 fn matches_f32_number() -> Result<()> {
47 verify_that!(0.0f32, is_finite())
48 }
49
50 #[test]
51 fn does_not_match_f32_pos_infinity() -> Result<()> {
52 verify_that!(f32::INFINITY, not(is_finite()))
53 }
54
55 #[test]
56 fn does_not_match_f32_neg_infinity() -> Result<()> {
57 verify_that!(f32::NEG_INFINITY, not(is_finite()))
58 }
59
60 #[test]
61 fn does_not_match_f32_nan() -> Result<()> {
62 verify_that!(f32::NAN, not(is_finite()))
63 }
64
65 #[test]
66 fn matches_f64_number() -> Result<()> {
67 verify_that!(0.0f64, is_finite())
68 }
69
70 #[test]
71 fn does_not_match_f64_pos_infinity() -> Result<()> {
72 verify_that!(f64::INFINITY, not(is_finite()))
73 }
74
75 #[test]
76 fn does_not_match_f64_neg_infinity() -> Result<()> {
77 verify_that!(f64::NEG_INFINITY, not(is_finite()))
78 }
79
80 #[test]
81 fn does_not_match_f64_nan() -> Result<()> {
82 verify_that!(f64::NAN, not(is_finite()))
83 }
84}