| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | #[cfg(test)] |
| 57 | const NEVER_EXERCISED: &[(&str, &str)] = &[ |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | ]; |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | #[cfg(test)] |
| 80 | const TOOL_UNIVERSE: &[&str] = &[ |
| 81 | "enable-1to4-partitions", |
| 82 | "enable-ab-partitions", |
| 83 | "enable-angle-delta", |
| 84 | "enable-cdef", |
| 85 | "enable-cfl-intra", |
| 86 | "enable-dist-wtd-comp", |
| 87 | "enable-dual-filter", |
| 88 | "enable-filter-intra", |
| 89 | "enable-flip-idtx", |
| 90 | "enable-global-motion", |
| 91 | "enable-interintra-comp", |
| 92 | "enable-intra-edge-filter", |
| 93 | "enable-intrabc", |
| 94 | "enable-masked-comp", |
| 95 | "enable-obmc", |
| 96 | "enable-order-hint", |
| 97 | "enable-paeth-intra", |
| 98 | "enable-palette", |
| 99 | "enable-rect-partitions", |
| 100 | "enable-rect-tx", |
| 101 | "enable-ref-frame-mvs", |
| 102 | "enable-restoration", |
| 103 | "enable-smooth-intra", |
| 104 | "enable-superres", |
| 105 | "enable-tx64", |
| 106 | "enable-warped-motion", |
| 107 | ]; |
| 108 | |
| 109 | |
| 110 | #[cfg(test)] |
| 111 | #[derive(Clone, Copy, PartialEq)] |
| 112 | enum On { |
| 113 | |
| 114 | NonZero, |
| 115 | |
| 116 | AtMost(u32), |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | #[cfg(test)] |
| 171 | const DEFAULT_ON_TOOLS: &[(&str, &[&str], &str, On)] = &[ |
| 172 | ( |
| 173 | "enable-tx-size-search", |
| 174 | &["enable-tx-size-search"], |
| 175 | "1", |
| 176 | On::NonZero, |
| 177 | ), |
| 178 | ( |
| 179 | "enable-directional-intra", |
| 180 | &["enable-directional-intra"], |
| 181 | "1", |
| 182 | On::NonZero, |
| 183 | ), |
| 184 | ( |
| 185 | "enable-smooth-interintra", |
| 186 | &["enable-smooth-interintra"], |
| 187 | "1", |
| 188 | On::NonZero, |
| 189 | ), |
| 190 | ( |
| 191 | "enable-interintra-wedge", |
| 192 | &["enable-interintra-wedge"], |
| 193 | "1", |
| 194 | On::NonZero, |
| 195 | ), |
| 196 | ( |
| 197 | "enable-diff-wtd-comp", |
| 198 | &["enable-diff-wtd-comp"], |
| 199 | "1", |
| 200 | On::NonZero, |
| 201 | ), |
| 202 | ( |
| 203 | "enable-onesided-comp", |
| 204 | &["enable-onesided-comp"], |
| 205 | "1", |
| 206 | On::NonZero, |
| 207 | ), |
| 208 | ("enable-fwd-kf", &["enable-fwd-kf"], "0", On::NonZero), |
| 209 | ("deblocking", &["loopfilter-control"], "1", On::NonZero), |
| 210 | ( |
| 211 | "multi-tile", |
| 212 | &["tile-columns", "tile-rows"], |
| 213 | "0", |
| 214 | On::NonZero, |
| 215 | ), |
| 216 | ("intrabc-search", &["cpu-used"], "0", On::AtMost(2)), |
| 217 | ]; |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | #[cfg(test)] |
| 233 | const ALIASES: &[(&str, &str)] = &[("superres-mode", "enable-superres")]; |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | #[cfg(test)] |
| 247 | const NEVER_EXERCISED_8BIT: &[(&str, &str)] = &[ |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | ]; |
| 341 | |
| 342 | #[cfg(test)] |
| 343 | const NEVER_EXERCISED_10BIT: &[(&str, &str)] = &[ |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | ]; |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | #[cfg(test)] |
| 437 | const NEVER_ON_8BIT: &[(&str, &str)] = &[]; |
| 438 | |
| 439 | |
| 440 | #[cfg(test)] |
| 441 | const NEVER_ON_10BIT: &[(&str, &str)] = &[]; |
| 442 | |
| 443 | #[cfg(test)] |
| 444 | mod tests { |
| 445 | use super::{ |
| 446 | ALIASES, DEFAULT_ON_TOOLS, NEVER_EXERCISED, NEVER_EXERCISED_8BIT, NEVER_EXERCISED_10BIT, |
| 447 | NEVER_ON_8BIT, NEVER_ON_10BIT, On, TOOL_UNIVERSE, |
| 448 | }; |
| 449 | use std::collections::{BTreeMap, BTreeSet}; |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | fn calls_fn(body: &str, name: &str) -> bool { |
| 456 | let ident = |b: u8| b.is_ascii_alphanumeric() || b == b'_'; |
| 457 | let mut at = 0usize; |
| 458 | while let Some(rel) = body[at..].find(name) { |
| 459 | let start = at + rel; |
| 460 | let end = start + name.len(); |
| 461 | let starts_clean = start == 0 || !ident(body.as_bytes()[start - 1]); |
| 462 | if starts_clean && body[end..].starts_with('(') { |
| 463 | return true; |
| 464 | } |
| 465 | at = end; |
| 466 | } |
| 467 | false |
| 468 | } |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | fn fn_units(src: &'static str) -> Vec<(&'static str, &'static str)> { |
| 476 | const OPEN: &str = "\n fn "; |
| 477 | let mut units = Vec::new(); |
| 478 | let mut at = 0usize; |
| 479 | while let Some(rel) = src[at..].find(OPEN) { |
| 480 | let head = at + rel + OPEN.len(); |
| 481 | let Some(paren) = src[head..].find('(') else { |
| 482 | break; |
| 483 | }; |
| 484 | let close = src[head..] |
| 485 | .find("\n }\n") |
| 486 | .map_or(src.len(), |e| head + e); |
| 487 | units.push((&src[head..head + paren], &src[head..close])); |
| 488 | at = close.max(head + paren + 1); |
| 489 | } |
| 490 | units |
| 491 | } |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | fn encoder_fns(src: &'static str) -> BTreeSet<&'static str> { |
| 504 | let units = fn_units(src); |
| 505 | let mut enc: BTreeSet<&'static str> = units |
| 506 | .iter() |
| 507 | .filter(|(_, body)| body.contains("aomenc_path()")) |
| 508 | .map(|(name, _)| *name) |
| 509 | .collect(); |
| 510 | |
| 511 | |
| 512 | loop { |
| 513 | let mut grew = false; |
| 514 | for (name, body) in &units { |
| 515 | if enc.contains(name) { |
| 516 | continue; |
| 517 | } |
| 518 | if enc.iter().any(|e| calls_fn(body, e)) { |
| 519 | enc.insert(name); |
| 520 | grew = true; |
| 521 | } |
| 522 | } |
| 523 | if !grew { |
| 524 | return enc; |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | fn legacy_gate_tokens(body: &str) -> bool { |
| 533 | body.contains("\"--passes=1\"") |
| 534 | || body.contains("ten_bit_tool_gate(") |
| 535 | || body.contains("encode_10bit_gradients") |
| 536 | } |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | fn gate_bodies() -> Vec<&'static str> { |
| 548 | let src = include_str!("stream.rs"); |
| 549 | let enc = encoder_fns(src); |
| 550 | let gates: Vec<&str> = src |
| 551 | .split("\n #[") |
| 552 | |
| 553 | |
| 554 | |
| 555 | .filter(|body| !body.starts_with("ignore")) |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | .filter(|body| { |
| 561 | legacy_gate_tokens(body) |
| 562 | || body.contains("aomenc_path()") |
| 563 | || enc.iter().any(|e| calls_fn(body, e)) |
| 564 | }) |
| 565 | .collect(); |
| 566 | assert!( |
| 567 | gates.len() >= 20, |
| 568 | "expected the real-aomenc gates, found {}", |
| 569 | gates.len() |
| 570 | ); |
| 571 | gates |
| 572 | } |
| 573 | |
| 574 | |
| 575 | fn flags_in(gate: &str) -> BTreeMap<String, char> { |
| 576 | let mut here = BTreeMap::new(); |
| 577 | for (i, _) in gate.match_indices("\"--enable-") { |
| 578 | let rest = &gate[i + 3..]; |
| 579 | let Some(end) = rest.find('"') else { continue }; |
| 580 | let Some((flag, value)) = rest[..end].split_once('=') else { |
| 581 | continue; |
| 582 | }; |
| 583 | let Some(value) = value.chars().next() else { |
| 584 | continue; |
| 585 | }; |
| 586 | here.insert(flag.to_owned(), value); |
| 587 | } |
| 588 | here |
| 589 | } |
| 590 | |
| 591 | |
| 592 | |
| 593 | fn settings_in(gate: &str) -> BTreeMap<String, String> { |
| 594 | let mut here = BTreeMap::new(); |
| 595 | for (i, _) in gate.match_indices("\"--") { |
| 596 | let rest = &gate[i + 3..]; |
| 597 | let Some(end) = rest.find('"') else { continue }; |
| 598 | let Some((flag, value)) = rest[..end].split_once('=') else { |
| 599 | continue; |
| 600 | }; |
| 601 | here.insert(flag.to_owned(), value.to_owned()); |
| 602 | } |
| 603 | here |
| 604 | } |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | fn bound_values(body: &str, var: &str) -> Option<BTreeSet<char>> { |
| 637 | |
| 638 | let quoted_digits = |t: &str| -> BTreeSet<char> { |
| 639 | let mut out = BTreeSet::new(); |
| 640 | let mut at = 0usize; |
| 641 | while let Some(rel) = t[at..].find('"') { |
| 642 | let start = at + rel + 1; |
| 643 | let Some(end) = t[start..].find('"') else { |
| 644 | break; |
| 645 | }; |
| 646 | let literal = &t[start..start + end]; |
| 647 | if literal.len() == 1 { |
| 648 | if let Some(c) = literal.chars().next() { |
| 649 | if c.is_ascii_digit() { |
| 650 | out.insert(c); |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | at = start + end + 1; |
| 655 | } |
| 656 | out |
| 657 | }; |
| 658 | |
| 659 | |
| 660 | let bare_digits = |t: &str| -> BTreeSet<char> { |
| 661 | let mut out = BTreeSet::new(); |
| 662 | let bytes = t.as_bytes(); |
| 663 | let mut i = 0usize; |
| 664 | while i < bytes.len() { |
| 665 | if !bytes[i].is_ascii_digit() { |
| 666 | i += 1; |
| 667 | continue; |
| 668 | } |
| 669 | let start = i; |
| 670 | while i < bytes.len() && bytes[i].is_ascii_digit() { |
| 671 | i += 1; |
| 672 | } |
| 673 | let clean_before = start == 0 || !is_ident_byte(bytes[start - 1]); |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | let rest = &t[i..]; |
| 680 | let suffix = rest |
| 681 | .bytes() |
| 682 | .take_while(|b| b.is_ascii_lowercase() || *b == b'_') |
| 683 | .count(); |
| 684 | let clean_after = i >= bytes.len() |
| 685 | || !is_ident_byte(bytes[i]) |
| 686 | || (suffix > 0 |
| 687 | && i + suffix < bytes.len() |
| 688 | && !is_ident_byte(bytes[i + suffix])); |
| 689 | if clean_before && clean_after && i - start == 1 { |
| 690 | out.insert(bytes[start] as char); |
| 691 | } |
| 692 | } |
| 693 | out |
| 694 | }; |
| 695 | |
| 696 | |
| 697 | fn split_top<'a>(t: &'a str) -> Vec<&'a str> { |
| 698 | let mut out = Vec::new(); |
| 699 | let (mut depth, mut start) = (0i32, 0usize); |
| 700 | for (i, c) in t.char_indices() { |
| 701 | match c { |
| 702 | '(' | '[' | '{' => depth += 1, |
| 703 | ')' | ']' | '}' => depth -= 1, |
| 704 | ',' if depth == 0 => { |
| 705 | out.push(t[start..i].trim()); |
| 706 | start = i + 1; |
| 707 | } |
| 708 | _ => {} |
| 709 | } |
| 710 | } |
| 711 | if start < t.len() { |
| 712 | out.push(t[start..].trim()); |
| 713 | } |
| 714 | out |
| 715 | } |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | fn loop_array_after<'a>(text: &'a str, head: &str) -> Option<&'a str> { |
| 722 | let at = text.find(head)? + head.len(); |
| 723 | let open = at + text[at..].find('[')?; |
| 724 | let mut depth = 0i32; |
| 725 | for (i, c) in text[open..].char_indices() { |
| 726 | match c { |
| 727 | '[' => depth += 1, |
| 728 | ']' => { |
| 729 | depth -= 1; |
| 730 | if depth == 0 { |
| 731 | return Some(&text[open + 1..open + i]); |
| 732 | } |
| 733 | } |
| 734 | _ => {} |
| 735 | } |
| 736 | } |
| 737 | None |
| 738 | } |
| 739 | let mut found: Option<BTreeSet<char>> = None; |
| 740 | let mut merge = |set: BTreeSet<char>| match &mut found { |
| 741 | Some(all) => all.extend(set), |
| 742 | None => found = Some(set), |
| 743 | }; |
| 744 | |
| 745 | if let Some(at) = body.find(&format!("let {var} =")) { |
| 746 | let init = &body[at..]; |
| 747 | let end = init.find(';').unwrap_or(init.len()); |
| 748 | merge(quoted_digits(&init[..end])); |
| 749 | } |
| 750 | |
| 751 | if let Some(array) = loop_array_after(body, &format!("for {var} in ")) { |
| 752 | merge(bare_digits(array)); |
| 753 | merge(quoted_digits(array)); |
| 754 | } |
| 755 | |
| 756 | if let Some(at) = body.find("for (") { |
| 757 | let head = &body[at..]; |
| 758 | if let Some(close) = head.find(')') { |
| 759 | let pattern = &head["for (".len()..close]; |
| 760 | if let Some(pos) = pattern.split(',').position(|p| p.trim() == var) { |
| 761 | let header = format!("for ({pattern}) in "); |
| 762 | if let Some(array) = loop_array_after(body, &header) { |
| 763 | let mut set: BTreeSet<char> = BTreeSet::new(); |
| 764 | for tuple in split_top(array) { |
| 765 | let Some(inner) = |
| 766 | tuple.strip_prefix('(').and_then(|t| t.strip_suffix(')')) |
| 767 | else { |
| 768 | continue; |
| 769 | }; |
| 770 | let fields = split_top(inner); |
| 771 | let Some(field) = fields.get(pos) else { |
| 772 | continue; |
| 773 | }; |
| 774 | set.extend(bare_digits(field)); |
| 775 | set.extend(quoted_digits(field)); |
| 776 | } |
| 777 | merge(set); |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | let named_column = |pos: usize| -> Option<BTreeSet<char>> { |
| 801 | let at = body.find("for (")?; |
| 802 | let head = &body[at..]; |
| 803 | let close = head.find(')')?; |
| 804 | let after = head[close + 1..].trim_start().strip_prefix("in ")?; |
| 805 | let name: &str = after |
| 806 | .split(|c: char| !is_ident_byte(c as u8)) |
| 807 | .next() |
| 808 | .filter(|n| !n.is_empty() && !n.starts_with(|c: char| c.is_ascii_digit()))?; |
| 809 | let decl = body.find(&format!("let {name}"))?; |
| 810 | let after_name = decl + "let ".len() + name.len(); |
| 811 | |
| 812 | if body[after_name..] |
| 813 | .chars() |
| 814 | .next() |
| 815 | .is_some_and(|c| is_ident_byte(c as u8)) |
| 816 | { |
| 817 | return None; |
| 818 | } |
| 819 | |
| 820 | |
| 821 | let eq = after_name + body[after_name..].find('=')?; |
| 822 | let open = eq + 1 + body[eq + 1..].find('[')?; |
| 823 | let mut depth = 0i32; |
| 824 | for (i, c) in body[open..].char_indices() { |
| 825 | match c { |
| 826 | '[' => depth += 1, |
| 827 | ']' => { |
| 828 | depth -= 1; |
| 829 | if depth == 0 { |
| 830 | let mut set: BTreeSet<char> = Default::default(); |
| 831 | for tuple in split_top(&body[open + 1..open + i]) { |
| 832 | let Some(inner) = |
| 833 | tuple.strip_prefix('(').and_then(|t| t.strip_suffix(')')) |
| 834 | else { |
| 835 | continue; |
| 836 | }; |
| 837 | let fields = split_top(inner); |
| 838 | let Some(field) = fields.get(pos) else { |
| 839 | continue; |
| 840 | }; |
| 841 | set.extend(bare_digits(field)); |
| 842 | set.extend(quoted_digits(field)); |
| 843 | } |
| 844 | return Some(set); |
| 845 | } |
| 846 | } |
| 847 | _ => {} |
| 848 | } |
| 849 | } |
| 850 | None |
| 851 | }; |
| 852 | if let Some(at) = body.find("for (") { |
| 853 | let head = &body[at..]; |
| 854 | if let Some(close) = head.find(')') { |
| 855 | let pattern = &head["for (".len()..close]; |
| 856 | if let Some(pos) = pattern.split(',').position(|p| p.trim() == var) { |
| 857 | if let Some(set) = named_column(pos) { |
| 858 | merge(set); |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | for (i, _) in body.match_indices("u8::from(") { |
| 865 | let arg = &body[i + "u8::from(".len()..]; |
| 866 | let Some(close) = arg.find(')') else { continue }; |
| 867 | let field = arg[..close].rsplit('.').next().unwrap_or_default().trim(); |
| 868 | if field.is_empty() || !field.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') { |
| 869 | continue; |
| 870 | } |
| 871 | let mut set = BTreeSet::new(); |
| 872 | for (row, value) in [("true", '1'), ("false", '0')] { |
| 873 | if body.contains(&format!("{field}: {row}")) { |
| 874 | set.insert(value); |
| 875 | } |
| 876 | } |
| 877 | merge(set); |
| 878 | } |
| 879 | found |
| 880 | } |
| 881 | |
| 882 | fn is_ident_byte(b: u8) -> bool { |
| 883 | b.is_ascii_alphanumeric() || b == b'_' |
| 884 | } |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | fn spelling_values(gate: &str, spelling: &str) -> BTreeSet<char> { |
| 896 | let mut out = BTreeSet::new(); |
| 897 | |
| 898 | |
| 899 | let literal = format!("\"--{spelling}="); |
| 900 | for (i, _) in gate.match_indices(&literal) { |
| 901 | let rest = &gate[i + literal.len()..]; |
| 902 | let Some(end) = rest.find('"') else { continue }; |
| 903 | if let Some(c) = rest[..end].chars().next() { |
| 904 | if c.is_ascii_digit() { |
| 905 | out.insert(c); |
| 906 | } |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | |
| 911 | |
| 912 | let template = format!("format!(\"--{spelling}="); |
| 913 | for (i, _) in gate.match_indices(&template) { |
| 914 | let rest = &gate[i + template.len()..]; |
| 915 | let Some(end) = rest.find('"') else { continue }; |
| 916 | let Some(var) = rest[..end] |
| 917 | .strip_prefix('{') |
| 918 | .and_then(|v| v.strip_suffix('}')) |
| 919 | else { |
| 920 | continue; |
| 921 | }; |
| 922 | if var.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') { |
| 923 | out.extend(bound_values(gate, var).unwrap_or_default()); |
| 924 | } |
| 925 | } |
| 926 | out |
| 927 | } |
| 928 | |
| 929 | |
| 930 | |
| 931 | |
| 932 | fn resolved_on_state(gate: &str, spellings: &[&str], on: On) -> Option<bool> { |
| 933 | let mut state = None; |
| 934 | for spelling in spellings { |
| 935 | for c in spelling_values(gate, spelling) { |
| 936 | let value = c.to_digit(10).unwrap_or(0); |
| 937 | let is_on = match on { |
| 938 | On::NonZero => value != 0, |
| 939 | On::AtMost(limit) => value <= limit, |
| 940 | }; |
| 941 | state = Some(state.unwrap_or(false) || is_on); |
| 942 | } |
| 943 | } |
| 944 | state |
| 945 | } |
| 946 | |
| 947 | |
| 948 | |
| 949 | fn tool_settings() -> (usize, BTreeMap<String, (usize, usize, usize)>) { |
| 950 | let gates = gate_bodies(); |
| 951 | |
| 952 | let mut per_tool: BTreeMap<String, (usize, usize, usize)> = BTreeMap::new(); |
| 953 | let mut names: BTreeSet<String> = BTreeSet::new(); |
| 954 | let mut seen: Vec<BTreeMap<String, char>> = Vec::new(); |
| 955 | for gate in &gates { |
| 956 | let here = flags_in(gate); |
| 957 | names.extend(here.keys().cloned()); |
| 958 | seen.push(here); |
| 959 | } |
| 960 | for name in names { |
| 961 | let off = seen.iter().filter(|g| g.get(&name) == Some(&'0')).count(); |
| 962 | let on = seen.iter().filter(|g| g.get(&name) == Some(&'1')).count(); |
| 963 | per_tool.insert(name, (off, on, gates.len() - off - on)); |
| 964 | } |
| 965 | (gates.len(), per_tool) |
| 966 | } |
| 967 | |
| 968 | #[test] |
| 969 | fn every_gate_disabling_a_tool_is_a_listed_coverage_hole() { |
| 970 | let (gate_count, per_tool) = tool_settings(); |
| 971 | |
| 972 | |
| 973 | let derived: BTreeSet<&str> = per_tool |
| 974 | .iter() |
| 975 | .filter(|&(_, &(off, on, defaulted))| off == gate_count && on == 0 && defaulted == 0) |
| 976 | .map(|(name, _)| name.as_str()) |
| 977 | .collect(); |
| 978 | |
| 979 | |
| 980 | |
| 981 | |
| 982 | |
| 983 | let exercised: BTreeSet<&str> = per_tool |
| 984 | .iter() |
| 985 | .filter(|&(_, &(_, on, _))| on > 0) |
| 986 | .map(|(name, _)| name.as_str()) |
| 987 | .collect(); |
| 988 | let listed: BTreeSet<&str> = NEVER_EXERCISED.iter().map(|&(flag, _)| flag).collect(); |
| 989 | |
| 990 | let unlisted: Vec<&&str> = derived.difference(&listed).collect(); |
| 991 | assert!( |
| 992 | unlisted.is_empty(), |
| 993 | "these aomenc tools are switched off in all {gate_count} real-aomenc gates and on in \ |
| 994 | none, so no real stream exercises them, and they are not listed in NEVER_EXERCISED: \ |
| 995 | {unlisted:?} -- either enable the tool with `=1` in its own gate or add it to the \ |
| 996 | list with a reason" |
| 997 | ); |
| 998 | let stale: Vec<&&str> = listed.intersection(&exercised).collect(); |
| 999 | assert!( |
| 1000 | stale.is_empty(), |
| 1001 | "NEVER_EXERCISED lists {stale:?}, but a gate now passes `=1` for them -- delete \ |
| 1002 | those entries, the coverage hole is closed" |
| 1003 | ); |
| 1004 | } |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | #[test] |
| 1010 | fn tool_settings_reads_whole_flags() { |
| 1011 | let (gate_count, per_tool) = tool_settings(); |
| 1012 | assert!( |
| 1013 | per_tool.contains_key("enable-cdef"), |
| 1014 | "expected --enable-cdef among the gate flags" |
| 1015 | ); |
| 1016 | for (name, &(off, on, defaulted)) in &per_tool { |
| 1017 | assert!( |
| 1018 | !name.is_empty() && !name.contains(['"', '=']), |
| 1019 | "malformed flag {name:?}" |
| 1020 | ); |
| 1021 | assert_eq!( |
| 1022 | off + on + defaulted, |
| 1023 | gate_count, |
| 1024 | "{name} counts do not cover every gate" |
| 1025 | ); |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | |
| 1034 | |
| 1035 | |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | |
| 1044 | |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | |
| 1055 | |
| 1056 | |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | |
| 1061 | fn covers_both_depths(body: &str) -> bool { |
| 1062 | body.contains("if bit_depth == 10") |
| 1063 | || body.contains("if depth == 10") |
| 1064 | || body.contains("for depth in [8usize, 10]") |
| 1065 | || (body.contains("\"yuv420p\"") && body.contains("\"yuv420p10le\"")) |
| 1066 | } |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | |
| 1074 | #[test] |
| 1075 | fn the_detector_sees_the_gates_the_spelling_filter_missed() { |
| 1076 | let src = include_str!("stream.rs"); |
| 1077 | let gates = gate_bodies(); |
| 1078 | |
| 1079 | |
| 1080 | |
| 1081 | for name in [ |
| 1082 | "a_real_aomenc_dual_filter_obmc_8x8_inter_sequence_decodes_pixel_exact", |
| 1083 | "a_real_obmc_stream_reads_a_recorded_switchable_filter_for_every_neighbour", |
| 1084 | ] { |
| 1085 | let body = gates |
| 1086 | .iter() |
| 1087 | .find(|b| b.contains(name)) |
| 1088 | .unwrap_or_else(|| panic!("{name}: not seen by gate_bodies()")); |
| 1089 | assert_eq!( |
| 1090 | flags_in(body).get("enable-dual-filter"), |
| 1091 | Some(&'1'), |
| 1092 | "{name}: seen, but its --enable-dual-filter=1 spelling is not visible to \ |
| 1093 | flags_in, so recognising the gate would not retire the entry" |
| 1094 | ); |
| 1095 | } |
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | let flip = gates |
| 1100 | .iter() |
| 1101 | .find(|b| b.contains("a_real_aomenc_stream_with_a_1d_tx_class_on_a_rect_transform")) |
| 1102 | .expect("the flip-idtx gate must be seen"); |
| 1103 | assert!( |
| 1104 | covers_both_depths(flip), |
| 1105 | "the flip-idtx gate spells both fixture formats, so covers_both_depths must \ |
| 1106 | credit it to the 8-bit bucket as well" |
| 1107 | ); |
| 1108 | assert!( |
| 1109 | covers_depth(flip, false) && covers_depth(flip, true), |
| 1110 | "the flip-idtx gate must cover BOTH depths" |
| 1111 | ); |
| 1112 | |
| 1113 | |
| 1114 | let legacy_only: Vec<&str> = src |
| 1115 | .split("\n #[") |
| 1116 | .filter(|b| !b.starts_with("ignore") && legacy_gate_tokens(b)) |
| 1117 | .collect(); |
| 1118 | assert!( |
| 1119 | legacy_only.len() < gates.len(), |
| 1120 | "the call-resolving filter found {} gates, the old spelling-only filter found \ |
| 1121 | {} -- the fix is expected to ADD gates, never to drop one", |
| 1122 | gates.len(), |
| 1123 | legacy_only.len() |
| 1124 | ); |
| 1125 | for body in &legacy_only { |
| 1126 | assert!( |
| 1127 | gates.contains(body), |
| 1128 | "a gate the spelling-only filter recognised is no longer recognised: \ |
| 1129 | {:?}", |
| 1130 | &body[..body.len().min(60)] |
| 1131 | ); |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | |
| 1136 | fn covers_depth(body: &str, ten_bit: bool) -> bool { |
| 1137 | covers_both_depths(body) || is_ten_bit(body) == ten_bit |
| 1138 | } |
| 1139 | |
| 1140 | fn is_ten_bit(body: &str) -> bool { |
| 1141 | |
| 1142 | |
| 1143 | |
| 1144 | |
| 1145 | body.contains("encode_10bit_gradients") |
| 1146 | || body.contains("ten_bit_tool_gate(") |
| 1147 | || body.contains("--bit-depth=10") |
| 1148 | || body.contains("--input-bit-depth=10") |
| 1149 | || body.contains("yuv420p10le") |
| 1150 | || body.contains("encode_12bit(") |
| 1151 | || body.contains("--bit-depth=12") |
| 1152 | || body.contains("--input-bit-depth=12") |
| 1153 | || body.contains("yuv420p12le") |
| 1154 | } |
| 1155 | |
| 1156 | |
| 1157 | fn enabled_at(ten_bit: bool) -> BTreeSet<String> { |
| 1158 | let mut on = BTreeSet::new(); |
| 1159 | for gate in gate_bodies() { |
| 1160 | if !covers_depth(gate, ten_bit) { |
| 1161 | continue; |
| 1162 | } |
| 1163 | for (flag, value) in flags_in(gate) { |
| 1164 | if value == '1' { |
| 1165 | on.insert(flag); |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | let here = settings_in(gate); |
| 1170 | for (alias, tool) in ALIASES { |
| 1171 | if here.get(*alias).is_some_and(|v| v != "0") { |
| 1172 | on.insert((*tool).to_owned()); |
| 1173 | } |
| 1174 | } |
| 1175 | } |
| 1176 | on |
| 1177 | } |
| 1178 | |
| 1179 | |
| 1180 | fn never_exercised_at(ten_bit: bool) -> BTreeSet<&'static str> { |
| 1181 | let on = enabled_at(ten_bit); |
| 1182 | TOOL_UNIVERSE |
| 1183 | .iter() |
| 1184 | .copied() |
| 1185 | .filter(|t| !on.contains(*t)) |
| 1186 | .collect() |
| 1187 | } |
| 1188 | |
| 1189 | fn check_depth(ten_bit: bool, listed: &[(&str, &str)]) { |
| 1190 | let derived = never_exercised_at(ten_bit); |
| 1191 | let listed: BTreeSet<&str> = listed.iter().map(|&(flag, _)| flag).collect(); |
| 1192 | let depth = if ten_bit { "10-bit" } else { "8-bit" }; |
| 1193 | let unlisted: Vec<&&str> = derived.difference(&listed).collect(); |
| 1194 | assert!( |
| 1195 | unlisted.is_empty(), |
| 1196 | "no {depth} gate passes `=1` for {unlisted:?}, so no real {depth} stream exercises \ |
| 1197 | them -- enable the tool in a {depth} gate that asserts it fired, or list it" |
| 1198 | ); |
| 1199 | let stale: Vec<&&str> = listed.difference(&derived).collect(); |
| 1200 | assert!( |
| 1201 | stale.is_empty(), |
| 1202 | "the {depth} list still names {stale:?}, but a {depth} gate now passes `=1` for them \ |
| 1203 | -- delete those entries, the coverage hole is closed" |
| 1204 | ); |
| 1205 | } |
| 1206 | |
| 1207 | #[test] |
| 1208 | fn never_exercised_8bit_matches_the_gate_recipes() { |
| 1209 | check_depth(false, NEVER_EXERCISED_8BIT); |
| 1210 | } |
| 1211 | |
| 1212 | #[test] |
| 1213 | fn never_exercised_10bit_matches_the_gate_recipes() { |
| 1214 | check_depth(true, NEVER_EXERCISED_10BIT); |
| 1215 | } |
| 1216 | |
| 1217 | |
| 1218 | |
| 1219 | #[test] |
| 1220 | fn print_never_exercised_per_bit_depth() { |
| 1221 | let ten: Vec<&str> = gate_bodies() |
| 1222 | .into_iter() |
| 1223 | .filter(|b| is_ten_bit(b)) |
| 1224 | .collect(); |
| 1225 | let total = gate_bodies().len(); |
| 1226 | println!( |
| 1227 | "gate_coverage: {} real-aomenc gates, {} of them 10-bit", |
| 1228 | total, |
| 1229 | ten.len() |
| 1230 | ); |
| 1231 | for (label, ten_bit) in [("8BIT", false), ("10BIT", true)] { |
| 1232 | let holes = never_exercised_at(ten_bit); |
| 1233 | println!( |
| 1234 | "NEVER_EXERCISED_{label} ({} of {}):", |
| 1235 | holes.len(), |
| 1236 | TOOL_UNIVERSE.len() |
| 1237 | ); |
| 1238 | for flag in &holes { |
| 1239 | println!(" --{flag}"); |
| 1240 | } |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | |
| 1245 | fn default_on_settings( |
| 1246 | ten_bit: bool, |
| 1247 | ) -> (usize, BTreeMap<&'static str, (usize, usize, usize)>) { |
| 1248 | let gates: Vec<&str> = gate_bodies() |
| 1249 | .into_iter() |
| 1250 | .filter(|b| covers_depth(b, ten_bit)) |
| 1251 | .collect(); |
| 1252 | let mut per_tool = BTreeMap::new(); |
| 1253 | for &(tool, spellings, _, on) in DEFAULT_ON_TOOLS { |
| 1254 | |
| 1255 | |
| 1256 | |
| 1257 | let states: Vec<Option<bool>> = gates |
| 1258 | .iter() |
| 1259 | .map(|g| resolved_on_state(g, spellings, on)) |
| 1260 | .collect(); |
| 1261 | let turned_on = states.iter().filter(|s| **s == Some(true)).count(); |
| 1262 | let turned_off = states.iter().filter(|s| **s == Some(false)).count(); |
| 1263 | per_tool.insert( |
| 1264 | tool, |
| 1265 | (turned_off, turned_on, gates.len() - turned_off - turned_on), |
| 1266 | ); |
| 1267 | } |
| 1268 | (gates.len(), per_tool) |
| 1269 | } |
| 1270 | |
| 1271 | |
| 1272 | fn never_on_at(ten_bit: bool) -> BTreeSet<&'static str> { |
| 1273 | default_on_settings(ten_bit) |
| 1274 | .1 |
| 1275 | .into_iter() |
| 1276 | .filter(|&(_, (_, on, _))| on == 0) |
| 1277 | .map(|(tool, _)| tool) |
| 1278 | .collect() |
| 1279 | } |
| 1280 | |
| 1281 | fn check_default_on(ten_bit: bool, listed: &[(&str, &str)]) { |
| 1282 | let derived = never_on_at(ten_bit); |
| 1283 | let listed: BTreeSet<&str> = listed.iter().map(|&(tool, _)| tool).collect(); |
| 1284 | let depth = if ten_bit { "10-bit" } else { "8-bit" }; |
| 1285 | let unlisted: Vec<&&str> = derived.difference(&listed).collect(); |
| 1286 | assert!( |
| 1287 | unlisted.is_empty(), |
| 1288 | "no {depth} gate spells an on-value for {unlisted:?}, so no real {depth} stream is \ |
| 1289 | proven to carry them -- switch the tool on in a {depth} gate that asserts it fired, \ |
| 1290 | or list it with a reason" |
| 1291 | ); |
| 1292 | let stale: Vec<&&str> = listed.difference(&derived).collect(); |
| 1293 | assert!( |
| 1294 | stale.is_empty(), |
| 1295 | "the {depth} default-on list still names {stale:?}, but a {depth} gate now switches \ |
| 1296 | them on -- delete those entries, the coverage hole is closed" |
| 1297 | ); |
| 1298 | } |
| 1299 | |
| 1300 | #[test] |
| 1301 | fn never_on_8bit_matches_the_gate_recipes() { |
| 1302 | check_default_on(false, NEVER_ON_8BIT); |
| 1303 | } |
| 1304 | |
| 1305 | #[test] |
| 1306 | fn never_on_10bit_matches_the_gate_recipes() { |
| 1307 | check_default_on(true, NEVER_ON_10BIT); |
| 1308 | } |
| 1309 | |
| 1310 | |
| 1311 | |
| 1312 | #[test] |
| 1313 | fn default_on_tools_do_not_duplicate_the_universe() { |
| 1314 | for &(tool, spellings, _, _) in DEFAULT_ON_TOOLS { |
| 1315 | assert!( |
| 1316 | !TOOL_UNIVERSE.contains(&tool), |
| 1317 | "{tool} is already covered by TOOL_UNIVERSE" |
| 1318 | ); |
| 1319 | assert!(!spellings.is_empty(), "{tool} has no aomenc spelling"); |
| 1320 | } |
| 1321 | for (alias, tool) in ALIASES { |
| 1322 | assert!( |
| 1323 | TOOL_UNIVERSE.contains(tool), |
| 1324 | "alias --{alias} names {tool}, which is not a TOOL_UNIVERSE tool" |
| 1325 | ); |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | |
| 1330 | |
| 1331 | #[test] |
| 1332 | fn print_never_on_per_bit_depth() { |
| 1333 | for (label, ten_bit) in [("8BIT", false), ("10BIT", true)] { |
| 1334 | let (gate_count, per_tool) = default_on_settings(ten_bit); |
| 1335 | let holes = never_on_at(ten_bit); |
| 1336 | println!( |
| 1337 | "NEVER_ON_{label} ({} of {}, over {gate_count} {label} gates):", |
| 1338 | holes.len(), |
| 1339 | DEFAULT_ON_TOOLS.len() |
| 1340 | ); |
| 1341 | for tool in &holes { |
| 1342 | let (off, _, defaulted) = per_tool[tool]; |
| 1343 | let spellings = DEFAULT_ON_TOOLS |
| 1344 | .iter() |
| 1345 | .find(|e| e.0 == *tool) |
| 1346 | .map(|e| e.1.join(",")) |
| 1347 | .unwrap_or_default(); |
| 1348 | println!( |
| 1349 | " {tool} (--{spellings}): off in {off}, defaulted in {defaulted}, on in 0" |
| 1350 | ); |
| 1351 | } |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | |
| 1356 | |
| 1357 | |
| 1358 | |
| 1359 | |
| 1360 | |
| 1361 | #[test] |
| 1362 | fn print_tx_size_search_census() { |
| 1363 | const TOOL: &str = "enable-tx-size-search"; |
| 1364 | let (zero, one, both, unresolvable, unnamed) = { |
| 1365 | let (mut zero, mut one, mut both, mut unresolvable, mut unnamed) = |
| 1366 | (0usize, 0usize, 0usize, 0usize, 0usize); |
| 1367 | for gate in gate_bodies() { |
| 1368 | let values = spelling_values(gate, TOOL); |
| 1369 | let name = gate |
| 1370 | .split("fn ") |
| 1371 | .nth(1) |
| 1372 | .and_then(|s| s.split('(').next()) |
| 1373 | .unwrap_or("<segment>") |
| 1374 | .to_owned(); |
| 1375 | let state = match (values.contains(&'0'), values.contains(&'1')) { |
| 1376 | (false, false) => { |
| 1377 | unnamed += 1; |
| 1378 | "not named (defaulted)" |
| 1379 | } |
| 1380 | (true, false) => { |
| 1381 | zero += 1; |
| 1382 | "=0" |
| 1383 | } |
| 1384 | (false, true) => { |
| 1385 | one += 1; |
| 1386 | "=1" |
| 1387 | } |
| 1388 | (true, true) => { |
| 1389 | both += 1; |
| 1390 | "=0 and =1 (one arm each)" |
| 1391 | } |
| 1392 | }; |
| 1393 | let via = if gate.contains(&format!("format!(\"--{TOOL}=")) { |
| 1394 | " [format! template resolved]" |
| 1395 | } else { |
| 1396 | "" |
| 1397 | }; |
| 1398 | let helper = if gate.contains(&format!("format!(\"--{TOOL}=")) && values.is_empty() |
| 1399 | { |
| 1400 | " [UNRESOLVED: variable is a shared helper's parameter]" |
| 1401 | } else { |
| 1402 | "" |
| 1403 | }; |
| 1404 | if !helper.is_empty() { |
| 1405 | unresolvable += 1; |
| 1406 | unnamed -= 1; |
| 1407 | } |
| 1408 | println!("{name}: {state}{via}{helper}"); |
| 1409 | } |
| 1410 | (zero, one, both, unresolvable, unnamed) |
| 1411 | }; |
| 1412 | let total = gate_bodies().len(); |
| 1413 | println!( |
| 1414 | "enable-tx-size-search over {total} census-selected gate bodies: \ |
| 1415 | =0 only {zero}, =1 only {one}, both {both}, unresolvable {unresolvable}, \ |
| 1416 | not named {unnamed}" |
| 1417 | ); |
| 1418 | assert_eq!( |
| 1419 | unresolvable, 0, |
| 1420 | "{unresolvable} gate bodies build --{TOOL} into a variable this census cannot bind -- \ |
| 1421 | the count is a floor again, and an unknown direction is the failure mode this file \ |
| 1422 | exists to prevent" |
| 1423 | ); |
| 1424 | } |
| 1425 | |
| 1426 | |
| 1427 | |
| 1428 | |
| 1429 | |
| 1430 | |
| 1431 | |
| 1432 | |
| 1433 | |
| 1434 | |
| 1435 | |
| 1436 | |
| 1437 | |
| 1438 | #[test] |
| 1439 | fn the_resolver_binds_a_format_built_flag_in_both_directions() { |
| 1440 | let gates = gate_bodies(); |
| 1441 | let find = |needle: &str| -> &'static str { |
| 1442 | gates |
| 1443 | .iter() |
| 1444 | .copied() |
| 1445 | .find(|b| b.contains(needle)) |
| 1446 | .unwrap_or_else(|| panic!("no gate body contains {needle:?}")) |
| 1447 | }; |
| 1448 | |
| 1449 | let cases = [ |
| 1450 | ( |
| 1451 | "format!(\"--enable-tx-size-search={txs}\")", |
| 1452 | "for txs in [0usize, 1]", |
| 1453 | "for txs in [0usize, 0]", |
| 1454 | "a_real_aomenc_palette_stream_with_8x8_leaves_decodes_pixel_exact", |
| 1455 | ), |
| 1456 | ( |
| 1457 | "format!(\"--enable-tx-size-search={tx_search}\")", |
| 1458 | "if attempt % 2 == 0 { \"0\" } else { \"1\" }", |
| 1459 | "if attempt % 2 == 0 { \"0\" } else { \"0\" }", |
| 1460 | "a_real_aomenc_inter_sequence_with_a_coded_rectangular_residual_decodes_pixel_exact", |
| 1461 | ), |
| 1462 | ( |
| 1463 | "format!(\"--enable-tx-size-search={tx_search}\")", |
| 1464 | "for (tx_search, bit_depth) in [(\"1\", 8u8), (\"0\", 8u8), (\"1\", 10u8)]", |
| 1465 | "for (tx_search, bit_depth) in [(\"0\", 8u8), (\"0\", 8u8), (\"0\", 10u8)]", |
| 1466 | "a_real_aomenc_stream_with_filter_intra_on_a_sub8_rect_leaf_decodes_pixel_exact", |
| 1467 | ), |
| 1468 | |
| 1469 | |
| 1470 | |
| 1471 | |
| 1472 | |
| 1473 | |
| 1474 | ( |
| 1475 | "format!(\"--enable-tx-size-search={search}\")", |
| 1476 | "\"tx size search on\",\n \"1\",", |
| 1477 | "\"tx size search on\",\n \"0\",", |
| 1478 | "a_real_aomenc_444_whole_64_root_tx_size_search_stream_decodes_pixel_exact_at_128x96", |
| 1479 | ), |
| 1480 | ]; |
| 1481 | for (template, from, to, name) in cases { |
| 1482 | let body = find(name); |
| 1483 | assert!( |
| 1484 | body.contains(template), |
| 1485 | "{name} does not spell {template}, so this case proves nothing" |
| 1486 | ); |
| 1487 | assert!( |
| 1488 | body.contains(from), |
| 1489 | "{name} does not contain the source binding {from:?}" |
| 1490 | ); |
| 1491 | let before = spelling_values(body, "enable-tx-size-search"); |
| 1492 | let mutated = body.replace(from, to); |
| 1493 | assert_ne!( |
| 1494 | mutated, body, |
| 1495 | "the mutation {from:?} -> {to:?} changed nothing" |
| 1496 | ); |
| 1497 | let after = spelling_values(&mutated, "enable-tx-size-search"); |
| 1498 | assert_eq!( |
| 1499 | before, |
| 1500 | BTreeSet::from(['0', '1']), |
| 1501 | "{name}: the unmutated body should resolve to both values, got {before:?}" |
| 1502 | ); |
| 1503 | assert_eq!( |
| 1504 | after, |
| 1505 | BTreeSet::from(['0']), |
| 1506 | "{name}: mutating the bound value {from:?} -> {to:?} did not move the \ |
| 1507 | classification (still {after:?}) -- the resolver is not reading the binding" |
| 1508 | ); |
| 1509 | } |
| 1510 | |
| 1511 | |
| 1512 | |
| 1513 | |
| 1514 | |
| 1515 | |
| 1516 | let mut pinned = 0usize; |
| 1517 | for gate in &gates { |
| 1518 | if !gate.contains("\"--enable-tx-size-search=1\"") { |
| 1519 | continue; |
| 1520 | } |
| 1521 | pinned += 1; |
| 1522 | assert!( |
| 1523 | spelling_values(gate, "enable-tx-size-search").contains(&'1'), |
| 1524 | "a gate that literally spells --enable-tx-size-search=1 is no longer \ |
| 1525 | classified on: {:?}", |
| 1526 | &gate[..gate.len().min(80)] |
| 1527 | ); |
| 1528 | } |
| 1529 | assert!( |
| 1530 | pinned >= 10, |
| 1531 | "expected the literal `=1` gates to still be there, found {pinned}" |
| 1532 | ); |
| 1533 | } |
| 1534 | |
| 1535 | |
| 1536 | |
| 1537 | |
| 1538 | |
| 1539 | |
| 1540 | |
| 1541 | |
| 1542 | |
| 1543 | |
| 1544 | |
| 1545 | |
| 1546 | |
| 1547 | |
| 1548 | |
| 1549 | |
| 1550 | |
| 1551 | |
| 1552 | |
| 1553 | |
| 1554 | |
| 1555 | |
| 1556 | |
| 1557 | |
| 1558 | |
| 1559 | |
| 1560 | |
| 1561 | |
| 1562 | |
| 1563 | |
| 1564 | #[test] |
| 1565 | fn no_tool_presence_check_outside_its_probe() { |
| 1566 | |
| 1567 | |
| 1568 | |
| 1569 | |
| 1570 | |
| 1571 | const MIN_SITES: usize = 5; |
| 1572 | |
| 1573 | const PROBES: &[(&str, &str)] = &[ |
| 1574 | ("stream.rs", "fn have_aomenc()"), |
| 1575 | ("stream.rs", "fn aomdec_available("), |
| 1576 | ("stream.rs", "fn have_affine_aomenc()"), |
| 1577 | ]; |
| 1578 | const PATHS: &[&str] = &[ |
| 1579 | "aomenc_path().is_file()", |
| 1580 | "aomdec_path().is_file()", |
| 1581 | "affine_aomenc_path().is_file()", |
| 1582 | ]; |
| 1583 | let files: [(&str, &str); 3] = [ |
| 1584 | ("stream.rs", include_str!("stream.rs")), |
| 1585 | ("decode.rs", include_str!("decode.rs")), |
| 1586 | ("encode.rs", include_str!("encode.rs")), |
| 1587 | ]; |
| 1588 | |
| 1589 | |
| 1590 | |
| 1591 | let mut probe_spans: Vec<(usize, usize)> = Vec::new(); |
| 1592 | for (name, src) in files { |
| 1593 | let lines: Vec<&str> = src.lines().collect(); |
| 1594 | for (probe_file, opener) in PROBES { |
| 1595 | if *probe_file != name { |
| 1596 | continue; |
| 1597 | } |
| 1598 | let Some(start) = lines.iter().position(|l| l.contains(opener)).map(|i| i + 1) |
| 1599 | else { |
| 1600 | continue; |
| 1601 | }; |
| 1602 | let end = lines |
| 1603 | .iter() |
| 1604 | .skip(start) |
| 1605 | .position(|l| l.trim_end() == "}") |
| 1606 | .map(|off| start + off) |
| 1607 | .unwrap_or(lines.len()); |
| 1608 | probe_spans.push((start, end)); |
| 1609 | } |
| 1610 | } |
| 1611 | assert!( |
| 1612 | probe_spans.len() == PROBES.len(), |
| 1613 | "the oracle-presence scan recognised {} of the {} probe bodies it is meant to \ |
| 1614 | exempt ({probe_spans:?}); a renamed or moved probe would silently exempt every \ |
| 1615 | site in the crate, so it reds here", |
| 1616 | probe_spans.len(), |
| 1617 | PROBES.len() |
| 1618 | ); |
| 1619 | |
| 1620 | let mut sites = 0usize; |
| 1621 | let mut allowed = 0usize; |
| 1622 | let mut per_pattern: BTreeMap<&str, usize> = PATHS.iter().map(|p| (*p, 0)).collect(); |
| 1623 | let mut offenders: Vec<String> = Vec::new(); |
| 1624 | for (name, src) in files { |
| 1625 | for (i, line) in src.lines().enumerate() { |
| 1626 | |
| 1627 | |
| 1628 | |
| 1629 | |
| 1630 | |
| 1631 | |
| 1632 | let Some(path) = PATHS |
| 1633 | .iter() |
| 1634 | .filter(|p| line.contains(**p)) |
| 1635 | .max_by_key(|p| p.len()) |
| 1636 | else { |
| 1637 | continue; |
| 1638 | }; |
| 1639 | sites += 1; |
| 1640 | *per_pattern.get_mut(path).expect("pattern came from PATHS") += 1; |
| 1641 | let lineno = i + 1; |
| 1642 | let trimmed = line.trim_start(); |
| 1643 | let in_probe = probe_spans |
| 1644 | .iter() |
| 1645 | .any(|(start, end)| lineno >= *start && lineno <= *end); |
| 1646 | let in_assert = trimmed.starts_with("assert!") |
| 1647 | || trimmed.starts_with("aomdec_path().is_file(),") |
| 1648 | || trimmed.starts_with("aomenc_path().is_file(),") |
| 1649 | || trimmed.starts_with("affine_aomenc_path().is_file(),"); |
| 1650 | let in_comment = trimmed.starts_with("//"); |
| 1651 | if in_probe || in_assert || in_comment { |
| 1652 | allowed += 1; |
| 1653 | } else { |
| 1654 | offenders.push(format!( |
| 1655 | "{name}:{lineno}: {}\n a tool presence check outside its probe — \ |
| 1656 | route it through the probe that owns this path (`have_aomenc`, \ |
| 1657 | `aomdec_available`, `have_affine_aomenc`): a bare `{path}` skips the \ |
| 1658 | gate with NO env escape, so under EC_AV1_REQUIRE_AOMENC=1 the gate still \ |
| 1659 | reports green with the oracle compare never run.", |
| 1660 | line.trim() |
| 1661 | )); |
| 1662 | } |
| 1663 | } |
| 1664 | } |
| 1665 | println!( |
| 1666 | "oracle-presence scan: {sites} site(s) inspected, {allowed} in a probe / assert / \ |
| 1667 | comment, {} offender(s); per pattern {per_pattern:?}", |
| 1668 | offenders.len() |
| 1669 | ); |
| 1670 | |
| 1671 | |
| 1672 | |
| 1673 | |
| 1674 | let never_matched: Vec<&str> = per_pattern |
| 1675 | .iter() |
| 1676 | .filter(|(_, n)| **n == 0) |
| 1677 | .map(|(p, _)| *p) |
| 1678 | .collect(); |
| 1679 | assert!( |
| 1680 | never_matched.is_empty(), |
| 1681 | "the oracle-presence scan matched NOTHING for {never_matched:?}: that pattern is \ |
| 1682 | stale (renamed helper, `exists()` instead of `is_file()`, a `format!` escape) and \ |
| 1683 | the gates it was meant to police would pass unreviewed. Matched per pattern: \ |
| 1684 | {per_pattern:?}" |
| 1685 | ); |
| 1686 | assert!( |
| 1687 | sites >= MIN_SITES, |
| 1688 | "the oracle-presence scan inspected only {sites} site(s), below its floor of \ |
| 1689 | {MIN_SITES}: the matcher has stopped matching this tree (renamed path helper, a \ |
| 1690 | `format!` escape, a moved probe), and with no floor this would pass while scanning \ |
| 1691 | nothing — the false-pass shape this crate has been bitten by twice" |
| 1692 | ); |
| 1693 | assert!( |
| 1694 | offenders.is_empty(), |
| 1695 | "{} bare tool presence check(s) outside a probe — each one is a gate that can report \ |
| 1696 | green without running the oracle compare:\n {}", |
| 1697 | offenders.len(), |
| 1698 | offenders.join("\n ") |
| 1699 | ); |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | |
| 1704 | |
| 1705 | |
| 1706 | |
| 1707 | |
| 1708 | |
| 1709 | |
| 1710 | |
| 1711 | |
| 1712 | |
| 1713 | |
| 1714 | |
| 1715 | |
| 1716 | |
| 1717 | |
| 1718 | |
| 1719 | #[cfg(test)] |
| 1720 | mod pin_inventory { |
| 1721 | |
| 1722 | pub struct PinRead { |
| 1723 | pub gate: String, |
| 1724 | pub file: String, |
| 1725 | pub shape: &'static str, |
| 1726 | } |
| 1727 | |
| 1728 | |
| 1729 | fn gate_bodies(src: &str) -> Vec<(String, &'static str)> { |
| 1730 | let mut out = Vec::new(); |
| 1731 | let mut cur: Option<(String, usize)> = None; |
| 1732 | for (i, line) in src.lines().enumerate() { |
| 1733 | let t = line.trim_start(); |
| 1734 | if let Some(rest) = t.strip_prefix("fn ") { |
| 1735 | if let Some(name) = rest.split('(').next() { |
| 1736 | if !name.contains(' ') { |
| 1737 | cur = Some((name.to_string(), i)); |
| 1738 | } |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | |
| 1743 | if let Some((name, start)) = &cur { |
| 1744 | if i > *start && line == " }" { |
| 1745 | let body = src |
| 1746 | .lines() |
| 1747 | .skip(*start) |
| 1748 | .take(i - *start + 1) |
| 1749 | .collect::<Vec<_>>() |
| 1750 | .join("\n"); |
| 1751 | out.push((name.clone(), &*Box::leak(body.into_boxed_str()))); |
| 1752 | cur = None; |
| 1753 | } |
| 1754 | } |
| 1755 | } |
| 1756 | out |
| 1757 | } |
| 1758 | |
| 1759 | |
| 1760 | pub fn pins_read(src: &str) -> Vec<PinRead> { |
| 1761 | let mut out = Vec::new(); |
| 1762 | for (gate, body) in gate_bodies(src) { |
| 1763 | |
| 1764 | for m in body.match_indices("crate_pin(\"") { |
| 1765 | let rest = &body[m.0 + 11..]; |
| 1766 | if let Some(e) = rest.find('"') { |
| 1767 | out.push(PinRead { |
| 1768 | gate: gate.clone(), |
| 1769 | file: rest[..e].to_string(), |
| 1770 | shape: "crate_pin", |
| 1771 | }); |
| 1772 | } |
| 1773 | } |
| 1774 | for m in body.match_indices("pin_dir().join(\"") { |
| 1775 | let rest = &body[m.0 + 16..]; |
| 1776 | if let Some(e) = rest.find('"') { |
| 1777 | out.push(PinRead { |
| 1778 | gate: gate.clone(), |
| 1779 | file: rest[..e].to_string(), |
| 1780 | shape: "pin_dir_machine_local", |
| 1781 | }); |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | |
| 1786 | for m in body.match_indices("/../../fixtures/") { |
| 1787 | let rest = &body[m.0 + 16..]; |
| 1788 | if let Some(e) = rest.find('"') { |
| 1789 | out.push(PinRead { |
| 1790 | gate: gate.clone(), |
| 1791 | file: rest[..e].to_string(), |
| 1792 | shape: "concat_literal_machine_local", |
| 1793 | }); |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | |
| 1798 | |
| 1799 | for (vi, line) in body.lines().enumerate() { |
| 1800 | let tl = line.trim(); |
| 1801 | |
| 1802 | |
| 1803 | let var = match ( |
| 1804 | tl.find("concat!(env!(\"CARGO_MANIFEST_DIR\"), \"/../../fixtures\")"), |
| 1805 | tl.find('='), |
| 1806 | ) { |
| 1807 | (Some(a), Some(eq)) if eq < a => tl[4..eq].trim().to_string(), |
| 1808 | _ => continue, |
| 1809 | }; |
| 1810 | if var.is_empty() { |
| 1811 | continue; |
| 1812 | } |
| 1813 | |
| 1814 | |
| 1815 | |
| 1816 | |
| 1817 | let marker = ["format!(\"", "{", &var, "}/{n}"].concat(); |
| 1818 | let Some(pos) = body.find(&marker) else { |
| 1819 | continue; |
| 1820 | }; |
| 1821 | |
| 1822 | |
| 1823 | let head = &body[..pos]; |
| 1824 | let Some(bracket) = head.rfind('[') else { |
| 1825 | continue; |
| 1826 | }; |
| 1827 | let names: Vec<String> = head[bracket..] |
| 1828 | .split('"') |
| 1829 | .skip(1) |
| 1830 | .step_by(2) |
| 1831 | .filter(|s| !s.is_empty() && !s.contains(' ') && *s != "]") |
| 1832 | .map(|s| s.to_string()) |
| 1833 | .collect(); |
| 1834 | |
| 1835 | |
| 1836 | let tail = &body[pos + marker.len()..]; |
| 1837 | let ext = tail |
| 1838 | .find('"') |
| 1839 | .map(|e| tail[..e].to_string()) |
| 1840 | .unwrap_or_default(); |
| 1841 | for n in names { |
| 1842 | out.push(PinRead { |
| 1843 | gate: gate.clone(), |
| 1844 | file: format!("{n}{ext}"), |
| 1845 | shape: "concat_dir_runtime_name", |
| 1846 | }); |
| 1847 | } |
| 1848 | let _ = vi; |
| 1849 | } |
| 1850 | |
| 1851 | |
| 1852 | |
| 1853 | |
| 1854 | |
| 1855 | |
| 1856 | |
| 1857 | |
| 1858 | |
| 1859 | for m in body.match_indices("crate_pin(&format!(") { |
| 1860 | let rest = &body[m.0 + 19..]; |
| 1861 | let Some(close) = rest.find(')') else { |
| 1862 | continue; |
| 1863 | }; |
| 1864 | let call = &rest[..close]; |
| 1865 | if !call.contains('"') { |
| 1866 | continue; |
| 1867 | } |
| 1868 | let head = &body[..m.0]; |
| 1869 | let Some(bracket) = head.rfind('[') else { |
| 1870 | continue; |
| 1871 | }; |
| 1872 | let names: Vec<String> = head[bracket..] |
| 1873 | .split('"') |
| 1874 | .skip(1) |
| 1875 | .step_by(2) |
| 1876 | .filter(|x| !x.is_empty() && !x.contains(' ') && *x != "]") |
| 1877 | .map(|x| x.to_string()) |
| 1878 | .collect(); |
| 1879 | |
| 1880 | let lit = call.split('"').nth(1).unwrap_or(""); |
| 1881 | let ext = lit |
| 1882 | .split_once("}") |
| 1883 | .map(|(_, e)| e.to_string()) |
| 1884 | .unwrap_or_default(); |
| 1885 | for n in names { |
| 1886 | out.push(PinRead { |
| 1887 | gate: gate.clone(), |
| 1888 | file: format!("{n}{ext}"), |
| 1889 | shape: "crate_pin_runtime_name", |
| 1890 | }); |
| 1891 | } |
| 1892 | } |
| 1893 | } |
| 1894 | out |
| 1895 | } |
| 1896 | |
| 1897 | |
| 1898 | |
| 1899 | |
| 1900 | pub fn resolves_committed(shape: &str) -> bool { |
| 1901 | matches!(shape, "crate_pin" | "crate_pin_runtime_name") |
| 1902 | } |
| 1903 | |
| 1904 | |
| 1905 | pub fn is_committed(file: &str) -> bool { |
| 1906 | std::path::Path::new(env!("CARGO_MANIFEST_DIR")) |
| 1907 | .join("fixtures") |
| 1908 | .join(file) |
| 1909 | .is_file() |
| 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | #[cfg(test)] |
| 1914 | mod pin_inventory_tests { |
| 1915 | use super::pin_inventory::{is_committed, pins_read}; |
| 1916 | |
| 1917 | |
| 1918 | |
| 1919 | |
| 1920 | |
| 1921 | |
| 1922 | |
| 1923 | #[test] |
| 1924 | fn the_pin_scanner_sees_the_directory_literal_shape() { |
| 1925 | let synthetic = r#" |
| 1926 | #[test] |
| 1927 | fn a_gate_using_the_old_shape() { |
| 1928 | let fixtures = concat!(env!("CARGO_MANIFEST_DIR"), "/../../fixtures"); |
| 1929 | let paths: Vec<String> = match std::env::var("EC_AV1_GATE_DUMP_PIN") { |
| 1930 | Ok(p) => vec![p], |
| 1931 | Err(_) => [ |
| 1932 | "warp-mismatch", |
| 1933 | "ii-flake-1", |
| 1934 | "rect-flake-3", |
| 1935 | ] |
| 1936 | .iter() |
| 1937 | .map(|n| format!("{fixtures}/{n}.obu")) |
| 1938 | .collect(), |
| 1939 | }; |
| 1940 | for path in paths { |
| 1941 | check(&path); |
| 1942 | } |
| 1943 | } |
| 1944 | "#; |
| 1945 | let reads = pins_read(synthetic); |
| 1946 | let names: Vec<&str> = reads.iter().map(|r| r.file.as_str()).collect(); |
| 1947 | assert_eq!( |
| 1948 | names, |
| 1949 | vec!["warp-mismatch.obu", "ii-flake-1.obu", "rect-flake-3.obu"], |
| 1950 | "the directory-literal + runtime-name shape resolved to {names:?} -- a scanner \ |
| 1951 | that cannot see this shape is how 14 uncommitted pins survived three rounds" |
| 1952 | ); |
| 1953 | assert!( |
| 1954 | reads.iter().all(|r| r.shape == "concat_dir_runtime_name"), |
| 1955 | "the synthetic pins were attributed to the wrong shape: {:?}", |
| 1956 | reads.iter().map(|r| r.shape).collect::<Vec<_>>() |
| 1957 | ); |
| 1958 | } |
| 1959 | |
| 1960 | |
| 1961 | |
| 1962 | #[test] |
| 1963 | fn the_pin_scanner_sees_every_shape() { |
| 1964 | let synthetic = r#" |
| 1965 | #[test] |
| 1966 | fn shapes() { |
| 1967 | let a = crate_pin("golden3-pin.obu"); |
| 1968 | let b = pin_dir().join("sbpart-pin.obu"); |
| 1969 | let c = std::fs::read(concat!(env!("CARGO_MANIFEST_DIR"), "/../../fixtures/x.obu")); |
| 1970 | } |
| 1971 | "#; |
| 1972 | let mut names: Vec<String> = pins_read(synthetic) |
| 1973 | .iter() |
| 1974 | .map(|r| format!("{}|{}", r.file, r.shape)) |
| 1975 | .collect(); |
| 1976 | names.sort(); |
| 1977 | assert_eq!( |
| 1978 | names, |
| 1979 | vec![ |
| 1980 | "golden3-pin.obu|crate_pin", |
| 1981 | "sbpart-pin.obu|pin_dir_machine_local", |
| 1982 | "x.obu|concat_literal_machine_local", |
| 1983 | ], |
| 1984 | "shape coverage changed: {names:?}" |
| 1985 | ); |
| 1986 | } |
| 1987 | |
| 1988 | |
| 1989 | |
| 1990 | |
| 1991 | #[test] |
| 1992 | fn every_pin_a_gate_reads_is_committed_under_the_crate() { |
| 1993 | let src = include_str!("stream.rs"); |
| 1994 | let reads = pins_read(src); |
| 1995 | assert!( |
| 1996 | reads.len() >= 20, |
| 1997 | "the enumerator found only {} pin reads in stream.rs -- it has lost a shape, \ |
| 1998 | which is the failure mode that let 14 pins go unseen", |
| 1999 | reads.len() |
| 2000 | ); |
| 2001 | let uncommitted: Vec<String> = reads |
| 2002 | .iter() |
| 2003 | .filter(|r| !is_committed(&r.file)) |
| 2004 | .map(|r| format!("{}:{} ({})", r.gate, r.file, r.shape)) |
| 2005 | .collect(); |
| 2006 | assert!( |
| 2007 | uncommitted.is_empty(), |
| 2008 | "{} pin(s) a gate reads have no committed copy under \ |
| 2009 | crates/ec-av1/fixtures/ -- each resolves only on a machine whose \ |
| 2010 | gitignored root fixtures/ happens to be populated, so a clean checkout \ |
| 2011 | silently skips or panics:\n {}", |
| 2012 | uncommitted.len(), |
| 2013 | uncommitted.join("\n ") |
| 2014 | ); |
| 2015 | let machine_local: Vec<String> = reads |
| 2016 | .iter() |
| 2017 | .filter(|r| !super::pin_inventory::resolves_committed(r.shape)) |
| 2018 | .map(|r| format!("{} reads {} via {}", r.gate, r.file, r.shape)) |
| 2019 | .collect(); |
| 2020 | assert!( |
| 2021 | machine_local.is_empty(), |
| 2022 | "{} pin read(s) still resolve through a machine-local shape instead of \ |
| 2023 | `crate_pin` (the committed crate-local copy), so they break on a clean \ |
| 2024 | checkout:\n {}", |
| 2025 | machine_local.len(), |
| 2026 | machine_local.join("\n ") |
| 2027 | ); |
| 2028 | } |
| 2029 | } |
| 2030 | |
| 2031 | |
| 2032 | |
| 2033 | |
| 2034 | |
| 2035 | |
| 2036 | |
| 2037 | |
| 2038 | |
| 2039 | |
| 2040 | |
| 2041 | |
| 2042 | |
| 2043 | |
| 2044 | |
| 2045 | |
| 2046 | |
| 2047 | |
| 2048 | |
| 2049 | |
| 2050 | |
| 2051 | |
| 2052 | |
| 2053 | |
| 2054 | |
| 2055 | |
| 2056 | |
| 2057 | |
| 2058 | |
| 2059 | |
| 2060 | |
| 2061 | #[cfg(test)] |
| 2062 | mod count_vacuity { |
| 2063 | |
| 2064 | pub struct Site { |
| 2065 | pub line: usize, |
| 2066 | pub gate: String, |
| 2067 | pub count_expr: String, |
| 2068 | |
| 2069 | |
| 2070 | |
| 2071 | pub pinned_by: Option<String>, |
| 2072 | |
| 2073 | |
| 2074 | pub pixel_comparing: bool, |
| 2075 | |
| 2076 | |
| 2077 | |
| 2078 | |
| 2079 | |
| 2080 | |
| 2081 | |
| 2082 | |
| 2083 | pub bucket: Option<&'static str>, |
| 2084 | pub bucket_reason: String, |
| 2085 | |
| 2086 | |
| 2087 | pub candidate: Option<String>, |
| 2088 | |
| 2089 | |
| 2090 | |
| 2091 | |
| 2092 | pub fixable_from: Option<String>, |
| 2093 | } |
| 2094 | |
| 2095 | |
| 2096 | |
| 2097 | |
| 2098 | |
| 2099 | |
| 2100 | |
| 2101 | |
| 2102 | |
| 2103 | |
| 2104 | |
| 2105 | |
| 2106 | |
| 2107 | |
| 2108 | |
| 2109 | |
| 2110 | |
| 2111 | pub const CALLEES: &[&str] = &[ |
| 2112 | "ffmpeg_decode_sequence(", |
| 2113 | "ffmpeg_decode_sequence_10bit(", |
| 2114 | "ffmpeg_decode_sequence_444(", |
| 2115 | ]; |
| 2116 | |
| 2117 | |
| 2118 | |
| 2119 | |
| 2120 | |
| 2121 | |
| 2122 | |
| 2123 | pub const PROBE_SUBSTRINGS: &[&str] = |
| 2124 | &["census", "probe", "sweep", "first_diff", "scratch", "rectx"]; |
| 2125 | |
| 2126 | |
| 2127 | |
| 2128 | |
| 2129 | |
| 2130 | |
| 2131 | |
| 2132 | |
| 2133 | |
| 2134 | |
| 2135 | |
| 2136 | pub const ENC_HELPERS: &[&str] = &[ |
| 2137 | "encode_aomenc_stream", |
| 2138 | "screen_intrabc_stream_at_depth", |
| 2139 | "screen_intrabc_stream_with", |
| 2140 | "libaom_encode_with", |
| 2141 | "libaom_encode", |
| 2142 | "run_multi_tile_gate", |
| 2143 | "edge32_gate", |
| 2144 | "rect_tx_tool_gate", |
| 2145 | "warp_gate", |
| 2146 | ]; |
| 2147 | |
| 2148 | |
| 2149 | |
| 2150 | |
| 2151 | pub const FIXABLE_CANDS: &[&str] = &["FRAMES", "frame_count"]; |
| 2152 | |
| 2153 | |
| 2154 | |
| 2155 | |
| 2156 | |
| 2157 | pub const PIN_WINDOW: usize = 14; |
| 2158 | |
| 2159 | |
| 2160 | |
| 2161 | pub const AOMENC_LEAF: &str = "aomenc_path()"; |
| 2162 | |
| 2163 | |
| 2164 | pub const DISK_READ_LEAVES: &[&str] = &["fs::read", "crate_pin"]; |
| 2165 | |
| 2166 | |
| 2167 | fn is_probe(gate: &str) -> bool { |
| 2168 | PROBE_SUBSTRINGS.iter().any(|m| gate.contains(m)) |
| 2169 | } |
| 2170 | |
| 2171 | |
| 2172 | fn enc_helpers(body: &str) -> Vec<&'static str> { |
| 2173 | ENC_HELPERS |
| 2174 | .iter() |
| 2175 | .copied() |
| 2176 | .filter(|n| body.contains(&format!("{n}("))) |
| 2177 | .collect() |
| 2178 | } |
| 2179 | |
| 2180 | |
| 2181 | |
| 2182 | |
| 2183 | |
| 2184 | |
| 2185 | |
| 2186 | pub fn fn_body_end(src: &str, fn_start: usize) -> usize { |
| 2187 | let bytes = src.as_bytes(); |
| 2188 | let mut depth = 0i32; |
| 2189 | let mut started = false; |
| 2190 | let mut fn_end = src.len(); |
| 2191 | let mut in_str = false; |
| 2192 | let mut in_line_comment = false; |
| 2193 | let mut k = fn_start; |
| 2194 | while k < bytes.len() { |
| 2195 | let c = bytes[k]; |
| 2196 | if in_line_comment { |
| 2197 | if c == b'\n' { |
| 2198 | in_line_comment = false; |
| 2199 | } |
| 2200 | k += 1; |
| 2201 | continue; |
| 2202 | } |
| 2203 | if in_str { |
| 2204 | if c == b'\\' { |
| 2205 | k += 2; |
| 2206 | continue; |
| 2207 | } |
| 2208 | if c == b'"' { |
| 2209 | in_str = false; |
| 2210 | } |
| 2211 | k += 1; |
| 2212 | continue; |
| 2213 | } |
| 2214 | match c { |
| 2215 | b'"' => in_str = true, |
| 2216 | b'/' if bytes.get(k + 1) == Some(&b'/') => { |
| 2217 | in_line_comment = true; |
| 2218 | k += 1; |
| 2219 | } |
| 2220 | b'{' => { |
| 2221 | depth += 1; |
| 2222 | started = true; |
| 2223 | } |
| 2224 | b'}' => { |
| 2225 | depth -= 1; |
| 2226 | if started && depth == 0 { |
| 2227 | fn_end = k + 1; |
| 2228 | break; |
| 2229 | } |
| 2230 | } |
| 2231 | _ => {} |
| 2232 | } |
| 2233 | k += 1; |
| 2234 | } |
| 2235 | fn_end |
| 2236 | } |
| 2237 | |
| 2238 | |
| 2239 | |
| 2240 | pub fn fn_slices(src: &str) -> Vec<(String, &str)> { |
| 2241 | let lines: Vec<&str> = src.lines().collect(); |
| 2242 | let mut offsets: Vec<usize> = Vec::with_capacity(lines.len()); |
| 2243 | { |
| 2244 | let mut o = 0usize; |
| 2245 | for l in &lines { |
| 2246 | offsets.push(o); |
| 2247 | o += l.len() + 1; |
| 2248 | } |
| 2249 | } |
| 2250 | let mut out: Vec<(String, &str)> = Vec::new(); |
| 2251 | for (i, line) in lines.iter().enumerate() { |
| 2252 | let t = line.trim_start(); |
| 2253 | let Some(rest) = t.strip_prefix("fn ") else { |
| 2254 | continue; |
| 2255 | }; |
| 2256 | let Some(nm) = rest.split('(').next() else { |
| 2257 | continue; |
| 2258 | }; |
| 2259 | if nm.contains(' ') { |
| 2260 | continue; |
| 2261 | } |
| 2262 | let at = offsets[i]; |
| 2263 | let end = fn_body_end(src, at); |
| 2264 | out.push((nm.to_string(), &src[at..end])); |
| 2265 | } |
| 2266 | out |
| 2267 | } |
| 2268 | |
| 2269 | pub fn sites(src: &str) -> Vec<Site> { |
| 2270 | let mut out = Vec::new(); |
| 2271 | let lines: Vec<&str> = src.lines().collect(); |
| 2272 | |
| 2273 | |
| 2274 | |
| 2275 | |
| 2276 | let mut offsets: Vec<usize> = Vec::with_capacity(lines.len()); |
| 2277 | { |
| 2278 | let mut o = 0usize; |
| 2279 | for l in &lines { |
| 2280 | offsets.push(o); |
| 2281 | o += l.len() + 1; |
| 2282 | } |
| 2283 | } |
| 2284 | let mut gate = String::new(); |
| 2285 | let mut fn_start = 0usize; |
| 2286 | for (i, line) in lines.iter().enumerate() { |
| 2287 | let t = line.trim_start(); |
| 2288 | if let Some(rest) = t.strip_prefix("fn ") { |
| 2289 | if let Some(n) = rest.split('(').next() { |
| 2290 | if !n.contains(' ') { |
| 2291 | gate = n.to_string(); |
| 2292 | fn_start = offsets[i]; |
| 2293 | } |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | |
| 2298 | |
| 2299 | |
| 2300 | |
| 2301 | |
| 2302 | |
| 2303 | |
| 2304 | |
| 2305 | |
| 2306 | |
| 2307 | |
| 2308 | if t.starts_with("//") { |
| 2309 | continue; |
| 2310 | } |
| 2311 | for callee in CALLEES { |
| 2312 | let Some(at) = line.find(callee) else { |
| 2313 | continue; |
| 2314 | }; |
| 2315 | let args = &line[at + callee.len()..]; |
| 2316 | |
| 2317 | |
| 2318 | |
| 2319 | let mut depth = 1usize; |
| 2320 | let mut close = None; |
| 2321 | for (k, ch) in args.char_indices() { |
| 2322 | match ch { |
| 2323 | '(' => depth += 1, |
| 2324 | ')' => { |
| 2325 | depth -= 1; |
| 2326 | if depth == 0 { |
| 2327 | close = Some(k); |
| 2328 | break; |
| 2329 | } |
| 2330 | } |
| 2331 | _ => {} |
| 2332 | } |
| 2333 | } |
| 2334 | let Some(close) = close else { continue }; |
| 2335 | let arglist = &args[..close]; |
| 2336 | let last = arglist.rsplit(',').next().unwrap_or("").trim(); |
| 2337 | if !(last.ends_with(".len()") || last.ends_with(".count()")) { |
| 2338 | continue; |
| 2339 | } |
| 2340 | |
| 2341 | |
| 2342 | |
| 2343 | let binding = last.trim_end_matches(".len()").trim_end_matches(".count()"); |
| 2344 | |
| 2345 | |
| 2346 | |
| 2347 | |
| 2348 | |
| 2349 | let want = format!("{binding}.len()"); |
| 2350 | let mut pinned = None; |
| 2351 | for back in 1..=PIN_WINDOW { |
| 2352 | if i < back { |
| 2353 | break; |
| 2354 | } |
| 2355 | let prev = lines[i - back].trim_start(); |
| 2356 | if !prev.starts_with("assert") || !prev.contains('(') { |
| 2357 | continue; |
| 2358 | } |
| 2359 | |
| 2360 | |
| 2361 | let here = prev.contains(&want); |
| 2362 | let next = lines |
| 2363 | .get(i - back + 1) |
| 2364 | .map(|l| l.trim_start().contains(&want)) |
| 2365 | .unwrap_or(false); |
| 2366 | if here || next { |
| 2367 | pinned = Some(prev.chars().take(60).collect()); |
| 2368 | break; |
| 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | |
| 2373 | |
| 2374 | |
| 2375 | |
| 2376 | |
| 2377 | |
| 2378 | |
| 2379 | |
| 2380 | |
| 2381 | |
| 2382 | |
| 2383 | |
| 2384 | |
| 2385 | |
| 2386 | let fn_end = fn_body_end(src, fn_start); |
| 2387 | let body = &src[fn_start..fn_end]; |
| 2388 | |
| 2389 | |
| 2390 | |
| 2391 | |
| 2392 | |
| 2393 | |
| 2394 | |
| 2395 | |
| 2396 | |
| 2397 | let pixel_comparing = body.contains("assert") |
| 2398 | && (body.contains(".y,") |
| 2399 | || body.contains(".y ==") |
| 2400 | || body.contains(".y)") |
| 2401 | || body.contains("vs ffmpeg")); |
| 2402 | |
| 2403 | let mut fixable = None; |
| 2404 | for cand in FIXABLE_CANDS { |
| 2405 | if body.contains(&format!("const {cand}")) |
| 2406 | || body.contains(&format!("let {cand}")) |
| 2407 | { |
| 2408 | fixable = Some(cand.to_string()); |
| 2409 | break; |
| 2410 | } |
| 2411 | } |
| 2412 | |
| 2413 | |
| 2414 | |
| 2415 | |
| 2416 | |
| 2417 | |
| 2418 | |
| 2419 | |
| 2420 | |
| 2421 | let candidate = enc_helpers(body).first().and_then(|h| { |
| 2422 | let at = body.find(&format!("{h}("))?; |
| 2423 | let open = at + h.len() + 1; |
| 2424 | let seg = &body[open..(open + 400).min(body.len())]; |
| 2425 | let close = seg.find(';')?; |
| 2426 | let nums: Vec<&str> = seg[..close] |
| 2427 | .split(',') |
| 2428 | .map(|x| x.trim()) |
| 2429 | .filter(|x| !x.is_empty() && x.chars().all(|c| c.is_ascii_digit())) |
| 2430 | .collect(); |
| 2431 | nums.last().map(|x| x.to_string()) |
| 2432 | }); |
| 2433 | let (bucket, bucket_reason) = if is_probe(&gate) { |
| 2434 | ( |
| 2435 | Some("c"), |
| 2436 | "census/probe: reports, does not assert a verdict".to_string(), |
| 2437 | ) |
| 2438 | } else if body.contains(AOMENC_LEAF) || !enc_helpers(body).is_empty() { |
| 2439 | ( |
| 2440 | Some("b"), |
| 2441 | "live aomenc encode: count belongs to the encoder".to_string(), |
| 2442 | ) |
| 2443 | } else if DISK_READ_LEAVES.iter().any(|leaf| body.contains(leaf)) { |
| 2444 | ( |
| 2445 | Some("a"), |
| 2446 | "stream read from disk: count belongs to the pin".to_string(), |
| 2447 | ) |
| 2448 | } else { |
| 2449 | ( |
| 2450 | None, |
| 2451 | "unclassified: no aomenc drive, no disk read, and not a probe -- \ |
| 2452 | needs a reader" |
| 2453 | .to_string(), |
| 2454 | ) |
| 2455 | }; |
| 2456 | out.push(Site { |
| 2457 | line: i + 1, |
| 2458 | gate: gate.clone(), |
| 2459 | count_expr: last.to_string(), |
| 2460 | pinned_by: pinned, |
| 2461 | pixel_comparing, |
| 2462 | fixable_from: fixable, |
| 2463 | bucket, |
| 2464 | bucket_reason, |
| 2465 | candidate, |
| 2466 | }); |
| 2467 | } |
| 2468 | } |
| 2469 | out |
| 2470 | } |
| 2471 | |
| 2472 | |
| 2473 | |
| 2474 | |
| 2475 | |
| 2476 | |
| 2477 | |
| 2478 | |
| 2479 | |
| 2480 | |
| 2481 | |
| 2482 | |
| 2483 | |
| 2484 | |
| 2485 | |
| 2486 | #[test] |
| 2487 | fn every_scanner_matcher_fires_on_a_known_site() { |
| 2488 | let src = include_str!("stream.rs"); |
| 2489 | let lines: Vec<&str> = src.lines().collect(); |
| 2490 | let fns = fn_slices(src); |
| 2491 | |
| 2492 | |
| 2493 | |
| 2494 | |
| 2495 | |
| 2496 | |
| 2497 | for callee in CALLEES { |
| 2498 | let hits = lines.iter().filter(|l| l.contains(callee)).count(); |
| 2499 | assert!( |
| 2500 | hits > 0, |
| 2501 | "the callee matcher `{callee}` fires on NO line of stream.rs -- it is dead \ |
| 2502 | coverage: either a gate stopped using that spelling (drop the entry) or a \ |
| 2503 | new one is expected to (add the call site first, not the matcher)" |
| 2504 | ); |
| 2505 | } |
| 2506 | |
| 2507 | |
| 2508 | for sub in PROBE_SUBSTRINGS { |
| 2509 | let hits = fns.iter().filter(|(g, _)| g.contains(sub)).count(); |
| 2510 | assert!( |
| 2511 | hits > 0, |
| 2512 | "the probe substring `{sub}` matches NO fn name in stream.rs -- bucket (c) is \ |
| 2513 | unpopulated and the substring is dead weight" |
| 2514 | ); |
| 2515 | } |
| 2516 | |
| 2517 | |
| 2518 | for helper in ENC_HELPERS { |
| 2519 | let hits = fns |
| 2520 | .iter() |
| 2521 | .filter(|(_, b)| b.contains(&format!("{helper}("))) |
| 2522 | .count(); |
| 2523 | assert!( |
| 2524 | hits > 0, |
| 2525 | "the encode-helper matcher `{helper}(` matches NO fn body in stream.rs -- it is \ |
| 2526 | dead coverage (grep the crate: if the helper has no caller, delete the entry; if \ |
| 2527 | it has a caller outside stream.rs, say so here and name the file)" |
| 2528 | ); |
| 2529 | } |
| 2530 | |
| 2531 | |
| 2532 | for cand in FIXABLE_CANDS { |
| 2533 | let hits = fns |
| 2534 | .iter() |
| 2535 | .filter(|(_, b)| { |
| 2536 | b.contains(&format!("const {cand}")) || b.contains(&format!("let {cand}")) |
| 2537 | }) |
| 2538 | .count(); |
| 2539 | assert!( |
| 2540 | hits > 0, |
| 2541 | "the spec-binding matcher `{cand}` matches NO fn body in stream.rs -- `fixable_from` \ |
| 2542 | can never name it, so it is dead coverage" |
| 2543 | ); |
| 2544 | } |
| 2545 | |
| 2546 | |
| 2547 | assert!( |
| 2548 | fns.iter().any(|(_, b)| b.contains(AOMENC_LEAF)), |
| 2549 | "no fn body in stream.rs calls `{AOMENC_LEAF}`, so the bucket-(b) direct-encode leaf \ |
| 2550 | never fires" |
| 2551 | ); |
| 2552 | for leaf in DISK_READ_LEAVES { |
| 2553 | let hits = fns.iter().filter(|(_, b)| b.contains(leaf)).count(); |
| 2554 | assert!( |
| 2555 | hits > 0, |
| 2556 | "the disk-read leaf `{leaf}` matches NO fn body in stream.rs -- bucket (a) is \ |
| 2557 | unpopulated by that spelling" |
| 2558 | ); |
| 2559 | } |
| 2560 | |
| 2561 | |
| 2562 | |
| 2563 | |
| 2564 | let s = sites(src); |
| 2565 | let pinned = s.iter().filter(|x| x.pinned_by.is_some()).count(); |
| 2566 | assert!( |
| 2567 | PIN_WINDOW > 0 && pinned > 0, |
| 2568 | "PIN_WINDOW={PIN_WINDOW} pins {pinned} of {} site(s) -- the window must be the one \ |
| 2569 | the sweep walks and must still pin at least one site", |
| 2570 | s.len() |
| 2571 | ); |
| 2572 | } |
| 2573 | } |
| 2574 | |
| 2575 | #[cfg(test)] |
| 2576 | mod count_vacuity_tests { |
| 2577 | use super::count_vacuity::sites; |
| 2578 | |
| 2579 | |
| 2580 | |
| 2581 | #[test] |
| 2582 | fn the_count_vacuity_sweep_finds_the_known_sites() { |
| 2583 | let src = include_str!("stream.rs"); |
| 2584 | let s = sites(src); |
| 2585 | assert!( |
| 2586 | s.len() >= 40, |
| 2587 | "the count-vacuity sweep found only {} site(s); expected 40+ -- it has lost a \ |
| 2588 | shape", |
| 2589 | s.len() |
| 2590 | ); |
| 2591 | |
| 2592 | |
| 2593 | |
| 2594 | |
| 2595 | |
| 2596 | |
| 2597 | |
| 2598 | |
| 2599 | |
| 2600 | const DECODE_DERIVED: &[&str] = &["pictures.len()", "decoded.len()", "frames.len()"]; |
| 2601 | assert!( |
| 2602 | !s.iter().any(|x| x.gate |
| 2603 | == "a_real_aomenc_segmentation_stream_with_map_inheritance_decodes_pixel_exact" |
| 2604 | && DECODE_DERIVED.contains(&x.count_expr.as_str())), |
| 2605 | "the r7 fix regressed: {} is passing a decode-derived count as the oracle's \ |
| 2606 | expected count again -- our decode's length is the thing UNDER TEST and \ |
| 2607 | must never be the expected value", |
| 2608 | "a_real_aomenc_segmentation_stream_with_map_inheritance_decodes_pixel_exact" |
| 2609 | ); |
| 2610 | |
| 2611 | |
| 2612 | |
| 2613 | |
| 2614 | |
| 2615 | |
| 2616 | |
| 2617 | |
| 2618 | |
| 2619 | |
| 2620 | |
| 2621 | |
| 2622 | |
| 2623 | |
| 2624 | |
| 2625 | |
| 2626 | |
| 2627 | |
| 2628 | |
| 2629 | |
| 2630 | |
| 2631 | |
| 2632 | |
| 2633 | |
| 2634 | |
| 2635 | |
| 2636 | |
| 2637 | |
| 2638 | |
| 2639 | |
| 2640 | |
| 2641 | |
| 2642 | const UNPINNED_CEILING: usize = 0; |
| 2643 | let unpinned = s.iter().filter(|x| x.pinned_by.is_none()).count(); |
| 2644 | assert!( |
| 2645 | unpinned <= UNPINNED_CEILING, |
| 2646 | "{unpinned} unpinned count site(s), ceiling is {UNPINNED_CEILING} -- a new one \ |
| 2647 | was added (lower the ceiling only when a site is actually fixed)" |
| 2648 | ); |
| 2649 | assert!( |
| 2650 | s.iter().any(|x| x.count_expr == "frames.len()"), |
| 2651 | "the dominant `frames.len()` shape is no longer found -- the sweep has lost a \ |
| 2652 | shape, not gained a fix" |
| 2653 | ); |
| 2654 | } |
| 2655 | |
| 2656 | |
| 2657 | |
| 2658 | |
| 2659 | |
| 2660 | |
| 2661 | #[test] |
| 2662 | fn every_locally_derived_oracle_count_is_reported() { |
| 2663 | let src = include_str!("stream.rs"); |
| 2664 | let s = sites(src); |
| 2665 | let unpinned: Vec<String> = s |
| 2666 | .iter() |
| 2667 | .filter(|x| x.pinned_by.is_none()) |
| 2668 | .map(|x| { |
| 2669 | format!( |
| 2670 | "stream.rs:{} | {} | {} | NOT SPEC-PINNED", |
| 2671 | x.line, x.gate, x.count_expr |
| 2672 | ) |
| 2673 | }) |
| 2674 | .collect(); |
| 2675 | let pinned = s.len() - unpinned.len(); |
| 2676 | eprintln!( |
| 2677 | "count-vacuity sweep: {} site(s); {} spec-pinned by a prior assert, {} not", |
| 2678 | s.len(), |
| 2679 | pinned, |
| 2680 | unpinned.len() |
| 2681 | ); |
| 2682 | for line in unpinned.iter() { |
| 2683 | eprintln!(" {line}"); |
| 2684 | } |
| 2685 | |
| 2686 | for x in s.iter().filter(|x| x.pinned_by.is_none()) { |
| 2687 | eprintln!( |
| 2688 | "ROW {} | {} | {} | bucket={} | candidate={} | spec_in_scope={} | {} | \ |
| 2689 | pixel_comparing={}", |
| 2690 | x.line, |
| 2691 | x.gate, |
| 2692 | x.count_expr, |
| 2693 | x.bucket.unwrap_or("?"), |
| 2694 | x.candidate.clone().unwrap_or_else(|| "-".into()), |
| 2695 | x.fixable_from.clone().unwrap_or_else(|| "-".into()), |
| 2696 | x.bucket_reason, |
| 2697 | x.pixel_comparing |
| 2698 | ); |
| 2699 | } |
| 2700 | assert!( |
| 2701 | !s.is_empty() && s.iter().all(|x| !x.gate.is_empty()), |
| 2702 | "a site has no owning gate -- the sweep's gate attribution is broken" |
| 2703 | ); |
| 2704 | } |
| 2705 | |
| 2706 | |
| 2707 | |
| 2708 | |
| 2709 | |
| 2710 | |
| 2711 | |
| 2712 | |
| 2713 | |
| 2714 | |
| 2715 | |
| 2716 | |
| 2717 | |
| 2718 | |
| 2719 | |
| 2720 | |
| 2721 | |
| 2722 | |
| 2723 | |
| 2724 | #[test] |
| 2725 | fn a_doc_commented_count_call_is_not_a_site_but_real_code_still_is() { |
| 2726 | let commented = r#" |
| 2727 | /// The old body passed the wrong count to the oracle: |
| 2728 | /// let reference = ffmpeg_decode_sequence(&stream, 192, 128, pics.len()); |
| 2729 | /// so a decoder that emitted zero frames asked ffmpeg for zero frames. |
| 2730 | fn a_gate_whose_doc_comment_quotes_the_old_body() { |
| 2731 | let pics = decode_stream(&data).unwrap(); |
| 2732 | } |
| 2733 | "#; |
| 2734 | let commented_sites = sites(commented); |
| 2735 | assert!( |
| 2736 | commented_sites.is_empty(), |
| 2737 | "a count call inside a `///` doc comment was counted as a site: {:?} -- a line-based \ |
| 2738 | scan cannot tell prose from code, and this row is why it now skips comment lines", |
| 2739 | commented_sites |
| 2740 | .iter() |
| 2741 | .map(|x| (x.line, x.count_expr.clone())) |
| 2742 | .collect::<Vec<_>>() |
| 2743 | ); |
| 2744 | |
| 2745 | let real = r#" |
| 2746 | fn a_gate_that_really_spends_its_own_decode_length() { |
| 2747 | let pics = decode_stream(&data).unwrap(); |
| 2748 | assert_eq!(pics.len(), 7, "{NAME}: expected seven key frames"); |
| 2749 | let reference = ffmpeg_decode_sequence(&stream, 192, 128, pics.len()); |
| 2750 | } |
| 2751 | "#; |
| 2752 | let found = sites(real); |
| 2753 | assert_eq!( |
| 2754 | found.len(), |
| 2755 | 1, |
| 2756 | "the control did not find its one real site: {:?} -- the comment skip is over-broad \ |
| 2757 | if the same call is skipped as code", |
| 2758 | found |
| 2759 | .iter() |
| 2760 | .map(|x| (x.line, x.count_expr.clone(), x.gate.clone())) |
| 2761 | .collect::<Vec<_>>() |
| 2762 | ); |
| 2763 | assert_eq!( |
| 2764 | found[0].count_expr, "pics.len()", |
| 2765 | "control: wrong count expr" |
| 2766 | ); |
| 2767 | assert_eq!( |
| 2768 | found[0].gate, "a_gate_that_really_spends_its_own_decode_length", |
| 2769 | "control: wrong gate attribution" |
| 2770 | ); |
| 2771 | assert!( |
| 2772 | found[0].pinned_by.is_some(), |
| 2773 | "the control's site is UNPINNED: the comment skip changed the pin window too, \ |
| 2774 | because a `//` line between an assert and its call is still walked" |
| 2775 | ); |
| 2776 | } |
| 2777 | } |