diff options
author | Chih-Yu Huang <akahuang@google.com> | 2022-04-15 16:41:57 +0900 |
---|---|---|
committer | Chih-Yu Huang <akahuang@google.com> | 2022-04-15 17:01:13 +0900 |
commit | 5f9ff7f9dca80ef44ffebb01125a026a00e1b52f (patch) | |
tree | aaf976e3ee61ebe9d933e20ee69bbb9e8a3397f4 /src | |
parent | a7145696b47550895847d15f311bc94c9e40007d (diff) | |
download | uwb-5f9ff7f9dca80ef44ffebb01125a026a00e1b52f.tar.gz |
uwb_core: verify the equality of TLV list by HashMap
Two list of TLV with the same items but different order should be
considered equal. This CL verifies the equalilty of AppConfigTlv and
DeviceConfigTlv list by converting the list to HashMap, then comparing
the HashMap.
Bug: 228136039
Test: cargo test
Test: cargo clippy
Change-Id: Ib8f86bce0b9520ce1c3c30f29d68f58cffa77345
Diffstat (limited to 'src')
-rw-r--r-- | src/rust/uwb_core/src/uci/mock_uci_manager.rs | 8 | ||||
-rw-r--r-- | src/rust/uwb_core/src/uci/params.rs | 23 | ||||
-rw-r--r-- | src/rust/uwb_core/src/uci/uci_manager.rs | 14 |
3 files changed, 29 insertions, 16 deletions
diff --git a/src/rust/uwb_core/src/uci/mock_uci_manager.rs b/src/rust/uwb_core/src/uci/mock_uci_manager.rs index ef1d8b2..05af30c 100644 --- a/src/rust/uwb_core/src/uci/mock_uci_manager.rs +++ b/src/rust/uwb_core/src/uci/mock_uci_manager.rs @@ -24,7 +24,7 @@ use tokio::time::timeout; use crate::uci::error::{Error, Result}; use crate::uci::notification::UciNotification; use crate::uci::params::{ - app_config_tlv_eq, device_config_tlv_eq, AppConfigTlv, AppConfigTlvType, CapTlv, Controlee, + app_config_tlvs_eq, device_config_tlvs_eq, AppConfigTlv, AppConfigTlvType, CapTlv, Controlee, CoreSetConfigResponse, CountryCode, DeviceConfigId, DeviceConfigTlv, GetDeviceInfoResponse, PowerStats, RawVendorMessage, ResetConfig, SessionId, SessionState, SessionType, SetAppConfigResponse, UpdateMulticastListAction, @@ -325,8 +325,7 @@ impl UciManager for MockUciManager { let mut expected_calls = self.expected_calls.lock().unwrap(); match expected_calls.pop_front() { Some(ExpectedCall::CoreSetConfig { expected_config_tlvs, out }) - if zip(&expected_config_tlvs, &config_tlvs) - .all(|(a, b)| device_config_tlv_eq(a, b)) => + if device_config_tlvs_eq(&expected_config_tlvs, &config_tlvs) => { self.expect_call_consumed.notify_one(); out @@ -409,8 +408,7 @@ impl UciManager for MockUciManager { expected_config_tlvs, out, }) if expected_session_id == session_id - && zip(&expected_config_tlvs, &config_tlvs) - .all(|(a, b)| app_config_tlv_eq(a, b)) => + && app_config_tlvs_eq(&expected_config_tlvs, &config_tlvs) => { self.expect_call_consumed.notify_one(); out diff --git a/src/rust/uwb_core/src/uci/params.rs b/src/rust/uwb_core/src/uci/params.rs index 71cf7b7..72c39b8 100644 --- a/src/rust/uwb_core/src/uci/params.rs +++ b/src/rust/uwb_core/src/uci/params.rs @@ -17,7 +17,8 @@ #![allow(clippy::eq_op)] -use std::iter::zip; +use std::collections::{hash_map::RandomState, HashMap}; +use std::iter::{zip, FromIterator}; use crate::uci::error::StatusCode; @@ -54,12 +55,24 @@ pub fn cap_tlv_eq(a: &CapTlv, b: &CapTlv) -> bool { a.t == b.t && a.v == b.v } -pub fn app_config_tlv_eq(a: &AppConfigTlv, b: &AppConfigTlv) -> bool { - a.cfg_id == b.cfg_id && a.v == b.v +pub fn app_config_tlvs_eq(a: &[AppConfigTlv], b: &[AppConfigTlv]) -> bool { + app_config_tlvs_to_map(a) == app_config_tlvs_to_map(b) } -pub fn device_config_tlv_eq(a: &DeviceConfigTlv, b: &DeviceConfigTlv) -> bool { - a.cfg_id == b.cfg_id && a.v == b.v +fn app_config_tlvs_to_map( + tlvs: &[AppConfigTlv], +) -> HashMap<AppConfigTlvType, &Vec<u8>, RandomState> { + HashMap::from_iter(tlvs.iter().map(|config| (config.cfg_id, &config.v))) +} + +pub fn device_config_tlvs_eq(a: &[DeviceConfigTlv], b: &[DeviceConfigTlv]) -> bool { + device_config_tlvs_to_map(a) == device_config_tlvs_to_map(b) +} + +fn device_config_tlvs_to_map( + tlvs: &[DeviceConfigTlv], +) -> HashMap<DeviceConfigId, &Vec<u8>, RandomState> { + HashMap::from_iter(tlvs.iter().map(|config| (config.cfg_id, &config.v))) } #[derive(Debug, Clone)] diff --git a/src/rust/uwb_core/src/uci/uci_manager.rs b/src/rust/uwb_core/src/uci/uci_manager.rs index d625fa5..1f72954 100644 --- a/src/rust/uwb_core/src/uci/uci_manager.rs +++ b/src/rust/uwb_core/src/uci/uci_manager.rs @@ -649,7 +649,7 @@ mod tests { use crate::uci::error::StatusCode; use crate::uci::mock_uci_hal::MockUciHal; use crate::uci::params::{ - app_config_tlv_eq, cap_tlv_eq, device_config_tlv_eq, power_stats_eq, CapTlvType, + app_config_tlvs_eq, cap_tlv_eq, device_config_tlvs_eq, power_stats_eq, CapTlvType, }; use crate::utils::init_test_logging; @@ -835,11 +835,13 @@ mod tests { .await; let config_id = DeviceConfigId::DeviceState; - let expected_result = - DeviceConfigTlv { cfg_id: DeviceConfigId::DeviceState, v: vec![0x12, 0x34, 0x56] }; + let expected_result = vec![DeviceConfigTlv { + cfg_id: DeviceConfigId::DeviceState, + v: vec![0x12, 0x34, 0x56], + }]; CoreSetConfigResponse { status: StatusCode::UciStatusOk, config_status: vec![] }; let result = uci_manager.core_get_config(vec![config_id]).await.unwrap(); - assert!(device_config_tlv_eq(&result[0], &expected_result)); + assert!(device_config_tlvs_eq(&result, &expected_result)); assert!(mock_hal.wait_expected_calls_done().await); } @@ -949,9 +951,9 @@ mod tests { let session_id = 0x123; let config_id = AppConfigTlvType::DeviceType; let expected_result = - AppConfigTlv { cfg_id: AppConfigTlvType::DeviceType, v: vec![0x12, 0x34, 0x56] }; + vec![AppConfigTlv { cfg_id: AppConfigTlvType::DeviceType, v: vec![0x12, 0x34, 0x56] }]; let result = uci_manager.session_get_app_config(session_id, vec![config_id]).await.unwrap(); - assert!(app_config_tlv_eq(&result[0], &expected_result)); + assert!(app_config_tlvs_eq(&result, &expected_result)); assert!(mock_hal.wait_expected_calls_done().await); } |