Skip to content

Commit 53b583b

Browse files
Merge pull request google#565 from calder:dev/finite-matchers
PiperOrigin-RevId: 726887315
2 parents cb8eaf9 + 9a6712c commit 53b583b

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 Finite.
23+
pub fn is_finite() -> IsFiniteMatcher {
24+
IsFiniteMatcher
25+
}
26+
27+
#[derive(MatcherBase)]
28+
pub struct IsFiniteMatcher;
29+
30+
impl<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)]
41+
mod 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+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 Infinite.
23+
pub fn is_infinite() -> IsInfiniteMatcher {
24+
IsInfiniteMatcher
25+
}
26+
27+
#[derive(MatcherBase)]
28+
pub struct IsInfiniteMatcher;
29+
30+
impl<T: Float + Debug + Copy> Matcher<T> for IsInfiniteMatcher {
31+
fn matches(&self, actual: T) -> MatcherResult {
32+
actual.is_infinite().into()
33+
}
34+
35+
fn describe(&self, matcher_result: MatcherResult) -> Description {
36+
if matcher_result.into() { "is Infinite" } else { "isn't Infinite" }.into()
37+
}
38+
}
39+
40+
#[cfg(test)]
41+
mod tests {
42+
use crate::prelude::*;
43+
use crate::Result;
44+
45+
#[test]
46+
fn matches_f32_pos_infinity() -> Result<()> {
47+
verify_that!(f32::INFINITY, is_infinite())
48+
}
49+
50+
#[test]
51+
fn matches_f32_neg_infinity() -> Result<()> {
52+
verify_that!(f32::NEG_INFINITY, is_infinite())
53+
}
54+
55+
#[test]
56+
fn does_not_match_f32_number() -> Result<()> {
57+
verify_that!(0.0f32, not(is_infinite()))
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_pos_infinity() -> Result<()> {
67+
verify_that!(f64::INFINITY, is_infinite())
68+
}
69+
70+
#[test]
71+
fn matches_f64_neg_infinity() -> Result<()> {
72+
verify_that!(f64::NEG_INFINITY, is_infinite())
73+
}
74+
75+
#[test]
76+
fn does_not_match_f64_number() -> Result<()> {
77+
verify_that!(0.0f64, not(is_infinite()))
78+
}
79+
80+
#[test]
81+
fn does_not_match_f64_nan() -> Result<()> {
82+
verify_that!(f64::NAN, not(is_finite()))
83+
}
84+
}

googletest/src/matchers/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ mod ge_matcher;
3636
mod gt_matcher;
3737
mod has_entry_matcher;
3838
mod is_encoded_string_matcher;
39+
mod is_finite_matcher;
40+
mod is_infinite_matcher;
3941
mod is_matcher;
4042
mod is_nan_matcher;
4143
mod le_matcher;
@@ -75,6 +77,8 @@ pub use ge_matcher::ge;
7577
pub use gt_matcher::gt;
7678
pub use has_entry_matcher::has_entry;
7779
pub use is_encoded_string_matcher::is_utf8_string;
80+
pub use is_finite_matcher::is_finite;
81+
pub use is_infinite_matcher::is_infinite;
7882
pub use is_nan_matcher::is_nan;
7983
pub use le_matcher::le;
8084
pub use len_matcher::len;

0 commit comments

Comments
 (0)