#![allow(unused_imports, unused_macros)]
use super::*;
use crate::IResult;
macro_rules! assert_parse(
($left: expr, $right: expr) => {
let res: $crate::IResult<_, _, (_, ErrorKind)> = $left;
assert_eq!(res, $right);
};
);
#[cfg(test)]
mod complete_tests {
use super::*;
use crate::error::ErrorKind;
use crate::internal::Err;
use proptest::prelude::*;
#[test]
fn i8_tests() {
assert_parse!(i8().parse_complete(&[0x00][..]), Ok((&b""[..], 0)));
assert_parse!(i8().parse_complete(&[0x7f][..]), Ok((&b""[..], 127)));
assert_parse!(i8().parse_complete(&[0xff][..]), Ok((&b""[..], -1)));
assert_parse!(i8().parse_complete(&[0x80][..]), Ok((&b""[..], -128)));
}
#[test]
fn be_i8_tests() {
assert_parse!(be_i8().parse_complete(&[0x00][..]), Ok((&b""[..], 0)));
assert_parse!(be_i8().parse_complete(&[0x7f][..]), Ok((&b""[..], 127)));
assert_parse!(be_i8().parse_complete(&[0xff][..]), Ok((&b""[..], -1)));
assert_parse!(be_i8().parse_complete(&[0x80][..]), Ok((&b""[..], -128)));
}
#[test]
fn be_i16_tests() {
assert_parse!(
be_i16().parse_complete(&[0x00, 0x00][..]),
Ok((&b""[..], 0))
);
assert_parse!(
be_i16().parse_complete(&[0x7f, 0xff][..]),
Ok((&b""[..], 32_767_i16))
);
assert_parse!(
be_i16().parse_complete(&[0xff, 0xff][..]),
Ok((&b""[..], -1))
);
assert_parse!(
be_i16().parse_complete(&[0x80, 0x00][..]),
Ok((&b""[..], -32_768_i16))
);
}
#[test]
fn be_u24_tests() {
assert_parse!(
be_u24().parse_complete(&[0x00, 0x00, 0x00][..]),
Ok((&b""[..], 0))
);
assert_parse!(
be_u24().parse_complete(&[0x00, 0xFF, 0xFF][..]),
Ok((&b""[..], 65_535_u32))
);
assert_parse!(
be_u24().parse_complete(&[0x12, 0x34, 0x56][..]),
Ok((&b""[..], 1_193_046_u32))
);
}
#[test]
fn be_i24_tests() {
assert_parse!(
be_i24().parse_complete(&[0xFF, 0xFF, 0xFF][..]),
Ok((&b""[..], -1_i32))
);
assert_parse!(
be_i24().parse_complete(&[0xFF, 0x00, 0x00][..]),
Ok((&b""[..], -65_536_i32))
);
assert_parse!(
be_i24().parse_complete(&[0xED, 0xCB, 0xAA][..]),
Ok((&b""[..], -1_193_046_i32))
);
}
#[test]
fn be_i32_tests() {
assert_parse!(
be_i32().parse_complete(&[0x00, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], 0))
);
assert_parse!(
be_i32().parse_complete(&[0x7f, 0xff, 0xff, 0xff][..]),
Ok((&b""[..], 2_147_483_647_i32))
);
assert_parse!(
be_i32().parse_complete(&[0xff, 0xff, 0xff, 0xff][..]),
Ok((&b""[..], -1))
);
assert_parse!(
be_i32().parse_complete(&[0x80, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], -2_147_483_648_i32))
);
}
#[test]
fn be_i64_tests() {
assert_parse!(
be_i64().parse_complete(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], 0))
);
assert_parse!(
be_i64().parse_complete(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
Ok((&b""[..], 9_223_372_036_854_775_807_i64))
);
assert_parse!(
be_i64().parse_complete(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
Ok((&b""[..], -1))
);
assert_parse!(
be_i64().parse_complete(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], -9_223_372_036_854_775_808_i64))
);
}
#[test]
fn be_i128_tests() {
assert_parse!(
be_i128().parse_complete(
&[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00
][..]
),
Ok((&b""[..], 0))
);
assert_parse!(
be_i128().parse_complete(
&[
0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff
][..]
),
Ok((
&b""[..],
170_141_183_460_469_231_731_687_303_715_884_105_727_i128
))
);
assert_parse!(
be_i128().parse_complete(
&[
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff
][..]
),
Ok((&b""[..], -1))
);
assert_parse!(
be_i128().parse_complete(
&[
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00
][..]
),
Ok((
&b""[..],
-170_141_183_460_469_231_731_687_303_715_884_105_728_i128
))
);
}
#[test]
fn le_i8_tests() {
assert_parse!(le_i8().parse_complete(&[0x00][..]), Ok((&b""[..], 0)));
assert_parse!(le_i8().parse_complete(&[0x7f][..]), Ok((&b""[..], 127)));
assert_parse!(le_i8().parse_complete(&[0xff][..]), Ok((&b""[..], -1)));
assert_parse!(le_i8().parse_complete(&[0x80][..]), Ok((&b""[..], -128)));
}
#[test]
fn le_i16_tests() {
assert_parse!(
le_i16().parse_complete(&[0x00, 0x00][..]),
Ok((&b""[..], 0))
);
assert_parse!(
le_i16().parse_complete(&[0xff, 0x7f][..]),
Ok((&b""[..], 32_767_i16))
);
assert_parse!(
le_i16().parse_complete(&[0xff, 0xff][..]),
Ok((&b""[..], -1))
);
assert_parse!(
le_i16().parse_complete(&[0x00, 0x80][..]),
Ok((&b""[..], -32_768_i16))
);
}
#[test]
fn le_u24_tests() {
assert_parse!(
le_u24().parse_complete(&[0x00, 0x00, 0x00][..]),
Ok((&b""[..], 0))
);
assert_parse!(
le_u24().parse_complete(&[0xFF, 0xFF, 0x00][..]),
Ok((&b""[..], 65_535_u32))
);
assert_parse!(
le_u24().parse_complete(&[0x56, 0x34, 0x12][..]),
Ok((&b""[..], 1_193_046_u32))
);
}
#[test]
fn le_i24_tests() {
assert_parse!(
le_i24().parse_complete(&[0xFF, 0xFF, 0xFF][..]),
Ok((&b""[..], -1_i32))
);
assert_parse!(
le_i24().parse_complete(&[0x00, 0x00, 0xFF][..]),
Ok((&b""[..], -65_536_i32))
);
assert_parse!(
le_i24().parse_complete(&[0xAA, 0xCB, 0xED][..]),
Ok((&b""[..], -1_193_046_i32))
);
}
#[test]
fn le_i32_tests() {
assert_parse!(
le_i32().parse_complete(&[0x00, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], 0))
);
assert_parse!(
le_i32().parse_complete(&[0xff, 0xff, 0xff, 0x7f][..]),
Ok((&b""[..], 2_147_483_647_i32))
);
assert_parse!(
le_i32().parse_complete(&[0xff, 0xff, 0xff, 0xff][..]),
Ok((&b""[..], -1))
);
assert_parse!(
le_i32().parse_complete(&[0x00, 0x00, 0x00, 0x80][..]),
Ok((&b""[..], -2_147_483_648_i32))
);
}
#[test]
fn le_i64_tests() {
assert_parse!(
le_i64().parse_complete(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], 0))
);
assert_parse!(
le_i64().parse_complete(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f][..]),
Ok((&b""[..], 9_223_372_036_854_775_807_i64))
);
assert_parse!(
le_i64().parse_complete(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
Ok((&b""[..], -1))
);
assert_parse!(
le_i64().parse_complete(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80][..]),
Ok((&b""[..], -9_223_372_036_854_775_808_i64))
);
}
#[test]
fn le_i128_tests() {
assert_parse!(
le_i128().parse_complete(
&[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00
][..]
),
Ok((&b""[..], 0))
);
assert_parse!(
le_i128().parse_complete(
&[
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x7f
][..]
),
Ok((
&b""[..],
170_141_183_460_469_231_731_687_303_715_884_105_727_i128
))
);
assert_parse!(
le_i128().parse_complete(
&[
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff
][..]
),
Ok((&b""[..], -1))
);
assert_parse!(
le_i128().parse_complete(
&[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80
][..]
),
Ok((
&b""[..],
-170_141_183_460_469_231_731_687_303_715_884_105_728_i128
))
);
}
#[test]
fn be_f32_tests() {
assert_parse!(
be_f32().parse_complete(&[0x00, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], 0_f32))
);
assert_parse!(
be_f32().parse_complete(&[0x4d, 0x31, 0x1f, 0xd8][..]),
Ok((&b""[..], 185_728_392_f32))
);
}
#[test]
fn be_f64_tests() {
assert_parse!(
be_f64().parse_complete(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], 0_f64))
);
assert_parse!(
be_f64().parse_complete(&[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], 185_728_392_f64))
);
}
#[test]
fn le_f32_tests() {
assert_parse!(
le_f32().parse_complete(&[0x00, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], 0_f32))
);
assert_parse!(
le_f32().parse_complete(&[0xd8, 0x1f, 0x31, 0x4d][..]),
Ok((&b""[..], 185_728_392_f32))
);
}
#[test]
fn le_f64_tests() {
assert_parse!(
le_f64().parse_complete(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], 0_f64))
);
assert_parse!(
le_f64().parse_complete(&[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41][..]),
Ok((&b""[..], 185_728_392_f64))
);
}
#[test]
fn hex_u32_tests() {
}
#[test]
#[cfg(feature = "std")]
fn float_test() {
let mut test_cases = vec![
"+3.14",
"3.14",
"-3.14",
"0",
"0.0",
"1.",
".789",
"-.5",
"1e7",
"-1E-7",
".3e-2",
"1.e4",
"1.2e4",
"12.34",
"-1.234E-12",
"-1.234e-12",
"0.00000000000000000087",
];
for test in test_cases.drain(..) {
let expected32 = str::parse::<f32>(test).unwrap();
let expected64 = str::parse::<f64>(test).unwrap();
println!("now parsing: {test} -> {expected32}");
assert_parse!(recognize_float().parse_complete(test), Ok(("", test)));
assert_parse!(
float().parse_complete(test.as_bytes()),
Ok((&b""[..], expected32))
);
assert_parse!(float().parse_complete(test), Ok(("", expected32)));
assert_parse!(
double().parse_complete(test.as_bytes()),
Ok((&b""[..], expected64))
);
assert_parse!(double().parse_complete(test), Ok(("", expected64)));
}
let remaining_exponent = "-1.234E-";
assert_parse!(
recognize_float().parse_complete(remaining_exponent),
Err(Err::Failure(("", ErrorKind::Digit)))
);
let (_i, nan) = float::<_, ()>().parse_complete("NaN").unwrap();
assert!(nan.is_nan());
let (_i, inf) = float::<_, ()>().parse_complete("inf").unwrap();
assert!(inf.is_infinite());
let (i, inf) = float::<_, ()>().parse_complete("infinity").unwrap();
assert!(inf.is_infinite());
assert!(i.is_empty());
}
#[test]
fn configurable_endianness() {
use crate::number::Endianness;
fn be_tst16(i: &[u8]) -> IResult<&[u8], u16> {
u16(Endianness::Big).parse_complete(i)
}
fn le_tst16(i: &[u8]) -> IResult<&[u8], u16> {
u16(Endianness::Little).parse_complete(i)
}
assert_eq!(be_tst16(&[0x80, 0x00]), Ok((&b""[..], 32_768_u16)));
assert_eq!(le_tst16(&[0x80, 0x00]), Ok((&b""[..], 128_u16)));
fn be_tst32(i: &[u8]) -> IResult<&[u8], u32> {
u32(Endianness::Big).parse_complete(i)
}
fn le_tst32(i: &[u8]) -> IResult<&[u8], u32> {
u32(Endianness::Little).parse_complete(i)
}
assert_eq!(
be_tst32(&[0x12, 0x00, 0x60, 0x00]),
Ok((&b""[..], 302_014_464_u32))
);
assert_eq!(
le_tst32(&[0x12, 0x00, 0x60, 0x00]),
Ok((&b""[..], 6_291_474_u32))
);
fn be_tst64(i: &[u8]) -> IResult<&[u8], u64> {
u64(Endianness::Big).parse_complete(i)
}
fn le_tst64(i: &[u8]) -> IResult<&[u8], u64> {
u64(Endianness::Little).parse_complete(i)
}
assert_eq!(
be_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
Ok((&b""[..], 1_297_142_246_100_992_000_u64))
);
assert_eq!(
le_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
Ok((&b""[..], 36_028_874_334_666_770_u64))
);
fn be_tsti16(i: &[u8]) -> IResult<&[u8], i16> {
i16(Endianness::Big).parse_complete(i)
}
fn le_tsti16(i: &[u8]) -> IResult<&[u8], i16> {
i16(Endianness::Little).parse_complete(i)
}
assert_eq!(be_tsti16(&[0x00, 0x80]), Ok((&b""[..], 128_i16)));
assert_eq!(le_tsti16(&[0x00, 0x80]), Ok((&b""[..], -32_768_i16)));
fn be_tsti32(i: &[u8]) -> IResult<&[u8], i32> {
i32(Endianness::Big).parse_complete(i)
}
fn le_tsti32(i: &[u8]) -> IResult<&[u8], i32> {
i32(Endianness::Little).parse_complete(i)
}
assert_eq!(
be_tsti32(&[0x00, 0x12, 0x60, 0x00]),
Ok((&b""[..], 1_204_224_i32))
);
assert_eq!(
le_tsti32(&[0x00, 0x12, 0x60, 0x00]),
Ok((&b""[..], 6_296_064_i32))
);
fn be_tsti64(i: &[u8]) -> IResult<&[u8], i64> {
i64(Endianness::Big).parse_complete(i)
}
fn le_tsti64(i: &[u8]) -> IResult<&[u8], i64> {
i64(Endianness::Little).parse_complete(i)
}
assert_eq!(
be_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
Ok((&b""[..], 71_881_672_479_506_432_i64))
);
assert_eq!(
le_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
Ok((&b""[..], 36_028_874_334_732_032_i64))
);
}
#[cfg(feature = "std")]
fn parse_f64(i: &str) -> IResult<&str, f64, ()> {
match recognize_float_or_exceptions().parse_complete(i) {
Err(e) => Err(e),
Ok((i, s)) => {
if s.is_empty() {
return Err(Err::Error(()));
}
match s.parse::<f64>() {
Ok(n) => Ok((i, n)),
Err(_) => Err(Err::Error(())),
}
}
}
}
proptest! {
#[test]
#[cfg(feature = "std")]
fn floats(s in "\\PC*") {
println!("testing {s}");
let res1 = parse_f64(&s);
let res2 = double::<_, ()>().parse_complete(s.as_str());
assert_eq!(res1, res2);
}
}
}
#[cfg(test)]
mod streaming_tests {
use super::*;
use crate::error::ErrorKind;
use crate::internal::{Err, Needed};
use proptest::prelude::*;
#[test]
fn i8_tests() {
assert_parse!(be_i8().parse(&[0x00][..]), Ok((&b""[..], 0)));
assert_parse!(be_i8().parse(&[0x7f][..]), Ok((&b""[..], 127)));
assert_parse!(be_i8().parse(&[0xff][..]), Ok((&b""[..], -1)));
assert_parse!(be_i8().parse(&[0x80][..]), Ok((&b""[..], -128)));
assert_parse!(be_i8().parse(&[][..]), Err(Err::Incomplete(Needed::new(1))));
}
#[test]
fn i16_tests() {
assert_parse!(be_i16().parse(&[0x00, 0x00][..]), Ok((&b""[..], 0)));
assert_parse!(
be_i16().parse(&[0x7f, 0xff][..]),
Ok((&b""[..], 32_767_i16))
);
assert_parse!(be_i16().parse(&[0xff, 0xff][..]), Ok((&b""[..], -1)));
assert_parse!(
be_i16().parse(&[0x80, 0x00][..]),
Ok((&b""[..], -32_768_i16))
);
assert_parse!(
be_i16().parse(&[][..]),
Err(Err::Incomplete(Needed::new(2)))
);
assert_parse!(
be_i16().parse(&[0x00][..]),
Err(Err::Incomplete(Needed::new(1)))
);
}
#[test]
fn u24_tests() {
assert_parse!(be_u24().parse(&[0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
assert_parse!(
be_u24().parse(&[0x00, 0xFF, 0xFF][..]),
Ok((&b""[..], 65_535_u32))
);
assert_parse!(
be_u24().parse(&[0x12, 0x34, 0x56][..]),
Ok((&b""[..], 1_193_046_u32))
);
assert_parse!(
be_u24().parse(&[][..]),
Err(Err::Incomplete(Needed::new(3)))
);
assert_parse!(
be_u24().parse(&[0x00][..]),
Err(Err::Incomplete(Needed::new(2)))
);
assert_parse!(
be_u24().parse(&[0x00, 0x00][..]),
Err(Err::Incomplete(Needed::new(1)))
);
}
#[test]
fn i24_tests() {
assert_parse!(
be_i24().parse(&[0xFF, 0xFF, 0xFF][..]),
Ok((&b""[..], -1_i32))
);
assert_parse!(
be_i24().parse(&[0xFF, 0x00, 0x00][..]),
Ok((&b""[..], -65_536_i32))
);
assert_parse!(
be_i24().parse(&[0xED, 0xCB, 0xAA][..]),
Ok((&b""[..], -1_193_046_i32))
);
assert_parse!(
be_i24().parse(&[][..]),
Err(Err::Incomplete(Needed::new(3)))
);
assert_parse!(
be_i24().parse(&[0x00][..]),
Err(Err::Incomplete(Needed::new(2)))
);
assert_parse!(
be_i24().parse(&[0x00, 0x00][..]),
Err(Err::Incomplete(Needed::new(1)))
);
}
#[test]
fn i32_tests() {
assert_parse!(
be_i32().parse(&[0x00, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], 0))
);
assert_parse!(
be_i32().parse(&[0x7f, 0xff, 0xff, 0xff][..]),
Ok((&b""[..], 2_147_483_647_i32))
);
assert_parse!(
be_i32().parse(&[0xff, 0xff, 0xff, 0xff][..]),
Ok((&b""[..], -1))
);
assert_parse!(
be_i32().parse(&[0x80, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], -2_147_483_648_i32))
);
assert_parse!(
be_i32().parse(&[][..]),
Err(Err::Incomplete(Needed::new(4)))
);
assert_parse!(
be_i32().parse(&[0x00][..]),
Err(Err::Incomplete(Needed::new(3)))
);
assert_parse!(
be_i32().parse(&[0x00, 0x00][..]),
Err(Err::Incomplete(Needed::new(2)))
);
assert_parse!(
be_i32().parse(&[0x00, 0x00, 0x00][..]),
Err(Err::Incomplete(Needed::new(1)))
);
}
#[test]
fn i64_tests() {
assert_parse!(
be_i64().parse(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], 0))
);
assert_parse!(
be_i64().parse(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
Ok((&b""[..], 9_223_372_036_854_775_807_i64))
);
assert_parse!(
be_i64().parse(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
Ok((&b""[..], -1))
);
assert_parse!(
be_i64().parse(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], -9_223_372_036_854_775_808_i64))
);
assert_parse!(
be_i64().parse(&[][..]),
Err(Err::Incomplete(Needed::new(8)))
);
assert_parse!(
be_i64().parse(&[0x00][..]),
Err(Err::Incomplete(Needed::new(7)))
);
assert_parse!(
be_i64().parse(&[0x00, 0x00][..]),
Err(Err::Incomplete(Needed::new(6)))
);
assert_parse!(
be_i64().parse(&[0x00, 0x00, 0x00][..]),
Err(Err::Incomplete(Needed::new(5)))
);
assert_parse!(
be_i64().parse(&[0x00, 0x00, 0x00, 0x00][..]),
Err(Err::Incomplete(Needed::new(4)))
);
assert_parse!(
be_i64().parse(&[0x00, 0x00, 0x00, 0x00, 0x00][..]),
Err(Err::Incomplete(Needed::new(3)))
);
assert_parse!(
be_i64().parse(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
Err(Err::Incomplete(Needed::new(2)))
);
assert_parse!(
be_i64().parse(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
Err(Err::Incomplete(Needed::new(1)))
);
}
#[test]
fn i128_tests() {
assert_parse!(
be_i128().parse(
&[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00
][..]
),
Ok((&b""[..], 0))
);
assert_parse!(
be_i128().parse(
&[
0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff
][..]
),
Ok((
&b""[..],
170_141_183_460_469_231_731_687_303_715_884_105_727_i128
))
);
assert_parse!(
be_i128().parse(
&[
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff
][..]
),
Ok((&b""[..], -1))
);
assert_parse!(
be_i128().parse(
&[
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00
][..]
),
Ok((
&b""[..],
-170_141_183_460_469_231_731_687_303_715_884_105_728_i128
))
);
assert_parse!(
be_i128().parse(&[][..]),
Err(Err::Incomplete(Needed::new(16)))
);
assert_parse!(
be_i128().parse(&[0x00][..]),
Err(Err::Incomplete(Needed::new(15)))
);
assert_parse!(
be_i128().parse(&[0x00, 0x00][..]),
Err(Err::Incomplete(Needed::new(14)))
);
assert_parse!(
be_i128().parse(&[0x00, 0x00, 0x00][..]),
Err(Err::Incomplete(Needed::new(13)))
);
assert_parse!(
be_i128().parse(&[0x00, 0x00, 0x00, 0x00][..]),
Err(Err::Incomplete(Needed::new(12)))
);
assert_parse!(
be_i128().parse(&[0x00, 0x00, 0x00, 0x00, 0x00][..]),
Err(Err::Incomplete(Needed::new(11)))
);
assert_parse!(
be_i128().parse(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
Err(Err::Incomplete(Needed::new(10)))
);
assert_parse!(
be_i128().parse(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
Err(Err::Incomplete(Needed::new(9)))
);
assert_parse!(
be_i128().parse(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
Err(Err::Incomplete(Needed::new(8)))
);
assert_parse!(
be_i128().parse(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
Err(Err::Incomplete(Needed::new(7)))
);
assert_parse!(
be_i128().parse(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
Err(Err::Incomplete(Needed::new(6)))
);
assert_parse!(
be_i128().parse(
&[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
][..]
),
Err(Err::Incomplete(Needed::new(5)))
);
assert_parse!(
be_i128().parse(
&[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
][..]
),
Err(Err::Incomplete(Needed::new(4)))
);
assert_parse!(
be_i128().parse(
&[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
][..]
),
Err(Err::Incomplete(Needed::new(3)))
);
assert_parse!(
be_i128().parse(
&[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
][..]
),
Err(Err::Incomplete(Needed::new(2)))
);
assert_parse!(
be_i128().parse(
&[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
][..]
),
Err(Err::Incomplete(Needed::new(1)))
);
}
#[test]
fn le_i8_tests() {
assert_parse!(le_i8().parse(&[0x00][..]), Ok((&b""[..], 0)));
assert_parse!(le_i8().parse(&[0x7f][..]), Ok((&b""[..], 127)));
assert_parse!(le_i8().parse(&[0xff][..]), Ok((&b""[..], -1)));
assert_parse!(le_i8().parse(&[0x80][..]), Ok((&b""[..], -128)));
}
#[test]
fn le_i16_tests() {
assert_parse!(le_i16().parse(&[0x00, 0x00][..]), Ok((&b""[..], 0)));
assert_parse!(
le_i16().parse(&[0xff, 0x7f][..]),
Ok((&b""[..], 32_767_i16))
);
assert_parse!(le_i16().parse(&[0xff, 0xff][..]), Ok((&b""[..], -1)));
assert_parse!(
le_i16().parse(&[0x00, 0x80][..]),
Ok((&b""[..], -32_768_i16))
);
}
#[test]
fn le_u16_test() {
assert_parse!(le_u16().parse(&[0x00, 0x03][..]), Ok((&b""[..], 0x0300)));
assert_parse!(le_u16().parse(&[b'a', b'b'][..]), Ok((&b""[..], 0x6261)));
assert_parse!(
le_u16().parse(&[0x01][..]),
Err(Err::Incomplete(Needed::new(1)))
);
}
#[test]
fn le_u24_tests() {
assert_parse!(le_u24().parse(&[0x00, 0x00, 0x00][..]), Ok((&b""[..], 0)));
assert_parse!(
le_u24().parse(&[0xFF, 0xFF, 0x00][..]),
Ok((&b""[..], 65_535_u32))
);
assert_parse!(
le_u24().parse(&[0x56, 0x34, 0x12][..]),
Ok((&b""[..], 1_193_046_u32))
);
}
#[test]
fn le_i24_tests() {
assert_parse!(
le_i24().parse(&[0xFF, 0xFF, 0xFF][..]),
Ok((&b""[..], -1_i32))
);
assert_parse!(
le_i24().parse(&[0x00, 0x00, 0xFF][..]),
Ok((&b""[..], -65_536_i32))
);
assert_parse!(
le_i24().parse(&[0xAA, 0xCB, 0xED][..]),
Ok((&b""[..], -1_193_046_i32))
);
}
#[test]
fn le_i32_tests() {
assert_parse!(
le_i32().parse(&[0x00, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], 0))
);
assert_parse!(
le_i32().parse(&[0xff, 0xff, 0xff, 0x7f][..]),
Ok((&b""[..], 2_147_483_647_i32))
);
assert_parse!(
le_i32().parse(&[0xff, 0xff, 0xff, 0xff][..]),
Ok((&b""[..], -1))
);
assert_parse!(
le_i32().parse(&[0x00, 0x00, 0x00, 0x80][..]),
Ok((&b""[..], -2_147_483_648_i32))
);
}
#[test]
fn le_u32_test() {
assert_parse!(
le_u32().parse(&[0x00, 0x03, 0x05, 0x07][..]),
Ok((&b""[..], 0x07050300))
);
assert_parse!(
le_u32().parse(&[b'a', b'b', b'c', b'd'][..]),
Ok((&b""[..], 0x64636261))
);
assert_parse!(
le_u32().parse(&[0x01][..]),
Err(Err::Incomplete(Needed::new(3)))
);
}
#[test]
fn le_i64_tests() {
assert_parse!(
le_i64().parse(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], 0))
);
assert_parse!(
le_i64().parse(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f][..]),
Ok((&b""[..], 9_223_372_036_854_775_807_i64))
);
assert_parse!(
le_i64().parse(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]),
Ok((&b""[..], -1))
);
assert_parse!(
le_i64().parse(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80][..]),
Ok((&b""[..], -9_223_372_036_854_775_808_i64))
);
}
#[test]
fn le_i128_tests() {
assert_parse!(
le_i128().parse(
&[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00
][..]
),
Ok((&b""[..], 0))
);
assert_parse!(
le_i128().parse(
&[
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x7f
][..]
),
Ok((
&b""[..],
170_141_183_460_469_231_731_687_303_715_884_105_727_i128
))
);
assert_parse!(
le_i128().parse(
&[
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff
][..]
),
Ok((&b""[..], -1))
);
assert_parse!(
le_i128().parse(
&[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80
][..]
),
Ok((
&b""[..],
-170_141_183_460_469_231_731_687_303_715_884_105_728_i128
))
);
}
#[test]
fn be_f32_tests() {
assert_parse!(
be_f32().parse(&[0x00, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], 0_f32))
);
assert_parse!(
be_f32().parse(&[0x4d, 0x31, 0x1f, 0xd8][..]),
Ok((&b""[..], 185_728_392_f32))
);
}
#[test]
fn be_f64_tests() {
assert_parse!(
be_f64().parse(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], 0_f64))
);
assert_parse!(
be_f64().parse(&[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], 185_728_392_f64))
);
}
#[test]
fn le_f32_tests() {
assert_parse!(
le_f32().parse(&[0x00, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], 0_f32))
);
assert_parse!(
le_f32().parse(&[0xd8, 0x1f, 0x31, 0x4d][..]),
Ok((&b""[..], 185_728_392_f32))
);
}
#[test]
fn le_f64_tests() {
assert_parse!(
le_f64().parse(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]),
Ok((&b""[..], 0_f64))
);
assert_parse!(
le_f64().parse(&[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41][..]),
Ok((&b""[..], 185_728_392_f64))
);
}
#[test]
fn hex_u32_tests() {
}
#[test]
#[cfg(feature = "std")]
fn float_test() {
let mut test_cases = vec![
"+3.14",
"3.14",
"-3.14",
"0",
"0.0",
"1.",
".789",
"-.5",
"1e7",
"-1E-7",
".3e-2",
"1.e4",
"1.2e4",
"12.34",
"-1.234E-12",
"-1.234e-12",
"0.00000000000000000087",
];
for test in test_cases.drain(..) {
let expected32 = str::parse::<f32>(test).unwrap();
let expected64 = str::parse::<f64>(test).unwrap();
println!("now parsing: {test} -> {expected32}");
let larger = format!("{test};");
assert_parse!(recognize_float().parse(&larger[..]), Ok((";", test)));
assert_parse!(
float().parse(larger.as_bytes()),
Ok((&b";"[..], expected32))
);
assert_parse!(float().parse(&larger[..]), Ok((";", expected32)));
assert_parse!(
double().parse(larger.as_bytes()),
Ok((&b";"[..], expected64))
);
assert_parse!(double().parse(&larger[..]), Ok((";", expected64)));
}
let remaining_exponent = "-1.234E-";
assert_parse!(
recognize_float().parse(remaining_exponent),
Err(Err::Incomplete(Needed::new(1)))
);
let (_i, nan) = float::<_, ()>().parse("NaN").unwrap();
assert!(nan.is_nan());
let (_i, inf) = float::<_, ()>().parse("inf").unwrap();
assert!(inf.is_infinite());
let (i, inf) = float::<_, ()>().parse("infinity").unwrap();
assert!(inf.is_infinite());
assert!(i.is_empty());
}
#[test]
fn configurable_endianness() {
use crate::number::Endianness;
fn be_tst16(i: &[u8]) -> IResult<&[u8], u16> {
u16(Endianness::Big).parse(i)
}
fn le_tst16(i: &[u8]) -> IResult<&[u8], u16> {
u16(Endianness::Little).parse(i)
}
assert_eq!(be_tst16(&[0x80, 0x00]), Ok((&b""[..], 32_768_u16)));
assert_eq!(le_tst16(&[0x80, 0x00]), Ok((&b""[..], 128_u16)));
fn be_tst32(i: &[u8]) -> IResult<&[u8], u32> {
u32(Endianness::Big).parse(i)
}
fn le_tst32(i: &[u8]) -> IResult<&[u8], u32> {
u32(Endianness::Little).parse(i)
}
assert_eq!(
be_tst32(&[0x12, 0x00, 0x60, 0x00]),
Ok((&b""[..], 302_014_464_u32))
);
assert_eq!(
le_tst32(&[0x12, 0x00, 0x60, 0x00]),
Ok((&b""[..], 6_291_474_u32))
);
fn be_tst64(i: &[u8]) -> IResult<&[u8], u64> {
u64(Endianness::Big).parse(i)
}
fn le_tst64(i: &[u8]) -> IResult<&[u8], u64> {
u64(Endianness::Little).parse(i)
}
assert_eq!(
be_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
Ok((&b""[..], 1_297_142_246_100_992_000_u64))
);
assert_eq!(
le_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
Ok((&b""[..], 36_028_874_334_666_770_u64))
);
fn be_tsti16(i: &[u8]) -> IResult<&[u8], i16> {
i16(Endianness::Big).parse(i)
}
fn le_tsti16(i: &[u8]) -> IResult<&[u8], i16> {
i16(Endianness::Little).parse(i)
}
assert_eq!(be_tsti16(&[0x00, 0x80]), Ok((&b""[..], 128_i16)));
assert_eq!(le_tsti16(&[0x00, 0x80]), Ok((&b""[..], -32_768_i16)));
fn be_tsti32(i: &[u8]) -> IResult<&[u8], i32> {
i32(Endianness::Big).parse(i)
}
fn le_tsti32(i: &[u8]) -> IResult<&[u8], i32> {
i32(Endianness::Little).parse(i)
}
assert_eq!(
be_tsti32(&[0x00, 0x12, 0x60, 0x00]),
Ok((&b""[..], 1_204_224_i32))
);
assert_eq!(
le_tsti32(&[0x00, 0x12, 0x60, 0x00]),
Ok((&b""[..], 6_296_064_i32))
);
fn be_tsti64(i: &[u8]) -> IResult<&[u8], i64> {
i64(Endianness::Big).parse(i)
}
fn le_tsti64(i: &[u8]) -> IResult<&[u8], i64> {
i64(Endianness::Little).parse(i)
}
assert_eq!(
be_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
Ok((&b""[..], 71_881_672_479_506_432_i64))
);
assert_eq!(
le_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]),
Ok((&b""[..], 36_028_874_334_732_032_i64))
);
}
#[cfg(feature = "std")]
fn parse_f64(i: &str) -> IResult<&str, f64, ()> {
use crate::traits::ParseTo;
match recognize_float_or_exceptions().parse(i) {
Err(e) => Err(e),
Ok((i, s)) => {
if s.is_empty() {
return Err(Err::Error(()));
}
match s.parse_to() {
Some(n) => Ok((i, n)),
None => Err(Err::Error(())),
}
}
}
}
proptest! {
#[test]
#[cfg(feature = "std")]
fn floats(s in "\\PC*") {
println!("testing {s}");
let res1 = parse_f64(&s);
let res2 = double::<_, ()>().parse(s.as_str());
assert_eq!(res1, res2);
}
}
}