diff options
author | Shuo Wang Hsu <shuohsu@google.com> | 2023-04-19 18:25:41 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2023-04-19 18:25:41 +0000 |
commit | 5f59d3e89d50689dabef4b48a95646c8ad3476d4 (patch) | |
tree | 17e2354680ae3508e907a5f34ceb0392e1732e24 | |
parent | 2d435533b322625649202d931efa0772b1403deb (diff) | |
parent | e4285ab6548fb01b589e06e6d0e9316f22bd08b3 (diff) | |
download | netsim-5f59d3e89d50689dabef4b48a95646c8ad3476d4.tar.gz |
Merge "Update MatchDevice for exact match" am: 05048107b7 am: 490eae7627 am: e4285ab654
Original change: https://android-review.googlesource.com/c/platform/tools/netsim/+/2545635
Change-Id: I4d521fbb4012e62d9d5f13d221c953cc48b1e104
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r-- | src/controller/scene_controller.cc | 19 | ||||
-rw-r--r-- | src/controller/scene_controller_test.cc | 4 |
2 files changed, 14 insertions, 9 deletions
diff --git a/src/controller/scene_controller.cc b/src/controller/scene_controller.cc index e88df795..a9c59f18 100644 --- a/src/controller/scene_controller.cc +++ b/src/controller/scene_controller.cc @@ -95,22 +95,25 @@ void SceneController::RemoveChip(uint32_t device_id, uint32_t chip_id) { // Returns a Device shared_ptr or nullptr std::shared_ptr<Device> SceneController::MatchDevice(const std::string &name) { - std::shared_ptr<Device> found = nullptr; + std::shared_ptr<Device> target = nullptr; + bool multiple_matches = false; if (name.empty()) { return nullptr; } for (auto &[_, device] : devices_) { - // query && name -> rename, only match by id - // query && !name -> match by query - // !query && name -> match by name + // match by device name auto pos = device->name.find(name); if (pos != std::string::npos) { - // check for multiple matches - if (found != nullptr) return nullptr; - found = device; + // check for exact match + if (device->name == name) return device; + // record if multiple ambiguous matches were found + if (target != nullptr) multiple_matches = true; + target = device; } } - return found; + // return nullptr if multiple ambiguous matches were found + if (multiple_matches) return nullptr; + return target; } // UI requesting a change in device info diff --git a/src/controller/scene_controller_test.cc b/src/controller/scene_controller_test.cc index 744739ba..c398e509 100644 --- a/src/controller/scene_controller_test.cc +++ b/src/controller/scene_controller_test.cc @@ -93,11 +93,13 @@ TEST_F(SceneControllerTest, MatchDeviceTest) { ASSERT_TRUE(match(device_name2)); ASSERT_TRUE(match(device_name3)); - // matches with name + // matches with unique substring of name ASSERT_TRUE(match("1-SceneControllerTest-MatchDeviceTest")); ASSERT_TRUE(match("2-SceneControllerTest-MatchDeviceTest")); ASSERT_TRUE(match("3-SceneControllerTest-MatchDeviceTest")); + // ambiguous matches and no match + ASSERT_TRUE(match("MatchDeviceTest") == nullptr); ASSERT_TRUE(match("non-existing-name") == nullptr); } |