| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | use std::path::PathBuf; |
| 10 | |
| 11 | use ec_subs::{SourceFormat, plain_text}; |
| 12 | |
| 13 | fn fixture(name: &str) -> Option<Vec<u8>> { |
| 14 | let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 15 | .join("../../fixtures/subs") |
| 16 | .join(name); |
| 17 | match std::fs::read(&path) { |
| 18 | Ok(bytes) => Some(bytes), |
| 19 | Err(_) => { |
| 20 | eprintln!("skipping: no {name} — run scripts/gen-subtitle-fixtures.sh"); |
| 21 | None |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | #[test] |
| 27 | fn a_real_subrip_track_parses_whole() { |
| 28 | let Some(bytes) = fixture("real.srt") else { |
| 29 | return; |
| 30 | }; |
| 31 | let track = ec_subs::srt::parse(&bytes).unwrap(); |
| 32 | assert_eq!(track.source, Some(SourceFormat::Srt)); |
| 33 | assert!(track.cues.len() > 100, "{} cues", track.cues.len()); |
| 34 | assert_eq!(track.skipped, 0, "a whole file should have no torn blocks"); |
| 35 | |
| 36 | for cue in &track.cues { |
| 37 | assert!(cue.end_us >= cue.start_us); |
| 38 | assert!( |
| 39 | !plain_text(&cue.segments).trim().is_empty(), |
| 40 | "empty cue at {}us", |
| 41 | cue.start_us |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | assert!( |
| 46 | track |
| 47 | .cues |
| 48 | .windows(2) |
| 49 | .all(|w| w[1].start_us >= w[0].start_us) |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | #[test] |
| 54 | fn a_real_ass_script_parses_whole() { |
| 55 | let Some(bytes) = fixture("real.ass") else { |
| 56 | return; |
| 57 | }; |
| 58 | let track = ec_ass::parse(&bytes).unwrap(); |
| 59 | assert_eq!(track.source, Some(SourceFormat::AssOrSsa)); |
| 60 | assert!(!track.styles.is_empty(), "a script declares styles"); |
| 61 | assert!(track.cues.len() > 100, "{} cues", track.cues.len()); |
| 62 | assert_eq!(track.skipped, 0, "a whole script should have no torn rows"); |
| 63 | |
| 64 | |
| 65 | let with_text = track |
| 66 | .cues |
| 67 | .iter() |
| 68 | .filter(|c| !plain_text(&c.segments).trim().is_empty()) |
| 69 | .count(); |
| 70 | assert!( |
| 71 | with_text * 10 > track.cues.len() * 9, |
| 72 | "{with_text} of {}", |
| 73 | track.cues.len() |
| 74 | ); |
| 75 | for cue in &track.cues { |
| 76 | assert!(cue.end_us >= cue.start_us); |
| 77 | if let Some(style) = &cue.style_ref { |
| 78 | assert!( |
| 79 | track.style(style).is_some(), |
| 80 | "cue names undeclared style {style:?}" |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | let text = plain_text(&cue.segments); |
| 85 | assert!(!text.contains("{\\"), "override tag in text: {text:?}"); |
| 86 | } |
| 87 | |
| 88 | assert!(track.extradata.windows(8).any(|w| w == b"[Events]")); |
| 89 | } |
| 90 | |
| 91 | #[test] |
| 92 | fn a_real_pgs_stream_decodes_to_plausible_bitmaps() { |
| 93 | let Some(bytes) = fixture("pgs-1080p.sup") else { |
| 94 | return; |
| 95 | }; |
| 96 | let mut decoder = ec_pgs::PgsDecoder::new(); |
| 97 | decoder.push(&bytes).unwrap(); |
| 98 | let mut frames = 0; |
| 99 | let mut painted = 0; |
| 100 | let mut coverage_max = 0.0f64; |
| 101 | while let Some(frame) = decoder.take_frame() { |
| 102 | frames += 1; |
| 103 | |
| 104 | assert_eq!((frame.width, frame.height), (1920, 1080)); |
| 105 | let stride = frame.planes[0].stride; |
| 106 | assert_eq!(stride, 1920 * 4); |
| 107 | let opaque = frame.planes[0] |
| 108 | .data |
| 109 | .as_chunks::<4>() |
| 110 | .0 |
| 111 | .iter() |
| 112 | .filter(|p| p[3] > 0) |
| 113 | .count(); |
| 114 | if opaque == 0 { |
| 115 | |
| 116 | continue; |
| 117 | } |
| 118 | painted += 1; |
| 119 | let coverage = opaque as f64 / (1920.0 * 1080.0); |
| 120 | coverage_max = coverage_max.max(coverage); |
| 121 | |
| 122 | assert!( |
| 123 | coverage > 0.000_1 && coverage < 0.25, |
| 124 | "display set {frames} covers {:.3}% of the canvas", |
| 125 | coverage * 100.0 |
| 126 | ); |
| 127 | |
| 128 | let rows = (0..1080) |
| 129 | .filter(|y| { |
| 130 | frame.planes[0].data[y * stride..(y + 1) * stride] |
| 131 | .as_chunks::<4>() |
| 132 | .0 |
| 133 | .iter() |
| 134 | .any(|p| p[3] > 0) |
| 135 | }) |
| 136 | .count(); |
| 137 | assert!(rows > 4, "display set {frames} paints {rows} rows"); |
| 138 | } |
| 139 | assert!(frames > 100, "{frames} display sets"); |
| 140 | assert!( |
| 141 | painted * 3 > frames, |
| 142 | "{painted} of {frames} display sets painted anything" |
| 143 | ); |
| 144 | assert!(coverage_max < 0.25, "widest cue covers {coverage_max}"); |
| 145 | eprintln!("pgs: {frames} display sets, {painted} painted, widest {coverage_max:.4}"); |
| 146 | } |