From f730407978cd4e927a3ccb92f645880b7009a16b Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 27 Feb 2023 00:03:01 +0800 Subject: [PATCH] test: rust codec ffi test framework --- src/cli/src/main.rs | 2 +- src/rust_ffi/src/codec/ffi.rs | 23 +++++++---- src/rust_ffi/src/codec/mod.rs | 3 +- src/rust_ffi/src/codec/tests.rs | 71 +++++++++++++++++++++++++++++++++ src/rust_ffi/src/lib.rs | 4 +- src/rust_ffi/src/metadata.rs | 21 +++++++++- test/ffi/codec.cc | 4 +- 7 files changed, 112 insertions(+), 16 deletions(-) create mode 100644 src/rust_ffi/src/codec/tests.rs diff --git a/src/cli/src/main.rs b/src/cli/src/main.rs index f39b6e8..f53f6eb 100644 --- a/src/cli/src/main.rs +++ b/src/cli/src/main.rs @@ -1,7 +1,7 @@ extern crate klotski_ffi; fn main() { - // println!("{:#?}", klotski_ffi::load_metadata()); + println!("{:#?}", klotski_ffi::metadata()); // println!("{}", klotski_ffi::is_short_code_available()); // println!("{}", klotski_ffi::is_short_code_available_fast()); diff --git a/src/rust_ffi/src/codec/ffi.rs b/src/rust_ffi/src/codec/ffi.rs index fdb634f..4a99a26 100644 --- a/src/rust_ffi/src/codec/ffi.rs +++ b/src/rust_ffi/src/codec/ffi.rs @@ -1,43 +1,50 @@ -use std::ffi::{c_char, CString}; use super::Core; +use std::ffi::{c_char, CString}; -fn short_code_enable() { +#[allow(dead_code)] +pub fn short_code_enable() { unsafe { Core::short_code_enable() } } -fn short_code_enable_fast() { +#[allow(dead_code)] +pub fn short_code_enable_fast() { unsafe { Core::short_code_enable_fast() } } -fn is_short_code_available() -> bool { +#[allow(dead_code)] +pub fn is_short_code_available() -> bool { unsafe { Core::is_short_code_available() } } -fn is_short_code_available_fast() -> bool { +#[allow(dead_code)] +pub fn is_short_code_available_fast() -> bool { unsafe { Core::is_short_code_available_fast() } } -fn raw_code_check(raw_code: u64) -> bool { +#[allow(dead_code)] +pub fn raw_code_check(raw_code: u64) -> bool { unsafe { Core::raw_code_check(raw_code) } } -fn short_code_check(short_code: u32) -> bool { +#[allow(dead_code)] +pub fn short_code_check(short_code: u32) -> bool { unsafe { Core::short_code_check(short_code) } } -fn common_code_check(common_code: u64) -> bool { +#[allow(dead_code)] +pub fn common_code_check(common_code: u64) -> bool { unsafe { Core::common_code_check(common_code) } diff --git a/src/rust_ffi/src/codec/mod.rs b/src/rust_ffi/src/codec/mod.rs index 8b84988..b92c889 100644 --- a/src/rust_ffi/src/codec/mod.rs +++ b/src/rust_ffi/src/codec/mod.rs @@ -1,3 +1,4 @@ -pub mod ffi; +mod ffi; +mod tests; use super::core::Core; diff --git a/src/rust_ffi/src/codec/tests.rs b/src/rust_ffi/src/codec/tests.rs new file mode 100644 index 0000000..baa00b8 --- /dev/null +++ b/src/rust_ffi/src/codec/tests.rs @@ -0,0 +1,71 @@ +mod ffi { + #[allow(unused_imports)] + use crate::codec::ffi as codec_ffi; + + #[test] + fn warm_up() { + assert_eq!(codec_ffi::is_short_code_available(), false); + codec_ffi::short_code_enable(); + assert_eq!(codec_ffi::is_short_code_available(), true); + + assert_eq!(codec_ffi::is_short_code_available_fast(), false); + codec_ffi::short_code_enable_fast(); + assert_eq!(codec_ffi::is_short_code_available_fast(), true); + } + + #[allow(dead_code)] + const TEST_SHORT_CODE_OK: u32 = 4091296; + + #[allow(dead_code)] + const TEST_SHORT_CODE_STR_OK: &str = "4WVE1"; + + #[allow(dead_code)] + const TEST_COMMON_CODE_OK: u64 = 0x1_A9BF_0C00; + + #[allow(dead_code)] + const TEST_COMMON_CODE_STR_OK: &str = "1A9BF0C00"; + + #[allow(dead_code)] + const TEST_COMMON_CODE_STR_SHR_OK: &str = "1A9BF0C"; + + #[allow(dead_code)] + const TEST_RAW_CODE_OK: u64 = 0x0603_EDF5_CAFF_F5E2; + + #[allow(dead_code)] + const TEST_SHORT_CODE_ERR: u32 = 29670987; + + #[allow(dead_code)] + const TEST_COMMON_CODE_ERR: u64 = 0x1_2190_2300; + + #[allow(dead_code)] + const TEST_RAW_CODE_ERR: u64 = 0x0A34_182B_3810_2D21; + + #[allow(dead_code)] + const TEST_SHORT_CODE_STR_ERR: &str = "R50EH"; + + #[allow(dead_code)] + const TEST_COMMON_CODE_STR_ERR: &str = "123J432A9"; + + #[test] + fn checker() { + assert_eq!(codec_ffi::raw_code_check(TEST_RAW_CODE_OK), true); + assert_eq!(codec_ffi::raw_code_check(TEST_RAW_CODE_ERR), false); + + assert_eq!(codec_ffi::short_code_check(TEST_SHORT_CODE_OK), true); + assert_eq!(codec_ffi::short_code_check(TEST_SHORT_CODE_ERR), false); + + assert_eq!(codec_ffi::common_code_check(TEST_COMMON_CODE_OK), true); + assert_eq!(codec_ffi::common_code_check(TEST_COMMON_CODE_ERR), false); + } + + // TODO: string + + // TODO: convert + + // TODO: convert_unsafe + + // TODO: mirror + + // TODO: mirror_unsafe + +} diff --git a/src/rust_ffi/src/lib.rs b/src/rust_ffi/src/lib.rs index 8e9d58a..a8ae7f5 100644 --- a/src/rust_ffi/src/lib.rs +++ b/src/rust_ffi/src/lib.rs @@ -2,6 +2,4 @@ mod core; mod codec; mod metadata; -pub use metadata::load_metadata; - -pub use codec::ffi::*; +pub use metadata::load_metadata as metadata; diff --git a/src/rust_ffi/src/metadata.rs b/src/rust_ffi/src/metadata.rs index c94c371..babe469 100644 --- a/src/rust_ffi/src/metadata.rs +++ b/src/rust_ffi/src/metadata.rs @@ -1,4 +1,4 @@ -use super::core::Core; +use crate::core::Core; use Core::CStringDump; #[derive(Debug)] @@ -35,3 +35,22 @@ pub fn load_metadata() -> Metadata { } } } + +#[test] +fn tests() { + let metadata = load_metadata(); + + assert_ne!(metadata.author, ""); + assert_ne!(metadata.system, ""); + assert_ne!(metadata.git_tag, ""); + assert_ne!(metadata.version, ""); + assert_ne!(metadata.compiler, ""); + assert_ne!(metadata.commit_id, ""); + assert_ne!(metadata.build_time, ""); + assert_ne!(metadata.git_branch, ""); + assert_ne!(metadata.project_url, ""); + + assert!(metadata.version_patch < 65536); + assert!(metadata.version_minor < 65536); + assert!(metadata.version_patch < 65536); +} diff --git a/test/ffi/codec.cc b/test/ffi/codec.cc index c834115..d5cb35c 100644 --- a/test/ffi/codec.cc +++ b/test/ffi/codec.cc @@ -151,7 +151,6 @@ TEST(FFI, codec_mirror) { bool result; uint64_t raw_code; - /// test of safe version EXPECT_EQ(is_vertical_mirror(TEST_RAW_CODE_ERR, &result), false); EXPECT_EQ(is_horizontal_mirror(TEST_RAW_CODE_ERR, &result), false); EXPECT_EQ(to_vertical_mirror(TEST_RAW_CODE_ERR, &raw_code), false); @@ -174,8 +173,9 @@ TEST(FFI, codec_mirror) { EXPECT_EQ(raw_code, TEST_MIRROR_H2); EXPECT_EQ(to_horizontal_mirror(TEST_MIRROR_H2, &raw_code), true); EXPECT_EQ(raw_code, TEST_MIRROR_H1); +} - /// test of unsafe version +TEST(FFI, codec_mirror_unsafe) { EXPECT_EQ(is_vertical_mirror_unsafe(TEST_MIRROR_V), true); EXPECT_EQ(is_vertical_mirror_unsafe(TEST_MIRROR_V1), false); EXPECT_EQ(is_vertical_mirror_unsafe(TEST_MIRROR_V2), false);