summaryrefslogtreecommitdiff
path: root/test/test.rs
diff options
context:
space:
mode:
authoriximeow <me@iximeow.net>2020-01-28 01:13:07 -0800
committeriximeow <me@iximeow.net>2020-01-28 01:15:31 -0800
commit87ac71fd89bd0dda7ee0e7b74f11c381718e4f84 (patch)
tree963389985b36fad40114aa8c6bf212e99c6336c8 /test/test.rs
draw the owl
Diffstat (limited to 'test/test.rs')
-rw-r--r--test/test.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/test/test.rs b/test/test.rs
new file mode 100644
index 0000000..2b59011
--- /dev/null
+++ b/test/test.rs
@@ -0,0 +1,52 @@
+use yaxpeax_arch::{Decoder, LengthedInstruction};
+use yaxpeax_m16c::InstDecoder;
+
+use std::fmt::Write;
+
+fn test_display(data: &[u8], expected: &'static str) {
+ test_display_under(&InstDecoder::default(), data, expected);
+}
+
+fn test_display_under(decoder: &InstDecoder, data: &[u8], expected: &'static str) {
+ let mut hex = String::new();
+ for b in data {
+ write!(hex, "{:02x}", b).unwrap();
+ }
+ match decoder.decode(data.into_iter().map(|x| *x)) {
+ Ok(instr) => {
+ let text = format!("{}", instr);
+ assert!(
+ text == expected,
+ "display error for {}:\n decoded: {:?} under decoder {}\n displayed: {}\n expected: {}\n",
+ hex,
+ instr,
+ decoder,
+ text,
+ expected
+ );
+ // while we're at it, test that the instruction is as long, and no longer, than its
+ // input
+ assert_eq!(instr.len() as usize, data.len(), "instruction length is incorrect, wanted instruction {}", expected);
+ },
+ Err(e) => {
+ assert!(false, "decode error ({}) for {} under decoder {}:\n expected: {}\n", e, hex, decoder, expected);
+ }
+ }
+}
+
+#[test]
+fn test() {
+ test_display(&[0x7c, 0xf2, 0x03], "enter #03h");
+ test_display(&[0x73, 0x1b, 0xfd], "mov.w r1, -3[fb]");
+ test_display(&[0xb6, 0xff], "mov.b #00h, -1[fb]");
+ test_display(&[0xe6, 0x4f, 0xff], "cmp.b #4fh, -1[fb]");
+ test_display(&[0x68, 0x0c], "jgeu $+12");
+ test_display(&[0x7d, 0xf2], "exitd");
+ test_display(&[0x32, 0xff], "mov.b -1[fb], a0");
+ test_display(&[0xa1, 0xb4, 0xfd], "add.w -3[fb], a0");
+ test_display(&[0xd8, 0xf6], "mov.b #-01h, [a0]");
+ test_display(&[0xa6, 0xff], "inc.b -1[fb]");
+ test_display(&[0xfe, 0xf1], "jmp.b $-15");
+ test_display(&[0x7d, 0xeb, 0x44, 0x66], "add.w #6644h, sp");
+ test_display(&[0x7c, 0xeb, 0x54], "add.b #54h, sp");
+}