From 923ab2f0a63ab87b30175921c397dd8f7c284cf3 Mon Sep 17 00:00:00 2001 From: kcudnik Date: Fri, 20 Aug 2021 10:28:21 +0200 Subject: [PATCH] Add SwitchContainer tests --- unittest/vslib/TestSwitchContainer.cpp | 74 ++++++++++++++++++++++++++ vslib/SwitchContainer.cpp | 10 ++++ 2 files changed, 84 insertions(+) create mode 100644 unittest/vslib/TestSwitchContainer.cpp diff --git a/unittest/vslib/TestSwitchContainer.cpp b/unittest/vslib/TestSwitchContainer.cpp new file mode 100644 index 000000000000..d367cf5b55e8 --- /dev/null +++ b/unittest/vslib/TestSwitchContainer.cpp @@ -0,0 +1,74 @@ +#include "SwitchContainer.h" + +#include + +using namespace saivs; + +TEST(SwitchContainer, insert) +{ + SwitchContainer sc; + + EXPECT_THROW(sc.insert(nullptr), std::runtime_error); + + auto s = std::make_shared(0x2100000000); + + sc.insert(s); + + EXPECT_THROW(sc.insert(s), std::runtime_error); +} + +TEST(SwitchContainer, removeSwitch) +{ + SwitchContainer sc; + + EXPECT_THROW(sc.removeSwitch(0), std::runtime_error); + + auto s = std::make_shared(0x2100000000); + + sc.insert(s); + + sc.removeSwitch(0x2100000000); + + s = std::make_shared(0x2100000000); + + sc.insert(s); + + EXPECT_THROW(sc.removeSwitch(nullptr), std::runtime_error); + + EXPECT_THROW(sc.removeSwitch(1), std::runtime_error); + + sc.removeSwitch(s); +} + +TEST(SwitchContainer, clear) +{ + SwitchContainer sc; + + sc.clear(); +} + +TEST(SwitchContainer, contains) +{ + SwitchContainer sc; + + auto s = std::make_shared(0x2100000000); + + sc.insert(s); + + EXPECT_TRUE(sc.contains(0x2100000000)); + + EXPECT_FALSE(sc.contains(1)); +} + +TEST(SwitchContainer, getSwitch) +{ + SwitchContainer sc; + + auto s = std::make_shared(0x2100000000); + + sc.insert(s); + + EXPECT_NE(sc.getSwitch(0x2100000000), nullptr); + + EXPECT_EQ(sc.getSwitch(1), nullptr); +} diff --git a/vslib/SwitchContainer.cpp b/vslib/SwitchContainer.cpp index 34c9c9066f7d..b289c4ca5fbc 100644 --- a/vslib/SwitchContainer.cpp +++ b/vslib/SwitchContainer.cpp @@ -10,6 +10,11 @@ void SwitchContainer::insert( { SWSS_LOG_ENTER(); + if (sw == nullptr) + { + SWSS_LOG_THROW("switch pointer can't be nullptr"); + } + auto switchId = sw->getSwitchId(); if (m_switchMap.find(switchId) != m_switchMap.end()) @@ -42,6 +47,11 @@ void SwitchContainer::removeSwitch( { SWSS_LOG_ENTER(); + if (sw == nullptr) + { + SWSS_LOG_THROW("switch pointer can't be nullptr"); + } + removeSwitch(sw->getSwitchId()); }