Skip to content

Commit

Permalink
Add missing tests for number parsing (#1769)
Browse files Browse the repository at this point in the history
Co-authored-by: cxworks <[email protected]>
Co-authored-by: Geoffroy Couprie <[email protected]>
  • Loading branch information
3 people authored Dec 8, 2024
1 parent 34cc438 commit 5281128
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/number/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1716,6 +1716,13 @@ mod tests {
assert_parse!(le_i16(&[0x00, 0x80][..]), Ok((&b""[..], -32_768_i16)));
}

#[test]
fn le_u16_test() {
assert_parse!(le_u16(&[0x00, 0x03][..]), Ok((&b""[..], 0x0300)));
assert_parse!(le_u16(&[b'a', b'b'][..]), Ok((&b""[..], 0x6261)));
assert_parse!(le_u16(&[0x01][..]), Err(Err::Incomplete(Needed::new(1))));
}

#[test]
fn le_u24_tests() {
assert_parse!(le_u24(&[0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
Expand Down Expand Up @@ -1750,6 +1757,19 @@ mod tests {
);
}

#[test]
fn le_u32_test() {
assert_parse!(
le_u32(&[0x00, 0x03, 0x05, 0x07][..]),
Ok((&b""[..], 0x07050300))
);
assert_parse!(
le_u32(&[b'a', b'b', b'c', b'd'][..]),
Ok((&b""[..], 0x64636261))
);
assert_parse!(le_u32(&[0x01][..]), Err(Err::Incomplete(Needed::new(3))));
}

#[test]
fn le_i64_tests() {
assert_parse!(
Expand Down
63 changes: 62 additions & 1 deletion tests/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ 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::error::ErrorKind;
use nom::number::complete::f32;
use nom::number::complete::f64;
use nom::number::Endianness;
use nom::sequence::{delimited, pair};
use nom::{Err, Needed};

Check warning on line 10 in tests/float.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

unused import: `Needed`

Check warning on line 10 in tests/float.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

unused import: `Needed`

Check warning on line 10 in tests/float.rs

View workflow job for this annotation

GitHub Actions / Test (beta)

unused import: `Needed`

Check warning on line 10 in tests/float.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --features "std")

unused import: `Needed`

Check warning on line 10 in tests/float.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

unused import: `Needed`

Check warning on line 10 in tests/float.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features --features "alloc")

unused import: `Needed`

Check warning on line 10 in tests/float.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features)

unused import: `Needed`

Check warning on line 10 in tests/float.rs

View workflow job for this annotation

GitHub Actions / Test (1.65.0)

unused import: `Needed`

Check warning on line 10 in tests/float.rs

View workflow job for this annotation

GitHub Actions / Test (stable)

unused import: `Needed`
use nom::{IResult, Parser};

use std::num::NonZeroUsize;

Check warning on line 12 in tests/float.rs

View workflow job for this annotation

GitHub Actions / Test (nightly)

unused import: `std::num::NonZeroUsize`

Check warning on line 12 in tests/float.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

unused import: `std::num::NonZeroUsize`

Check warning on line 12 in tests/float.rs

View workflow job for this annotation

GitHub Actions / Test (beta)

unused import: `std::num::NonZeroUsize`

Check warning on line 12 in tests/float.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --features "std")

unused import: `std::num::NonZeroUsize`

Check warning on line 12 in tests/float.rs

View workflow job for this annotation

GitHub Actions / Test (nightly, --no-default-features)

unused import: `std::num::NonZeroUsize`

Check warning on line 12 in tests/float.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features --features "alloc")

unused import: `std::num::NonZeroUsize`

Check warning on line 12 in tests/float.rs

View workflow job for this annotation

GitHub Actions / Test (stable, --no-default-features)

unused import: `std::num::NonZeroUsize`

Check warning on line 12 in tests/float.rs

View workflow job for this annotation

GitHub Actions / Test (1.65.0)

unused import: `std::num::NonZeroUsize`

Check warning on line 12 in tests/float.rs

View workflow job for this annotation

GitHub Actions / Test (stable)

unused import: `std::num::NonZeroUsize`
use std::str;
use std::str::FromStr;

Expand Down Expand Up @@ -45,3 +50,59 @@ fn float_test() {
assert_eq!(float(&b"+123.456;"[..]), Ok((&b";"[..], 123.456)));
assert_eq!(float(&b"-123.456;"[..]), Ok((&b";"[..], -123.456)));
}

#[test]
fn test_f32_big_endian() {
let be_f32 = |s| f32::<_, (_, ErrorKind)>(Endianness::Big)(s);

assert_eq!(
be_f32(&[0x41, 0x48, 0x00, 0x00][..]),
Ok((&[] as &[u8], 12.5))
);
}

#[test]
fn test_f32_little_endian() {
let le_f32 = |s| f32::<_, (_, ErrorKind)>(Endianness::Little)(s);

assert_eq!(
le_f32(&[0x00, 0x00, 0x48, 0x41][..]),
Ok((&[] as &[u8], 12.5))
);
}

#[test]
fn test_f64_big_endian() {
let be_f64 = |s| f64::<&[u8], (&[u8], ErrorKind)>(Endianness::Big)(s);

let input = &[0x40, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..];
let expected = 12.5f64;
match be_f64(input) {
Ok((rest, value)) => {
assert!(rest.is_empty());
assert_eq!(value, expected);
}
Err(_) => assert!(false, "Failed to parse big-endian f64"),
}

let incomplete_input = &b"abc"[..];
assert!(matches!(be_f64(incomplete_input), Err(Err::Error(_))));
}

#[test]
fn test_f64_little_endian() {
let le_f64 = |s| f64::<&[u8], (&[u8], ErrorKind)>(Endianness::Little)(s);

let input = &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x40][..];
let expected = 12.5f64;
match le_f64(input) {
Ok((rest, value)) => {
assert!(rest.is_empty());
assert_eq!(value, expected);
}
Err(_) => assert!(false, "Failed to parse little-endian f64"),
}

let incomplete_input = &b"abc"[..];
assert!(matches!(le_f64(incomplete_input), Err(Err::Error(_))));
}

0 comments on commit 5281128

Please sign in to comment.