1 use super::*; 2 use crate::unpeek; 3 use crate::IResult; 4 5 mod complete { 6 use super::*; 7 use crate::error::InputError; 8 9 macro_rules! assert_parse( 10 ($left: expr, $right: expr) => { 11 let res: $crate::IResult<_, _, InputError<_>> = $left; 12 assert_eq!(res, $right); 13 }; 14 ); 15 16 #[test] i8_tests()17 fn i8_tests() { 18 assert_parse!(i8.parse_peek(&[0x00][..]), Ok((&b""[..], 0))); 19 assert_parse!(i8.parse_peek(&[0x7f][..]), Ok((&b""[..], 127))); 20 assert_parse!(i8.parse_peek(&[0xff][..]), Ok((&b""[..], -1))); 21 assert_parse!(i8.parse_peek(&[0x80][..]), Ok((&b""[..], -128))); 22 } 23 24 #[test] be_i8_tests()25 fn be_i8_tests() { 26 assert_parse!(be_i8.parse_peek(&[0x00][..]), Ok((&b""[..], 0))); 27 assert_parse!(be_i8.parse_peek(&[0x7f][..]), Ok((&b""[..], 127))); 28 assert_parse!(be_i8.parse_peek(&[0xff][..]), Ok((&b""[..], -1))); 29 assert_parse!(be_i8.parse_peek(&[0x80][..]), Ok((&b""[..], -128))); 30 } 31 32 #[test] be_i16_tests()33 fn be_i16_tests() { 34 assert_parse!(be_i16.parse_peek(&[0x00, 0x00][..]), Ok((&b""[..], 0))); 35 assert_parse!( 36 be_i16.parse_peek(&[0x7f, 0xff][..]), 37 Ok((&b""[..], 32_767_i16)) 38 ); 39 assert_parse!(be_i16.parse_peek(&[0xff, 0xff][..]), Ok((&b""[..], -1))); 40 assert_parse!( 41 be_i16.parse_peek(&[0x80, 0x00][..]), 42 Ok((&b""[..], -32_768_i16)) 43 ); 44 } 45 46 #[test] be_u24_tests()47 fn be_u24_tests() { 48 assert_parse!( 49 be_u24.parse_peek(&[0x00, 0x00, 0x00][..]), 50 Ok((&b""[..], 0)) 51 ); 52 assert_parse!( 53 be_u24.parse_peek(&[0x00, 0xFF, 0xFF][..]), 54 Ok((&b""[..], 65_535_u32)) 55 ); 56 assert_parse!( 57 be_u24.parse_peek(&[0x12, 0x34, 0x56][..]), 58 Ok((&b""[..], 1_193_046_u32)) 59 ); 60 } 61 62 #[test] be_i24_tests()63 fn be_i24_tests() { 64 assert_parse!( 65 be_i24.parse_peek(&[0xFF, 0xFF, 0xFF][..]), 66 Ok((&b""[..], -1_i32)) 67 ); 68 assert_parse!( 69 be_i24.parse_peek(&[0xFF, 0x00, 0x00][..]), 70 Ok((&b""[..], -65_536_i32)) 71 ); 72 assert_parse!( 73 be_i24.parse_peek(&[0xED, 0xCB, 0xAA][..]), 74 Ok((&b""[..], -1_193_046_i32)) 75 ); 76 } 77 78 #[test] be_i32_tests()79 fn be_i32_tests() { 80 assert_parse!( 81 be_i32.parse_peek(&[0x00, 0x00, 0x00, 0x00][..]), 82 Ok((&b""[..], 0)) 83 ); 84 assert_parse!( 85 be_i32.parse_peek(&[0x7f, 0xff, 0xff, 0xff][..]), 86 Ok((&b""[..], 2_147_483_647_i32)) 87 ); 88 assert_parse!( 89 be_i32.parse_peek(&[0xff, 0xff, 0xff, 0xff][..]), 90 Ok((&b""[..], -1)) 91 ); 92 assert_parse!( 93 be_i32.parse_peek(&[0x80, 0x00, 0x00, 0x00][..]), 94 Ok((&b""[..], -2_147_483_648_i32)) 95 ); 96 } 97 98 #[test] be_i64_tests()99 fn be_i64_tests() { 100 assert_parse!( 101 be_i64.parse_peek(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), 102 Ok((&b""[..], 0)) 103 ); 104 assert_parse!( 105 be_i64.parse_peek(&[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]), 106 Ok((&b""[..], 9_223_372_036_854_775_807_i64)) 107 ); 108 assert_parse!( 109 be_i64.parse_peek(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]), 110 Ok((&b""[..], -1)) 111 ); 112 assert_parse!( 113 be_i64.parse_peek(&[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), 114 Ok((&b""[..], -9_223_372_036_854_775_808_i64)) 115 ); 116 } 117 118 #[test] be_i128_tests()119 fn be_i128_tests() { 120 assert_parse!( 121 be_i128.parse_peek( 122 &[ 123 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 124 0x00, 0x00, 0x00 125 ][..] 126 ), 127 Ok((&b""[..], 0)) 128 ); 129 assert_parse!( 130 be_i128.parse_peek( 131 &[ 132 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 133 0xff, 0xff, 0xff 134 ][..] 135 ), 136 Ok(( 137 &b""[..], 138 170_141_183_460_469_231_731_687_303_715_884_105_727_i128 139 )) 140 ); 141 assert_parse!( 142 be_i128.parse_peek( 143 &[ 144 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 145 0xff, 0xff, 0xff 146 ][..] 147 ), 148 Ok((&b""[..], -1)) 149 ); 150 assert_parse!( 151 be_i128.parse_peek( 152 &[ 153 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 154 0x00, 0x00, 0x00 155 ][..] 156 ), 157 Ok(( 158 &b""[..], 159 -170_141_183_460_469_231_731_687_303_715_884_105_728_i128 160 )) 161 ); 162 } 163 164 #[test] le_i8_tests()165 fn le_i8_tests() { 166 assert_parse!(le_i8.parse_peek(&[0x00][..]), Ok((&b""[..], 0))); 167 assert_parse!(le_i8.parse_peek(&[0x7f][..]), Ok((&b""[..], 127))); 168 assert_parse!(le_i8.parse_peek(&[0xff][..]), Ok((&b""[..], -1))); 169 assert_parse!(le_i8.parse_peek(&[0x80][..]), Ok((&b""[..], -128))); 170 } 171 172 #[test] le_i16_tests()173 fn le_i16_tests() { 174 assert_parse!(le_i16.parse_peek(&[0x00, 0x00][..]), Ok((&b""[..], 0))); 175 assert_parse!( 176 le_i16.parse_peek(&[0xff, 0x7f][..]), 177 Ok((&b""[..], 32_767_i16)) 178 ); 179 assert_parse!(le_i16.parse_peek(&[0xff, 0xff][..]), Ok((&b""[..], -1))); 180 assert_parse!( 181 le_i16.parse_peek(&[0x00, 0x80][..]), 182 Ok((&b""[..], -32_768_i16)) 183 ); 184 } 185 186 #[test] le_u24_tests()187 fn le_u24_tests() { 188 assert_parse!( 189 le_u24.parse_peek(&[0x00, 0x00, 0x00][..]), 190 Ok((&b""[..], 0)) 191 ); 192 assert_parse!( 193 le_u24.parse_peek(&[0xFF, 0xFF, 0x00][..]), 194 Ok((&b""[..], 65_535_u32)) 195 ); 196 assert_parse!( 197 le_u24.parse_peek(&[0x56, 0x34, 0x12][..]), 198 Ok((&b""[..], 1_193_046_u32)) 199 ); 200 } 201 202 #[test] le_i24_tests()203 fn le_i24_tests() { 204 assert_parse!( 205 le_i24.parse_peek(&[0xFF, 0xFF, 0xFF][..]), 206 Ok((&b""[..], -1_i32)) 207 ); 208 assert_parse!( 209 le_i24.parse_peek(&[0x00, 0x00, 0xFF][..]), 210 Ok((&b""[..], -65_536_i32)) 211 ); 212 assert_parse!( 213 le_i24.parse_peek(&[0xAA, 0xCB, 0xED][..]), 214 Ok((&b""[..], -1_193_046_i32)) 215 ); 216 } 217 218 #[test] le_i32_tests()219 fn le_i32_tests() { 220 assert_parse!( 221 le_i32.parse_peek(&[0x00, 0x00, 0x00, 0x00][..]), 222 Ok((&b""[..], 0)) 223 ); 224 assert_parse!( 225 le_i32.parse_peek(&[0xff, 0xff, 0xff, 0x7f][..]), 226 Ok((&b""[..], 2_147_483_647_i32)) 227 ); 228 assert_parse!( 229 le_i32.parse_peek(&[0xff, 0xff, 0xff, 0xff][..]), 230 Ok((&b""[..], -1)) 231 ); 232 assert_parse!( 233 le_i32.parse_peek(&[0x00, 0x00, 0x00, 0x80][..]), 234 Ok((&b""[..], -2_147_483_648_i32)) 235 ); 236 } 237 238 #[test] le_i64_tests()239 fn le_i64_tests() { 240 assert_parse!( 241 le_i64.parse_peek(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), 242 Ok((&b""[..], 0)) 243 ); 244 assert_parse!( 245 le_i64.parse_peek(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f][..]), 246 Ok((&b""[..], 9_223_372_036_854_775_807_i64)) 247 ); 248 assert_parse!( 249 le_i64.parse_peek(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..]), 250 Ok((&b""[..], -1)) 251 ); 252 assert_parse!( 253 le_i64.parse_peek(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80][..]), 254 Ok((&b""[..], -9_223_372_036_854_775_808_i64)) 255 ); 256 } 257 258 #[test] le_i128_tests()259 fn le_i128_tests() { 260 assert_parse!( 261 le_i128.parse_peek( 262 &[ 263 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 264 0x00, 0x00, 0x00 265 ][..] 266 ), 267 Ok((&b""[..], 0)) 268 ); 269 assert_parse!( 270 le_i128.parse_peek( 271 &[ 272 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 273 0xff, 0xff, 0x7f 274 ][..] 275 ), 276 Ok(( 277 &b""[..], 278 170_141_183_460_469_231_731_687_303_715_884_105_727_i128 279 )) 280 ); 281 assert_parse!( 282 le_i128.parse_peek( 283 &[ 284 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 285 0xff, 0xff, 0xff 286 ][..] 287 ), 288 Ok((&b""[..], -1)) 289 ); 290 assert_parse!( 291 le_i128.parse_peek( 292 &[ 293 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 294 0x00, 0x00, 0x80 295 ][..] 296 ), 297 Ok(( 298 &b""[..], 299 -170_141_183_460_469_231_731_687_303_715_884_105_728_i128 300 )) 301 ); 302 } 303 304 #[test] be_f32_tests()305 fn be_f32_tests() { 306 assert_parse!( 307 be_f32.parse_peek(&[0x00, 0x00, 0x00, 0x00][..]), 308 Ok((&b""[..], 0_f32)) 309 ); 310 assert_parse!( 311 be_f32.parse_peek(&[0x4d, 0x31, 0x1f, 0xd8][..]), 312 Ok((&b""[..], 185_728_380_f32)) 313 ); 314 } 315 316 #[test] be_f64_tests()317 fn be_f64_tests() { 318 assert_parse!( 319 be_f64.parse_peek(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), 320 Ok((&b""[..], 0_f64)) 321 ); 322 assert_parse!( 323 be_f64.parse_peek(&[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00][..]), 324 Ok((&b""[..], 185_728_392_f64)) 325 ); 326 } 327 328 #[test] le_f32_tests()329 fn le_f32_tests() { 330 assert_parse!( 331 le_f32.parse_peek(&[0x00, 0x00, 0x00, 0x00][..]), 332 Ok((&b""[..], 0_f32)) 333 ); 334 assert_parse!( 335 le_f32.parse_peek(&[0xd8, 0x1f, 0x31, 0x4d][..]), 336 Ok((&b""[..], 185_728_380_f32)) 337 ); 338 } 339 340 #[test] le_f64_tests()341 fn le_f64_tests() { 342 assert_parse!( 343 le_f64.parse_peek(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..]), 344 Ok((&b""[..], 0_f64)) 345 ); 346 assert_parse!( 347 le_f64.parse_peek(&[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41][..]), 348 Ok((&b""[..], 185_728_392_f64)) 349 ); 350 } 351 352 #[test] configurable_endianness()353 fn configurable_endianness() { 354 use crate::binary::Endianness; 355 356 fn be_tst16(i: &[u8]) -> IResult<&[u8], u16> { 357 u16(Endianness::Big).parse_peek(i) 358 } 359 fn le_tst16(i: &[u8]) -> IResult<&[u8], u16> { 360 u16(Endianness::Little).parse_peek(i) 361 } 362 assert_eq!(be_tst16(&[0x80, 0x00]), Ok((&b""[..], 32_768_u16))); 363 assert_eq!(le_tst16(&[0x80, 0x00]), Ok((&b""[..], 128_u16))); 364 365 fn be_tst32(i: &[u8]) -> IResult<&[u8], u32> { 366 u32(Endianness::Big).parse_peek(i) 367 } 368 fn le_tst32(i: &[u8]) -> IResult<&[u8], u32> { 369 u32(Endianness::Little).parse_peek(i) 370 } 371 assert_eq!( 372 be_tst32(&[0x12, 0x00, 0x60, 0x00]), 373 Ok((&b""[..], 302_014_464_u32)) 374 ); 375 assert_eq!( 376 le_tst32(&[0x12, 0x00, 0x60, 0x00]), 377 Ok((&b""[..], 6_291_474_u32)) 378 ); 379 380 fn be_tst64(i: &[u8]) -> IResult<&[u8], u64> { 381 u64(Endianness::Big).parse_peek(i) 382 } 383 fn le_tst64(i: &[u8]) -> IResult<&[u8], u64> { 384 u64(Endianness::Little).parse_peek(i) 385 } 386 assert_eq!( 387 be_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), 388 Ok((&b""[..], 1_297_142_246_100_992_000_u64)) 389 ); 390 assert_eq!( 391 le_tst64(&[0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), 392 Ok((&b""[..], 36_028_874_334_666_770_u64)) 393 ); 394 395 fn be_tsti16(i: &[u8]) -> IResult<&[u8], i16> { 396 i16(Endianness::Big).parse_peek(i) 397 } 398 fn le_tsti16(i: &[u8]) -> IResult<&[u8], i16> { 399 i16(Endianness::Little).parse_peek(i) 400 } 401 assert_eq!(be_tsti16(&[0x00, 0x80]), Ok((&b""[..], 128_i16))); 402 assert_eq!(le_tsti16(&[0x00, 0x80]), Ok((&b""[..], -32_768_i16))); 403 404 fn be_tsti32(i: &[u8]) -> IResult<&[u8], i32> { 405 i32(Endianness::Big).parse_peek(i) 406 } 407 fn le_tsti32(i: &[u8]) -> IResult<&[u8], i32> { 408 i32(Endianness::Little).parse_peek(i) 409 } 410 assert_eq!( 411 be_tsti32(&[0x00, 0x12, 0x60, 0x00]), 412 Ok((&b""[..], 1_204_224_i32)) 413 ); 414 assert_eq!( 415 le_tsti32(&[0x00, 0x12, 0x60, 0x00]), 416 Ok((&b""[..], 6_296_064_i32)) 417 ); 418 419 fn be_tsti64(i: &[u8]) -> IResult<&[u8], i64> { 420 i64(Endianness::Big).parse_peek(i) 421 } 422 fn le_tsti64(i: &[u8]) -> IResult<&[u8], i64> { 423 i64(Endianness::Little).parse_peek(i) 424 } 425 assert_eq!( 426 be_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), 427 Ok((&b""[..], 71_881_672_479_506_432_i64)) 428 ); 429 assert_eq!( 430 le_tsti64(&[0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00]), 431 Ok((&b""[..], 36_028_874_334_732_032_i64)) 432 ); 433 } 434 } 435 436 mod partial { 437 use super::*; 438 use crate::error::ErrMode; 439 use crate::error::InputError; 440 use crate::error::Needed; 441 #[cfg(feature = "alloc")] 442 use crate::lib::std::vec::Vec; 443 use crate::Partial; 444 use crate::{ 445 ascii::digit1 as digit, 446 binary::{be_u16, be_u8}, 447 error::ErrorKind, 448 lib::std::str::{self, FromStr}, 449 IResult, 450 }; 451 452 macro_rules! assert_parse( 453 ($left: expr, $right: expr) => { 454 let res: $crate::IResult<_, _, InputError<_>> = $left; 455 assert_eq!(res, $right); 456 }; 457 ); 458 459 #[test] i8_tests()460 fn i8_tests() { 461 assert_parse!( 462 be_i8.parse_peek(Partial::new(&[0x00][..])), 463 Ok((Partial::new(&b""[..]), 0)) 464 ); 465 assert_parse!( 466 be_i8.parse_peek(Partial::new(&[0x7f][..])), 467 Ok((Partial::new(&b""[..]), 127)) 468 ); 469 assert_parse!( 470 be_i8.parse_peek(Partial::new(&[0xff][..])), 471 Ok((Partial::new(&b""[..]), -1)) 472 ); 473 assert_parse!( 474 be_i8.parse_peek(Partial::new(&[0x80][..])), 475 Ok((Partial::new(&b""[..]), -128)) 476 ); 477 assert_parse!( 478 be_i8.parse_peek(Partial::new(&[][..])), 479 Err(ErrMode::Incomplete(Needed::new(1))) 480 ); 481 } 482 483 #[test] i16_tests()484 fn i16_tests() { 485 assert_parse!( 486 be_i16.parse_peek(Partial::new(&[0x00, 0x00][..])), 487 Ok((Partial::new(&b""[..]), 0)) 488 ); 489 assert_parse!( 490 be_i16.parse_peek(Partial::new(&[0x7f, 0xff][..])), 491 Ok((Partial::new(&b""[..]), 32_767_i16)) 492 ); 493 assert_parse!( 494 be_i16.parse_peek(Partial::new(&[0xff, 0xff][..])), 495 Ok((Partial::new(&b""[..]), -1)) 496 ); 497 assert_parse!( 498 be_i16.parse_peek(Partial::new(&[0x80, 0x00][..])), 499 Ok((Partial::new(&b""[..]), -32_768_i16)) 500 ); 501 assert_parse!( 502 be_i16.parse_peek(Partial::new(&[][..])), 503 Err(ErrMode::Incomplete(Needed::new(2))) 504 ); 505 assert_parse!( 506 be_i16.parse_peek(Partial::new(&[0x00][..])), 507 Err(ErrMode::Incomplete(Needed::new(1))) 508 ); 509 } 510 511 #[test] u24_tests()512 fn u24_tests() { 513 assert_parse!( 514 be_u24.parse_peek(Partial::new(&[0x00, 0x00, 0x00][..])), 515 Ok((Partial::new(&b""[..]), 0)) 516 ); 517 assert_parse!( 518 be_u24.parse_peek(Partial::new(&[0x00, 0xFF, 0xFF][..])), 519 Ok((Partial::new(&b""[..]), 65_535_u32)) 520 ); 521 assert_parse!( 522 be_u24.parse_peek(Partial::new(&[0x12, 0x34, 0x56][..])), 523 Ok((Partial::new(&b""[..]), 1_193_046_u32)) 524 ); 525 assert_parse!( 526 be_u24.parse_peek(Partial::new(&[][..])), 527 Err(ErrMode::Incomplete(Needed::new(3))) 528 ); 529 assert_parse!( 530 be_u24.parse_peek(Partial::new(&[0x00][..])), 531 Err(ErrMode::Incomplete(Needed::new(2))) 532 ); 533 assert_parse!( 534 be_u24.parse_peek(Partial::new(&[0x00, 0x00][..])), 535 Err(ErrMode::Incomplete(Needed::new(1))) 536 ); 537 } 538 539 #[test] i24_tests()540 fn i24_tests() { 541 assert_parse!( 542 be_i24.parse_peek(Partial::new(&[0xFF, 0xFF, 0xFF][..])), 543 Ok((Partial::new(&b""[..]), -1_i32)) 544 ); 545 assert_parse!( 546 be_i24.parse_peek(Partial::new(&[0xFF, 0x00, 0x00][..])), 547 Ok((Partial::new(&b""[..]), -65_536_i32)) 548 ); 549 assert_parse!( 550 be_i24.parse_peek(Partial::new(&[0xED, 0xCB, 0xAA][..])), 551 Ok((Partial::new(&b""[..]), -1_193_046_i32)) 552 ); 553 assert_parse!( 554 be_i24.parse_peek(Partial::new(&[][..])), 555 Err(ErrMode::Incomplete(Needed::new(3))) 556 ); 557 assert_parse!( 558 be_i24.parse_peek(Partial::new(&[0x00][..])), 559 Err(ErrMode::Incomplete(Needed::new(2))) 560 ); 561 assert_parse!( 562 be_i24.parse_peek(Partial::new(&[0x00, 0x00][..])), 563 Err(ErrMode::Incomplete(Needed::new(1))) 564 ); 565 } 566 567 #[test] i32_tests()568 fn i32_tests() { 569 assert_parse!( 570 be_i32.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), 571 Ok((Partial::new(&b""[..]), 0)) 572 ); 573 assert_parse!( 574 be_i32.parse_peek(Partial::new(&[0x7f, 0xff, 0xff, 0xff][..])), 575 Ok((Partial::new(&b""[..]), 2_147_483_647_i32)) 576 ); 577 assert_parse!( 578 be_i32.parse_peek(Partial::new(&[0xff, 0xff, 0xff, 0xff][..])), 579 Ok((Partial::new(&b""[..]), -1)) 580 ); 581 assert_parse!( 582 be_i32.parse_peek(Partial::new(&[0x80, 0x00, 0x00, 0x00][..])), 583 Ok((Partial::new(&b""[..]), -2_147_483_648_i32)) 584 ); 585 assert_parse!( 586 be_i32.parse_peek(Partial::new(&[][..])), 587 Err(ErrMode::Incomplete(Needed::new(4))) 588 ); 589 assert_parse!( 590 be_i32.parse_peek(Partial::new(&[0x00][..])), 591 Err(ErrMode::Incomplete(Needed::new(3))) 592 ); 593 assert_parse!( 594 be_i32.parse_peek(Partial::new(&[0x00, 0x00][..])), 595 Err(ErrMode::Incomplete(Needed::new(2))) 596 ); 597 assert_parse!( 598 be_i32.parse_peek(Partial::new(&[0x00, 0x00, 0x00][..])), 599 Err(ErrMode::Incomplete(Needed::new(1))) 600 ); 601 } 602 603 #[test] i64_tests()604 fn i64_tests() { 605 assert_parse!( 606 be_i64.parse_peek(Partial::new( 607 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] 608 )), 609 Ok((Partial::new(&b""[..]), 0)) 610 ); 611 assert_parse!( 612 be_i64.parse_peek(Partial::new( 613 &[0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..] 614 )), 615 Ok((Partial::new(&b""[..]), 9_223_372_036_854_775_807_i64)) 616 ); 617 assert_parse!( 618 be_i64.parse_peek(Partial::new( 619 &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..] 620 )), 621 Ok((Partial::new(&b""[..]), -1)) 622 ); 623 assert_parse!( 624 be_i64.parse_peek(Partial::new( 625 &[0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] 626 )), 627 Ok((Partial::new(&b""[..]), -9_223_372_036_854_775_808_i64)) 628 ); 629 assert_parse!( 630 be_i64.parse_peek(Partial::new(&[][..])), 631 Err(ErrMode::Incomplete(Needed::new(8))) 632 ); 633 assert_parse!( 634 be_i64.parse_peek(Partial::new(&[0x00][..])), 635 Err(ErrMode::Incomplete(Needed::new(7))) 636 ); 637 assert_parse!( 638 be_i64.parse_peek(Partial::new(&[0x00, 0x00][..])), 639 Err(ErrMode::Incomplete(Needed::new(6))) 640 ); 641 assert_parse!( 642 be_i64.parse_peek(Partial::new(&[0x00, 0x00, 0x00][..])), 643 Err(ErrMode::Incomplete(Needed::new(5))) 644 ); 645 assert_parse!( 646 be_i64.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), 647 Err(ErrMode::Incomplete(Needed::new(4))) 648 ); 649 assert_parse!( 650 be_i64.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00][..])), 651 Err(ErrMode::Incomplete(Needed::new(3))) 652 ); 653 assert_parse!( 654 be_i64.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..])), 655 Err(ErrMode::Incomplete(Needed::new(2))) 656 ); 657 assert_parse!( 658 be_i64.parse_peek(Partial::new( 659 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] 660 )), 661 Err(ErrMode::Incomplete(Needed::new(1))) 662 ); 663 } 664 665 #[test] i128_tests()666 fn i128_tests() { 667 assert_parse!( 668 be_i128.parse_peek(Partial::new( 669 &[ 670 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 671 0x00, 0x00, 0x00 672 ][..] 673 )), 674 Ok((Partial::new(&b""[..]), 0)) 675 ); 676 assert_parse!( 677 be_i128.parse_peek(Partial::new( 678 &[ 679 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 680 0xff, 0xff, 0xff 681 ][..] 682 )), 683 Ok(( 684 Partial::new(&b""[..]), 685 170_141_183_460_469_231_731_687_303_715_884_105_727_i128 686 )) 687 ); 688 assert_parse!( 689 be_i128.parse_peek(Partial::new( 690 &[ 691 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 692 0xff, 0xff, 0xff 693 ][..] 694 )), 695 Ok((Partial::new(&b""[..]), -1)) 696 ); 697 assert_parse!( 698 be_i128.parse_peek(Partial::new( 699 &[ 700 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 701 0x00, 0x00, 0x00 702 ][..] 703 )), 704 Ok(( 705 Partial::new(&b""[..]), 706 -170_141_183_460_469_231_731_687_303_715_884_105_728_i128 707 )) 708 ); 709 assert_parse!( 710 be_i128.parse_peek(Partial::new(&[][..])), 711 Err(ErrMode::Incomplete(Needed::new(16))) 712 ); 713 assert_parse!( 714 be_i128.parse_peek(Partial::new(&[0x00][..])), 715 Err(ErrMode::Incomplete(Needed::new(15))) 716 ); 717 assert_parse!( 718 be_i128.parse_peek(Partial::new(&[0x00, 0x00][..])), 719 Err(ErrMode::Incomplete(Needed::new(14))) 720 ); 721 assert_parse!( 722 be_i128.parse_peek(Partial::new(&[0x00, 0x00, 0x00][..])), 723 Err(ErrMode::Incomplete(Needed::new(13))) 724 ); 725 assert_parse!( 726 be_i128.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), 727 Err(ErrMode::Incomplete(Needed::new(12))) 728 ); 729 assert_parse!( 730 be_i128.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00][..])), 731 Err(ErrMode::Incomplete(Needed::new(11))) 732 ); 733 assert_parse!( 734 be_i128.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..])), 735 Err(ErrMode::Incomplete(Needed::new(10))) 736 ); 737 assert_parse!( 738 be_i128.parse_peek(Partial::new( 739 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] 740 )), 741 Err(ErrMode::Incomplete(Needed::new(9))) 742 ); 743 assert_parse!( 744 be_i128.parse_peek(Partial::new( 745 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] 746 )), 747 Err(ErrMode::Incomplete(Needed::new(8))) 748 ); 749 assert_parse!( 750 be_i128.parse_peek(Partial::new( 751 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] 752 )), 753 Err(ErrMode::Incomplete(Needed::new(7))) 754 ); 755 assert_parse!( 756 be_i128.parse_peek(Partial::new( 757 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] 758 )), 759 Err(ErrMode::Incomplete(Needed::new(6))) 760 ); 761 assert_parse!( 762 be_i128.parse_peek(Partial::new( 763 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] 764 )), 765 Err(ErrMode::Incomplete(Needed::new(5))) 766 ); 767 assert_parse!( 768 be_i128.parse_peek(Partial::new( 769 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] 770 )), 771 Err(ErrMode::Incomplete(Needed::new(4))) 772 ); 773 assert_parse!( 774 be_i128.parse_peek(Partial::new( 775 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] 776 )), 777 Err(ErrMode::Incomplete(Needed::new(3))) 778 ); 779 assert_parse!( 780 be_i128.parse_peek(Partial::new( 781 &[ 782 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 783 0x00 784 ][..] 785 )), 786 Err(ErrMode::Incomplete(Needed::new(2))) 787 ); 788 assert_parse!( 789 be_i128.parse_peek(Partial::new( 790 &[ 791 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 792 0x00, 0x00 793 ][..] 794 )), 795 Err(ErrMode::Incomplete(Needed::new(1))) 796 ); 797 } 798 799 #[test] le_u16_tests()800 fn le_u16_tests() { 801 assert_parse!( 802 le_u16.parse_peek(Partial::new(&[0x00, 0x03][..])), 803 Ok((Partial::new(&b""[..]), 0x0300)) 804 ); 805 assert_parse!( 806 le_u16.parse_peek(Partial::new(&[b'a', b'b'][..])), 807 Ok((Partial::new(&b""[..]), 0x6261)) 808 ); 809 assert_parse!( 810 le_u16.parse_peek(Partial::new(&[0x01][..])), 811 Err(ErrMode::Incomplete(Needed::new(1))) 812 ); 813 } 814 815 #[test] le_i8_tests()816 fn le_i8_tests() { 817 assert_parse!( 818 le_i8.parse_peek(Partial::new(&[0x00][..])), 819 Ok((Partial::new(&b""[..]), 0)) 820 ); 821 assert_parse!( 822 le_i8.parse_peek(Partial::new(&[0x7f][..])), 823 Ok((Partial::new(&b""[..]), 127)) 824 ); 825 assert_parse!( 826 le_i8.parse_peek(Partial::new(&[0xff][..])), 827 Ok((Partial::new(&b""[..]), -1)) 828 ); 829 assert_parse!( 830 le_i8.parse_peek(Partial::new(&[0x80][..])), 831 Ok((Partial::new(&b""[..]), -128)) 832 ); 833 } 834 835 #[test] le_i16_tests()836 fn le_i16_tests() { 837 assert_parse!( 838 le_i16.parse_peek(Partial::new(&[0x00, 0x00][..])), 839 Ok((Partial::new(&b""[..]), 0)) 840 ); 841 assert_parse!( 842 le_i16.parse_peek(Partial::new(&[0xff, 0x7f][..])), 843 Ok((Partial::new(&b""[..]), 32_767_i16)) 844 ); 845 assert_parse!( 846 le_i16.parse_peek(Partial::new(&[0xff, 0xff][..])), 847 Ok((Partial::new(&b""[..]), -1)) 848 ); 849 assert_parse!( 850 le_i16.parse_peek(Partial::new(&[0x00, 0x80][..])), 851 Ok((Partial::new(&b""[..]), -32_768_i16)) 852 ); 853 } 854 855 #[test] le_u24_tests()856 fn le_u24_tests() { 857 assert_parse!( 858 le_u24.parse_peek(Partial::new(&[0x00, 0x00, 0x00][..])), 859 Ok((Partial::new(&b""[..]), 0)) 860 ); 861 assert_parse!( 862 le_u24.parse_peek(Partial::new(&[0xFF, 0xFF, 0x00][..])), 863 Ok((Partial::new(&b""[..]), 65_535_u32)) 864 ); 865 assert_parse!( 866 le_u24.parse_peek(Partial::new(&[0x56, 0x34, 0x12][..])), 867 Ok((Partial::new(&b""[..]), 1_193_046_u32)) 868 ); 869 } 870 871 #[test] le_i24_tests()872 fn le_i24_tests() { 873 assert_parse!( 874 le_i24.parse_peek(Partial::new(&[0xFF, 0xFF, 0xFF][..])), 875 Ok((Partial::new(&b""[..]), -1_i32)) 876 ); 877 assert_parse!( 878 le_i24.parse_peek(Partial::new(&[0x00, 0x00, 0xFF][..])), 879 Ok((Partial::new(&b""[..]), -65_536_i32)) 880 ); 881 assert_parse!( 882 le_i24.parse_peek(Partial::new(&[0xAA, 0xCB, 0xED][..])), 883 Ok((Partial::new(&b""[..]), -1_193_046_i32)) 884 ); 885 } 886 887 #[test] le_u32_test()888 fn le_u32_test() { 889 assert_parse!( 890 le_u32.parse_peek(Partial::new(&[0x00, 0x03, 0x05, 0x07][..])), 891 Ok((Partial::new(&b""[..]), 0x07050300)) 892 ); 893 assert_parse!( 894 le_u32.parse_peek(Partial::new(&[b'a', b'b', b'c', b'd'][..])), 895 Ok((Partial::new(&b""[..]), 0x64636261)) 896 ); 897 assert_parse!( 898 le_u32.parse_peek(Partial::new(&[0x01][..])), 899 Err(ErrMode::Incomplete(Needed::new(3))) 900 ); 901 } 902 903 #[test] le_i32_tests()904 fn le_i32_tests() { 905 assert_parse!( 906 le_i32.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), 907 Ok((Partial::new(&b""[..]), 0)) 908 ); 909 assert_parse!( 910 le_i32.parse_peek(Partial::new(&[0xff, 0xff, 0xff, 0x7f][..])), 911 Ok((Partial::new(&b""[..]), 2_147_483_647_i32)) 912 ); 913 assert_parse!( 914 le_i32.parse_peek(Partial::new(&[0xff, 0xff, 0xff, 0xff][..])), 915 Ok((Partial::new(&b""[..]), -1)) 916 ); 917 assert_parse!( 918 le_i32.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x80][..])), 919 Ok((Partial::new(&b""[..]), -2_147_483_648_i32)) 920 ); 921 } 922 923 #[test] le_i64_tests()924 fn le_i64_tests() { 925 assert_parse!( 926 le_i64.parse_peek(Partial::new( 927 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] 928 )), 929 Ok((Partial::new(&b""[..]), 0)) 930 ); 931 assert_parse!( 932 le_i64.parse_peek(Partial::new( 933 &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f][..] 934 )), 935 Ok((Partial::new(&b""[..]), 9_223_372_036_854_775_807_i64)) 936 ); 937 assert_parse!( 938 le_i64.parse_peek(Partial::new( 939 &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..] 940 )), 941 Ok((Partial::new(&b""[..]), -1)) 942 ); 943 assert_parse!( 944 le_i64.parse_peek(Partial::new( 945 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80][..] 946 )), 947 Ok((Partial::new(&b""[..]), -9_223_372_036_854_775_808_i64)) 948 ); 949 } 950 951 #[test] le_i128_tests()952 fn le_i128_tests() { 953 assert_parse!( 954 le_i128.parse_peek(Partial::new( 955 &[ 956 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 957 0x00, 0x00, 0x00 958 ][..] 959 )), 960 Ok((Partial::new(&b""[..]), 0)) 961 ); 962 assert_parse!( 963 le_i128.parse_peek(Partial::new( 964 &[ 965 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 966 0xff, 0xff, 0x7f 967 ][..] 968 )), 969 Ok(( 970 Partial::new(&b""[..]), 971 170_141_183_460_469_231_731_687_303_715_884_105_727_i128 972 )) 973 ); 974 assert_parse!( 975 le_i128.parse_peek(Partial::new( 976 &[ 977 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 978 0xff, 0xff, 0xff 979 ][..] 980 )), 981 Ok((Partial::new(&b""[..]), -1)) 982 ); 983 assert_parse!( 984 le_i128.parse_peek(Partial::new( 985 &[ 986 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 987 0x00, 0x00, 0x80 988 ][..] 989 )), 990 Ok(( 991 Partial::new(&b""[..]), 992 -170_141_183_460_469_231_731_687_303_715_884_105_728_i128 993 )) 994 ); 995 } 996 997 #[test] be_f32_tests()998 fn be_f32_tests() { 999 assert_parse!( 1000 be_f32.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), 1001 Ok((Partial::new(&b""[..]), 0_f32)) 1002 ); 1003 assert_parse!( 1004 be_f32.parse_peek(Partial::new(&[0x4d, 0x31, 0x1f, 0xd8][..])), 1005 Ok((Partial::new(&b""[..]), 185_728_380_f32)) 1006 ); 1007 } 1008 1009 #[test] be_f64_tests()1010 fn be_f64_tests() { 1011 assert_parse!( 1012 be_f64.parse_peek(Partial::new( 1013 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] 1014 )), 1015 Ok((Partial::new(&b""[..]), 0_f64)) 1016 ); 1017 assert_parse!( 1018 be_f64.parse_peek(Partial::new( 1019 &[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00][..] 1020 )), 1021 Ok((Partial::new(&b""[..]), 185_728_392_f64)) 1022 ); 1023 } 1024 1025 #[test] le_f32_tests()1026 fn le_f32_tests() { 1027 assert_parse!( 1028 le_f32.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), 1029 Ok((Partial::new(&b""[..]), 0_f32)) 1030 ); 1031 assert_parse!( 1032 le_f32.parse_peek(Partial::new(&[0xd8, 0x1f, 0x31, 0x4d][..])), 1033 Ok((Partial::new(&b""[..]), 185_728_380_f32)) 1034 ); 1035 } 1036 1037 #[test] le_f64_tests()1038 fn le_f64_tests() { 1039 assert_parse!( 1040 le_f64.parse_peek(Partial::new( 1041 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] 1042 )), 1043 Ok((Partial::new(&b""[..]), 0_f64)) 1044 ); 1045 assert_parse!( 1046 le_f64.parse_peek(Partial::new( 1047 &[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41][..] 1048 )), 1049 Ok((Partial::new(&b""[..]), 185_728_392_f64)) 1050 ); 1051 } 1052 1053 #[test] configurable_endianness()1054 fn configurable_endianness() { 1055 use crate::binary::Endianness; 1056 1057 fn be_tst16(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u16> { 1058 u16(Endianness::Big).parse_peek(i) 1059 } 1060 fn le_tst16(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u16> { 1061 u16(Endianness::Little).parse_peek(i) 1062 } 1063 assert_eq!( 1064 be_tst16(Partial::new(&[0x80, 0x00])), 1065 Ok((Partial::new(&b""[..]), 32_768_u16)) 1066 ); 1067 assert_eq!( 1068 le_tst16(Partial::new(&[0x80, 0x00])), 1069 Ok((Partial::new(&b""[..]), 128_u16)) 1070 ); 1071 1072 fn be_tst32(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> { 1073 u32(Endianness::Big).parse_peek(i) 1074 } 1075 fn le_tst32(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> { 1076 u32(Endianness::Little).parse_peek(i) 1077 } 1078 assert_eq!( 1079 be_tst32(Partial::new(&[0x12, 0x00, 0x60, 0x00])), 1080 Ok((Partial::new(&b""[..]), 302_014_464_u32)) 1081 ); 1082 assert_eq!( 1083 le_tst32(Partial::new(&[0x12, 0x00, 0x60, 0x00])), 1084 Ok((Partial::new(&b""[..]), 6_291_474_u32)) 1085 ); 1086 1087 fn be_tst64(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u64> { 1088 u64(Endianness::Big).parse_peek(i) 1089 } 1090 fn le_tst64(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u64> { 1091 u64(Endianness::Little).parse_peek(i) 1092 } 1093 assert_eq!( 1094 be_tst64(Partial::new(&[ 1095 0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00 1096 ])), 1097 Ok((Partial::new(&b""[..]), 1_297_142_246_100_992_000_u64)) 1098 ); 1099 assert_eq!( 1100 le_tst64(Partial::new(&[ 1101 0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00 1102 ])), 1103 Ok((Partial::new(&b""[..]), 36_028_874_334_666_770_u64)) 1104 ); 1105 1106 fn be_tsti16(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i16> { 1107 i16(Endianness::Big).parse_peek(i) 1108 } 1109 fn le_tsti16(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i16> { 1110 i16(Endianness::Little).parse_peek(i) 1111 } 1112 assert_eq!( 1113 be_tsti16(Partial::new(&[0x00, 0x80])), 1114 Ok((Partial::new(&b""[..]), 128_i16)) 1115 ); 1116 assert_eq!( 1117 le_tsti16(Partial::new(&[0x00, 0x80])), 1118 Ok((Partial::new(&b""[..]), -32_768_i16)) 1119 ); 1120 1121 fn be_tsti32(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i32> { 1122 i32(Endianness::Big).parse_peek(i) 1123 } 1124 fn le_tsti32(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i32> { 1125 i32(Endianness::Little).parse_peek(i) 1126 } 1127 assert_eq!( 1128 be_tsti32(Partial::new(&[0x00, 0x12, 0x60, 0x00])), 1129 Ok((Partial::new(&b""[..]), 1_204_224_i32)) 1130 ); 1131 assert_eq!( 1132 le_tsti32(Partial::new(&[0x00, 0x12, 0x60, 0x00])), 1133 Ok((Partial::new(&b""[..]), 6_296_064_i32)) 1134 ); 1135 1136 fn be_tsti64(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i64> { 1137 i64(Endianness::Big).parse_peek(i) 1138 } 1139 fn le_tsti64(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i64> { 1140 i64(Endianness::Little).parse_peek(i) 1141 } 1142 assert_eq!( 1143 be_tsti64(Partial::new(&[ 1144 0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00 1145 ])), 1146 Ok((Partial::new(&b""[..]), 71_881_672_479_506_432_i64)) 1147 ); 1148 assert_eq!( 1149 le_tsti64(Partial::new(&[ 1150 0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00 1151 ])), 1152 Ok((Partial::new(&b""[..]), 36_028_874_334_732_032_i64)) 1153 ); 1154 } 1155 1156 #[test] 1157 #[cfg(feature = "alloc")] length_repeat_test()1158 fn length_repeat_test() { 1159 fn number(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> { 1160 digit 1161 .try_map(str::from_utf8) 1162 .try_map(FromStr::from_str) 1163 .parse_peek(i) 1164 } 1165 1166 fn cnt(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { 1167 length_repeat(unpeek(number), "abc").parse_peek(i) 1168 } 1169 1170 assert_eq!( 1171 cnt(Partial::new(&b"2abcabcabcdef"[..])), 1172 Ok((Partial::new(&b"abcdef"[..]), vec![&b"abc"[..], &b"abc"[..]])) 1173 ); 1174 assert_eq!( 1175 cnt(Partial::new(&b"2ab"[..])), 1176 Err(ErrMode::Incomplete(Needed::new(1))) 1177 ); 1178 assert_eq!( 1179 cnt(Partial::new(&b"3abcab"[..])), 1180 Err(ErrMode::Incomplete(Needed::new(1))) 1181 ); 1182 assert_eq!( 1183 cnt(Partial::new(&b"xxx"[..])), 1184 Err(ErrMode::Backtrack(error_position!( 1185 &Partial::new(&b"xxx"[..]), 1186 ErrorKind::Slice 1187 ))) 1188 ); 1189 assert_eq!( 1190 cnt(Partial::new(&b"2abcxxx"[..])), 1191 Err(ErrMode::Backtrack(error_position!( 1192 &Partial::new(&b"xxx"[..]), 1193 ErrorKind::Tag 1194 ))) 1195 ); 1196 } 1197 1198 #[test] partial_length_bytes()1199 fn partial_length_bytes() { 1200 use crate::binary::le_u8; 1201 1202 fn x(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { 1203 length_take(le_u8).parse_peek(i) 1204 } 1205 assert_eq!( 1206 x(Partial::new(b"\x02..>>")), 1207 Ok((Partial::new(&b">>"[..]), &b".."[..])) 1208 ); 1209 assert_eq!( 1210 x(Partial::new(b"\x02..")), 1211 Ok((Partial::new(&[][..]), &b".."[..])) 1212 ); 1213 assert_eq!( 1214 x(Partial::new(b"\x02.")), 1215 Err(ErrMode::Incomplete(Needed::new(1))) 1216 ); 1217 assert_eq!( 1218 x(Partial::new(b"\x02")), 1219 Err(ErrMode::Incomplete(Needed::new(2))) 1220 ); 1221 1222 fn y(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { 1223 let (i, _) = "magic".parse_peek(i)?; 1224 length_take(le_u8).parse_peek(i) 1225 } 1226 assert_eq!( 1227 y(Partial::new(b"magic\x02..>>")), 1228 Ok((Partial::new(&b">>"[..]), &b".."[..])) 1229 ); 1230 assert_eq!( 1231 y(Partial::new(b"magic\x02..")), 1232 Ok((Partial::new(&[][..]), &b".."[..])) 1233 ); 1234 assert_eq!( 1235 y(Partial::new(b"magic\x02.")), 1236 Err(ErrMode::Incomplete(Needed::new(1))) 1237 ); 1238 assert_eq!( 1239 y(Partial::new(b"magic\x02")), 1240 Err(ErrMode::Incomplete(Needed::new(2))) 1241 ); 1242 } 1243 1244 #[test] length_take_test()1245 fn length_take_test() { 1246 fn number(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> { 1247 digit 1248 .try_map(str::from_utf8) 1249 .try_map(FromStr::from_str) 1250 .parse_peek(i) 1251 } 1252 1253 fn take(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { 1254 length_take(unpeek(number)).parse_peek(i) 1255 } 1256 1257 assert_eq!( 1258 take(Partial::new(&b"6abcabcabcdef"[..])), 1259 Ok((Partial::new(&b"abcdef"[..]), &b"abcabc"[..])) 1260 ); 1261 assert_eq!( 1262 take(Partial::new(&b"3ab"[..])), 1263 Err(ErrMode::Incomplete(Needed::new(1))) 1264 ); 1265 assert_eq!( 1266 take(Partial::new(&b"xxx"[..])), 1267 Err(ErrMode::Backtrack(error_position!( 1268 &Partial::new(&b"xxx"[..]), 1269 ErrorKind::Slice 1270 ))) 1271 ); 1272 assert_eq!( 1273 take(Partial::new(&b"2abcxxx"[..])), 1274 Ok((Partial::new(&b"cxxx"[..]), &b"ab"[..])) 1275 ); 1276 } 1277 1278 #[test] length_and_then_test()1279 fn length_and_then_test() { 1280 use crate::stream::StreamIsPartial; 1281 1282 fn length_and_then_1(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u16> { 1283 length_and_then(be_u8, be_u16).parse_peek(i) 1284 } 1285 fn length_and_then_2(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, (u8, u8)> { 1286 length_and_then(be_u8, (be_u8, be_u8)).parse_peek(i) 1287 } 1288 1289 let mut empty_complete = Partial::new(&b""[..]); 1290 let _ = empty_complete.complete(); 1291 1292 let i1 = [0, 5, 6]; 1293 assert_eq!( 1294 length_and_then_1(Partial::new(&i1)), 1295 Err(ErrMode::Backtrack(error_position!( 1296 &empty_complete, 1297 ErrorKind::Slice 1298 ))) 1299 ); 1300 assert_eq!( 1301 length_and_then_2(Partial::new(&i1)), 1302 Err(ErrMode::Backtrack(error_position!( 1303 &empty_complete, 1304 ErrorKind::Token 1305 ))) 1306 ); 1307 1308 let i2 = [1, 5, 6, 3]; 1309 { 1310 let mut middle_complete = Partial::new(&i2[1..2]); 1311 let _ = middle_complete.complete(); 1312 assert_eq!( 1313 length_and_then_1(Partial::new(&i2)), 1314 Err(ErrMode::Backtrack(error_position!( 1315 &middle_complete, 1316 ErrorKind::Slice 1317 ))) 1318 ); 1319 assert_eq!( 1320 length_and_then_2(Partial::new(&i2)), 1321 Err(ErrMode::Backtrack(error_position!( 1322 &empty_complete, 1323 ErrorKind::Token 1324 ))) 1325 ); 1326 } 1327 1328 let i3 = [2, 5, 6, 3, 4, 5, 7]; 1329 assert_eq!( 1330 length_and_then_1(Partial::new(&i3)), 1331 Ok((Partial::new(&i3[3..]), 1286)) 1332 ); 1333 assert_eq!( 1334 length_and_then_2(Partial::new(&i3)), 1335 Ok((Partial::new(&i3[3..]), (5, 6))) 1336 ); 1337 1338 let i4 = [3, 5, 6, 3, 4, 5]; 1339 assert_eq!( 1340 length_and_then_1(Partial::new(&i4)), 1341 Ok((Partial::new(&i4[4..]), 1286)) 1342 ); 1343 assert_eq!( 1344 length_and_then_2(Partial::new(&i4)), 1345 Ok((Partial::new(&i4[4..]), (5, 6))) 1346 ); 1347 } 1348 } 1349