From 1959c20b6e4863808b7df35cb6e3ad5e1a781824 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 1 Feb 2023 19:44:19 +0100 Subject: [PATCH] Debuging and Fixes --- src/lib.rs | 48 ++++++++++++++++++++++++++------------------ tests/Pattern.schem | Bin 0 -> 228 bytes tests/Random.schem | Bin 0 -> 256 bytes 3 files changed, 29 insertions(+), 19 deletions(-) create mode 100644 tests/Pattern.schem create mode 100644 tests/Random.schem diff --git a/src/lib.rs b/src/lib.rs index baecb33..1c0bcfa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,35 +33,43 @@ pub fn search( let mut matches: Vec<(u16, u16, u16)> = Vec::new(); - for i in 0..schem.width - pattern_schem.width { - for j in 0..schem.height - pattern_schem.height { - for k in 0..schem.length - pattern_schem.length { + println!("{:?}", schem); + println!("{:?}", pattern_schem); + + for x in 0..=schem.width - pattern_schem.width { + for y in 0..=schem.height - pattern_schem.height { + for z in 0..=schem.length - pattern_schem.length { let mut match_found = true; - for x in 0..pattern_schem.width { - for y in 0..pattern_schem.height { - for z in 0..pattern_schem.length { - let index = (i + x) as usize + (j + y) as usize * schem.width as usize + (k + z) as usize * schem.width as usize * schem.height as usize; - let pattern_index = x as usize + y as usize * pattern_schem.width as usize + z as usize * pattern_schem.width as usize * pattern_schem.height as usize; - if schem.block_data[index] != pattern_schem.block_data[pattern_index] { + 'outer: for i in 0..pattern_schem.width { + for j in 0..pattern_schem.height { + for k in 0..pattern_schem.length { + let index = (x + i) + (y + j) * schem.width + (z + k) * schem.width * schem.height; + let pattern_index = i + j * pattern_schem.width + k * pattern_schem.width * pattern_schem.height; + if schem.block_data.get(index as usize) != pattern_schem.block_data.get(pattern_index as usize) { match_found = false; - break; + break 'outer; } } - if !match_found { - break; - } - } - if !match_found { - break; } } if match_found { - matches.push((i, j, k)); + matches.push((x, y, z)); } } } } + /* + [ + 0, -1, 1, 1, 2, + 0, -1, 2, 1, 0, + 2, -1, -1, 2, -1, + 2, 0, 0, 2, -1, + 2, 1, 2, 2, 1 + ] + + */ + return matches; } @@ -128,9 +136,9 @@ mod tests { #[test] fn test_search() { - let file = std::fs::File::open("tests/simple.schem").expect("Failed to open file"); + let file = std::fs::File::open("tests/Random.schem").expect("Failed to open file"); let schematic = &std::io::Read::bytes(file).map(|b| b.unwrap()).collect(); - let file = std::fs::File::open("tests/endstone.schem").expect("Failed to open file"); + let file = std::fs::File::open("tests/Pattern.schem").expect("Failed to open file"); let pattern = &std::io::Read::bytes(file).map(|b| b.unwrap()).collect(); let matches = search(schematic, pattern, SearchBehavior { @@ -140,5 +148,7 @@ mod tests { }); println!("{:?}", matches); + assert_eq!(matches.len(), 1); + assert_eq!(matches[0], (1, 0, 3)); } } diff --git a/tests/Pattern.schem b/tests/Pattern.schem new file mode 100644 index 0000000000000000000000000000000000000000..76671b91e058a5ee73bcf82d4e1fc4a5ba368da1 GIT binary patch literal 228 zcmVH^gV(99DeTP!Lx2MM8VqhGhw~ z7v_Gw-ptJo10kG`d+tew^tI|}quEpn*^9QsOiTR*KxIe{gu?a4II4Qx@ZFBA_1n*_ z_f}kI3bRpbdm?v~pl~%xJ=I4c5+zwtKN#-wCQbvbZ&jM4zzgBjBnlm literal 0 HcmV?d00001 diff --git a/tests/Random.schem b/tests/Random.schem new file mode 100644 index 0000000000000000000000000000000000000000..a53a57edb7b4b38bdfd0fc53259c728c5e6cdd32 GIT binary patch literal 256 zcmV+b0ssCViwFP!00000|5Z;-OT;h`oo3vvZS^AV!QUW=XK#uX4=yeUcJ(5mowmVj zx}+nk|6S0DZta>7hMD&LO2>RPt^dR-1w1vZ#oz36-@8QHQd4^h5b3P zt~6_e?B=q_N1mvXBEPTep7`lY&QAX