Skip to content

Commit 743e341

Browse files
committed
Tests: use macro to initialize memory
1 parent bf4173d commit 743e341

32 files changed

+966
-1717
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,17 +241,17 @@ ax_test![add_al_byte_ptr_rbx_cf;
241241
0x2, 0x3;
242242
// A setup function that is called before the instruction is executed:
243243
|a: &mut Axecutor| {
244-
write_reg_value!(b; a; AL; 0x20);
245-
244+
write_reg_value!(b; a; AL; 0x8);
246245
// This sets up a memory area with a size of one byte, containing 0xff
246+
init_mem_value!(b; a; 0x1000; 0xff); // -1
247+
// RBX points to that memory area
247248
write_reg_value!(q; a; RBX; 0x1000);
248-
a.mem_init_zero(0x1000, 1).unwrap();
249-
a.mem_write_8(0x1000, 0xff).unwrap();
250249
};
251250
// This function is called after the instruction ran and checks the result:
252251
|a: Axecutor| {
253-
assert_reg_value!(b; a; AL; 0x1f);
252+
assert_reg_value!(b; a; AL; 0x7);
254253
// Also make sure the source operand is unchanged
254+
assert_reg_value!(q; a; RBX; 0x1000);
255255
assert_mem_value!(b; a; 0x1000; 0xff);
256256
};
257257
// On the left side of `;` are the flags that must be set after the instruction ran,

generate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def generate_mnemonic_text(instruction_codes: list[Instruction], mnemonic: str):
198198
#[cfg(test)]
199199
mod tests {
200200
use crate::helpers::tests::{{
201-
assert_mem_value, assert_reg_value, ax_test, jmp_test, test_async, write_reg_value, write_flags,
201+
assert_mem_value, assert_reg_value, ax_test, jmp_test, test_async, write_reg_value, write_flags, init_mem_value,
202202
}};
203203
use crate::{axecutor::Axecutor, state::flags::*};
204204
use iced_x86::Register::*;

src/helpers/operand.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,7 @@ mod tests {
357357
Immediate { data: 1, size: 1 },
358358
];
359359
|a: &mut Axecutor| {
360-
use iced_x86::Register::*;
361-
a.reg_write_64(RSP.into(), 0x1000).unwrap();
360+
write_reg_value!(q; a; RSP; 0x1000);
362361
};
363362
vec![
364363
0x1000
@@ -379,8 +378,7 @@ mod tests {
379378
Immediate { data: 1, size: 4 },
380379
];
381380
|a: &mut Axecutor| {
382-
use iced_x86::Register::*;
383-
a.reg_write_64(RSP.into(), 0x1000).unwrap();
381+
write_reg_value!(q; a; RSP; 0x1000);
384382
};
385383
vec![
386384
0x1000
@@ -401,8 +399,7 @@ mod tests {
401399
Register(Register::R15D.into()),
402400
];
403401
|a: &mut Axecutor| {
404-
use iced_x86::Register::*;
405-
a.reg_write_64(RSP.into(), 0x1000).unwrap();
402+
write_reg_value!(q; a; RSP; 0x1000);
406403
};
407404
vec![
408405
0x1001
@@ -424,8 +421,7 @@ mod tests {
424421
Register(Register::R15D.into()),
425422
];
426423
|a: &mut Axecutor| {
427-
use iced_x86::Register::*;
428-
a.reg_write_64(RSP.into(), 0x1000).unwrap();
424+
write_reg_value!(q; a; RSP; 0x1000);
429425
};
430426
vec![
431427
0x0fff
@@ -446,9 +442,8 @@ mod tests {
446442
Immediate { data: 1, size: 8 },
447443
];
448444
|a: &mut Axecutor| {
449-
use iced_x86::Register::*;
450-
a.reg_write_64(R11.into(), 0x8001).unwrap();
451-
a.reg_write_64(RCX.into(), 5).unwrap();
445+
write_reg_value!(q; a; R11; 0x8001);
446+
write_reg_value!(q; a; RCX; 5);
452447
};
453448
vec![
454449
0x8015

src/helpers/tests.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,53 @@ macro_rules! write_reg_value {
287287
#[cfg(test)]
288288
pub(crate) use write_reg_value;
289289

290+
#[cfg(test)]
291+
macro_rules! init_mem_value {
292+
(b; $axecutor:expr; $addr:expr; $value:expr) => {
293+
$axecutor
294+
.mem_init_zero($addr, 1)
295+
.expect("could not initialize 8-bit memory");
296+
$axecutor
297+
.mem_write_8($addr, $value as u64)
298+
.expect("could not write 8-bit memory");
299+
};
300+
(w; $axecutor:expr; $addr:expr; $value:expr) => {
301+
$axecutor
302+
.mem_init_zero($addr, 2)
303+
.expect("could not initialize 16-bit memory");
304+
$axecutor
305+
.mem_write_16($addr, $value as u64)
306+
.expect("could not write 16-bit memory");
307+
};
308+
(d; $axecutor:expr; $addr:expr; $value:expr) => {
309+
$axecutor
310+
.mem_init_zero($addr, 4)
311+
.expect("could not initialize 32-bit memory");
312+
$axecutor
313+
.mem_write_32($addr, $value as u64)
314+
.expect("could not write 32-bit memory");
315+
};
316+
(q; $axecutor:expr; $addr:expr; $value:expr) => {
317+
$axecutor
318+
.mem_init_zero($addr, 8)
319+
.expect("could not initialize 64-bit memory");
320+
$axecutor
321+
.mem_write_64($addr, $value as u64)
322+
.expect("could not write 64-bit memory");
323+
};
324+
(x; $axecutor:expr; $addr:expr; $value:expr) => {
325+
$axecutor
326+
.mem_init_zero($addr, 16)
327+
.expect("could not initialize 128-bit memory");
328+
$axecutor
329+
.mem_write_128($addr, $value as u128)
330+
.expect("could not write 128-bit memory");
331+
};
332+
}
333+
334+
#[cfg(test)]
335+
pub(crate) use init_mem_value;
336+
290337
#[cfg(test)]
291338
macro_rules! write_flags {
292339
($axecutor:expr; $flags:expr) => {

0 commit comments

Comments
 (0)