From 6aa30462f9c2de4675e1d4c4a81731b76d75b3c3 Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Wed, 22 Jun 2022 23:31:23 +0100 Subject: [PATCH] Replace memcpy tests with a test which does all offsets for lots of sizes Found a bug in agbabi with size 0, source offset 1, target offset 3 --- agb/src/agbabi/memcpy.s | 7 ++- agb/src/agbabi/mod.rs | 134 +++++++++------------------------------- 2 files changed, 32 insertions(+), 109 deletions(-) diff --git a/agb/src/agbabi/memcpy.s b/agb/src/agbabi/memcpy.s index a8126540..08a88b41 100644 --- a/agb/src/agbabi/memcpy.s +++ b/agb/src/agbabi/memcpy.s @@ -22,6 +22,10 @@ __agbabi_memcpy: .global __aeabi_memcpy __aeabi_memcpy: + // Handle <= 2 byte copies byte-by-byte + cmp r2, #2 + ble .Lcopy1 + // Check pointer alignment eor r3, r1, r0 // JoaoBapt carry & sign bit test @@ -30,9 +34,6 @@ __aeabi_memcpy: bcs .Lcopy2 .Lcopy4: - // Handle <= 2 byte copies byte-by-byte - cmp r2, #2 - ble .Lcopy1 // Copy half and byte head rsb r3, r0, #4 diff --git a/agb/src/agbabi/mod.rs b/agb/src/agbabi/mod.rs index daee68ff..ee8dabb0 100644 --- a/agb/src/agbabi/mod.rs +++ b/agb/src/agbabi/mod.rs @@ -145,115 +145,37 @@ mod test { } #[test_case] - fn test_memcpy_bytes_with_different_sizes(_gba: &mut Gba) { - let mut input = vec![0u8; 70]; - let mut output = vec![0u8; 70]; + fn test_all_of_memcpy(_gba: &mut Gba) { + let mut input = vec![0u8; 100]; + let mut output = vec![0u8; 100]; - for size in 0..68 { - for (i, value) in input.iter_mut().enumerate() { - *value = i as u8 * 3; - } + for size in 0..80 { + for offset_input in 0..10 { + for offset_output in 0..10 { + // initialise the buffers + for (i, value) in input.iter_mut().enumerate() { + *value = i as u8; + output[i] = 0; + } - output.fill(0); + unsafe { + __agbabi_memcpy( + output.as_mut_ptr().add(offset_output), + input.as_ptr().add(offset_input), + size, + ); + } - unsafe { - __agbabi_memcpy(output.as_mut_ptr(), input.as_ptr(), size); - } - - for i in 0..size { - assert_eq!(input[i], output[i], "Failed with size = {size} at i = {i}"); - } - - for (i, value) in output.iter().enumerate().skip(size) { - assert_eq!(*value, 0, "overrun with size = {size} at i = {i}"); - } - } - } - - #[test_case] - fn test_memcpy_bytes_output_offsetted_with_different_sizes(_gba: &mut Gba) { - let mut input = vec![0u8; 70]; - let mut output = vec![0u8; 70]; - - for size in 0..60 { - for (i, value) in input.iter_mut().enumerate() { - *value = i as u8 * 3; - } - - output.fill(0); - - unsafe { - __agbabi_memcpy(output.as_mut_ptr().add(1), input.as_ptr(), size); - } - - for i in 0..size { - assert_eq!( - input[i], - output[i + 1], - "Failed with size = {size} at i = {i}" - ); - } - - for (i, value) in output.iter().enumerate().skip(size + 1) { - assert_eq!(*value, 0, "overrun with size = {size} at i = {i}"); - } - } - } - - #[test_case] - fn test_memcpy_bytes_input_offsetted_with_different_sizes(_gba: &mut Gba) { - let mut input = vec![0u8; 70]; - let mut output = vec![0u8; 70]; - - for size in 0..60 { - for (i, value) in input.iter_mut().enumerate() { - *value = i as u8 * 3; - } - - output.fill(0); - - unsafe { - __agbabi_memcpy(output.as_mut_ptr(), input.as_ptr().add(1), size); - } - - for i in 0..size { - assert_eq!( - input[i + 1], - output[i], - "Failed with size = {size} at i = {i}" - ); - } - - for (i, value) in output.iter().enumerate().skip(size) { - assert_eq!(*value, 0, "overrun with size = {size} at i = {i}"); - } - } - } - - #[test_case] - fn test_memcpy_bytes_input_output_offsetted_with_different_sizes(_gba: &mut Gba) { - let mut input = vec![0u8; 70]; - let mut output = vec![0u8; 70]; - - for size in 0..60 { - for (i, value) in input.iter_mut().enumerate() { - *value = i as u8 * 3; - } - - output.fill(0); - - unsafe { - __agbabi_memcpy(output.as_mut_ptr().add(1), input.as_ptr().add(1), size); - } - - assert_eq!(output[0], 0); - - for i in 1..size + 1 { - assert_eq!(input[i], output[i], "Failed with size = {size} at i = {i}"); - } - - for (i, value) in output.iter().enumerate().skip(size + 1) { - assert_eq!(*value, 0, "overrun with size = {size} at i = {i}"); + for (i, &v) in output.iter().enumerate() { + if i < offset_output { + assert_eq!(v, 0, "underrun, size: {size}, input offset: {offset_input}, output offset: {offset_output}, i: {i}"); + } else if i < offset_output + size { + assert_eq!(v, (i - offset_output + offset_input) as u8, "incorrect copy, size: {size}, input offset: {offset_input}, output offset: {offset_output}, i: {i}"); + } else { + assert_eq!(v, 0, "overrun, size: {size}, input offset: {offset_input}, output offset: {offset_output}, i: {i}"); + } + } + } } } }