Skip to content

Commit e139854

Browse files
committed
Compress many common UTs into umbrellas
Share the load
1 parent 683399d commit e139854

File tree

1 file changed

+66
-171
lines changed

1 file changed

+66
-171
lines changed

src/tests.rs

+66-171
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const CIELAB: &'static [[f64; 3]] = &[
6868
[675.44970111, 14.25078120, -436.42562428],
6969
[-650.06570921, 155.94479927, 599.90623227],
7070
];
71-
const LCH: &'static [[f64; 3]] = &[
71+
const CIELCH: &'static [[f64; 3]] = &[
7272
[0.00000000, 0.00000000, 0.00000000],
7373
[53.23288179, 104.57928635, 40.00102571],
7474
[87.73703347, 119.78188649, 136.01306869],
@@ -144,8 +144,8 @@ const _ICTCP2: &'static [[f64; 3]] = &[
144144

145145
// ### COLOUR-REFS ### }}}
146146

147-
// ### Comparison FNs ### {{{
148147
fn pix_cmp(input: &[[f64; 3]], reference: &[[f64; 3]], epsilon: f64, skips: &'static [usize]) {
148+
// {{{
149149
let mut err = String::new();
150150
let mut cum_err = 0.0;
151151
for (n, (i, r)) in input.iter().zip(reference.iter()).enumerate() {
@@ -173,95 +173,37 @@ fn pix_cmp(input: &[[f64; 3]], reference: &[[f64; 3]], epsilon: f64, skips: &'st
173173
if !err.is_empty() {
174174
panic!("{}\nCUM ERR²: {}", err, cum_err)
175175
}
176-
}
177-
178-
fn func_cmp_full(
179-
input: &[[f64; 3]],
180-
reference: &[[f64; 3]],
181-
function: fn(&mut [f64; 3]),
182-
epsilon: f64,
183-
skips: &'static [usize],
184-
) {
185-
let mut input = input.to_owned();
186-
input.iter_mut().for_each(|p| function(p));
187-
pix_cmp(&input, reference, epsilon, skips);
188-
}
189-
190-
fn func_cmp(input: &[[f64; 3]], reference: &[[f64; 3]], function: fn(&mut [f64; 3])) {
191-
func_cmp_full(input, reference, function, 1e-3, &[])
192-
}
193-
194-
fn conv_cmp_full(
195-
input_space: Space,
196-
input: &[[f64; 3]],
197-
reference_space: Space,
198-
reference: &[[f64; 3]],
199-
epsilon: f64,
200-
skips: &'static [usize],
201-
) {
202-
let mut input = input.to_owned();
203-
convert_space_chunked(input_space, reference_space, &mut input);
204-
pix_cmp(&input, reference, epsilon, skips)
205-
}
206-
207-
fn conv_cmp(input_space: Space, input: &[[f64; 3]], reference_space: Space, reference: &[[f64; 3]]) {
208-
// skip places where hue can wrap
209-
conv_cmp_full(input_space, input, reference_space, reference, 1e-3, &[0, 1, 7])
210-
}
211-
// ### Comparison FNs ### }}}
176+
} // }}}
212177

213178
// ### Single FN Accuracy ### {{{
214179
#[test]
215-
fn irgb_to() {
216-
assert_eq!(IRGB, srgb_to_irgb([0.2, 0.35, 0.95]))
217-
}
180+
fn irgb_convert() {
181+
println!("SRGB_TO_IRGB");
182+
assert_eq!(IRGB, srgb_to_irgb([0.2, 0.35, 0.95]));
183+
assert_eq!(IRGBA, srgb_to_irgb([0.2, 0.35, 0.95, 0.35]));
218184

219-
#[test]
220-
fn irgb_to_alpha() {
221-
assert_eq!(IRGBA, srgb_to_irgb([0.2, 0.35, 0.95, 0.35]))
222-
}
185+
println!("IRGB_TO_SRGB");
223186

224-
#[test]
225-
fn irgb_from() {
226187
let mut srgb = irgb_to_srgb::<f32, 3>(IRGB);
227-
// Round decimal to hundredths
228-
srgb.iter_mut().for_each(|c| *c = (*c * 100.0).round() / 100.0);
229-
assert_eq!([0.2, 0.35, 0.95], srgb)
230-
}
231-
232-
#[test]
233-
fn irgb_from_alpha() {
234-
let mut srgb = irgb_to_srgb::<f32, 4>(IRGBA);
235-
// Round decimal to hundredths
188+
// Round decimal to hundredths for exact EQ
236189
srgb.iter_mut().for_each(|c| *c = (*c * 100.0).round() / 100.0);
237-
assert_eq!([0.2, 0.35, 0.95, 0.35], srgb)
238-
}
190+
assert_eq!([0.2, 0.35, 0.95], srgb);
239191

240-
#[test]
241-
fn hex_to() {
242-
assert_eq!(HEX, irgb_to_hex(IRGB))
243-
}
244-
245-
#[test]
246-
fn hex_to_alpha() {
247-
assert_eq!(HEXA, irgb_to_hex(IRGBA))
192+
let mut srgba = irgb_to_srgb::<f32, 4>(IRGBA);
193+
srgba.iter_mut().for_each(|c| *c = (*c * 100.0).round() / 100.0);
194+
assert_eq!([0.2, 0.35, 0.95, 0.35], srgba);
248195
}
249196

250197
#[test]
251-
fn hex_from() {
198+
fn hex_convert() {
199+
println!("IRGB_TO_HEX");
200+
assert_eq!(HEX, irgb_to_hex(IRGB));
201+
assert_eq!(HEXA, irgb_to_hex(IRGBA));
202+
println!("HEX_TO_IRGB");
252203
assert_eq!(IRGB, hex_to_irgb(HEX).unwrap());
253204
assert_eq!(IRGB, hex_to_irgb(HEXA).unwrap());
254205
}
255206

256-
#[test]
257-
fn hex_from_alpha() {
258-
assert_eq!(
259-
[IRGB[0], IRGB[1], IRGB[2], 123],
260-
hex_to_irgb_default::<4, 123>(HEX).unwrap()
261-
);
262-
assert_eq!(IRGBA, hex_to_irgb(HEXA).unwrap());
263-
}
264-
265207
#[test]
266208
fn hex_validations() {
267209
for hex in [
@@ -286,67 +228,35 @@ fn hex_validations() {
286228
}
287229

288230
#[test]
289-
fn hsv_forwards() {
290-
func_cmp(SRGB, HSV, srgb_to_hsv)
291-
}
292-
#[test]
293-
fn hsv_backwards() {
294-
func_cmp(HSV, SRGB, hsv_to_srgb)
295-
}
296-
297-
#[test]
298-
fn lrgb_forwards() {
299-
func_cmp(SRGB, LRGB, srgb_to_lrgb)
300-
}
301-
#[test]
302-
fn lrgb_backwards() {
303-
func_cmp(LRGB, SRGB, lrgb_to_srgb)
304-
}
305-
306-
#[test]
307-
fn xyz_forwards() {
308-
func_cmp(LRGB, XYZ, lrgb_to_xyz)
309-
}
310-
#[test]
311-
fn xyz_backwards() {
312-
func_cmp(XYZ, LRGB, xyz_to_lrgb)
313-
}
314-
315-
#[test]
316-
fn lab_forwards() {
317-
func_cmp(XYZ, CIELAB, xyz_to_cielab)
318-
}
319-
#[test]
320-
fn lab_backwards() {
321-
func_cmp(CIELAB, XYZ, cielab_to_xyz)
322-
}
323-
324-
#[test]
325-
fn lch_forwards() {
326-
func_cmp(CIELAB, LCH, lab_to_lch)
327-
}
328-
#[test]
329-
fn lch_backwards() {
330-
func_cmp(LCH, CIELAB, lch_to_lab)
331-
}
231+
fn individual() {
232+
let runs: &[(&str, &[[f64; 3]], &[[f64; 3]], fn(pixel: &mut [f64; 3]))] = &[
233+
("SRGB->HSV", SRGB, HSV, srgb_to_hsv),
234+
("HSV->SRGB", HSV, SRGB, hsv_to_srgb),
235+
("SRGB->LRGB", SRGB, LRGB, srgb_to_lrgb),
236+
("LRGB->SRGB", LRGB, SRGB, lrgb_to_srgb),
237+
("LRGB->XYZ", LRGB, XYZ, lrgb_to_xyz),
238+
("XYZ->LRGB", XYZ, LRGB, xyz_to_lrgb),
239+
("XYZ->CIELAB", XYZ, CIELAB, xyz_to_cielab),
240+
("CIELAB->XYZ", CIELAB, XYZ, cielab_to_xyz),
241+
("XYZ->OKLAB", XYZ, OKLAB, xyz_to_oklab),
242+
("OKLAB->XYZ", OKLAB, XYZ, oklab_to_xyz),
243+
//("XYZ->JZAZBZ", XYZ, JZAZBZ, xyz_to_jzazbz)
244+
("JZAZBZ->XYZ", JZAZBZ, XYZ, jzazbz_to_xyz),
245+
("CIELAB->CIELCH", CIELAB, CIELCH, lab_to_lch),
246+
("CIELCH->CIELAB", CIELCH, CIELAB, lch_to_lab),
247+
];
332248

333-
#[test]
334-
fn oklab_forwards() {
335-
func_cmp(XYZ, OKLAB, xyz_to_oklab)
336-
}
337-
#[test]
338-
fn oklab_backwards() {
339-
func_cmp(OKLAB, XYZ, oklab_to_xyz)
340-
}
249+
for (label, from, to, func) in runs {
250+
println!("{}", label);
251+
let mut input = from.to_vec();
252+
input.iter_mut().for_each(|p| (*func)(p));
253+
pix_cmp(&input, to, 1e-3, &[]);
254+
}
341255

342-
// Lower epsilon because of the extremely wide gamut creating tiny values
343-
#[test]
344-
fn jzazbz_forwards() {
345-
func_cmp_full(XYZ, JZAZBZ, xyz_to_jzazbz, 1e-2, &[])
346-
}
347-
#[test]
348-
fn jzazbz_backwards() {
349-
func_cmp(JZAZBZ, XYZ, jzazbz_to_xyz)
256+
println!("XYZ->JZAZBZ");
257+
let mut input = XYZ.to_vec();
258+
input.iter_mut().for_each(|p| xyz_to_jzazbz(p));
259+
pix_cmp(&input, JZAZBZ, 1e-2, &[]);
350260
}
351261

352262
#[test]
@@ -371,48 +281,33 @@ fn inversions() {
371281
pix_cmp(&owned, pixel, 1e-3, &[]);
372282
}
373283
}
374-
// Disable reference tests for public commits
375-
//
376-
// #[test]
377-
// fn ictcp_forwards() {
378-
// func_cmp(LRGB, _ICTCP2, _lrgb_to_ictcp)
379-
// }
380-
// #[test]
381-
// fn ictcp_backwards() {
382-
// func_cmp(_ICTCP2, LRGB, _ictcp_to_lrgb)
383-
// }
384284
// ### Single FN Accuracy ### }}}
385285

386286
/// ### Other Tests ### {{{
387287
#[test]
388288
fn tree_jump() {
389-
// forwards
390-
println!("HSV -> LCH");
391-
conv_cmp(Space::HSV, HSV, Space::CIELCH, LCH);
392-
393-
println!("LCH -> OKLCH");
394-
conv_cmp(Space::CIELCH, LCH, Space::OKLCH, OKLCH);
395-
396-
println!("OKLCH -> JZCZHZ");
397-
conv_cmp(Space::OKLCH, OKLCH, Space::JZCZHZ, JZCZHZ);
289+
macro_rules! conv_cmp {
290+
($label:literal, $from_space:expr, $from_data:expr, $to_space:expr, $to_data:expr) => {
291+
println!($label);
292+
let mut input = $from_data.to_vec();
293+
convert_space_chunked::<f64, 3>($from_space, $to_space, &mut input);
294+
// strange this is 1e-3 while indiv is 1e-2
295+
// also skip places where hue can wrap
296+
pix_cmp(&input, $to_data, 1e-3, &[0, 1, 7])
297+
};
298+
}
398299

399-
println!("JZCZHZ -> HSV");
400-
conv_cmp(Space::JZCZHZ, JZCZHZ, Space::HSV, HSV);
300+
// forwards
301+
conv_cmp!("HSV->LCH", Space::HSV, HSV, Space::CIELCH, CIELCH);
302+
conv_cmp!("LCH -> OKLCH", Space::CIELCH, CIELCH, Space::OKLCH, OKLCH);
303+
conv_cmp!("OKLCH -> JZCZHZ", Space::OKLCH, OKLCH, Space::JZCZHZ, JZCZHZ);
304+
conv_cmp!("JZCZHZ -> HSV", Space::JZCZHZ, JZCZHZ, Space::HSV, HSV);
401305

402306
// backwards
403-
println!("HSV -> JZCZHZ");
404-
conv_cmp(Space::HSV, HSV, Space::JZCZHZ, JZCZHZ);
405-
406-
println!("JZCZHZ -> OKLCH");
407-
conv_cmp(Space::JZCZHZ, JZCZHZ, Space::OKLCH, OKLCH);
408-
409-
println!("OKLCH -> LCH");
410-
conv_cmp(Space::OKLCH, OKLCH, Space::CIELCH, LCH);
411-
412-
// add 1 to skip because the hue wraps from 0.0000 to 0.9999
413-
// fuck you precision
414-
println!("LCH -> HSV");
415-
conv_cmp(Space::CIELCH, LCH, Space::HSV, HSV);
307+
conv_cmp!("HSV -> JZCZHZ", Space::HSV, HSV, Space::JZCZHZ, JZCZHZ);
308+
conv_cmp!("JZCZHZ -> OKLCH", Space::JZCZHZ, JZCZHZ, Space::OKLCH, OKLCH);
309+
conv_cmp!("OKLCH -> LCH", Space::OKLCH, OKLCH, Space::CIELCH, CIELCH);
310+
conv_cmp!("LCH -> HSV", Space::CIELCH, CIELCH, Space::HSV, HSV);
416311
}
417312

418313
#[test]
@@ -470,7 +365,7 @@ fn sliced() {
470365
.chunks_exact(3)
471366
.map(|c| c.try_into().unwrap())
472367
.collect::<Vec<[f64; 3]>>(),
473-
LCH,
368+
CIELCH,
474369
1e-2,
475370
&[],
476371
);
@@ -489,7 +384,7 @@ fn sliced_odd() {
489384
.chunks_exact(3)
490385
.map(|c| c.try_into().unwrap())
491386
.collect::<Vec<[f64; 3]>>(),
492-
LCH,
387+
CIELCH,
493388
1e-2,
494389
&[],
495390
);

0 commit comments

Comments
 (0)