summaryrefslogtreecommitdiffstats
path: root/third_party/rust/nom/tests/float.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/nom/tests/float.rs
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
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.rs46
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..634b189899
--- /dev/null
+++ b/third_party/rust/nom/tests/float.rs
@@ -0,0 +1,46 @@
+use nom::branch::alt;
+use nom::bytes::complete::tag;
+use nom::character::streaming::digit1 as digit;
+use nom::combinator::{map, map_res, opt, recognize};
+use nom::sequence::{delimited, pair};
+use nom::IResult;
+
+use std::str;
+use std::str::FromStr;
+
+fn unsigned_float(i: &[u8]) -> IResult<&[u8], f32> {
+ let float_bytes = recognize(alt((
+ delimited(digit, tag("."), opt(digit)),
+ delimited(opt(digit), tag("."), digit),
+ )));
+ let float_str = map_res(float_bytes, str::from_utf8);
+ map_res(float_str, FromStr::from_str)(i)
+}
+
+fn float(i: &[u8]) -> IResult<&[u8], f32> {
+ map(
+ pair(opt(alt((tag("+"), tag("-")))), unsigned_float),
+ |(sign, value)| {
+ sign
+ .and_then(|s| if s[0] == b'-' { Some(-1f32) } else { None })
+ .unwrap_or(1f32)
+ * value
+ },
+ )(i)
+}
+
+#[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)));
+}