From 8de1f9c3a51c2b3ae95e9fc90f00684883435c9f Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 28 Feb 2023 16:20:19 +0800 Subject: [PATCH] test: add CommonCode rust codec test --- src/rust_ffi/src/codec/common_code.rs | 104 ++++++++++++++++++++++++++ src/rust_ffi/src/codec/short_code.rs | 10 +-- 2 files changed, 109 insertions(+), 5 deletions(-) diff --git a/src/rust_ffi/src/codec/common_code.rs b/src/rust_ffi/src/codec/common_code.rs index c7b2fd4..8b12cb1 100644 --- a/src/rust_ffi/src/codec/common_code.rs +++ b/src/rust_ffi/src/codec/common_code.rs @@ -231,10 +231,114 @@ impl CommonCode { #[cfg(test)] pub(crate) mod tests { + use crate::{RawCode, ShortCode, CommonCode}; + use super::super::raw_code::tests as raw_code; + use super::super::short_code::tests as short_code; + 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"; pub(crate) const TEST_CODE_ERR: u64 = 0x1_A9BF_FC00; pub(crate) const TEST_CODE_STR_ERR: &str = "123J432A9"; + + + #[test] + fn construct() { + assert!(CommonCode::check(TEST_CODE)); + assert!(!CommonCode::check(TEST_CODE_ERR)); + + assert!(CommonCode::from(TEST_CODE).is_ok()); + assert!(CommonCode::from(TEST_CODE_ERR).is_err()); + + assert!(CommonCode::from_str(TEST_CODE_STR).is_ok()); + assert!(CommonCode::from_str(TEST_CODE_STR_ERR).is_err()); + + assert_eq!( + CommonCode::new(TEST_CODE), + CommonCode::from(TEST_CODE).unwrap() + ); + assert_eq!( + CommonCode::from(TEST_CODE).unwrap(), + CommonCode::from_str(TEST_CODE_STR).unwrap() + ); + assert_eq!( + CommonCode::from(TEST_CODE).unwrap(), + CommonCode::from_str(TEST_CODE_STR_SHR).unwrap() + ); + } + + #[test] + fn convert() { + // CommonCode <-- raw code + assert_eq!( + CommonCode::from_raw_code( + &RawCode::from(raw_code::TEST_CODE).unwrap() + ), + CommonCode::from(TEST_CODE).unwrap() + ); + + assert_eq!( + CommonCode::from_raw_code_val(raw_code::TEST_CODE).unwrap(), + CommonCode::from(TEST_CODE).unwrap() + ); + + assert!( + CommonCode::from_raw_code_val(raw_code::TEST_CODE_ERR).is_err() + ); + + // CommonCode <-- short code + assert_eq!( + CommonCode::from_short_code( + &ShortCode::from(short_code::TEST_CODE).unwrap() + ), + CommonCode::from(TEST_CODE).unwrap() + ); + + assert_eq!( + CommonCode::from_short_code_val(short_code::TEST_CODE).unwrap(), + CommonCode::from(TEST_CODE).unwrap() + ); + + assert_eq!( + CommonCode::from_short_code_str(short_code::TEST_CODE_STR).unwrap(), + CommonCode::from(TEST_CODE).unwrap() + ); + + assert!( + CommonCode::from_short_code_val(short_code::TEST_CODE_ERR).is_err() + ); + + assert!( + CommonCode::from_short_code_str(short_code::TEST_CODE_STR_ERR).is_err() + ); + } + + #[test] + fn export() { + assert_eq!( + CommonCode::from(TEST_CODE).unwrap().unwrap(), + TEST_CODE + ); + + assert_eq!( + CommonCode::from(TEST_CODE).unwrap().to_string(), + TEST_CODE_STR + ); + + assert_eq!( + CommonCode::from(TEST_CODE).unwrap().to_string_shorten(), + TEST_CODE_STR_SHR + ); + + assert_eq!( + CommonCode::from(TEST_CODE).unwrap().to_raw_code(), + RawCode::from(raw_code::TEST_CODE).unwrap() + ); + + assert_eq!( + CommonCode::from(TEST_CODE).unwrap().to_short_code(), + ShortCode::from(short_code::TEST_CODE).unwrap() + ); + } } diff --git a/src/rust_ffi/src/codec/short_code.rs b/src/rust_ffi/src/codec/short_code.rs index fb21afc..45cbaec 100644 --- a/src/rust_ffi/src/codec/short_code.rs +++ b/src/rust_ffi/src/codec/short_code.rs @@ -260,16 +260,16 @@ impl ShortCode { } #[cfg(test)] -mod tests { +pub(crate) 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"; + pub(crate) const TEST_CODE: u32 = 4091296; + pub(crate) const TEST_CODE_STR: &str = "4WVE1"; - const TEST_CODE_ERR: u32 = 29670987; - const TEST_CODE_STR_ERR: &str = "R50EH"; + pub(crate) const TEST_CODE_ERR: u32 = 29670987; + pub(crate) const TEST_CODE_STR_ERR: &str = "R50EH"; #[test] fn construct() {