From 37487c95a6335ac881a106a139689eb79367ae34 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 28 Feb 2023 20:29:08 +0800 Subject: [PATCH] feat: all cases fetch support in rust ffi --- src/cli/src/main.rs | 5 ++- src/rust_ffi/src/all_cases.rs | 62 +++++++++++++++++++++++++++-------- src/rust_ffi/src/lib.rs | 4 +-- 3 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/cli/src/main.rs b/src/cli/src/main.rs index e8c27ac..bd245b9 100644 --- a/src/cli/src/main.rs +++ b/src/cli/src/main.rs @@ -40,6 +40,9 @@ fn main() { // println!("{} => {}", r, r.to_common_code()); - klotski_ffi::demo(); + let all_cases = klotski_ffi::all_cases_fetch(); + for &code in &all_cases[..16] { + println!("{:09X}", code); + } } diff --git a/src/rust_ffi/src/all_cases.rs b/src/rust_ffi/src/all_cases.rs index ae21b6f..4668c0e 100644 --- a/src/rust_ffi/src/all_cases.rs +++ b/src/rust_ffi/src/all_cases.rs @@ -1,37 +1,71 @@ use crate::core::Core; lazy_static! { - static ref BASIC_RANGES: Vec = { - unsafe { - Core::basic_ranges_build(); - let mut buffer: Vec = vec![0; Core::BASIC_RANGES_SIZE as usize]; - Core::export_basic_ranges(&mut buffer[0]); - buffer - } - }; + static ref ALL_CASES: Vec = build_all_cases(); + static ref BASIC_RANGES: Vec = build_basic_ranges(); } -pub fn demo() { - - println!("demo start"); - +#[inline] +pub fn all_cases_fetch() -> &'static Vec { + &(*ALL_CASES) +} +#[inline] +#[allow(dead_code)] +pub fn basic_ranges_fetch() -> &'static Vec { + &(*BASIC_RANGES) +} - println!("demo complete"); +fn build_all_cases() -> Vec { + unsafe { + Core::all_cases_build(); + let mut buffer: Vec = vec![0; Core::ALL_CASES_SIZE as usize]; + Core::export_all_cases(&mut buffer[0]); + buffer + } +} +fn build_basic_ranges() -> Vec { + unsafe { + Core::basic_ranges_build(); + let mut buffer: Vec = vec![0; Core::BASIC_RANGES_SIZE as usize]; + Core::export_basic_ranges(&mut buffer[0]); + buffer + } } #[cfg(test)] mod tests { use crypto::md5::Md5; use crypto::digest::Digest; - use crate::all_cases::BASIC_RANGES; + use crate::all_cases::{ALL_CASES, all_cases_fetch, BASIC_RANGES, basic_ranges_fetch}; const ALL_CASES_MD5: &str = "3888e9fab8d3cbb50908b12b147cfb23"; const BASIC_RANGES_MD5: &str = "6f385dc171e201089ff96bb010b47212"; + #[test] + fn all_cases() { + assert_eq!(&(*ALL_CASES), all_cases_fetch()); + } + #[test] fn basic_ranges() { + assert_eq!(&(*BASIC_RANGES), basic_ranges_fetch()); + } + + #[test] + fn all_cases_data() { + let mut md5 = Md5::new(); + let mut tmp = String::new(); + for &common_code in &(*ALL_CASES) { + tmp.push_str(&format!("{:09X}\n", common_code)); + } + md5.input_str(&tmp); + assert_eq!(md5.result_str(), ALL_CASES_MD5); + } + + #[test] + fn basic_ranges_data() { let mut md5 = Md5::new(); let mut tmp = String::new(); for &range in &(*BASIC_RANGES) { diff --git a/src/rust_ffi/src/lib.rs b/src/rust_ffi/src/lib.rs index c09e78a..fb3e24e 100644 --- a/src/rust_ffi/src/lib.rs +++ b/src/rust_ffi/src/lib.rs @@ -10,6 +10,6 @@ pub use codec::RawCode; pub use codec::ShortCode; pub use codec::CommonCode; -pub use metadata::load_metadata as metadata; +pub use all_cases::all_cases_fetch; -pub use all_cases::demo; +pub use metadata::load_metadata as metadata;