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_i8_tests()800 fn le_i8_tests() { 801 assert_parse!( 802 le_i8.parse_peek(Partial::new(&[0x00][..])), 803 Ok((Partial::new(&b""[..]), 0)) 804 ); 805 assert_parse!( 806 le_i8.parse_peek(Partial::new(&[0x7f][..])), 807 Ok((Partial::new(&b""[..]), 127)) 808 ); 809 assert_parse!( 810 le_i8.parse_peek(Partial::new(&[0xff][..])), 811 Ok((Partial::new(&b""[..]), -1)) 812 ); 813 assert_parse!( 814 le_i8.parse_peek(Partial::new(&[0x80][..])), 815 Ok((Partial::new(&b""[..]), -128)) 816 ); 817 } 818 819 #[test] le_i16_tests()820 fn le_i16_tests() { 821 assert_parse!( 822 le_i16.parse_peek(Partial::new(&[0x00, 0x00][..])), 823 Ok((Partial::new(&b""[..]), 0)) 824 ); 825 assert_parse!( 826 le_i16.parse_peek(Partial::new(&[0xff, 0x7f][..])), 827 Ok((Partial::new(&b""[..]), 32_767_i16)) 828 ); 829 assert_parse!( 830 le_i16.parse_peek(Partial::new(&[0xff, 0xff][..])), 831 Ok((Partial::new(&b""[..]), -1)) 832 ); 833 assert_parse!( 834 le_i16.parse_peek(Partial::new(&[0x00, 0x80][..])), 835 Ok((Partial::new(&b""[..]), -32_768_i16)) 836 ); 837 } 838 839 #[test] le_u24_tests()840 fn le_u24_tests() { 841 assert_parse!( 842 le_u24.parse_peek(Partial::new(&[0x00, 0x00, 0x00][..])), 843 Ok((Partial::new(&b""[..]), 0)) 844 ); 845 assert_parse!( 846 le_u24.parse_peek(Partial::new(&[0xFF, 0xFF, 0x00][..])), 847 Ok((Partial::new(&b""[..]), 65_535_u32)) 848 ); 849 assert_parse!( 850 le_u24.parse_peek(Partial::new(&[0x56, 0x34, 0x12][..])), 851 Ok((Partial::new(&b""[..]), 1_193_046_u32)) 852 ); 853 } 854 855 #[test] le_i24_tests()856 fn le_i24_tests() { 857 assert_parse!( 858 le_i24.parse_peek(Partial::new(&[0xFF, 0xFF, 0xFF][..])), 859 Ok((Partial::new(&b""[..]), -1_i32)) 860 ); 861 assert_parse!( 862 le_i24.parse_peek(Partial::new(&[0x00, 0x00, 0xFF][..])), 863 Ok((Partial::new(&b""[..]), -65_536_i32)) 864 ); 865 assert_parse!( 866 le_i24.parse_peek(Partial::new(&[0xAA, 0xCB, 0xED][..])), 867 Ok((Partial::new(&b""[..]), -1_193_046_i32)) 868 ); 869 } 870 871 #[test] le_i32_tests()872 fn le_i32_tests() { 873 assert_parse!( 874 le_i32.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), 875 Ok((Partial::new(&b""[..]), 0)) 876 ); 877 assert_parse!( 878 le_i32.parse_peek(Partial::new(&[0xff, 0xff, 0xff, 0x7f][..])), 879 Ok((Partial::new(&b""[..]), 2_147_483_647_i32)) 880 ); 881 assert_parse!( 882 le_i32.parse_peek(Partial::new(&[0xff, 0xff, 0xff, 0xff][..])), 883 Ok((Partial::new(&b""[..]), -1)) 884 ); 885 assert_parse!( 886 le_i32.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x80][..])), 887 Ok((Partial::new(&b""[..]), -2_147_483_648_i32)) 888 ); 889 } 890 891 #[test] le_i64_tests()892 fn le_i64_tests() { 893 assert_parse!( 894 le_i64.parse_peek(Partial::new( 895 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] 896 )), 897 Ok((Partial::new(&b""[..]), 0)) 898 ); 899 assert_parse!( 900 le_i64.parse_peek(Partial::new( 901 &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f][..] 902 )), 903 Ok((Partial::new(&b""[..]), 9_223_372_036_854_775_807_i64)) 904 ); 905 assert_parse!( 906 le_i64.parse_peek(Partial::new( 907 &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff][..] 908 )), 909 Ok((Partial::new(&b""[..]), -1)) 910 ); 911 assert_parse!( 912 le_i64.parse_peek(Partial::new( 913 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80][..] 914 )), 915 Ok((Partial::new(&b""[..]), -9_223_372_036_854_775_808_i64)) 916 ); 917 } 918 919 #[test] le_i128_tests()920 fn le_i128_tests() { 921 assert_parse!( 922 le_i128.parse_peek(Partial::new( 923 &[ 924 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 925 0x00, 0x00, 0x00 926 ][..] 927 )), 928 Ok((Partial::new(&b""[..]), 0)) 929 ); 930 assert_parse!( 931 le_i128.parse_peek(Partial::new( 932 &[ 933 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 934 0xff, 0xff, 0x7f 935 ][..] 936 )), 937 Ok(( 938 Partial::new(&b""[..]), 939 170_141_183_460_469_231_731_687_303_715_884_105_727_i128 940 )) 941 ); 942 assert_parse!( 943 le_i128.parse_peek(Partial::new( 944 &[ 945 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 946 0xff, 0xff, 0xff 947 ][..] 948 )), 949 Ok((Partial::new(&b""[..]), -1)) 950 ); 951 assert_parse!( 952 le_i128.parse_peek(Partial::new( 953 &[ 954 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 955 0x00, 0x00, 0x80 956 ][..] 957 )), 958 Ok(( 959 Partial::new(&b""[..]), 960 -170_141_183_460_469_231_731_687_303_715_884_105_728_i128 961 )) 962 ); 963 } 964 965 #[test] be_f32_tests()966 fn be_f32_tests() { 967 assert_parse!( 968 be_f32.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), 969 Ok((Partial::new(&b""[..]), 0_f32)) 970 ); 971 assert_parse!( 972 be_f32.parse_peek(Partial::new(&[0x4d, 0x31, 0x1f, 0xd8][..])), 973 Ok((Partial::new(&b""[..]), 185_728_380_f32)) 974 ); 975 } 976 977 #[test] be_f64_tests()978 fn be_f64_tests() { 979 assert_parse!( 980 be_f64.parse_peek(Partial::new( 981 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] 982 )), 983 Ok((Partial::new(&b""[..]), 0_f64)) 984 ); 985 assert_parse!( 986 be_f64.parse_peek(Partial::new( 987 &[0x41, 0xa6, 0x23, 0xfb, 0x10, 0x00, 0x00, 0x00][..] 988 )), 989 Ok((Partial::new(&b""[..]), 185_728_392_f64)) 990 ); 991 } 992 993 #[test] le_f32_tests()994 fn le_f32_tests() { 995 assert_parse!( 996 le_f32.parse_peek(Partial::new(&[0x00, 0x00, 0x00, 0x00][..])), 997 Ok((Partial::new(&b""[..]), 0_f32)) 998 ); 999 assert_parse!( 1000 le_f32.parse_peek(Partial::new(&[0xd8, 0x1f, 0x31, 0x4d][..])), 1001 Ok((Partial::new(&b""[..]), 185_728_380_f32)) 1002 ); 1003 } 1004 1005 #[test] le_f64_tests()1006 fn le_f64_tests() { 1007 assert_parse!( 1008 le_f64.parse_peek(Partial::new( 1009 &[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00][..] 1010 )), 1011 Ok((Partial::new(&b""[..]), 0_f64)) 1012 ); 1013 assert_parse!( 1014 le_f64.parse_peek(Partial::new( 1015 &[0x00, 0x00, 0x00, 0x10, 0xfb, 0x23, 0xa6, 0x41][..] 1016 )), 1017 Ok((Partial::new(&b""[..]), 185_728_392_f64)) 1018 ); 1019 } 1020 1021 #[test] configurable_endianness()1022 fn configurable_endianness() { 1023 use crate::binary::Endianness; 1024 1025 fn be_tst16(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u16> { 1026 u16(Endianness::Big).parse_peek(i) 1027 } 1028 fn le_tst16(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u16> { 1029 u16(Endianness::Little).parse_peek(i) 1030 } 1031 assert_eq!( 1032 be_tst16(Partial::new(&[0x80, 0x00])), 1033 Ok((Partial::new(&b""[..]), 32_768_u16)) 1034 ); 1035 assert_eq!( 1036 le_tst16(Partial::new(&[0x80, 0x00])), 1037 Ok((Partial::new(&b""[..]), 128_u16)) 1038 ); 1039 1040 fn be_tst32(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> { 1041 u32(Endianness::Big).parse_peek(i) 1042 } 1043 fn le_tst32(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> { 1044 u32(Endianness::Little).parse_peek(i) 1045 } 1046 assert_eq!( 1047 be_tst32(Partial::new(&[0x12, 0x00, 0x60, 0x00])), 1048 Ok((Partial::new(&b""[..]), 302_014_464_u32)) 1049 ); 1050 assert_eq!( 1051 le_tst32(Partial::new(&[0x12, 0x00, 0x60, 0x00])), 1052 Ok((Partial::new(&b""[..]), 6_291_474_u32)) 1053 ); 1054 1055 fn be_tst64(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u64> { 1056 u64(Endianness::Big).parse_peek(i) 1057 } 1058 fn le_tst64(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u64> { 1059 u64(Endianness::Little).parse_peek(i) 1060 } 1061 assert_eq!( 1062 be_tst64(Partial::new(&[ 1063 0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00 1064 ])), 1065 Ok((Partial::new(&b""[..]), 1_297_142_246_100_992_000_u64)) 1066 ); 1067 assert_eq!( 1068 le_tst64(Partial::new(&[ 1069 0x12, 0x00, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00 1070 ])), 1071 Ok((Partial::new(&b""[..]), 36_028_874_334_666_770_u64)) 1072 ); 1073 1074 fn be_tsti16(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i16> { 1075 i16(Endianness::Big).parse_peek(i) 1076 } 1077 fn le_tsti16(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i16> { 1078 i16(Endianness::Little).parse_peek(i) 1079 } 1080 assert_eq!( 1081 be_tsti16(Partial::new(&[0x00, 0x80])), 1082 Ok((Partial::new(&b""[..]), 128_i16)) 1083 ); 1084 assert_eq!( 1085 le_tsti16(Partial::new(&[0x00, 0x80])), 1086 Ok((Partial::new(&b""[..]), -32_768_i16)) 1087 ); 1088 1089 fn be_tsti32(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i32> { 1090 i32(Endianness::Big).parse_peek(i) 1091 } 1092 fn le_tsti32(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i32> { 1093 i32(Endianness::Little).parse_peek(i) 1094 } 1095 assert_eq!( 1096 be_tsti32(Partial::new(&[0x00, 0x12, 0x60, 0x00])), 1097 Ok((Partial::new(&b""[..]), 1_204_224_i32)) 1098 ); 1099 assert_eq!( 1100 le_tsti32(Partial::new(&[0x00, 0x12, 0x60, 0x00])), 1101 Ok((Partial::new(&b""[..]), 6_296_064_i32)) 1102 ); 1103 1104 fn be_tsti64(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i64> { 1105 i64(Endianness::Big).parse_peek(i) 1106 } 1107 fn le_tsti64(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, i64> { 1108 i64(Endianness::Little).parse_peek(i) 1109 } 1110 assert_eq!( 1111 be_tsti64(Partial::new(&[ 1112 0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00 1113 ])), 1114 Ok((Partial::new(&b""[..]), 71_881_672_479_506_432_i64)) 1115 ); 1116 assert_eq!( 1117 le_tsti64(Partial::new(&[ 1118 0x00, 0xFF, 0x60, 0x00, 0x12, 0x00, 0x80, 0x00 1119 ])), 1120 Ok((Partial::new(&b""[..]), 36_028_874_334_732_032_i64)) 1121 ); 1122 } 1123 1124 #[test] 1125 #[cfg(feature = "alloc")] length_repeat_test()1126 fn length_repeat_test() { 1127 fn number(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> { 1128 digit 1129 .try_map(str::from_utf8) 1130 .try_map(FromStr::from_str) 1131 .parse_peek(i) 1132 } 1133 1134 fn cnt(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, Vec<&[u8]>> { 1135 length_repeat(unpeek(number), "abc").parse_peek(i) 1136 } 1137 1138 assert_eq!( 1139 cnt(Partial::new(&b"2abcabcabcdef"[..])), 1140 Ok((Partial::new(&b"abcdef"[..]), vec![&b"abc"[..], &b"abc"[..]])) 1141 ); 1142 assert_eq!( 1143 cnt(Partial::new(&b"2ab"[..])), 1144 Err(ErrMode::Incomplete(Needed::new(1))) 1145 ); 1146 assert_eq!( 1147 cnt(Partial::new(&b"3abcab"[..])), 1148 Err(ErrMode::Incomplete(Needed::new(1))) 1149 ); 1150 assert_eq!( 1151 cnt(Partial::new(&b"xxx"[..])), 1152 Err(ErrMode::Backtrack(error_position!( 1153 &Partial::new(&b"xxx"[..]), 1154 ErrorKind::Slice 1155 ))) 1156 ); 1157 assert_eq!( 1158 cnt(Partial::new(&b"2abcxxx"[..])), 1159 Err(ErrMode::Backtrack(error_position!( 1160 &Partial::new(&b"xxx"[..]), 1161 ErrorKind::Tag 1162 ))) 1163 ); 1164 } 1165 1166 #[test] partial_length_bytes()1167 fn partial_length_bytes() { 1168 use crate::binary::le_u8; 1169 1170 fn x(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { 1171 length_take(le_u8).parse_peek(i) 1172 } 1173 assert_eq!( 1174 x(Partial::new(b"\x02..>>")), 1175 Ok((Partial::new(&b">>"[..]), &b".."[..])) 1176 ); 1177 assert_eq!( 1178 x(Partial::new(b"\x02..")), 1179 Ok((Partial::new(&[][..]), &b".."[..])) 1180 ); 1181 assert_eq!( 1182 x(Partial::new(b"\x02.")), 1183 Err(ErrMode::Incomplete(Needed::new(1))) 1184 ); 1185 assert_eq!( 1186 x(Partial::new(b"\x02")), 1187 Err(ErrMode::Incomplete(Needed::new(2))) 1188 ); 1189 1190 fn y(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { 1191 let (i, _) = "magic".parse_peek(i)?; 1192 length_take(le_u8).parse_peek(i) 1193 } 1194 assert_eq!( 1195 y(Partial::new(b"magic\x02..>>")), 1196 Ok((Partial::new(&b">>"[..]), &b".."[..])) 1197 ); 1198 assert_eq!( 1199 y(Partial::new(b"magic\x02..")), 1200 Ok((Partial::new(&[][..]), &b".."[..])) 1201 ); 1202 assert_eq!( 1203 y(Partial::new(b"magic\x02.")), 1204 Err(ErrMode::Incomplete(Needed::new(1))) 1205 ); 1206 assert_eq!( 1207 y(Partial::new(b"magic\x02")), 1208 Err(ErrMode::Incomplete(Needed::new(2))) 1209 ); 1210 } 1211 1212 #[test] length_take_test()1213 fn length_take_test() { 1214 fn number(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u32> { 1215 digit 1216 .try_map(str::from_utf8) 1217 .try_map(FromStr::from_str) 1218 .parse_peek(i) 1219 } 1220 1221 fn take(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, &[u8]> { 1222 length_take(unpeek(number)).parse_peek(i) 1223 } 1224 1225 assert_eq!( 1226 take(Partial::new(&b"6abcabcabcdef"[..])), 1227 Ok((Partial::new(&b"abcdef"[..]), &b"abcabc"[..])) 1228 ); 1229 assert_eq!( 1230 take(Partial::new(&b"3ab"[..])), 1231 Err(ErrMode::Incomplete(Needed::new(1))) 1232 ); 1233 assert_eq!( 1234 take(Partial::new(&b"xxx"[..])), 1235 Err(ErrMode::Backtrack(error_position!( 1236 &Partial::new(&b"xxx"[..]), 1237 ErrorKind::Slice 1238 ))) 1239 ); 1240 assert_eq!( 1241 take(Partial::new(&b"2abcxxx"[..])), 1242 Ok((Partial::new(&b"cxxx"[..]), &b"ab"[..])) 1243 ); 1244 } 1245 1246 #[test] length_and_then_test()1247 fn length_and_then_test() { 1248 use crate::stream::StreamIsPartial; 1249 1250 fn length_and_then_1(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u16> { 1251 length_and_then(be_u8, be_u16).parse_peek(i) 1252 } 1253 fn length_and_then_2(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, (u8, u8)> { 1254 length_and_then(be_u8, (be_u8, be_u8)).parse_peek(i) 1255 } 1256 1257 let mut empty_complete = Partial::new(&b""[..]); 1258 let _ = empty_complete.complete(); 1259 1260 let i1 = [0, 5, 6]; 1261 assert_eq!( 1262 length_and_then_1(Partial::new(&i1)), 1263 Err(ErrMode::Backtrack(error_position!( 1264 &empty_complete, 1265 ErrorKind::Slice 1266 ))) 1267 ); 1268 assert_eq!( 1269 length_and_then_2(Partial::new(&i1)), 1270 Err(ErrMode::Backtrack(error_position!( 1271 &empty_complete, 1272 ErrorKind::Token 1273 ))) 1274 ); 1275 1276 let i2 = [1, 5, 6, 3]; 1277 { 1278 let mut middle_complete = Partial::new(&i2[1..2]); 1279 let _ = middle_complete.complete(); 1280 assert_eq!( 1281 length_and_then_1(Partial::new(&i2)), 1282 Err(ErrMode::Backtrack(error_position!( 1283 &middle_complete, 1284 ErrorKind::Slice 1285 ))) 1286 ); 1287 assert_eq!( 1288 length_and_then_2(Partial::new(&i2)), 1289 Err(ErrMode::Backtrack(error_position!( 1290 &empty_complete, 1291 ErrorKind::Token 1292 ))) 1293 ); 1294 } 1295 1296 let i3 = [2, 5, 6, 3, 4, 5, 7]; 1297 assert_eq!( 1298 length_and_then_1(Partial::new(&i3)), 1299 Ok((Partial::new(&i3[3..]), 1286)) 1300 ); 1301 assert_eq!( 1302 length_and_then_2(Partial::new(&i3)), 1303 Ok((Partial::new(&i3[3..]), (5, 6))) 1304 ); 1305 1306 let i4 = [3, 5, 6, 3, 4, 5]; 1307 assert_eq!( 1308 length_and_then_1(Partial::new(&i4)), 1309 Ok((Partial::new(&i4[4..]), 1286)) 1310 ); 1311 assert_eq!( 1312 length_and_then_2(Partial::new(&i4)), 1313 Ok((Partial::new(&i4[4..]), (5, 6))) 1314 ); 1315 } 1316 } 1317