forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust381.5 MiBb0cfde8Tahinli12 hours ago4261 commits

cli.rs129 lines, 4.1 KiBHistoryRawDownload
1//! Self-test of the `oracle` CLI itself: drives the built binary the way a codec
2//! crate's test will, using ffmpeg as both the "our decoder" stand-in and the
3//! reference. Exercises the PASS and the FAIL path of every subcommand.
4//!
5
6use std::path::{Path, PathBuf};
7use std::process::{Command, Stdio};
8
9// Per-test directory: the test binary runs its cases as threads of one process,
10// so a pid-only name would let one case delete another's raw files.
11fn workdir(name: &str) -> PathBuf {
12 let d = std::env::temp_dir().join(format!("oracle-selftest-{}-{name}", std::process::id()));
13 std::fs::create_dir_all(&d).unwrap();
14 d
15}
16
17/// Decode `input` to a raw file with ffmpeg — stands in for "our decoder's output".
18fn decode_raw(input: &Path, out: &Path, extra: &[&str]) {
19 let f = std::fs::File::create(out).unwrap();
20 let st = ec_testkit::ffmpeg()
21 .args(["-nostdin", "-y", "-v", "error", "-i"])
22 .arg(input)
23 .args(extra)
24 .arg("-")
25 .stdout(Stdio::from(f))
26 .status()
27 .expect("ffmpeg must be installed to run the oracle self-test");
28 assert!(st.success(), "ffmpeg failed decoding {}", input.display());
29}
30
31fn oracle(args: &[&str]) -> i32 {
32 Command::new(env!("CARGO_BIN_EXE_oracle"))
33 .args(args)
34 .status()
35 .expect("oracle binary")
36 .code()
37 .expect("oracle exited by signal")
38}
39
40const F32: &[&str] = &["-map", "0:a:0", "-f", "f32le", "-acodec", "pcm_f32le"];
41
42#[test]
43fn bit_exact_passes_on_same_decode_and_fails_on_a_different_one() {
44 let a = ec_testkit::fixture("audio/flac-stereo-48000.flac");
45 let b = ec_testkit::fixture("audio/flac-mono-48000.flac");
46 let w = workdir("bitexact");
47 let (r1, r2, r3) = (w.join("a1.raw"), w.join("a2.raw"), w.join("b.raw"));
48 decode_raw(&a, &r1, F32);
49 decode_raw(&a, &r2, F32);
50 decode_raw(&b, &r3, F32);
51
52 assert_eq!(
53 oracle(&["bit-exact", r1.to_str().unwrap(), r2.to_str().unwrap()]),
54 0,
55 "same file decoded twice must be bit-exact"
56 );
57 assert_eq!(
58 oracle(&["bit-exact", r1.to_str().unwrap(), r3.to_str().unwrap()]),
59 1,
60 "different fixtures must not be bit-exact"
61 );
62 let _ = std::fs::remove_dir_all(&w);
63}
64
65#[test]
66fn audio_compare_passes_against_its_own_source_and_fails_across_rates() {
67 let a = ec_testkit::fixture("audio/flac-stereo-48000.flac");
68 let b = ec_testkit::fixture("audio/flac-stereo-44100.flac");
69 let w = workdir("audio");
70 let (ra, rb) = (w.join("a.raw"), w.join("b.raw"));
71 decode_raw(&a, &ra, F32);
72 decode_raw(&b, &rb, F32);
73
74 assert_eq!(
75 oracle(&["audio-compare", ra.to_str().unwrap(), a.to_str().unwrap()]),
76 0,
77 "lossless decode vs its own source must correlate at 1.0"
78 );
79 // 44.1 kHz samples against a 48 kHz reference: wrong length and wrong phase.
80 assert_eq!(
81 oracle(&["audio-compare", rb.to_str().unwrap(), a.to_str().unwrap()]),
82 1,
83 "a different-rate decode must be rejected"
84 );
85 let _ = std::fs::remove_dir_all(&w);
86}
87
88#[test]
89fn video_compare_passes_against_its_own_source_and_fails_on_frame_count() {
90 let a = ec_testkit::fixture("video/h264-1080p-23.976-8bit.mp4");
91 let b = ec_testkit::fixture("video/h264-1080p-60-8bit.mp4");
92 let w = workdir("video");
93 let raw = w.join("a.raw");
94 decode_raw(
95 &a,
96 &raw,
97 &["-map", "0:v:0", "-f", "rawvideo", "-pix_fmt", "yuv420p"],
98 );
99
100 assert_eq!(
101 oracle(&[
102 "video-compare",
103 raw.to_str().unwrap(),
104 a.to_str().unwrap(),
105 "--pix-fmt",
106 "yuv420p",
107 ]),
108 0,
109 "decode vs its own source must be lossless (infinite PSNR)"
110 );
111 assert_eq!(
112 oracle(&[
113 "video-compare",
114 raw.to_str().unwrap(),
115 b.to_str().unwrap(),
116 "--pix-fmt",
117 "yuv420p",
118 ]),
119 1,
120 "48 frames against a 120-frame reference must fail"
121 );
122 let _ = std::fs::remove_dir_all(&w);
123}
124
125#[test]
126fn bad_usage_exits_two() {
127 assert_eq!(oracle(&["no-such-subcommand"]), 2);
128 assert_eq!(oracle(&["bit-exact", "only-one-arg"]), 2);
129}