mediaframe 0.5.0

A common media-stream descriptor vocabulary (pixel-format, colour, and frame metadata for video — audio/subtitle to follow) for media processing pipelines.
Documentation
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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
343
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
use super::*;

#[test]
fn dimensions_construction_and_accessors() {
  let d = Dimensions::new(1920, 1080);
  assert_eq!(d.width(), 1920);
  assert_eq!(d.height(), 1080);
  assert!(!d.is_zero());
  assert!(Dimensions::default().is_zero());
}

#[test]
fn dimensions_builder() {
  let d = Dimensions::new(0, 0).with_width(640).with_height(480);
  assert_eq!(d.width(), 640);
  assert_eq!(d.height(), 480);
}

#[cfg(feature = "std")]
#[test]
fn dimensions_display() {
  assert_eq!(std::format!("{}", Dimensions::new(1920, 1080)), "1920x1080");
}

#[test]
fn rect_construction_and_accessors() {
  let r = Rect::new(10, 20, 1280, 720);
  assert_eq!(r.x(), 10);
  assert_eq!(r.y(), 20);
  assert_eq!(r.width(), 1280);
  assert_eq!(r.height(), 720);
}

#[test]
fn rect_builder_chains() {
  let r = Rect::default()
    .with_x(8)
    .with_y(8)
    .with_width(640)
    .with_height(360);
  assert_eq!((r.x(), r.y(), r.width(), r.height()), (8, 8, 640, 360));
}

#[test]
fn rotation_defaults_and_as_str() {
  assert!(matches!(Rotation::default(), Rotation::D0));
  assert_eq!(Rotation::D0.as_str(), "0");
  assert_eq!(Rotation::D90.as_str(), "90");
  assert_eq!(Rotation::D180.as_str(), "180");
  assert_eq!(Rotation::D270.as_str(), "270");
  assert!(Rotation::D90.is_d_90());
}

#[test]
fn rotation_u32_round_trip_and_escape() {
  for r in [Rotation::D0, Rotation::D90, Rotation::D180, Rotation::D270] {
    assert_eq!(Rotation::from_u32(r.to_u32().unwrap()), Some(r));
  }
  assert_eq!(Rotation::from_u32(0), Some(Rotation::D0));
  assert_eq!(Rotation::from_u32(3), Some(Rotation::D270));
  // Unrecognised → rejected, never a silent collapse to D0.
  assert_eq!(Rotation::from_u32(99), None);
}

/// The escape carries a name, and has no numeric spelling. Needs the
/// allocator — at the no-alloc tier the vocabulary is closed.
#[cfg(any(feature = "std", feature = "alloc"))]
#[test]
fn rotation_escape_keeps_its_name() {
  let odd = Rotation::other("45");
  assert_eq!(odd.as_str(), "45");
  assert_eq!(odd.to_u32(), None);
  assert_eq!("45".parse(), Ok(odd));
}

#[test]
fn sample_aspect_ratio_default_is_square() {
  let s = SampleAspectRatio::default();
  assert_eq!(s.num(), 1);
  assert_eq!(s.den().get(), 1);
  assert!(s.is_square());
}

#[test]
fn sample_aspect_ratio_construction_and_builders() {
  let nz = |n: i64| core::num::NonZeroI64::new(n).unwrap();
  let s = SampleAspectRatio::new(40, nz(33));
  assert_eq!(s.num(), 40);
  assert_eq!(s.den().get(), 33);
  assert!(!s.is_square());
  let s2 = SampleAspectRatio::default().with_num(16).with_den(nz(9));
  assert_eq!((s2.num(), s2.den().get()), (16, 9));
  let mut s3 = SampleAspectRatio::default();
  s3.set_num(4).set_den(nz(3));
  assert_eq!((s3.num(), s3.den().get()), (4, 3));
}

#[cfg(feature = "std")]
#[test]
fn sample_aspect_ratio_display() {
  let nz = core::num::NonZeroI64::new(11).unwrap();
  assert_eq!(std::format!("{}", SampleAspectRatio::new(10, nz)), "10:11");
}

#[test]
fn plane_holds_owned_buffer() {
  let p: Plane<[u8; 4]> = Plane::new([1, 2, 3, 4], 4);
  assert_eq!(p.stride(), 4);
  assert_eq!(p.data_ref(), &[1, 2, 3, 4]);
  let raw = p.into_data();
  assert_eq!(raw, [1, 2, 3, 4]);
}

#[test]
fn plane_holds_borrowed_buffer() {
  let backing = [10u8, 20, 30, 40];
  let p: Plane<&[u8]> = Plane::new(&backing[..], 2);
  assert_eq!(p.stride(), 2);
  assert_eq!(*p.data_ref(), &[10, 20, 30, 40][..]);
}

#[test]
fn plane_with_stride_builder() {
  let p = Plane::new([0u8; 2], 0).with_stride(64);
  assert_eq!(p.stride(), 64);
}

// ---------- VideoFrame -------------------------------------------------

use crate::{color::Info, pixel_format::PixelFormat};

#[test]
fn video_frame_construction_defaults() {
  let planes: [Plane<&[u8]>; 4] = [
    Plane::new(&[][..], 16),
    Plane::new(&[][..], 8),
    Plane::new(&[][..], 8),
    Plane::new(&[][..], 0),
  ];
  let vf = VideoFrame::new(Dimensions::new(16, 16), PixelFormat::Yuv420p, planes, 3);
  assert_eq!(vf.dimensions(), Dimensions::new(16, 16));
  assert_eq!(vf.width(), 16);
  assert_eq!(vf.height(), 16);
  assert_eq!(*vf.pixel_format_ref(), PixelFormat::Yuv420p);
  assert_eq!(vf.plane_count(), 3);
  assert!(vf.visible_rect().is_none());
  assert_eq!(vf.color(), Info::UNSPECIFIED);
}

#[test]
fn video_frame_planes_slice_uses_plane_count() {
  let planes: [Plane<u32>; 4] = [
    Plane::new(1, 0),
    Plane::new(2, 0),
    Plane::new(3, 0),
    Plane::new(4, 0),
  ];
  let vf = VideoFrame::new(Dimensions::new(2, 2), PixelFormat::Yuv420p, planes, 2);
  assert_eq!(vf.planes().len(), 2);
  assert_eq!(*vf.plane(0).unwrap().data_ref(), 1);
  assert_eq!(*vf.plane(1).unwrap().data_ref(), 2);
  assert!(vf.plane(2).is_none());
  assert!(vf.plane(7).is_none());
}

#[test]
#[should_panic(expected = "plane_count exceeds the fixed 4-plane array")]
fn video_frame_new_panics_on_plane_count_over_4() {
  let planes: [Plane<()>; 4] = [Plane::new((), 0); 4];
  let _ = VideoFrame::new(Dimensions::new(1, 1), PixelFormat::Yuv420p, planes, 5);
}

#[test]
fn video_frame_with_visible_rect_and_color_chain() {
  let planes: [Plane<()>; 4] = [Plane::new((), 0); 4];
  let vf = VideoFrame::new(Dimensions::new(8, 8), PixelFormat::Yuv420p, planes, 3)
    .with_visible_rect(Rect::new(0, 0, 6, 6));
  assert_eq!(vf.visible_rect(), Some(Rect::new(0, 0, 6, 6)));
}

// ---------- TimestampedFrame ------------------------------------------

#[test]
fn timestamped_frame_construction_defaults() {
  let tf: TimestampedFrame<&'static str> = TimestampedFrame::new("payload");
  assert!(tf.pts().is_none());
  assert!(tf.duration().is_none());
  assert_eq!(*tf.frame_ref(), "payload");
}

#[test]
fn timestamped_frame_into_frame_consumes() {
  let tf = TimestampedFrame::new(42u32);
  let raw = tf.into_frame();
  assert_eq!(raw, 42);
}

#[test]
fn timestamped_frame_pts_builder() {
  let tb = mediatime::Timebase::new(1, core::num::NonZeroI32::new(1000).unwrap());
  let ts = mediatime::Timestamp::new(1000, tb);
  let tf = TimestampedFrame::new(0u8).with_pts(ts).with_duration(ts);
  assert_eq!(tf.pts(), Some(ts));
  assert_eq!(tf.duration(), Some(ts));
}

#[test]
fn timestamped_frame_wraps_video_frame() {
  let planes: [Plane<()>; 4] = [Plane::new((), 0); 4];
  let vf = VideoFrame::new(Dimensions::new(4, 4), PixelFormat::Yuv420p, planes, 3);
  let tf = TimestampedFrame::new(vf);
  assert_eq!(tf.frame_ref().dimensions(), Dimensions::new(4, 4));
}

// ---------- Rational --------------------------------------------------

#[test]
fn rational_default_is_one_over_one() {
  let r = Rational::default();
  assert_eq!(r.num(), 1);
  assert_eq!(r.den().get(), 1);
  assert!(!r.is_zero());
}

#[test]
fn rational_construction_builders_and_is_zero() {
  let nz = |n: i64| core::num::NonZeroI64::new(n).unwrap();
  let r = Rational::new(30000, nz(1001));
  assert_eq!(r.num(), 30000);
  assert_eq!(r.den().get(), 1001);
  assert!(!r.is_zero());
  let z = Rational::new(0, nz(1));
  assert!(z.is_zero());
  let r2 = Rational::default().with_num(24).with_den(nz(1));
  assert_eq!((r2.num(), r2.den().get()), (24, 1));
  let mut r3 = Rational::default();
  r3.set_num(16).set_den(nz(9));
  assert_eq!((r3.num(), r3.den().get()), (16, 9));
}

#[cfg(feature = "std")]
#[test]
fn rational_display() {
  let nz = core::num::NonZeroI64::new(1001).unwrap();
  assert_eq!(std::format!("{}", Rational::new(30000, nz)), "30000/1001");
}

// ---------- Rational sign / width invariants --------------------------
//
// `num`/`den` were `u32`/`NonZeroU32`, where the types made every
// invariant unrepresentable. Under `i64`/`NonZeroI64` only
// "denominator is non-zero" is still enforced by the type; the sign
// half moved into `new`, so it needs pinning here.

#[test]
fn rational_rejects_negative_numerator() {
  let nz = |n: i64| core::num::NonZeroI64::new(n).unwrap();
  assert!(Rational::try_new(-1, nz(1)).is_none());
  assert!(Rational::try_new(i64::MIN, nz(1)).is_none());
  // `0` is a legal degenerate ratio (FFmpeg's "unknown" `0/1`).
  assert!(Rational::try_new(0, nz(1)).is_some());
}

#[test]
fn rational_rejects_negative_denominator() {
  let nz = |n: i64| core::num::NonZeroI64::new(n).unwrap();
  assert!(Rational::try_new(1, nz(-1)).is_none());
  assert!(Rational::try_new(1, nz(i64::MIN)).is_none());
}

#[test]
fn rational_zero_denominator_is_unrepresentable() {
  // Not a runtime check in `new` — `NonZeroI64` has no zero value at
  // all, so the state cannot be constructed to be rejected.
  assert!(core::num::NonZeroI64::new(0).is_none());
}

#[test]
#[should_panic(expected = "rational numerator must not be negative")]
fn rational_new_panics_on_negative_numerator() {
  let _ = Rational::new(-1, DEN_ONE);
}

#[test]
#[should_panic(expected = "rational denominator must be positive")]
fn rational_new_panics_on_negative_denominator() {
  let nz = core::num::NonZeroI64::new(-2).unwrap();
  let _ = Rational::new(1, nz);
}

#[test]
#[should_panic(expected = "rational numerator must not be negative")]
fn rational_set_num_routes_through_new() {
  // The four mutators are the invariant hole a direct field
  // assignment would leave open; each goes through `new`.
  let mut r = Rational::default();
  r.set_num(-1);
}

#[test]
#[should_panic(expected = "rational denominator must be positive")]
fn rational_set_den_routes_through_new() {
  let mut r = Rational::default();
  r.set_den(core::num::NonZeroI64::new(-3).unwrap());
}

#[test]
#[should_panic(expected = "rational numerator must not be negative")]
fn rational_with_num_routes_through_new() {
  let _ = Rational::default().with_num(-1);
}

#[test]
#[should_panic(expected = "rational denominator must be positive")]
fn rational_with_den_routes_through_new() {
  let _ = Rational::default().with_den(core::num::NonZeroI64::new(-3).unwrap());
}

#[test]
fn rational_accepts_i64_max_at_both_positions() {
  let nz = core::num::NonZeroI64::new(i64::MAX).unwrap();
  let r = Rational::new(i64::MAX, nz);
  assert_eq!(r.num(), i64::MAX);
  assert_eq!(r.den().get(), i64::MAX);
}

#[test]
fn rational_accepts_values_above_u32_max() {
  // The capability the widening buys: a numerator (and denominator)
  // the previous `u32` representation could not hold at all.
  let big = i64::from(u32::MAX) + 1;
  let nz = core::num::NonZeroI64::new(big).unwrap();
  let r = Rational::new(big, nz);
  assert_eq!(r.num(), big);
  assert_eq!(r.den().get(), big);
  // And through the semantic wrappers, which carry no width of their own.
  let sar = SampleAspectRatio::new(big, nz);
  assert_eq!((sar.num(), sar.den().get()), (big, big));
  assert_eq!(FrameRate::new(r, false).rate(), r);
}

#[test]
fn sample_aspect_ratio_fallible_path_is_try_new_plus_from() {
  // `SampleAspectRatio::new` panics like `Rational::new`; the
  // fallible route is the existing `From<Rational>`.
  let nz = |n: i64| core::num::NonZeroI64::new(n).unwrap();
  let ok = Rational::try_new(40, nz(33)).map(SampleAspectRatio::from);
  assert_eq!(ok, Some(SampleAspectRatio::new(40, nz(33))));
  let bad = Rational::try_new(-40, nz(33)).map(SampleAspectRatio::from);
  assert!(bad.is_none());
}

// ---------- SampleAspectRatio ↔ Rational interop ----------------------

#[test]
fn sample_aspect_ratio_rational_interop() {
  let nz = |n: i64| core::num::NonZeroI64::new(n).unwrap();
  let sar = SampleAspectRatio::new(40, nz(33));
  let via_method: Rational = sar.as_rational();
  let via_from: Rational = Rational::from(sar);
  let via_into: Rational = sar.into();
  assert_eq!(via_method, Rational::new(40, nz(33)));
  assert_eq!(via_method, via_from);
  assert_eq!(via_from, via_into);
  // Default 1:1 SAR maps to the 1/1 Rational default.
  assert_eq!(
    SampleAspectRatio::default().as_rational(),
    Rational::default()
  );
}

#[test]
fn sample_aspect_ratio_rational_round_trip_both_ways() {
  let nz = |n: i64| core::num::NonZeroI64::new(n).unwrap();
  // SAR -> Rational -> SAR
  let sar = SampleAspectRatio::new(40, nz(33));
  let r: Rational = sar.into();
  let back: SampleAspectRatio = r.into();
  assert_eq!(back, sar);
  assert_eq!(sar.rational(), r);
  assert_eq!(sar.rational(), sar.as_rational());
  // Rational -> SAR -> Rational
  let r2 = Rational::new(16, nz(9));
  let s2 = SampleAspectRatio::from(r2);
  assert_eq!((s2.num(), s2.den().get()), (16, 9));
  assert_eq!(Rational::from(s2), r2);
}

#[test]
fn sample_aspect_ratio_default_is_one_to_one() {
  let d = SampleAspectRatio::default();
  assert_eq!((d.num(), d.den().get()), (1, 1));
  assert!(d.is_square());
  assert_eq!(d, SampleAspectRatio::new(1, DEN_ONE));
}

#[test]
fn sample_aspect_ratio_eq_and_hash_parity() {
  use core::hash::{Hash, Hasher};
  let nz = |n: i64| core::num::NonZeroI64::new(n).unwrap();
  let a = SampleAspectRatio::new(40, nz(33));
  let b = SampleAspectRatio::default().with_num(40).with_den(nz(33));
  assert_eq!(a, b);

  fn h(s: &SampleAspectRatio) -> u64 {
    // `no_std`-friendly deterministic hasher (FNV-1a).
    struct Fnv(u64);
    impl Hasher for Fnv {
      fn finish(&self) -> u64 {
        self.0
      }
      fn write(&mut self, bytes: &[u8]) {
        for &x in bytes {
          self.0 = (self.0 ^ x as u64).wrapping_mul(0x0100_0000_01b3);
        }
      }
    }
    let mut hasher = Fnv(0xcbf2_9ce4_8422_2325);
    s.hash(&mut hasher);
    hasher.finish()
  }
  assert_eq!(h(&a), h(&b));
}

// ---------- FrameRate -------------------------------------------------

#[test]
fn frame_rate_default_is_one_over_one_cfr() {
  let fr = FrameRate::default();
  assert_eq!(fr.rate(), Rational::default());
  assert!(!fr.is_vfr());
}

#[test]
fn frame_rate_construction_and_builders() {
  let nz = |n: i64| core::num::NonZeroI64::new(n).unwrap();
  let ntsc = Rational::new(30000, nz(1001));
  let fr = FrameRate::new(ntsc, false);
  assert_eq!(fr.rate(), ntsc);
  assert!(!fr.is_vfr());
  let vfr = FrameRate::default().with_rate(ntsc).with_is_vfr();
  assert_eq!(vfr.rate(), ntsc);
  assert!(vfr.is_vfr());
  let mut fr3 = FrameRate::default();
  fr3.set_rate(Rational::new(25, nz(1))).set_is_vfr();
  assert_eq!(fr3.rate(), Rational::new(25, nz(1)));
  assert!(fr3.is_vfr());
  // raw-wrapper + clear forms
  let fr4 = FrameRate::default().maybe_is_vfr(true);
  assert!(fr4.is_vfr());
  let mut fr5 = FrameRate::default();
  fr5.update_is_vfr(true);
  assert!(fr5.is_vfr());
  fr5.clear_is_vfr();
  assert!(!fr5.is_vfr());
}

// ---------- FieldOrder ------------------------------------------------

#[test]
fn field_order_default_is_unknown_and_as_str() {
  assert_eq!(FieldOrder::default(), FieldOrder::Unknown);
  assert_eq!(FieldOrder::Unknown.as_str(), "unknown");
  // FFmpeg names its own absence, so `"unknown"` round-trips exactly —
  // it is a variant, not the old payload-collapsing escape.
  assert_eq!("unknown".parse(), Ok(FieldOrder::Unknown));
  assert_eq!(FieldOrder::Progressive.as_str(), "progressive");
  assert_eq!(FieldOrder::Tt.as_str(), "tt");
  assert_eq!(FieldOrder::Bb.as_str(), "bb");
  assert_eq!(FieldOrder::Tb.as_str(), "tb");
  assert_eq!(FieldOrder::Bt.as_str(), "bt");
  assert!(FieldOrder::Progressive.is_progressive());
}

#[test]
fn field_order_u32_round_trip_and_escape() {
  for f in [
    FieldOrder::Unknown,
    FieldOrder::Progressive,
    FieldOrder::Tt,
    FieldOrder::Bb,
    FieldOrder::Tb,
    FieldOrder::Bt,
  ] {
    assert_eq!(FieldOrder::from_u32(f.to_u32().unwrap()), Some(f));
  }
  assert_eq!(FieldOrder::from_u32(1), Some(FieldOrder::Progressive));
  assert_eq!(FieldOrder::from_u32(5), Some(FieldOrder::Bt));
  // FFmpeg's own UNKNOWN sentinel (0) decodes to the named variant.
  assert_eq!(FieldOrder::from_u32(0), Some(FieldOrder::Unknown));
  assert_eq!(FieldOrder::from_u32(99), None);
}

// ---------- StereoMode ------------------------------------------------

#[test]
fn stereo_mode_default_is_mono_and_as_str() {
  assert_eq!(StereoMode::default(), StereoMode::Mono);
  assert_eq!(StereoMode::Mono.as_str(), "mono");
  assert_eq!(StereoMode::SideBySide.as_str(), "side-by-side");
  assert_eq!(StereoMode::Columns.as_str(), "columns");
  assert!(StereoMode::Mono.is_mono());
}

#[test]
fn stereo_mode_u32_round_trip_and_escape() {
  for s in [
    StereoMode::Mono,
    StereoMode::SideBySide,
    StereoMode::TopBottom,
    StereoMode::FrameSequence,
    StereoMode::Checkerboard,
    StereoMode::SideBySideQuincunx,
    StereoMode::Lines,
    StereoMode::Columns,
  ] {
    assert_eq!(StereoMode::from_u32(s.to_u32().unwrap()), Some(s));
  }
  assert_eq!(StereoMode::from_u32(0), Some(StereoMode::Mono));
  assert_eq!(StereoMode::from_u32(7), Some(StereoMode::Columns));
  assert_eq!(StereoMode::from_u32(99), None);
}

#[cfg(any(feature = "std", feature = "alloc"))]
#[test]
fn stereo_mode_escape_keeps_its_name() {
  let vendor = StereoMode::other("Anaglyph");
  assert_eq!(vendor.as_str(), "anaglyph");
  assert_eq!(vendor.to_u32(), None);
  assert_eq!("anaglyph".parse(), Ok(vendor));
}

/// Every named variant of the three coded frame enums must survive
/// `as_str()` → `FromStr`, with no shared slugs.
#[test]
fn every_named_frame_enum_variant_round_trips_through_its_slug() {
  macro_rules! sweep {
    ($ty:ty) => {{
      let mut named = 0usize;
      let mut codes = [0u32; 32];
      for code in 0..=1024u32 {
        let Some(value) = <$ty>::from_u32(code) else {
          continue;
        };
        let slug = value.as_str();
        assert_eq!(
          slug.parse::<$ty>(),
          Ok(value.clone()),
          "{} slug {slug:?} does not parse back to {value:?}",
          stringify!($ty)
        );
        assert!(
          !slug.bytes().any(|b| b.is_ascii_uppercase()),
          "{} slug {slug:?} is not lowercase-canonical",
          stringify!($ty)
        );
        {
          let mut upper = [0u8; 64];
          let n = slug.len();
          upper[..n].copy_from_slice(slug.as_bytes());
          upper[..n].make_ascii_uppercase();
          let upper = core::str::from_utf8(&upper[..n]).unwrap();
          assert_eq!(
            upper.parse::<$ty>(),
            Ok(value.clone()),
            "{} does not fold {upper:?} onto {slug:?}",
            stringify!($ty)
          );
        }
        for prior in codes.iter().take(named) {
          let prior = <$ty>::from_u32(*prior).expect("recorded code names a variant");
          assert_ne!(
            prior.as_str(),
            slug,
            "{} has two variants spelled {slug:?}",
            stringify!($ty)
          );
        }
        codes[named] = code;
        named += 1;
      }
      assert!(
        named > 0,
        "{} sweep found no named variants",
        stringify!($ty)
      );
    }};
  }

  sweep!(Rotation);
  sweep!(FieldOrder);
  sweep!(StereoMode);
}

#[test]
fn field_order_names_its_own_unknown() {
  // `"unknown"` is a *name* now, not a payload-collapsing arm: on
  // `FieldOrder` it is FFmpeg's own variant.
  assert_eq!("unknown".parse(), Ok(FieldOrder::Unknown));
}

#[cfg(any(feature = "std", feature = "alloc"))]
#[test]
fn frame_enum_escape_keeps_the_name_it_was_given() {
  // Elsewhere `"unknown"` rides the escape like any other name this
  // build does not enumerate.
  assert_eq!("unknown".parse(), Ok(Rotation::other("unknown")));
  assert_eq!("unknown".parse(), Ok(StereoMode::other("unknown")));
  assert_eq!(
    "not-a-rotation".parse::<Rotation>().unwrap().as_str(),
    "not-a-rotation"
  );
}

/// The geometry types render an injective form, so `FromStr` is a true
/// inverse of `Display` for every value — not only the named ones.
// `std::format!` needs the allocator; these types themselves are
// available at the no-alloc tier, where the round trip is untestable.
#[cfg(any(feature = "std", feature = "alloc"))]
#[test]
fn geometry_display_round_trips_through_from_str() {
  use core::num::NonZeroI64;

  let nz = |n: i64| NonZeroI64::new(n).unwrap();

  for dims in [
    Dimensions::default(),
    Dimensions::new(1920, 1080),
    Dimensions::new(u32::MAX, u32::MAX),
  ] {
    assert_eq!(std::format!("{dims}").parse(), Ok(dims));
  }

  for ratio in [
    Rational::default(),
    Rational::new(30_000, nz(1001)),
    Rational::new(0, nz(1)),
    Rational::new(i64::MAX, nz(i64::MAX)),
  ] {
    assert_eq!(std::format!("{ratio}").parse(), Ok(ratio));
  }

  for sar in [
    SampleAspectRatio::default(),
    SampleAspectRatio::new(40, nz(33)),
    SampleAspectRatio::new(16, nz(9)),
  ] {
    assert_eq!(std::format!("{sar}").parse(), Ok(sar));
  }
}

/// The separators are part of each type's contract: a SAR is written
/// `a:b` and a bare ratio `a/b`, so neither accepts the other's form.
#[test]
fn geometry_separators_are_not_interchangeable() {
  assert!("40/33".parse::<SampleAspectRatio>().is_err());
  assert!("40:33".parse::<Rational>().is_err());
  assert!("1920X1080".parse::<Dimensions>().is_err());
}

/// Parsing routes through `Rational::try_new`, so it cannot mint a
/// value the constructor rejects — the invariant has exactly one gate.
#[test]
fn geometry_parsing_cannot_bypass_the_constructor_invariant() {
  // `num < 0` and `den <= 0` are what `Rational::try_new` refuses.
  assert!("-5/4".parse::<Rational>().is_err());
  assert!("5/-4".parse::<Rational>().is_err());
  assert!("5/0".parse::<Rational>().is_err());
  assert!("-1:1".parse::<SampleAspectRatio>().is_err());

  // A ratio fails for two reasons a caller reports differently, so
  // unlike the name vocabularies its error carries which.
  assert_eq!(
    "-5/4".parse::<Rational>().unwrap_err().kind(),
    RatioParseKind::OutOfRange
  );
  assert_eq!(
    "not-a-ratio".parse::<Rational>().unwrap_err().kind(),
    RatioParseKind::Malformed
  );
  assert_eq!(
    "-1:1".parse::<SampleAspectRatio>().unwrap_err().kind(),
    RatioParseKind::OutOfRange
  );
}

#[test]
fn geometry_rejects_malformed_input() {
  for bad in ["", "1920", "1920x", "x1080", "1920x1080x1", "axb", " 1x2"] {
    assert!(
      bad.parse::<Dimensions>().is_err(),
      "{bad:?} should not parse as Dimensions"
    );
  }
  for bad in ["", "30000", "30000/", "/1001", "a/b", "1/2/3"] {
    assert!(
      bad.parse::<Rational>().is_err(),
      "{bad:?} should not parse as Rational"
    );
  }
}

/// The runtime half of the `ROSTER` contract for `Rotation` / `FieldOrder` / `StereoMode` — no duplicate
/// entry, no two entries sharing a slug, and `as_str` → `FromStr` the
/// identity on every named variant. Completeness is the compile-time
/// half: the witness beside each declaration is `E0004` the moment a
/// variant is added without being rostered.
#[test]
fn rosters_are_well_formed() {
  crate::roster_tests::check(Rotation::ROSTER, "Rotation", Rotation::as_str);
  crate::roster_tests::check(FieldOrder::ROSTER, "FieldOrder", FieldOrder::as_str);
  crate::roster_tests::check(StereoMode::ROSTER, "StereoMode", StereoMode::as_str);
}

/// **Type-level** totality for the vocabularies whose escape arm exists at
/// this tier: each `let Ok(..) = ..` below is an *irrefutable* pattern, and
/// it can only be irrefutable because `FromStr::Err` is uninhabited here.
/// Narrow `Self::Err` back to a refusal type and these stop compiling
/// (`E0005`) — the totality is checked, not merely asserted.
///
/// The no-alloc half of the same claim is the build itself: the sibling
/// tests gated on `not(any(std, alloc))` still name the vocabulary's own
/// error, and a `--no-default-features` build type-checks them.
#[cfg(any(feature = "std", feature = "alloc"))]
#[test]
fn unnamed_slugs_parse_totally_at_this_tier() {
  let Ok(v) = "not-a-rotation".parse::<Rotation>();
  assert_eq!(v, Rotation::other("not-a-rotation"));
  let Ok(v) = "not-a-field-order".parse::<FieldOrder>();
  assert_eq!(v, FieldOrder::other("not-a-field-order"));
  let Ok(v) = "not-a-stereo-mode".parse::<StereoMode>();
  assert_eq!(v, StereoMode::other("not-a-stereo-mode"));
}