From 8ceabc516a773756ce41d1304ec41770131e7818 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 27 Feb 2023 21:28:41 +0800 Subject: [PATCH] test: complete rust codec ffi test --- src/klotski_core/ffi/tmain.cc | 9 +- src/rust_ffi/src/codec/ffi.rs | 84 ++++++---- src/rust_ffi/src/codec/mod.rs | 2 +- src/rust_ffi/src/codec/tests.rs | 282 +++++++++++++++++++++++++++++--- test/ffi/codec.cc | 12 +- 5 files changed, 333 insertions(+), 56 deletions(-) diff --git a/src/klotski_core/ffi/tmain.cc b/src/klotski_core/ffi/tmain.cc index 180ea9f..004c9bb 100644 --- a/src/klotski_core/ffi/tmain.cc +++ b/src/klotski_core/ffi/tmain.cc @@ -21,9 +21,14 @@ void tmain() { // std::cout << ret.to_common_code() << std::endl << ret << std::endl; // klotski::RawCode::from_common_code(0x1A9BF0C00).is_vertical_mirror(); - auto ret = klotski::RawCode::from_common_code(0x1A9BF0C00).to_vertical_mirror(); +// auto ret = klotski::RawCode::from_common_code(0x1A9BF0C00).to_vertical_mirror(); +// +// std::cout << ret.to_common_code() << std::endl << ret << std::endl; - std::cout << ret.to_common_code() << std::endl << ret << std::endl; +// std::cout << klotski::RawCode::from_common_code(0x4FEA13400) << std::endl; +// std::cout << klotski::RawCode::from_common_code(0x8346AFC00) << std::endl; +// std::cout << klotski::RawCode::from_common_code(0x1A9BF0C00) << std::endl; + std::cout << klotski::RawCode::from_common_code(0x6BFA47000) << std::endl; return; diff --git a/src/rust_ffi/src/codec/ffi.rs b/src/rust_ffi/src/codec/ffi.rs index 4a99a26..a154712 100644 --- a/src/rust_ffi/src/codec/ffi.rs +++ b/src/rust_ffi/src/codec/ffi.rs @@ -50,7 +50,8 @@ pub fn common_code_check(common_code: u64) -> bool { } } -fn raw_code_to_short_code(raw_code: u64) -> Result { +#[allow(dead_code)] +pub fn raw_code_to_short_code(raw_code: u64) -> Result { let mut short_code: u32 = 0; unsafe { match Core::raw_code_to_short_code(raw_code, &mut short_code) { @@ -60,7 +61,8 @@ fn raw_code_to_short_code(raw_code: u64) -> Result { } } -fn short_code_to_raw_code(short_code: u32) -> Result { +#[allow(dead_code)] +pub fn short_code_to_raw_code(short_code: u32) -> Result { let mut raw_code: u64 = 0; unsafe { match Core::short_code_to_raw_code(short_code, &mut raw_code) { @@ -70,7 +72,8 @@ fn short_code_to_raw_code(short_code: u32) -> Result { } } -fn raw_code_to_common_code(raw_code: u64) -> Result { +#[allow(dead_code)] +pub fn raw_code_to_common_code(raw_code: u64) -> Result { let mut common_code: u64 = 0; unsafe { match Core::raw_code_to_common_code(raw_code, &mut common_code) { @@ -80,7 +83,8 @@ fn raw_code_to_common_code(raw_code: u64) -> Result { } } -fn common_code_to_raw_code(common_code: u64) -> Result { +#[allow(dead_code)] +pub fn common_code_to_raw_code(common_code: u64) -> Result { let mut raw_code: u64 = 0; unsafe { match Core::common_code_to_raw_code(common_code, &mut raw_code) { @@ -90,7 +94,8 @@ fn common_code_to_raw_code(common_code: u64) -> Result { } } -fn short_code_to_common_code(short_code: u32) -> Result { +#[allow(dead_code)] +pub fn short_code_to_common_code(short_code: u32) -> Result { let mut common_code: u64 = 0; unsafe { match Core::short_code_to_common_code(short_code, &mut common_code) { @@ -100,7 +105,8 @@ fn short_code_to_common_code(short_code: u32) -> Result { } } -fn common_code_to_short_code(common_code: u64) -> Result { +#[allow(dead_code)] +pub fn common_code_to_short_code(common_code: u64) -> Result { let mut short_code: u32 = 0; unsafe { match Core::common_code_to_short_code(common_code, &mut short_code) { @@ -110,43 +116,50 @@ fn common_code_to_short_code(common_code: u64) -> Result { } } -fn raw_code_to_short_code_unsafe(raw_code: u64) -> u32 { +#[allow(dead_code)] +pub fn raw_code_to_short_code_unsafe(raw_code: u64) -> u32 { unsafe { Core::raw_code_to_short_code_unsafe(raw_code) } } -fn short_code_to_raw_code_unsafe(short_code: u32) -> u64 { +#[allow(dead_code)] +pub fn short_code_to_raw_code_unsafe(short_code: u32) -> u64 { unsafe { Core::short_code_to_raw_code_unsafe(short_code) } } -fn raw_code_to_common_code_unsafe(raw_code: u64) -> u64 { +#[allow(dead_code)] +pub fn raw_code_to_common_code_unsafe(raw_code: u64) -> u64 { unsafe { Core::raw_code_to_common_code_unsafe(raw_code) } } -fn common_code_to_raw_code_unsafe(common_code: u64) -> u64 { +#[allow(dead_code)] +pub fn common_code_to_raw_code_unsafe(common_code: u64) -> u64 { unsafe { Core::common_code_to_raw_code_unsafe(common_code) } } -fn short_code_to_common_code_unsafe(short_code: u32) -> u64 { +#[allow(dead_code)] +pub fn short_code_to_common_code_unsafe(short_code: u32) -> u64 { unsafe { Core::short_code_to_common_code_unsafe(short_code) } } -fn common_code_to_short_code_unsafe(common_code: u64) -> u32 { +#[allow(dead_code)] +pub fn common_code_to_short_code_unsafe(common_code: u64) -> u32 { unsafe { Core::common_code_to_short_code_unsafe(common_code) } } -fn is_vertical_mirror(raw_code: u64) -> Result { +#[allow(dead_code)] +pub fn is_vertical_mirror(raw_code: u64) -> Result { let mut result: bool = false; unsafe { match Core::is_vertical_mirror(raw_code, &mut result) { @@ -156,7 +169,8 @@ fn is_vertical_mirror(raw_code: u64) -> Result { } } -fn is_horizontal_mirror(raw_code: u64) -> Result { +#[allow(dead_code)] +pub fn is_horizontal_mirror(raw_code: u64) -> Result { let mut result: bool = false; unsafe { match Core::is_horizontal_mirror(raw_code, &mut result) { @@ -166,7 +180,8 @@ fn is_horizontal_mirror(raw_code: u64) -> Result { } } -fn to_vertical_mirror(raw_code: u64) -> Result { +#[allow(dead_code)] +pub fn to_vertical_mirror(raw_code: u64) -> Result { let mut result: u64 = 0; unsafe { match Core::to_vertical_mirror(raw_code, &mut result) { @@ -176,7 +191,8 @@ fn to_vertical_mirror(raw_code: u64) -> Result { } } -fn to_horizontal_mirror(raw_code: u64) -> Result { +#[allow(dead_code)] +pub fn to_horizontal_mirror(raw_code: u64) -> Result { let mut result: u64 = 0; unsafe { match Core::to_horizontal_mirror(raw_code, &mut result) { @@ -186,31 +202,36 @@ fn to_horizontal_mirror(raw_code: u64) -> Result { } } -fn is_vertical_mirror_unsafe(raw_code: u64) -> bool { +#[allow(dead_code)] +pub fn is_vertical_mirror_unsafe(raw_code: u64) -> bool { unsafe { Core::is_vertical_mirror_unsafe(raw_code) } } -fn is_horizontal_mirror_unsafe(raw_code: u64) -> bool { +#[allow(dead_code)] +pub fn is_horizontal_mirror_unsafe(raw_code: u64) -> bool { unsafe { Core::is_horizontal_mirror_unsafe(raw_code) } } -fn to_vertical_mirror_unsafe(raw_code: u64) -> u64 { +#[allow(dead_code)] +pub fn to_vertical_mirror_unsafe(raw_code: u64) -> u64 { unsafe { Core::to_vertical_mirror_unsafe(raw_code) } } -fn to_horizontal_mirror_unsafe(raw_code: u64) -> u64 { +#[allow(dead_code)] +pub fn to_horizontal_mirror_unsafe(raw_code: u64) -> u64 { unsafe { Core::to_horizontal_mirror_unsafe(raw_code) } } -fn short_code_to_string(short_code: u32) -> Result { +#[allow(dead_code)] +pub fn short_code_to_string(short_code: u32) -> Result { unsafe { let mut buffer: Vec = vec![0; Core::SHORT_CODE_STR_SIZE as usize]; match Core::short_code_to_string(short_code, buffer.as_mut_ptr()) { @@ -226,7 +247,8 @@ fn short_code_to_string(short_code: u32) -> Result { } } -fn short_code_to_string_unsafe(short_code: u32) -> String { +#[allow(dead_code)] +pub fn short_code_to_string_unsafe(short_code: u32) -> String { unsafe { let mut buffer: Vec = vec![0; Core::SHORT_CODE_STR_SIZE as usize]; Core::short_code_to_string(short_code, buffer.as_mut_ptr()); @@ -238,7 +260,8 @@ fn short_code_to_string_unsafe(short_code: u32) -> String { } } -fn short_code_from_string(short_code: &str) -> Result { +#[allow(dead_code)] +pub fn short_code_from_string(short_code: &str) -> Result { unsafe { let mut result: u32 = 0; match Core::short_code_from_string( @@ -251,7 +274,8 @@ fn short_code_from_string(short_code: &str) -> Result { } } -fn common_code_to_string(common_code: u64) -> Result { +#[allow(dead_code)] +pub fn common_code_to_string(common_code: u64) -> Result { unsafe { let mut buffer: Vec = vec![0; Core::COMMON_CODE_STR_SIZE as usize]; match Core::common_code_to_string(common_code, buffer.as_mut_ptr()) { @@ -267,7 +291,8 @@ fn common_code_to_string(common_code: u64) -> Result { } } -fn common_code_to_string_unsafe(common_code: u64) -> String { +#[allow(dead_code)] +pub fn common_code_to_string_unsafe(common_code: u64) -> String { unsafe { let mut buffer: Vec = vec![0; Core::COMMON_CODE_STR_SIZE as usize]; Core::common_code_to_string_unsafe(common_code, buffer.as_mut_ptr()); @@ -279,7 +304,8 @@ fn common_code_to_string_unsafe(common_code: u64) -> String { } } -fn common_code_to_string_shorten(common_code: u64) -> Result { +#[allow(dead_code)] +pub fn common_code_to_string_shorten(common_code: u64) -> Result { unsafe { let mut buffer: Vec = vec![0; Core::COMMON_CODE_STR_SIZE as usize]; match Core::common_code_to_string_shorten(common_code, buffer.as_mut_ptr()) { @@ -296,7 +322,8 @@ fn common_code_to_string_shorten(common_code: u64) -> Result String { +#[allow(dead_code)] +pub fn common_code_to_string_shorten_unsafe(common_code: u64) -> String { unsafe { let mut buffer: Vec = vec![0; Core::COMMON_CODE_STR_SIZE as usize]; Core::common_code_to_string_shorten(common_code, buffer.as_mut_ptr()); @@ -309,7 +336,8 @@ fn common_code_to_string_shorten_unsafe(common_code: u64) -> String { } } -fn common_code_from_string(common_code: &str) -> Result { +#[allow(dead_code)] +pub fn common_code_from_string(common_code: &str) -> Result { unsafe { let mut result: u64 = 0; match Core::common_code_from_string( diff --git a/src/rust_ffi/src/codec/mod.rs b/src/rust_ffi/src/codec/mod.rs index b92c889..733a2de 100644 --- a/src/rust_ffi/src/codec/mod.rs +++ b/src/rust_ffi/src/codec/mod.rs @@ -1,4 +1,4 @@ mod ffi; mod tests; -use super::core::Core; +use crate::core::Core; diff --git a/src/rust_ffi/src/codec/tests.rs b/src/rust_ffi/src/codec/tests.rs index baa00b8..24b5db6 100644 --- a/src/rust_ffi/src/codec/tests.rs +++ b/src/rust_ffi/src/codec/tests.rs @@ -1,5 +1,6 @@ +#[cfg(test)] mod ffi { - #[allow(unused_imports)] + // #[allow(unused_imports)] use crate::codec::ffi as codec_ffi; #[test] @@ -13,39 +14,28 @@ mod ffi { 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"; + const TEST_MIRROR_V: u64 = 0x0_FC0_480_6DB_FC0_480; + const TEST_MIRROR_V1: u64 = 0x0_E58_FC8_5FF_EBC_4DB; + const TEST_MIRROR_V2: u64 = 0x0_EDB_5FF_EBC_5C8_E58; + + const TEST_MIRROR_H: u64 = 0x0_603_EDF_5CA_FFF_5E2; + const TEST_MIRROR_H1: u64 = 0x0_E58_FC8_5FF_EBC_4DB; + const TEST_MIRROR_H2: u64 = 0x0_0F9_1CF_FFA_F17_6DA; + #[test] fn checker() { assert_eq!(codec_ffi::raw_code_check(TEST_RAW_CODE_OK), true); @@ -58,14 +48,258 @@ mod ffi { assert_eq!(codec_ffi::common_code_check(TEST_COMMON_CODE_ERR), false); } + #[test] + fn convert() { + // raw code -> short code + assert!(codec_ffi::raw_code_to_short_code(TEST_RAW_CODE_ERR).is_err()); + assert!(codec_ffi::raw_code_to_short_code(TEST_RAW_CODE_OK).is_ok()); + assert_eq!( + codec_ffi::raw_code_to_short_code(TEST_RAW_CODE_OK).unwrap(), + TEST_SHORT_CODE_OK + ); + + // short code -> raw code + assert!(codec_ffi::short_code_to_raw_code(TEST_SHORT_CODE_ERR).is_err()); + assert!(codec_ffi::short_code_to_raw_code(TEST_SHORT_CODE_OK).is_ok()); + assert_eq!( + codec_ffi::short_code_to_raw_code(TEST_SHORT_CODE_OK).unwrap(), + TEST_RAW_CODE_OK + ); + + // raw code -> common code + assert!(codec_ffi::raw_code_to_common_code(TEST_RAW_CODE_ERR).is_err()); + assert!(codec_ffi::raw_code_to_common_code(TEST_RAW_CODE_OK).is_ok()); + assert_eq!( + codec_ffi::raw_code_to_common_code(TEST_RAW_CODE_OK).unwrap(), + TEST_COMMON_CODE_OK + ); + + // common code -> raw code + assert!(codec_ffi::common_code_to_raw_code(TEST_COMMON_CODE_ERR).is_err()); + assert!(codec_ffi::common_code_to_raw_code(TEST_COMMON_CODE_OK).is_ok()); + assert_eq!( + codec_ffi::common_code_to_raw_code(TEST_COMMON_CODE_OK).unwrap(), + TEST_RAW_CODE_OK + ); + + // short code -> common code + assert!(codec_ffi::short_code_to_common_code(TEST_SHORT_CODE_ERR).is_err()); + assert!(codec_ffi::short_code_to_common_code(TEST_SHORT_CODE_OK).is_ok()); + assert_eq!( + codec_ffi::short_code_to_common_code(TEST_SHORT_CODE_OK).unwrap(), + TEST_COMMON_CODE_OK + ); + + // common code -> short code + assert!(codec_ffi::common_code_to_short_code(TEST_COMMON_CODE_ERR).is_err()); + assert!(codec_ffi::common_code_to_short_code(TEST_COMMON_CODE_OK).is_ok()); + assert_eq!( + codec_ffi::common_code_to_short_code(TEST_COMMON_CODE_OK).unwrap(), + TEST_SHORT_CODE_OK + ); + } + + #[test] + fn convert_unsafe() { + // raw code <---> short code + assert_eq!( + codec_ffi::raw_code_to_short_code_unsafe(TEST_RAW_CODE_OK), + TEST_SHORT_CODE_OK + ); + assert_eq!( + codec_ffi::short_code_to_raw_code_unsafe(TEST_SHORT_CODE_OK), + TEST_RAW_CODE_OK + ); + + // raw code <---> common code + assert_eq!( + codec_ffi::raw_code_to_common_code_unsafe(TEST_RAW_CODE_OK), + TEST_COMMON_CODE_OK + ); + assert_eq!( + codec_ffi::common_code_to_raw_code_unsafe(TEST_COMMON_CODE_OK), + TEST_RAW_CODE_OK + ); + + // short code <---> common code + assert_eq!( + codec_ffi::short_code_to_common_code_unsafe(TEST_SHORT_CODE_OK), + TEST_COMMON_CODE_OK + ); + assert_eq!( + codec_ffi::common_code_to_short_code_unsafe(TEST_COMMON_CODE_OK), + TEST_SHORT_CODE_OK + ); + } + + #[test] + fn mirror() { + assert!(codec_ffi::is_vertical_mirror(TEST_RAW_CODE_ERR).is_err()); + assert!(codec_ffi::is_horizontal_mirror(TEST_RAW_CODE_ERR).is_err()); + assert!(codec_ffi::to_vertical_mirror(TEST_RAW_CODE_ERR).is_err()); + assert!(codec_ffi::to_horizontal_mirror(TEST_RAW_CODE_ERR).is_err()); + + let tmp = codec_ffi::is_vertical_mirror(TEST_RAW_CODE_OK); + assert!(tmp.is_ok()); + assert_eq!(tmp.unwrap(), false); + let tmp = codec_ffi::is_vertical_mirror(TEST_MIRROR_V1); + assert!(tmp.is_ok()); + assert_eq!(tmp.unwrap(), false); + let tmp = codec_ffi::is_vertical_mirror(TEST_MIRROR_V2); + assert!(tmp.is_ok()); + assert_eq!(tmp.unwrap(), false); + + let tmp = codec_ffi::to_vertical_mirror(TEST_MIRROR_V1); + assert!(tmp.is_ok()); + assert_eq!(tmp.unwrap(), TEST_MIRROR_V2); + let tmp = codec_ffi::to_vertical_mirror(TEST_MIRROR_V2); + assert!(tmp.is_ok()); + assert_eq!(tmp.unwrap(), TEST_MIRROR_V1); + + let tmp = codec_ffi::is_horizontal_mirror(TEST_MIRROR_H); + assert!(tmp.is_ok()); + assert_eq!(tmp.unwrap(), true); + let tmp = codec_ffi::is_horizontal_mirror(TEST_MIRROR_H1); + assert!(tmp.is_ok()); + assert_eq!(tmp.unwrap(), false); + let tmp = codec_ffi::is_horizontal_mirror(TEST_MIRROR_H2); + assert!(tmp.is_ok()); + assert_eq!(tmp.unwrap(), false); + + let tmp = codec_ffi::to_horizontal_mirror(TEST_MIRROR_H); + assert!(tmp.is_ok()); + assert_eq!(tmp.unwrap(), TEST_MIRROR_H); + let tmp = codec_ffi::to_horizontal_mirror(TEST_MIRROR_H1); + assert!(tmp.is_ok()); + assert_eq!(tmp.unwrap(), TEST_MIRROR_H2); + let tmp = codec_ffi::to_horizontal_mirror(TEST_MIRROR_H2); + assert!(tmp.is_ok()); + assert_eq!(tmp.unwrap(), TEST_MIRROR_H1); + } + + #[test] + fn mirror_unsafe() { + assert!(codec_ffi::is_vertical_mirror_unsafe(TEST_MIRROR_V)); + assert!(!codec_ffi::is_vertical_mirror_unsafe(TEST_MIRROR_V1)); + assert!(!codec_ffi::is_vertical_mirror_unsafe(TEST_MIRROR_V2)); + + assert!(codec_ffi::is_horizontal_mirror_unsafe(TEST_MIRROR_H)); + assert!(!codec_ffi::is_horizontal_mirror_unsafe(TEST_MIRROR_H1)); + assert!(!codec_ffi::is_horizontal_mirror_unsafe(TEST_MIRROR_H2)); + + assert_eq!( + codec_ffi::to_vertical_mirror_unsafe(TEST_MIRROR_V), + TEST_MIRROR_V + ); + assert_eq!( + codec_ffi::to_vertical_mirror_unsafe(TEST_MIRROR_V1), + TEST_MIRROR_V2 + ); + assert_eq!( + codec_ffi::to_vertical_mirror_unsafe(TEST_MIRROR_V2), + TEST_MIRROR_V1 + ); + + assert_eq!( + codec_ffi::to_horizontal_mirror_unsafe(TEST_MIRROR_H), + TEST_MIRROR_H + ); + assert_eq!( + codec_ffi::to_horizontal_mirror_unsafe(TEST_MIRROR_H1), + TEST_MIRROR_H2 + ); + assert_eq!( + codec_ffi::to_horizontal_mirror_unsafe(TEST_MIRROR_H2), + TEST_MIRROR_H1 + ); + } + // TODO: string - // TODO: convert + // EXPECT_EQ(SHORT_CODE_STR_SIZE, 6); + // EXPECT_EQ(COMMON_CODE_STR_SIZE, 10); + // + // /// short code string verify + // uint32_t short_code; + // char short_code_buffer[SHORT_CODE_STR_SIZE]; + // EXPECT_EQ(short_code_to_string(TEST_SHORT_CODE_ERR, short_code_buffer), false); + // EXPECT_EQ(short_code_from_string(TEST_SHORT_CODE_STR_ERR, &short_code), false); + // + // EXPECT_EQ(short_code_to_string(TEST_SHORT_CODE_OK, short_code_buffer), true); + // EXPECT_STREQ(short_code_buffer, TEST_SHORT_CODE_STR_OK); + // short_code_to_string_unsafe(TEST_SHORT_CODE_OK, short_code_buffer); + // EXPECT_STREQ(short_code_buffer, TEST_SHORT_CODE_STR_OK); + // + // EXPECT_EQ(short_code_from_string(TEST_SHORT_CODE_STR_OK, &short_code), true); + // EXPECT_EQ(short_code, TEST_SHORT_CODE_OK); + // + // /// common code string verify + // uint64_t common_code; + // char common_code_buffer[COMMON_CODE_STR_SIZE]; + // EXPECT_EQ(common_code_to_string(TEST_COMMON_CODE_ERR, common_code_buffer), false); + // EXPECT_EQ(common_code_from_string(TEST_COMMON_CODE_STR_ERR, &common_code), false); + // + // EXPECT_EQ(common_code_to_string(TEST_COMMON_CODE_OK, common_code_buffer), true); + // EXPECT_STREQ(common_code_buffer, TEST_COMMON_CODE_STR_OK); + // common_code_to_string_unsafe(TEST_COMMON_CODE_OK, common_code_buffer); + // EXPECT_STREQ(common_code_buffer, TEST_COMMON_CODE_STR_OK); + // + // EXPECT_EQ(common_code_to_string_shorten(TEST_COMMON_CODE_OK, common_code_buffer), true); + // EXPECT_STREQ(common_code_buffer, TEST_COMMON_CODE_STR_SHR_OK); + // common_code_to_string_shorten_unsafe(TEST_COMMON_CODE_OK, common_code_buffer); + // EXPECT_STREQ(common_code_buffer, TEST_COMMON_CODE_STR_SHR_OK); + // + // EXPECT_EQ(common_code_from_string(TEST_COMMON_CODE_STR_OK, &common_code), true); + // EXPECT_EQ(common_code, TEST_COMMON_CODE_OK); + // EXPECT_EQ(common_code_from_string(TEST_COMMON_CODE_STR_SHR_OK, &common_code), true); + // EXPECT_EQ(common_code, TEST_COMMON_CODE_OK); + + #[test] + fn string() { + // short code string + assert!(codec_ffi::short_code_to_string(TEST_SHORT_CODE_ERR).is_err()); + assert!(codec_ffi::short_code_from_string(TEST_SHORT_CODE_STR_ERR).is_err()); - // TODO: convert_unsafe + let tmp = codec_ffi::short_code_to_string(TEST_SHORT_CODE_OK); + assert!(tmp.is_ok()); + assert_eq!(tmp.unwrap(), TEST_SHORT_CODE_STR_OK); + assert_eq!( + codec_ffi::short_code_to_string_unsafe(TEST_SHORT_CODE_OK), + TEST_SHORT_CODE_STR_OK + ); - // TODO: mirror + // common code string + assert!( + codec_ffi::common_code_to_string(TEST_COMMON_CODE_ERR).is_err() + ); + assert!( + codec_ffi::common_code_to_string_shorten(TEST_COMMON_CODE_ERR).is_err() + ); + assert!( + codec_ffi::common_code_from_string(TEST_COMMON_CODE_STR_ERR).is_err() + ); - // TODO: mirror_unsafe + let tmp = codec_ffi::common_code_to_string(TEST_COMMON_CODE_OK); + assert!(tmp.is_ok()); + assert_eq!(tmp.unwrap(), TEST_COMMON_CODE_STR_OK); + assert_eq!( + codec_ffi::common_code_to_string_unsafe(TEST_COMMON_CODE_OK), + TEST_COMMON_CODE_STR_OK + ); + let tmp = codec_ffi::common_code_to_string_shorten(TEST_COMMON_CODE_OK); + assert!(tmp.is_ok()); + assert_eq!(tmp.unwrap(), TEST_COMMON_CODE_STR_SHR_OK); + assert_eq!( + codec_ffi::common_code_to_string_shorten_unsafe(TEST_COMMON_CODE_OK), + TEST_COMMON_CODE_STR_SHR_OK + ); + + let tmp = codec_ffi::common_code_from_string(TEST_COMMON_CODE_STR_OK); + assert!(tmp.is_ok()); + assert_eq!(tmp.unwrap(), TEST_COMMON_CODE_OK); + let tmp = codec_ffi::common_code_from_string(TEST_COMMON_CODE_STR_SHR_OK); + assert!(tmp.is_ok()); + assert_eq!(tmp.unwrap(), TEST_COMMON_CODE_OK); + } } diff --git a/test/ffi/codec.cc b/test/ffi/codec.cc index d5cb35c..ae5bd37 100644 --- a/test/ffi/codec.cc +++ b/test/ffi/codec.cc @@ -79,6 +79,7 @@ TEST(FFI, codec_string) { uint64_t common_code; char common_code_buffer[COMMON_CODE_STR_SIZE]; EXPECT_EQ(common_code_to_string(TEST_COMMON_CODE_ERR, common_code_buffer), false); + EXPECT_EQ(common_code_to_string_shorten(TEST_COMMON_CODE_ERR, common_code_buffer), false); EXPECT_EQ(common_code_from_string(TEST_COMMON_CODE_STR_ERR, &common_code), false); EXPECT_EQ(common_code_to_string(TEST_COMMON_CODE_OK, common_code_buffer), true); @@ -156,8 +157,14 @@ TEST(FFI, codec_mirror) { EXPECT_EQ(to_vertical_mirror(TEST_RAW_CODE_ERR, &raw_code), false); EXPECT_EQ(to_horizontal_mirror(TEST_RAW_CODE_ERR, &raw_code), false); + // function always return false EXPECT_EQ(is_vertical_mirror(TEST_RAW_CODE_OK, &result), true); - EXPECT_EQ(result, false); // function always return false + EXPECT_EQ(result, false); + EXPECT_EQ(is_vertical_mirror(TEST_MIRROR_V1, &result), true); + EXPECT_EQ(result, false); + EXPECT_EQ(is_vertical_mirror(TEST_MIRROR_V2, &result), true); + EXPECT_EQ(result, false); + EXPECT_EQ(to_vertical_mirror(TEST_MIRROR_V1, &raw_code), true); EXPECT_EQ(raw_code, TEST_MIRROR_V2); EXPECT_EQ(to_vertical_mirror(TEST_MIRROR_V2, &raw_code), true); @@ -169,6 +176,9 @@ TEST(FFI, codec_mirror) { EXPECT_EQ(result, false); EXPECT_EQ(is_horizontal_mirror(TEST_MIRROR_H2, &result), true); EXPECT_EQ(result, false); + + EXPECT_EQ(to_horizontal_mirror(TEST_MIRROR_H, &raw_code), true); + EXPECT_EQ(raw_code, TEST_MIRROR_H); EXPECT_EQ(to_horizontal_mirror(TEST_MIRROR_H1, &raw_code), true); EXPECT_EQ(raw_code, TEST_MIRROR_H2); EXPECT_EQ(to_horizontal_mirror(TEST_MIRROR_H2, &raw_code), true);