You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

65 lines
1.7 KiB

#include <gtest/gtest.h>
#include "../src/types.hh"
typedef pEp::StringPair SP;
TEST( StringPair, Simple )
{
pEp::StringPair s1{"key", "value"};
pEp::StringPairList spl;
EXPECT_TRUE(spl.empty());
EXPECT_EQ(spl.size(), 0);
// pEp::StringPair s2{ std::string{"key2"}, std::string{"value"} };
}
TEST( StringPair, InitList )
{
const std::initializer_list<pEp::StringPair> il{ SP{"key0", "value0"}, SP{"key1", "value1"} };
pEp::StringPairList spl( il );
EXPECT_EQ( spl.size(), 2);
spl.clear();
EXPECT_EQ(spl.size(), 0);
EXPECT_TRUE( spl.empty() );
}
TEST( StringPair, Dynamic )
{
static const unsigned NumberOfElements = 17;
char key[16];
char value[16];
pEp::StringPairList spl;
EXPECT_EQ(spl.size(), 0);
EXPECT_TRUE( spl.empty() );
for(unsigned u=0; u<NumberOfElements; ++u)
{
EXPECT_EQ(spl.size(), u);
snprintf(key, 15, "k%u", u);
snprintf(value, 15, "v%u", u*91);
spl.push_back( pEp::StringPair{key, value}.move_out() );
}
auto find_by_key = [key](const ::stringpair_t* sp){ return strcmp(sp->key, key)==0; };
// delete random elements.
for(unsigned u=0; u<NumberOfElements; ++u)
{
EXPECT_EQ(spl.size(), NumberOfElements-u);
snprintf(key, 15, "k%u", (u*7) % NumberOfElements); // permutate keys order
auto q = std::find_if( spl.begin(), spl.end(), find_by_key );
EXPECT_NE( q, spl.end() ); // element with key is found
spl.erase(q);
q = std::find_if( spl.begin(), spl.end(), find_by_key );
EXPECT_EQ( q, spl.end() ); // element with that key is no longer found
}
}