Skip to content

Commit 3e30d2f

Browse files
committed
improve irctl encode compare tests
Signed-off-by: Sean Young <[email protected]>
1 parent 85407b5 commit 3e30d2f

File tree

5 files changed

+251
-223
lines changed

5 files changed

+251
-223
lines changed

Diff for: TODO

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
- cir decode irp should read IrpProtocols.xml
1414
- cir decode keymap should support keymap.toml
1515
- access to io_error in aya
16+
- parse of keymap.toml should ensure that raw codes have trailing spaces
17+
-> remove Message.extend() hack
18+
- cir transmit rawir -S sony15:12 -r 2 ??
1619

1720
irp language oddities
1821

Diff for: src/keymap/protocol.rs

+8-174
Original file line numberDiff line numberDiff line change
@@ -217,180 +217,14 @@ const LINUX_PROTOCOLS: &[LinuxProtocol] = &[
217217
}
218218
];
219219

220-
#[cfg(test)]
221-
mod test {
222-
use super::LinuxProtocol;
223-
use irp::{Irp, Vartable};
224-
use rand::RngCore;
225-
use std::ffi::CStr;
220+
#[test]
221+
fn find_like() {
222+
let p = LinuxProtocol::find_like("rc6-mce").unwrap();
223+
assert_eq!(p.name, "rc6_mce");
226224

227-
#[test]
228-
fn compare_encode_to_irctl() {
229-
for proto in [
230-
libirctl::rc_proto::RC_PROTO_RC5,
231-
libirctl::rc_proto::RC_PROTO_RC5X_20,
232-
libirctl::rc_proto::RC_PROTO_RC5_SZ,
233-
libirctl::rc_proto::RC_PROTO_JVC,
234-
libirctl::rc_proto::RC_PROTO_SONY12,
235-
libirctl::rc_proto::RC_PROTO_SONY15,
236-
libirctl::rc_proto::RC_PROTO_SONY20,
237-
libirctl::rc_proto::RC_PROTO_NEC,
238-
libirctl::rc_proto::RC_PROTO_NECX,
239-
libirctl::rc_proto::RC_PROTO_NEC32,
240-
libirctl::rc_proto::RC_PROTO_SANYO,
241-
libirctl::rc_proto::RC_PROTO_RC6_0,
242-
libirctl::rc_proto::RC_PROTO_RC6_6A_20,
243-
libirctl::rc_proto::RC_PROTO_RC6_6A_24,
244-
libirctl::rc_proto::RC_PROTO_RC6_6A_32,
245-
libirctl::rc_proto::RC_PROTO_RC6_MCE,
246-
libirctl::rc_proto::RC_PROTO_SHARP,
247-
libirctl::rc_proto::RC_PROTO_RCMM12,
248-
libirctl::rc_proto::RC_PROTO_RCMM24,
249-
libirctl::rc_proto::RC_PROTO_RCMM32,
250-
libirctl::rc_proto::RC_PROTO_XBOX_DVD,
251-
] {
252-
let name = unsafe { CStr::from_ptr(libirctl::protocol_name(proto)) }
253-
.to_str()
254-
.unwrap();
255-
let linux = LinuxProtocol::find(name).unwrap();
225+
let p = LinuxProtocol::find_like("rcmm12").unwrap();
226+
assert_eq!(p.name, "rc-mm-12");
256227

257-
assert_eq!(linux.scancode_mask, unsafe {
258-
libirctl::protocol_scancode_mask(proto)
259-
});
260-
assert_eq!(linux.protocol_no, proto as u32);
261-
let mut rng = rand::thread_rng();
262-
263-
if unsafe { libirctl::protocol_encoder_available(proto) } {
264-
let irp = Irp::parse(linux.irp.unwrap()).unwrap();
265-
266-
if proto == libirctl::rc_proto::RC_PROTO_NEC
267-
|| proto == libirctl::rc_proto::RC_PROTO_NECX
268-
|| proto == libirctl::rc_proto::RC_PROTO_NEC32
269-
{
270-
assert_eq!(irp.carrier(), 38400);
271-
} else if proto == libirctl::rc_proto::RC_PROTO_JVC {
272-
assert_eq!(irp.carrier(), 37900);
273-
} else {
274-
assert_eq!(irp.carrier(), unsafe {
275-
libirctl::protocol_carrier(proto) as i64
276-
});
277-
}
278-
279-
let max_size = unsafe { libirctl::protocol_max_size(proto) } as usize;
280-
281-
let mut irctl = vec![0u32; max_size];
282-
283-
for _ in 0..1000 {
284-
let scancode = rng.next_u32() & linux.scancode_mask;
285-
286-
irctl.resize(max_size as usize, 0);
287-
288-
let len =
289-
unsafe { libirctl::protocol_encode(proto, scancode, irctl.as_mut_ptr()) };
290-
291-
assert!(
292-
len as usize <= max_size,
293-
"{len} {max_size} proto:{proto:?} scancode:{scancode:#x}"
294-
);
295-
296-
irctl.resize(len as usize, 0);
297-
298-
let mut vars = Vartable::new();
299-
300-
vars.set("CODE".into(), scancode as i64);
301-
302-
let mut our = irp.encode_raw(vars, 0).unwrap();
303-
our.remove_trailing_gap();
304-
305-
if [
306-
libirctl::rc_proto::RC_PROTO_JVC,
307-
libirctl::rc_proto::RC_PROTO_NEC,
308-
libirctl::rc_proto::RC_PROTO_NECX,
309-
libirctl::rc_proto::RC_PROTO_NEC32,
310-
libirctl::rc_proto::RC_PROTO_SANYO,
311-
libirctl::rc_proto::RC_PROTO_SHARP,
312-
libirctl::rc_proto::RC_PROTO_RC6_0,
313-
libirctl::rc_proto::RC_PROTO_RC6_6A_20,
314-
libirctl::rc_proto::RC_PROTO_RC6_6A_24,
315-
libirctl::rc_proto::RC_PROTO_RC6_6A_32,
316-
libirctl::rc_proto::RC_PROTO_RC6_MCE,
317-
]
318-
.contains(&proto)
319-
{
320-
assert!(compare_with_rounding(&irctl, &our.raw));
321-
} else {
322-
assert_eq!(irctl, our.raw);
323-
}
324-
}
325-
} else if let Some(irp) = linux.irp {
326-
let irp = Irp::parse(irp).unwrap();
327-
328-
for _ in 0..1000 {
329-
let scancode = rng.next_u32() & linux.scancode_mask;
330-
331-
let mut vars = Vartable::new();
332-
333-
vars.set("CODE".into(), scancode as i64);
334-
335-
let our = irp.encode_raw(vars, 0).unwrap();
336-
337-
assert!(!our.raw.is_empty());
338-
}
339-
}
340-
}
341-
}
342-
343-
fn compare_with_rounding(l: &[u32], r: &[u32]) -> bool {
344-
if l == r {
345-
return true;
346-
}
347-
348-
if l.len() != r.len() {
349-
println!(
350-
"comparing:\n{:?} with\n{:?}\n have different lengths {} and {}",
351-
l,
352-
r,
353-
l.len(),
354-
r.len()
355-
);
356-
357-
return false;
358-
}
359-
360-
for i in 0..l.len() {
361-
// sharp gap in the middle
362-
if l[i] == 40000 && r[i] == 43560 {
363-
continue;
364-
}
365-
366-
let diff = l[i].abs_diff(r[i]);
367-
368-
// is the difference more than 168
369-
if diff > 168 {
370-
println!(
371-
"comparing:\nleft:{:?} with\nright:{:?}\nfailed at position {} out of {} diff {diff}",
372-
l,
373-
r,
374-
i,
375-
l.len()
376-
);
377-
378-
return false;
379-
}
380-
}
381-
382-
true
383-
}
384-
385-
#[test]
386-
fn find_like() {
387-
let p = LinuxProtocol::find_like("rc6-mce").unwrap();
388-
assert_eq!(p.name, "rc6_mce");
389-
390-
let p = LinuxProtocol::find_like("rcmm12").unwrap();
391-
assert_eq!(p.name, "rc-mm-12");
392-
393-
let p = LinuxProtocol::find_like("sony-12").unwrap();
394-
assert_eq!(p.name, "sony12");
395-
}
228+
let p = LinuxProtocol::find_like("sony-12").unwrap();
229+
assert_eq!(p.name, "sony12");
396230
}

Diff for: tests/bpf_encode_tests.rs

-46
This file was deleted.

0 commit comments

Comments
 (0)