aboutsummaryrefslogtreecommitdiff
path: root/tests/lib.rs
blob: 9dc14492d188b33e8d5dddddc62801141354f02e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use yaxpeax_arch::AddressBase;

mod reader;

#[test]
fn test_u16() {
    for l in 0..100 {
        for r in 0..=core::u16::MAX {
            assert_eq!(r.wrapping_offset(l.diff(&r).expect("u16 addresses always have valid diffs")), l);
        }
    }
}

#[test]
#[cfg(std)]
fn generic_error_can_bail() {
    use yaxpeax_arch::{Arch, Decoder, Reader};

    #[allow(dead_code)]
    fn decode<A: Arch, U: Into<impl Reader<A::Address, A::Word>>>(data: U, decoder: &A::Decoder) -> anyhow::Result<()> {
        let mut reader = data.into();
        decoder.decode(&mut reader)?;
        Ok(())
    }
}
#[test]
#[cfg(std)]
fn error_can_bail() {
    use yaxpeax_arch::{Arch, AddressDiff, Decoder, Reader, LengthedInstruction, Instruction, StandardDecodeError, U8Reader};
    struct TestIsa {}
    #[derive(Debug, Default)]
    struct TestInst {}
    impl Arch for TestIsa {
        type Word = u8;
        type Address = u64;
        type Instruction = TestInst;
        type Decoder = TestIsaDecoder;
        type DecodeError = StandardDecodeError;
        type Operand = ();
    }

    impl Instruction for TestInst {
        fn well_defined(&self) -> bool { true }
    }

    impl LengthedInstruction for TestInst {
        type Unit = AddressDiff<u64>;
        fn len(&self) -> Self::Unit { AddressDiff::from_const(1) }
        fn min_size() -> Self::Unit { AddressDiff::from_const(1) }
    }

    struct TestIsaDecoder {}

    impl Default for TestIsaDecoder {
        fn default() -> Self {
            TestIsaDecoder {}
        }
    }

    impl Decoder<TestIsa> for TestIsaDecoder {
        fn decode_into<T: Reader<u64, u8>>(&self, _inst: &mut TestInst, _words: &mut T) -> Result<(), StandardDecodeError> {

            Err(StandardDecodeError::ExhaustedInput)
        }
    }

    #[derive(Debug, PartialEq, thiserror::Error)]
    pub enum Error {
        #[error("decode error")]
        TestDecode(#[from] StandardDecodeError),
    }

    fn exercise_eq() -> Result<(), Error> {
        let mut reader = U8Reader::new(&[]);
        TestIsaDecoder::default().decode(&mut reader)?;
        Ok(())
    }

    assert_eq!(exercise_eq(), Err(Error::TestDecode(StandardDecodeError::ExhaustedInput)));
}

#[test]
fn example_arch_impl() {
    use yaxpeax_arch::{Arch, AddressDiff, Decoder, Reader, LengthedInstruction, Instruction, StandardDecodeError, U8Reader};
    struct TestIsa {}
    #[derive(Debug, Default)]
    struct TestInst {}
    impl Arch for TestIsa {
        type Word = u8;
        type Address = u64;
        type Instruction = TestInst;
        type Decoder = TestIsaDecoder;
        type DecodeError = StandardDecodeError;
        type Operand = ();
    }

    impl Instruction for TestInst {
        fn well_defined(&self) -> bool { true }
    }

    impl LengthedInstruction for TestInst {
        type Unit = AddressDiff<u64>;
        fn len(&self) -> Self::Unit { AddressDiff::from_const(1) }
        fn min_size() -> Self::Unit { AddressDiff::from_const(1) }
    }

    struct TestIsaDecoder {}

    impl Default for TestIsaDecoder {
        fn default() -> Self {
            TestIsaDecoder {}
        }
    }

    impl Decoder<TestIsa> for TestIsaDecoder {
        fn decode_into<T: Reader<u64, u8>>(&self, _inst: &mut TestInst, _words: &mut T) -> Result<(), StandardDecodeError> {
            Err(StandardDecodeError::ExhaustedInput)
        }
    }

    fn exercise_eq() -> Result<(), StandardDecodeError> {
        let mut reader = U8Reader::new(&[]);
        TestIsaDecoder::default().decode(&mut reader)?;
        Ok(())
    }

    assert_eq!(exercise_eq(), Err(StandardDecodeError::ExhaustedInput));
}