From b6683c2dc8823af2e86a0551edcf5003af7d14ca Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 28 Feb 2023 16:11:48 +0800 Subject: [PATCH] test: add ShortCode rust codec test --- src/rust_ffi/src/codec/common_code.rs | 12 ++-- src/rust_ffi/src/codec/raw_code.rs | 6 +- src/rust_ffi/src/codec/short_code.rs | 94 +++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 9 deletions(-) diff --git a/src/rust_ffi/src/codec/common_code.rs b/src/rust_ffi/src/codec/common_code.rs index a6d0765..c7b2fd4 100644 --- a/src/rust_ffi/src/codec/common_code.rs +++ b/src/rust_ffi/src/codec/common_code.rs @@ -230,11 +230,11 @@ impl CommonCode { } #[cfg(test)] -mod tests { - const TEST_CODE: u64 = 0x1_A9BF_0C00; - const TEST_CODE_STR: &str = "1A9BF0C00"; - const TEST_CODE_STR_SHR: &str = "1A9BF0C"; +pub(crate) mod tests { + pub(crate) const TEST_CODE: u64 = 0x1_A9BF_0C00; + pub(crate) const TEST_CODE_STR: &str = "1A9BF0C00"; + pub(crate) const TEST_CODE_STR_SHR: &str = "1A9BF0C"; - const TEST_CODE_ERR: u64 = 0x1_A9BF_FC00; - const TEST_CODE_STR_ERR: &str = "123J432A9"; + pub(crate) const TEST_CODE_ERR: u64 = 0x1_A9BF_FC00; + pub(crate) const TEST_CODE_STR_ERR: &str = "123J432A9"; } diff --git a/src/rust_ffi/src/codec/raw_code.rs b/src/rust_ffi/src/codec/raw_code.rs index 5ad0724..23f52df 100644 --- a/src/rust_ffi/src/codec/raw_code.rs +++ b/src/rust_ffi/src/codec/raw_code.rs @@ -315,7 +315,7 @@ impl RawCode { } #[cfg(test)] -mod tests { - const TEST_CODE: u64 = 0x0_603_EDF_5CA_FFF_5E2; - const TEST_CODE_ERR: u64 = 0x0_A34_182_B38_102_D21; +pub(crate) mod tests { + pub(crate) const TEST_CODE: u64 = 0x0_603_EDF_5CA_FFF_5E2; + pub(crate) const TEST_CODE_ERR: u64 = 0x0_A34_182_B38_102_D21; } diff --git a/src/rust_ffi/src/codec/short_code.rs b/src/rust_ffi/src/codec/short_code.rs index eeeef12..fb21afc 100644 --- a/src/rust_ffi/src/codec/short_code.rs +++ b/src/rust_ffi/src/codec/short_code.rs @@ -261,9 +261,103 @@ impl ShortCode { #[cfg(test)] mod tests { + use crate::{RawCode, ShortCode, CommonCode}; + use super::super::raw_code::tests as raw_code; + use super::super::common_code::tests as common_code; + const TEST_CODE: u32 = 4091296; const TEST_CODE_STR: &str = "4WVE1"; const TEST_CODE_ERR: u32 = 29670987; const TEST_CODE_STR_ERR: &str = "R50EH"; + + #[test] + fn construct() { + assert!(ShortCode::check(TEST_CODE)); + assert!(!ShortCode::check(TEST_CODE_ERR)); + + assert!(ShortCode::from(TEST_CODE).is_ok()); + assert!(ShortCode::from(TEST_CODE_ERR).is_err()); + + assert!(ShortCode::from_str(TEST_CODE_STR).is_ok()); + assert!(ShortCode::from_str(TEST_CODE_STR_ERR).is_err()); + + assert_eq!( + ShortCode::new(TEST_CODE), + ShortCode::from(TEST_CODE).unwrap() + ); + assert_eq!( + ShortCode::from(TEST_CODE).unwrap(), + ShortCode::from_str(TEST_CODE_STR).unwrap() + ); + } + + #[test] + fn convert() { + // ShortCode <-- raw code + assert_eq!( + ShortCode::from_raw_code( + &RawCode::from(raw_code::TEST_CODE).unwrap() + ), + ShortCode::from(TEST_CODE).unwrap() + ); + + assert_eq!( + ShortCode::from_raw_code_val(raw_code::TEST_CODE).unwrap(), + ShortCode::from(TEST_CODE).unwrap() + ); + + assert!( + ShortCode::from_raw_code_val(raw_code::TEST_CODE_ERR).is_err() + ); + + // ShortCode <-- common code + assert_eq!( + ShortCode::from_common_code( + &CommonCode::from(common_code::TEST_CODE).unwrap() + ), + ShortCode::from(TEST_CODE).unwrap() + ); + + assert_eq!( + ShortCode::from_common_code_val(common_code::TEST_CODE).unwrap(), + ShortCode::from(TEST_CODE).unwrap() + ); + + assert_eq!( + ShortCode::from_common_code_str(common_code::TEST_CODE_STR).unwrap(), + ShortCode::from(TEST_CODE).unwrap() + ); + + assert!( + ShortCode::from_common_code_val(common_code::TEST_CODE_ERR).is_err() + ); + + assert!( + ShortCode::from_common_code_str(common_code::TEST_CODE_STR_ERR).is_err() + ); + } + + #[test] + fn export() { + assert_eq!(ShortCode::from(TEST_CODE).unwrap().unwrap(), TEST_CODE); + + assert_eq!(ShortCode::from(TEST_CODE).unwrap().to_string(), TEST_CODE_STR); + + assert_eq!( + ShortCode::from(TEST_CODE).unwrap().to_raw_code(), + RawCode::from(raw_code::TEST_CODE).unwrap() + ); + + assert_eq!( + ShortCode::from(TEST_CODE).unwrap().to_common_code(), + CommonCode::from(common_code::TEST_CODE).unwrap() + ); + } + + #[test] + fn warm_up() { + ShortCode::warm_up(); + ShortCode::warm_up_fast(); + } }