Enumerate the inter 16x16 partition chain's branch conditions
Browse files at this commit5f52cbd86ca00fb72c69d642c16295e512433ff8Parent: 00c1b3f- test
- enumerate the inter 16x16 partition chain's branch conditions
1 changed file +153 -32
crates/ec-av1/src/refusal_inventory.rsmodified+153 -32
| @@ -241,6 +241,12 @@ const PROVEN: &[(&str, &str)] = &[ | |||
| 241 | "a superblock-level partition value outside PARTITION_NONE..PARTITION_VERT_4", | 241 | "a superblock-level partition value outside PARTITION_NONE..PARTITION_VERT_4", |
| 242 | "every_partition_value_of_an_enumerated_alphabet_has_an_arm", | 242 | "every_partition_value_of_an_enumerated_alphabet_has_an_arm", |
| 243 | ), | 243 | ), |
| 244 | + // lane-t900 r24, enumeration: the inter 16x16-level if/else chain's | ||
| 245 | + // branches name all ten `partition_w16` values between them. | ||
| 246 | + ( | ||
| 247 | + "an inter 16x16-level partition value outside NONE/HORZ/VERT/SPLIT/AB/1:4", | ||
| 248 | + "every_partition_value_of_an_if_chain_alphabet_is_named_by_a_branch", | ||
| 249 | + ), | ||
| 244 | ( | 250 | ( |
| 245 | "a bit depth of 12 (this decoder is gated at 8 and 10 only: warp/MC/wiener rounding shifts change at 12-bit and no 12-bit gate exists)", | 251 | "a bit depth of 12 (this decoder is gated at 8 and 10 only: warp/MC/wiener rounding shifts change at 12-bit and no 12-bit gate exists)", |
| 246 | "a_twelve_bit_sequence_header_is_refused_by_name", | 252 | "a_twelve_bit_sequence_header_is_refused_by_name", |
| @@ -428,6 +434,41 @@ mod tests { | |||
| 428 | ); | 434 | ); |
| 429 | } | 435 | } |
| 430 | 436 | ||
| 437 | + /// The partition alphabet, in CDF symbol order. | ||
| 438 | + const PARTITION_NAMES: [&str; 10] = [ | ||
| 439 | + "PARTITION_NONE", | ||
| 440 | + "PARTITION_HORZ", | ||
| 441 | + "PARTITION_VERT", | ||
| 442 | + "PARTITION_SPLIT", | ||
| 443 | + "PARTITION_HORZ_A", | ||
| 444 | + "PARTITION_HORZ_B", | ||
| 445 | + "PARTITION_VERT_A", | ||
| 446 | + "PARTITION_VERT_B", | ||
| 447 | + "PARTITION_HORZ_4", | ||
| 448 | + "PARTITION_VERT_4", | ||
| 449 | + ]; | ||
| 450 | + | ||
| 451 | + /// The partition values a `match` arm pattern -- or an `if` chain's | ||
| 452 | + /// condition -- names: its own tokens, plus every value of an inclusive | ||
| 453 | + /// range (`(PARTITION_HORZ_A..=PARTITION_VERT_B).contains(&p)` is four | ||
| 454 | + /// values in one). Tokenised, never `contains()`d: a misspelled constant | ||
| 455 | + /// in a match pattern is an irrefutable binding that compiles and swallows | ||
| 456 | + /// the alphabet, and a substring check would call that covered. | ||
| 457 | + fn partition_values_named(head: &str) -> BTreeSet<usize> { | ||
| 458 | + let mut out = BTreeSet::new(); | ||
| 459 | + let idx: Vec<usize> = head | ||
| 460 | + .split(|c: char| !(c.is_ascii_alphanumeric() || c == '_')) | ||
| 461 | + .filter_map(|tok| PARTITION_NAMES.iter().position(|n| *n == tok)) | ||
| 462 | + .collect(); | ||
| 463 | + if head.contains("..=") && idx.len() == 2 { | ||
| 464 | + for v in idx[0]..=idx[1] { | ||
| 465 | + out.insert(v); | ||
| 466 | + } | ||
| 467 | + } | ||
| 468 | + out.extend(idx); | ||
| 469 | + out | ||
| 470 | + } | ||
| 471 | + | ||
| 431 | /// The partition alphabets, enumerated against the arms that code them. | 472 | /// The partition alphabets, enumerated against the arms that code them. |
| 432 | /// | 473 | /// |
| 433 | /// A partition value is a CDF symbol, and `SymbolDecoder::symbol_fixed` | 474 | /// A partition value is a CDF symbol, and `SymbolDecoder::symbol_fixed` |
| @@ -449,18 +490,6 @@ mod tests { | |||
| 449 | /// ladder rather than a match, so it needs its own enumerator. | 490 | /// ladder rather than a match, so it needs its own enumerator. |
| 450 | #[test] | 491 | #[test] |
| 451 | fn every_partition_value_of_an_enumerated_alphabet_has_an_arm() { | 492 | fn every_partition_value_of_an_enumerated_alphabet_has_an_arm() { |
| 452 | - const NAMES: [&str; 10] = [ | ||
| 453 | - "PARTITION_NONE", | ||
| 454 | - "PARTITION_HORZ", | ||
| 455 | - "PARTITION_VERT", | ||
| 456 | - "PARTITION_SPLIT", | ||
| 457 | - "PARTITION_HORZ_A", | ||
| 458 | - "PARTITION_HORZ_B", | ||
| 459 | - "PARTITION_VERT_A", | ||
| 460 | - "PARTITION_VERT_B", | ||
| 461 | - "PARTITION_HORZ_4", | ||
| 462 | - "PARTITION_VERT_4", | ||
| 463 | - ]; | ||
| 464 | // (the refusal in the fallback arm, the alphabet its CDF codes) | 493 | // (the refusal in the fallback arm, the alphabet its CDF codes) |
| 465 | let blocks: [(&str, usize); 4] = [ | 494 | let blocks: [(&str, usize); 4] = [ |
| 466 | ("a 32x32 partition type this decoder does not code", crate::cdf::PARTITION_W32[0].len() - 1), | 495 | ("a 32x32 partition type this decoder does not code", crate::cdf::PARTITION_W32[0].len() - 1), |
| @@ -475,24 +504,6 @@ mod tests { | |||
| 475 | crate::cdf::PARTITION_W64[0].len() - 1, | 504 | crate::cdf::PARTITION_W64[0].len() - 1, |
| 476 | ), | 505 | ), |
| 477 | ]; | 506 | ]; |
| 478 | - // A pattern's own tokens, plus every value of an inclusive range | ||
| 479 | - // (`p if (PARTITION_HORZ_A..=PARTITION_VERT_B).contains(&p)` is four | ||
| 480 | - // arms in one). | ||
| 481 | - let covered = |head: &str| -> BTreeSet<usize> { | ||
| 482 | - let mut out = BTreeSet::new(); | ||
| 483 | - let idx: Vec<usize> = head | ||
| 484 | - .split(|c: char| !(c.is_ascii_alphanumeric() || c == '_')) | ||
| 485 | - .filter_map(|tok| NAMES.iter().position(|n| *n == tok)) | ||
| 486 | - .collect(); | ||
| 487 | - if head.contains("..=") && idx.len() == 2 { | ||
| 488 | - for v in idx[0]..=idx[1] { | ||
| 489 | - out.insert(v); | ||
| 490 | - } | ||
| 491 | - } | ||
| 492 | - out.extend(idx); | ||
| 493 | - out | ||
| 494 | - }; | ||
| 495 | - | ||
| 496 | let src = include_str!("decode.rs"); | 507 | let src = include_str!("decode.rs"); |
| 497 | let lines: Vec<&str> = src.lines().collect(); | 508 | let lines: Vec<&str> = src.lines().collect(); |
| 498 | let indent = |l: &str| l.len() - l.trim_start().len(); | 509 | let indent = |l: &str| l.len() - l.trim_start().len(); |
| @@ -523,7 +534,7 @@ mod tests { | |||
| 523 | continue; | 534 | continue; |
| 524 | } | 535 | } |
| 525 | in_fallback = l.trim_start().starts_with("_ =>"); | 536 | in_fallback = l.trim_start().starts_with("_ =>"); |
| 526 | - arms.extend(covered(l.split("=>").next().unwrap_or(""))); | 537 | + arms.extend(partition_values_named(l.split("=>").next().unwrap_or(""))); |
| 527 | } | 538 | } |
| 528 | let Some((reason, alphabet)) = | 539 | let Some((reason, alphabet)) = |
| 529 | blocks.iter().find(|(reason, _)| fallback.contains(reason)) | 540 | blocks.iter().find(|(reason, _)| fallback.contains(reason)) |
| @@ -537,7 +548,7 @@ mod tests { | |||
| 537 | "the `match` at decode.rs:{} refuses {reason:?} but has no arm for {:?} -- that \ | 548 | "the `match` at decode.rs:{} refuses {reason:?} but has no arm for {:?} -- that \ |
| 538 | is a real gap in its {alphabet}-value alphabet, not a dead refusal", | 549 | is a real gap in its {alphabet}-value alphabet, not a dead refusal", |
| 539 | i + 1, | 550 | i + 1, |
| 540 | - missing.iter().map(|v| NAMES[*v]).collect::<Vec<_>>() | 551 | + missing.iter().map(|v| PARTITION_NAMES[*v]).collect::<Vec<_>>() |
| 541 | ); | 552 | ); |
| 542 | } | 553 | } |
| 543 | assert_eq!( | 554 | assert_eq!( |
| @@ -547,6 +558,116 @@ mod tests { | |||
| 547 | ); | 558 | ); |
| 548 | } | 559 | } |
| 549 | 560 | ||
| 561 | + /// The same enumeration for a partition refusal that sits in an `if` | ||
| 562 | + /// chain rather than a `match` fallback. | ||
| 563 | + /// | ||
| 564 | + /// The inter 16x16-level ladder tests `part16` against constants arm by | ||
| 565 | + /// arm and refuses in its second-to-last branch, with `PARTITION_SPLIT` | ||
| 566 | + /// left to the final `else`. Nothing about that shape is weaker than a | ||
| 567 | + /// `match`: `part16` is a `partition_w16` symbol (or, at a frame edge, a | ||
| 568 | + /// gathered value of the same alphabet), so if the chain's conditions | ||
| 569 | + /// between them name every value of that alphabet, the refusing branch is | ||
| 570 | + /// unreachable. | ||
| 571 | + /// | ||
| 572 | + /// The conditions are read from the source, tokenised (never | ||
| 573 | + /// substring-matched -- lane-t900 r21), and a condition carrying `&&` is | ||
| 574 | + /// rejected outright: a value it names is only conditionally handled, and | ||
| 575 | + /// counting it would overstate the coverage. | ||
| 576 | + #[test] | ||
| 577 | + fn every_partition_value_of_an_if_chain_alphabet_is_named_by_a_branch() { | ||
| 578 | + // (the refusal in the chain's refusing branch, the alphabet its CDF codes) | ||
| 579 | + let chains: [(&str, usize); 1] = [( | ||
| 580 | + "an inter 16x16-level partition value outside NONE/HORZ/VERT/SPLIT/AB/1:4", | ||
| 581 | + crate::cdf::PARTITION_W16[0].len() - 1, | ||
| 582 | + )]; | ||
| 583 | + | ||
| 584 | + let src = include_str!("decode.rs"); | ||
| 585 | + let lines: Vec<&str> = src.lines().collect(); | ||
| 586 | + let indent = |l: &str| l.len() - l.trim_start().len(); | ||
| 587 | + let mut found = 0usize; | ||
| 588 | + for (reason, alphabet) in chains { | ||
| 589 | + let refusal = lines | ||
| 590 | + .iter() | ||
| 591 | + .position(|l| l.contains(reason)) | ||
| 592 | + .unwrap_or_else(|| panic!("{reason:?} is not in decode.rs")); | ||
| 593 | + // The branch that carries the refusal: the nearest `} else if` | ||
| 594 | + // above it. Its indentation is the chain's own. | ||
| 595 | + let head = (0..refusal) | ||
| 596 | + .rev() | ||
| 597 | + .find(|&i| lines[i].trim_start().starts_with("} else if ")) | ||
| 598 | + .unwrap_or_else(|| panic!("{reason:?} is not inside an if/else chain")); | ||
| 599 | + let outer = indent(lines[head]); | ||
| 600 | + let var = lines[head].trim_start()["} else if ".len()..] | ||
| 601 | + .split_whitespace() | ||
| 602 | + .next() | ||
| 603 | + .unwrap_or("") | ||
| 604 | + .to_owned(); | ||
| 605 | + assert!(!var.is_empty(), "the chain at decode.rs:{} tests nothing", head + 1); | ||
| 606 | + | ||
| 607 | + // Walk up collecting every branch head of this chain, each read to | ||
| 608 | + // the line that opens its body. | ||
| 609 | + let mut named = BTreeSet::new(); | ||
| 610 | + let mut i = head + 1; | ||
| 611 | + let mut heads = 0usize; | ||
| 612 | + loop { | ||
| 613 | + i -= 1; | ||
| 614 | + let t = lines[i].trim_start(); | ||
| 615 | + if indent(lines[i]) != outer || !(t.starts_with("} else if ") || t.starts_with("if ")) | ||
| 616 | + { | ||
| 617 | + if i == 0 { | ||
| 618 | + break; | ||
| 619 | + } | ||
| 620 | + continue; | ||
| 621 | + } | ||
| 622 | + let mut cond = String::new(); | ||
| 623 | + for l in &lines[i..] { | ||
| 624 | + cond.push(' '); | ||
| 625 | + cond.push_str(l.trim()); | ||
| 626 | + if l.trim_end().ends_with('{') { | ||
| 627 | + break; | ||
| 628 | + } | ||
| 629 | + } | ||
| 630 | + assert!( | ||
| 631 | + cond.contains(&var), | ||
| 632 | + "the branch at decode.rs:{} sits in the chain that refuses {reason:?} but \ | ||
| 633 | + does not test {var}", | ||
| 634 | + i + 1 | ||
| 635 | + ); | ||
| 636 | + assert!( | ||
| 637 | + !cond.contains("&&"), | ||
| 638 | + "the branch at decode.rs:{} handles its partition values only conditionally \ | ||
| 639 | + ({cond:?}) -- the enumeration below would overstate what the chain covers", | ||
| 640 | + i + 1 | ||
| 641 | + ); | ||
| 642 | + named.extend(partition_values_named(&cond)); | ||
| 643 | + heads += 1; | ||
| 644 | + if t.starts_with("if ") { | ||
| 645 | + break; | ||
| 646 | + } | ||
| 647 | + assert!(i > 0, "the chain that refuses {reason:?} has no start"); | ||
| 648 | + } | ||
| 649 | + // The value the refusing branch excludes (`part16 != PARTITION_SPLIT`) | ||
| 650 | + // is handled by the chain's final `else`, so require one. | ||
| 651 | + let close = " ".repeat(outer) + "} else {"; | ||
| 652 | + assert!( | ||
| 653 | + lines[refusal..].iter().any(|l| *l == close), | ||
| 654 | + "the chain that refuses {reason:?} has no final `else`, so the value its \ | ||
| 655 | + refusing branch excludes is handled nowhere" | ||
| 656 | + ); | ||
| 657 | + assert!(heads >= 2, "only {heads} branch(es) found for {reason:?}"); | ||
| 658 | + found += 1; | ||
| 659 | + let missing: Vec<usize> = (0..alphabet).filter(|v| !named.contains(v)).collect(); | ||
| 660 | + assert!( | ||
| 661 | + missing.is_empty(), | ||
| 662 | + "the chain at decode.rs:{} refuses {reason:?} but no branch names {:?} -- that \ | ||
| 663 | + is a real gap in its {alphabet}-value alphabet, not a dead refusal", | ||
| 664 | + head + 1, | ||
| 665 | + missing.iter().map(|v| PARTITION_NAMES[*v]).collect::<Vec<_>>() | ||
| 666 | + ); | ||
| 667 | + } | ||
| 668 | + assert_eq!(found, chains.len()); | ||
| 669 | + } | ||
| 670 | + | ||
| 550 | /// Every entry of [`PROVEN`] must still name a live refusal and a test | 671 | /// Every entry of [`PROVEN`] must still name a live refusal and a test |
| 551 | /// that exists, and the count it prints is the inventory's numerator. | 672 | /// that exists, and the count it prints is the inventory's numerator. |
| 552 | #[test] | 673 | #[test] |