use super::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum OldTier {
Native,
RowStage,
Filter,
}
fn old_tier(native_eligible: bool, with_native: bool, area_plan: bool) -> OldTier {
if !area_plan {
return OldTier::Filter;
}
if native_eligible && with_native {
OldTier::Native
} else {
OldTier::RowStage
}
}
fn expected_point(tier: OldTier) -> InsertionPoint {
match tier {
OldTier::Native => InsertionPoint::NativeCodes,
OldTier::RowStage | OldTier::Filter => InsertionPoint::EncodedOutput,
}
}
#[test]
fn selector_reproduces_old_take_native_decision_exhaustively() {
for native_eligible in [false, true] {
for with_native in [false, true] {
for area_plan in [false, true] {
for need_output in [false, true] {
for frozen_route in [None, Some(true), Some(false)] {
let ctx = InsertionContext {
native_eligible,
with_native,
area_plan,
};
let got = select_insertion_point(AveragingDomain::Encoded, ctx);
let expected = expected_point(old_tier(native_eligible, with_native, area_plan));
assert_eq!(
got, expected,
"splice mismatch for native_eligible={native_eligible} \
with_native={with_native} area_plan={area_plan} \
need_output={need_output} frozen_route={frozen_route:?}",
);
let _ = (need_output, frozen_route);
}
}
}
}
}
}
#[test]
fn phase1_rollout_native_eligible_formats_reproduce_if_native_boolean() {
for with_native in [false, true] {
let ctx = InsertionContext {
native_eligible: true,
with_native,
area_plan: true,
};
let got = select_insertion_point(AveragingDomain::Encoded, ctx);
let expected_native = with_native; assert_eq!(
got == InsertionPoint::NativeCodes,
expected_native,
"Phase 1 splice mismatch: native_eligible=true area_plan=true \
with_native={with_native} must map NativeCodes<=>with_native",
);
}
}
#[test]
fn encoded_area_native_eligible_with_native_selects_native_codes() {
let ctx = InsertionContext {
native_eligible: true,
with_native: true,
area_plan: true,
};
assert_eq!(
select_insertion_point(AveragingDomain::Encoded, ctx),
InsertionPoint::NativeCodes,
);
}
#[test]
fn encoded_disabling_native_forces_encoded_output() {
let ctx = InsertionContext {
native_eligible: true,
with_native: false,
area_plan: true,
};
assert_eq!(
select_insertion_point(AveragingDomain::Encoded, ctx),
InsertionPoint::EncodedOutput,
);
}
#[test]
fn encoded_filter_plan_never_splices_at_native_codes() {
for with_native in [false, true] {
let ctx = InsertionContext {
native_eligible: true,
with_native,
area_plan: false,
};
assert_eq!(
select_insertion_point(AveragingDomain::Encoded, ctx),
InsertionPoint::EncodedOutput,
);
}
}
#[test]
fn ineligible_format_never_splices_at_native_codes() {
for with_native in [false, true] {
for area_plan in [false, true] {
let ctx = InsertionContext {
native_eligible: false,
with_native,
area_plan,
};
assert_eq!(
select_insertion_point(AveragingDomain::Encoded, ctx),
InsertionPoint::EncodedOutput,
);
}
}
}
#[test]
fn linear_domain_resolves_to_linear_light() {
for native_eligible in [false, true] {
for with_native in [false, true] {
for area_plan in [false, true] {
let ctx = InsertionContext {
native_eligible,
with_native,
area_plan,
};
assert_eq!(
select_insertion_point(AveragingDomain::Linear, ctx),
InsertionPoint::LinearLight,
"Linear must splice at LinearLight regardless of native inputs",
);
}
}
}
}
#[test]
#[should_panic(expected = "Premultiplied is rejected at dispatch")]
fn premultiplied_domain_has_no_splice_and_is_unreachable() {
let ctx = InsertionContext {
native_eligible: true,
with_native: true,
area_plan: true,
};
let _ = select_insertion_point(AveragingDomain::Premultiplied, ctx);
}
#[test]
fn averaging_domain_as_str_round_trips_variants() {
assert_eq!(AveragingDomain::Encoded.as_str(), "encoded");
assert_eq!(AveragingDomain::Linear.as_str(), "linear");
assert_eq!(AveragingDomain::Premultiplied.as_str(), "premultiplied");
}
#[test]
fn resample_strategy_default_is_encoded_area() {
let strat = ResampleStrategy::default();
assert_eq!(strat.domain(), AveragingDomain::Encoded);
assert_eq!(strat.filter(), FilterSpec::Area);
}
#[test]
fn transfer_function_default_is_bt1886() {
assert_eq!(TransferFunction::default(), TransferFunction::Bt1886);
}
#[test]
fn transfer_function_as_str_round_trips_variants() {
assert_eq!(TransferFunction::LinearPassthrough.as_str(), "linear");
assert_eq!(TransferFunction::Srgb.as_str(), "srgb");
assert_eq!(TransferFunction::Bt1886.as_str(), "bt1886");
assert_eq!(TransferFunction::Gamma22.as_str(), "gamma22");
}
#[test]
fn transfer_function_eotf_oetf_are_inverses() {
for tf in [
TransferFunction::LinearPassthrough,
TransferFunction::Srgb,
TransferFunction::Bt1886,
TransferFunction::Gamma22,
] {
for i in 0..=256 {
let c = i as f32 / 256.0;
let round = tf.oetf(tf.eotf(c));
assert!(
(round - c).abs() <= 2e-4,
"{}: oetf(eotf({c})) = {round}, want {c}",
tf.as_str(),
);
let round2 = tf.eotf(tf.oetf(c));
assert!(
(round2 - c).abs() <= 2e-4,
"{}: eotf(oetf({c})) = {round2}, want {c}",
tf.as_str(),
);
}
}
}
#[test]
fn transfer_function_endpoints_are_fixed() {
for tf in [
TransferFunction::LinearPassthrough,
TransferFunction::Srgb,
TransferFunction::Bt1886,
TransferFunction::Gamma22,
] {
assert!(tf.eotf(0.0).abs() <= 1e-6, "{}: eotf(0)", tf.as_str());
assert!(
(tf.eotf(1.0) - 1.0).abs() <= 1e-6,
"{}: eotf(1)",
tf.as_str()
);
assert!(tf.oetf(0.0).abs() <= 1e-6, "{}: oetf(0)", tf.as_str());
assert!(
(tf.oetf(1.0) - 1.0).abs() <= 1e-6,
"{}: oetf(1)",
tf.as_str()
);
}
}
#[test]
fn transfer_function_curves_are_distinct() {
let c = 0.5_f32;
let srgb = TransferFunction::Srgb.eotf(c);
let bt1886 = TransferFunction::Bt1886.eotf(c);
let g22 = TransferFunction::Gamma22.eotf(c);
let lin = TransferFunction::LinearPassthrough.eotf(c);
assert!((srgb - bt1886).abs() > 1e-3, "sRGB vs BT.1886 must differ");
assert!(
(bt1886 - g22).abs() > 1e-3,
"BT.1886 vs gamma2.2 must differ"
);
assert!(
(lin - bt1886).abs() > 1e-3,
"passthrough vs BT.1886 must differ"
);
}
#[test]
fn transfer_function_for_matrix_default_mapping() {
use crate::ColorMatrix;
assert_eq!(
TransferFunction::for_matrix(&ColorMatrix::Rgb),
TransferFunction::Srgb,
);
for matrix in [
ColorMatrix::Bt601,
ColorMatrix::Bt709,
ColorMatrix::Bt2020Ncl,
ColorMatrix::Bt2020Cl,
ColorMatrix::Smpte170M,
ColorMatrix::Smpte240m,
ColorMatrix::Fcc,
ColorMatrix::Bt470Bg,
ColorMatrix::YCgCo,
ColorMatrix::Unspecified,
ColorMatrix::Ictcp,
ColorMatrix::other("acescct"),
] {
assert_eq!(
TransferFunction::for_matrix(&matrix),
TransferFunction::Bt1886,
"{} must resolve to BT.1886",
matrix.as_str(),
);
}
}