use super::{Ligature, Lookup, Lookups};
fn lookup(pairs: &[(u32, u32, u32)]) -> Lookup {
let mut map = Lookup::new();
for &(first, second, glyph) in pairs {
map.entry(first).or_default().push((vec![second], glyph));
}
map
}
fn two_lookups() -> Lookups {
Lookups(vec![lookup(&[(1, 2, 3)]), lookup(&[(3, 3, 4)])])
}
#[test]
fn a_later_lookup_joins_the_ligatures_of_an_earlier_one() {
assert_eq!(
two_lookups().apply(&[1, 2, 1, 2, 1, 2, 7]),
[
Ligature {
start: 0,
len: 4,
glyph: 4,
},
Ligature {
start: 4,
len: 2,
glyph: 3,
},
]
);
let again = Lookups(vec![lookup(&[(1, 1, 1)])]);
assert_eq!(
again.apply(&[1, 1, 1]),
[Ligature {
start: 0,
len: 2,
glyph: 1,
}]
);
}
#[test]
fn joins_the_ligatures_of_a_long_run_in_one_walk_of_each_lookup() {
let (sender, receiver) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let glyphs: Vec<u32> = [1, 2].repeat(500_000);
let _ = sender.send(two_lookups().apply(&glyphs));
});
let found = receiver
.recv_timeout(std::time::Duration::from_secs(30))
.expect("the ligatures of the run take too long");
assert_eq!(found.len(), 250_000);
assert!(found.iter().enumerate().all(|(n, ligature)| *ligature
== Ligature {
start: 4 * n,
len: 4,
glyph: 4,
}));
}