Browse Source

feat: all cases fetch support in rust ffi

legacy
Dnomd343 2 years ago
parent
commit
37487c95a6
  1. 5
      src/cli/src/main.rs
  2. 62
      src/rust_ffi/src/all_cases.rs
  3. 4
      src/rust_ffi/src/lib.rs

5
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);
}
}

62
src/rust_ffi/src/all_cases.rs

@ -1,37 +1,71 @@
use crate::core::Core;
lazy_static! {
static ref BASIC_RANGES: Vec<u32> = {
unsafe {
Core::basic_ranges_build();
let mut buffer: Vec<u32> = vec![0; Core::BASIC_RANGES_SIZE as usize];
Core::export_basic_ranges(&mut buffer[0]);
buffer
}
};
static ref ALL_CASES: Vec<u64> = build_all_cases();
static ref BASIC_RANGES: Vec<u32> = build_basic_ranges();
}
pub fn demo() {
println!("demo start");
#[inline]
pub fn all_cases_fetch() -> &'static Vec<u64> {
&(*ALL_CASES)
}
#[inline]
#[allow(dead_code)]
pub fn basic_ranges_fetch() -> &'static Vec<u32> {
&(*BASIC_RANGES)
}
println!("demo complete");
fn build_all_cases() -> Vec<u64> {
unsafe {
Core::all_cases_build();
let mut buffer: Vec<u64> = vec![0; Core::ALL_CASES_SIZE as usize];
Core::export_all_cases(&mut buffer[0]);
buffer
}
}
fn build_basic_ranges() -> Vec<u32> {
unsafe {
Core::basic_ranges_build();
let mut buffer: Vec<u32> = 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) {

4
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;

Loading…
Cancel
Save