Skip to content

Commit

Permalink
Add LaneMapContainer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kcudnik committed Aug 23, 2021
1 parent 2997892 commit 38a52ac
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 6 deletions.
3 changes: 2 additions & 1 deletion unittest/vslib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ tests_SOURCES = main.cpp \
TestEventQueue.cpp \
TestFdbInfo.cpp \
TestSaiAttrWrap.cpp \
TestLaneMap.cpp
TestLaneMap.cpp \
TestLaneMapContainer.cpp

tests_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON)
tests_LDADD = $(LDADD_GTEST) $(top_srcdir)/vslib/libSaiVS.a -lhiredis -lswsscommon -lpthread -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta -lzmq $(CODE_COVERAGE_LIBS)
Expand Down
110 changes: 110 additions & 0 deletions unittest/vslib/TestLaneMapContainer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include "LaneMapContainer.h"

#include <gtest/gtest.h>

using namespace saivs;

TEST(LaneMapContainer, insert)
{
auto map = std::make_shared<LaneMap>(0);

std::vector<uint32_t> ok{1};

EXPECT_EQ(map->add("foo", ok), true);

LaneMapContainer lmc;

EXPECT_EQ(lmc.insert(map), true);

EXPECT_EQ(lmc.size(), 1);

EXPECT_EQ(lmc.insert(map), false);
}

TEST(LaneMapContainer, remove)
{
auto map = std::make_shared<LaneMap>(0);

std::vector<uint32_t> ok{1};

EXPECT_EQ(map->add("foo", ok), true);

LaneMapContainer lmc;

lmc.insert(map);

EXPECT_EQ(lmc.size(), 1);

EXPECT_TRUE(lmc.remove(0));

EXPECT_EQ(lmc.size(), 0);

EXPECT_FALSE(lmc.remove(0));
}

TEST(LaneMapContainer, getLaneMap)
{
auto map = std::make_shared<LaneMap>(0);

std::vector<uint32_t> ok{1};

EXPECT_EQ(map->add("foo", ok), true);

LaneMapContainer lmc;

EXPECT_EQ(lmc.insert(map), true);

EXPECT_NE(lmc.getLaneMap(0), nullptr);

EXPECT_EQ(lmc.getLaneMap(1), nullptr);
}

TEST(LaneMapContainer, clear)
{
auto map = std::make_shared<LaneMap>(0);

std::vector<uint32_t> ok{1};

EXPECT_EQ(map->add("foo", ok), true);

LaneMapContainer lmc;

EXPECT_EQ(lmc.insert(map), true);

EXPECT_EQ(lmc.size(), 1);

lmc.clear();

EXPECT_EQ(lmc.size(), 0);
}

TEST(LaneMapContainer, hasLaneMap)
{
LaneMapContainer lmc;

EXPECT_FALSE(lmc.hasLaneMap(0));
}

TEST(LaneMapContainer, removeEmptyLaneMaps)
{
LaneMapContainer lmc;

EXPECT_FALSE(lmc.hasLaneMap(0));

auto map1 = std::make_shared<LaneMap>(0);
auto map2 = std::make_shared<LaneMap>(1);

std::vector<uint32_t> ok{1};

EXPECT_EQ(map1->add("foo", ok), true);

EXPECT_TRUE(lmc.insert(map1));

EXPECT_TRUE(lmc.insert(map2));

EXPECT_EQ(lmc.size(), 2);

lmc.removeEmptyLaneMaps();

EXPECT_EQ(lmc.size(), 1);
}
21 changes: 18 additions & 3 deletions vslib/LaneMapContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@

using namespace saivs;

void LaneMapContainer::insert(
bool LaneMapContainer::insert(
_In_ std::shared_ptr<LaneMap> laneMap)
{
SWSS_LOG_ENTER();

m_map[laneMap->getSwitchIndex()] = laneMap;
auto idx = laneMap->getSwitchIndex();

if (m_map.find(idx) != m_map.end())
{
SWSS_LOG_ERROR("map for switch index %u already exists", idx);

return false;
}

m_map[idx] = laneMap;

return true;
}

void LaneMapContainer::remove(
bool LaneMapContainer::remove(
_In_ uint32_t switchIndex)
{
SWSS_LOG_ENTER();
Expand All @@ -22,7 +33,11 @@ void LaneMapContainer::remove(
if (it != m_map.end())
{
m_map.erase(it);

return true;
}

return false;
}

std::shared_ptr<LaneMap> LaneMapContainer::getLaneMap(
Expand Down
4 changes: 2 additions & 2 deletions vslib/LaneMapContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ namespace saivs

public:

void insert(
bool insert(
_In_ std::shared_ptr<LaneMap> laneMap);

void remove(
bool remove(
_In_ uint32_t switchIndex);

std::shared_ptr<LaneMap> getLaneMap(
Expand Down

0 comments on commit 38a52ac

Please sign in to comment.