Browse Source

unittest to show how the intended usage will be.

master
roker 4 months ago
parent
commit
392662b981
1 changed files with 56 additions and 0 deletions
  1. +56
    -0
      test/unittest_stringlist.cc

+ 56
- 0
test/unittest_stringlist.cc View File

@ -0,0 +1,56 @@
#include <gtest/gtest.h>
#include "../src/types.hh"
TEST( StringList, Simple )
{
pEp::StringList sl;
EXPECT_TRUE(sl.empty());
EXPECT_EQ(sl.size(), 0);
}
TEST( StringList, InitList )
{
pEp::StringList sl( {"Hello", "world"} );
EXPECT_EQ( sl.size(), 2);
sl.clear();
EXPECT_EQ(sl.size(), 0);
EXPECT_TRUE( sl.empty() );
}
TEST( StringList, Dynamic )
{
static const unsigned NumberOfElements = 17;
pEp::StringList sl;
EXPECT_EQ(sl.size(), 0);
EXPECT_TRUE( sl.empty() );
char buffer[16];
for(unsigned u=0; u<NumberOfElements; ++u)
{
EXPECT_EQ(sl.size(), u);
snprintf(buffer, 15, "k%u", u);
sl.push_back( buffer );
}
auto find_value = [&buffer](const char* v){ return strcmp(v, buffer)==0; };
// delete random elements.
for(unsigned u=0; u<NumberOfElements; ++u)
{
EXPECT_EQ(sl.size(), NumberOfElements-u);
snprintf(buffer, 15, "k%u", (u*7) % NumberOfElements); // permutate keys order
auto q = std::find_if( sl.begin(), sl.end(), find_value );
ASSERT_NE( q, sl.end() ); // element with key is found
EXPECT_STREQ( *q, buffer);
sl.erase(q);
q = std::find_if( sl.begin(), sl.end(), find_value );
EXPECT_EQ( q, sl.end() ); // element with that key is no longer found
}
}

Loading…
Cancel
Save