diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/nom/tests/float.rs | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/nom/tests/float.rs')
-rw-r--r-- | third_party/rust/nom/tests/float.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/third_party/rust/nom/tests/float.rs b/third_party/rust/nom/tests/float.rs new file mode 100644 index 0000000000..eb82804f29 --- /dev/null +++ b/third_party/rust/nom/tests/float.rs @@ -0,0 +1,46 @@ +#[macro_use] +extern crate nom; + +use nom::character::streaming::digit1 as digit; + +use std::str; +use std::str::FromStr; + +named!( + unsigned_float<f32>, + map_res!( + map_res!( + recognize!(alt!( + delimited!(digit, tag!("."), opt!(digit)) | delimited!(opt!(digit), tag!("."), digit) + )), + str::from_utf8 + ), + FromStr::from_str + ) +); + +named!( + float<f32>, + map!( + pair!(opt!(alt!(tag!("+") | tag!("-"))), unsigned_float), + |(sign, value): (Option<&[u8]>, f32)| sign + .and_then(|s| if s[0] == b'-' { Some(-1f32) } else { None }) + .unwrap_or(1f32) * value + ) +); + +#[test] +fn unsigned_float_test() { + assert_eq!(unsigned_float(&b"123.456;"[..]), Ok((&b";"[..], 123.456))); + assert_eq!(unsigned_float(&b"0.123;"[..]), Ok((&b";"[..], 0.123))); + assert_eq!(unsigned_float(&b"123.0;"[..]), Ok((&b";"[..], 123.0))); + assert_eq!(unsigned_float(&b"123.;"[..]), Ok((&b";"[..], 123.0))); + assert_eq!(unsigned_float(&b".123;"[..]), Ok((&b";"[..], 0.123))); +} + +#[test] +fn float_test() { + assert_eq!(float(&b"123.456;"[..]), Ok((&b";"[..], 123.456))); + assert_eq!(float(&b"+123.456;"[..]), Ok((&b";"[..], 123.456))); + assert_eq!(float(&b"-123.456;"[..]), Ok((&b";"[..], -123.456))); +} |