mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-27 02:53:10 +01:00
Add spop and srandmember
This commit is contained in:
@@ -157,6 +157,12 @@ public:
|
||||
static Command smove(const std::string& source, const std::string& destination, const std::string& member);
|
||||
/// Returns a SMOVE command
|
||||
|
||||
static Command spop(const std::string& set, Int64 count = 0);
|
||||
/// Returns a SPOP command
|
||||
|
||||
static Command srandmember(const std::string& set, Int64 count = 0);
|
||||
/// Returns a SRANDMEMBER command
|
||||
|
||||
static Command srem(const std::string& set, const std::string& member);
|
||||
/// Returns a SREM command
|
||||
|
||||
|
||||
@@ -408,6 +408,26 @@ Command Command::smove(const std::string& source, const std::string& destination
|
||||
return cmd;
|
||||
}
|
||||
|
||||
Command Command::spop(const std::string& set, Int64 count)
|
||||
{
|
||||
Command cmd("SPOP");
|
||||
|
||||
cmd << set;
|
||||
if( count != 0 ) cmd << NumberFormatter::format(count);
|
||||
|
||||
return cmd;
|
||||
}
|
||||
|
||||
Command Command::srandmember(const std::string& set, Int64 count)
|
||||
{
|
||||
Command cmd("SRANDMEMBER");
|
||||
|
||||
cmd << set;
|
||||
if( count != 0 ) cmd << NumberFormatter::format(count);
|
||||
|
||||
return cmd;
|
||||
}
|
||||
|
||||
Command Command::srem(const std::string& set1, const std::string& member)
|
||||
{
|
||||
Command cmd("SREM");
|
||||
|
||||
@@ -1639,6 +1639,167 @@ void RedisTest::testSMove()
|
||||
}
|
||||
}
|
||||
|
||||
void RedisTest::testSPop()
|
||||
{
|
||||
if (!_connected)
|
||||
{
|
||||
std::cout << "Not connected, test skipped." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
delKey("myset");
|
||||
|
||||
Command sadd = Command::sadd("myset", "one");
|
||||
try
|
||||
{
|
||||
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
|
||||
assert(result == 1);
|
||||
}
|
||||
catch(RedisException &e)
|
||||
{
|
||||
fail(e.message());
|
||||
}
|
||||
|
||||
sadd = Command::sadd("myset", "two");
|
||||
try
|
||||
{
|
||||
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
|
||||
assert(result == 1);
|
||||
}
|
||||
catch(RedisException &e)
|
||||
{
|
||||
fail(e.message());
|
||||
}
|
||||
|
||||
sadd = Command::sadd("myset", "three");
|
||||
try
|
||||
{
|
||||
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
|
||||
assert(result == 1);
|
||||
}
|
||||
catch(RedisException &e)
|
||||
{
|
||||
fail(e.message());
|
||||
}
|
||||
|
||||
Command spop = Command::spop("myset");
|
||||
try
|
||||
{
|
||||
BulkString result = _redis.execute<BulkString>(spop);
|
||||
assert(!result.isNull());
|
||||
}
|
||||
catch(RedisException &e)
|
||||
{
|
||||
fail(e.message());
|
||||
}
|
||||
|
||||
Command smembers = Command::smembers("myset");
|
||||
try
|
||||
{
|
||||
Array result = _redis.execute<Array>(smembers);
|
||||
assert(result.size() == 2);
|
||||
}
|
||||
catch(RedisException &e)
|
||||
{
|
||||
fail(e.message());
|
||||
}
|
||||
|
||||
sadd = Command::sadd("myset", "four");
|
||||
try
|
||||
{
|
||||
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
|
||||
assert(result == 1);
|
||||
}
|
||||
catch(RedisException &e)
|
||||
{
|
||||
fail(e.message());
|
||||
}
|
||||
|
||||
sadd = Command::sadd("myset", "five");
|
||||
try
|
||||
{
|
||||
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
|
||||
assert(result == 1);
|
||||
}
|
||||
catch(RedisException &e)
|
||||
{
|
||||
fail(e.message());
|
||||
}
|
||||
// Redis server 3.0.5 doesn't support this yet ..
|
||||
/*
|
||||
spop = Command::spop("myset", 3);
|
||||
try
|
||||
{
|
||||
Array result = _redis.execute<Array>(spop);
|
||||
assert(result.size() == 3);
|
||||
}
|
||||
catch(RedisException &e)
|
||||
{
|
||||
fail(e.message());
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
void RedisTest::testSRandMember()
|
||||
{
|
||||
if (!_connected)
|
||||
{
|
||||
std::cout << "Not connected, test skipped." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
delKey("myset");
|
||||
|
||||
std::vector<std::string> members;
|
||||
members.push_back("one");
|
||||
members.push_back("two");
|
||||
members.push_back("three");
|
||||
|
||||
Command sadd = Command::sadd("myset", members);
|
||||
try
|
||||
{
|
||||
Poco::Int64 result = _redis.execute<Poco::Int64>(sadd);
|
||||
assert(result == 3);
|
||||
}
|
||||
catch(RedisException &e)
|
||||
{
|
||||
fail(e.message());
|
||||
}
|
||||
|
||||
Command srandmember = Command::srandmember("myset");
|
||||
try
|
||||
{
|
||||
BulkString result = _redis.execute<BulkString>(srandmember);
|
||||
assert(!result.isNull());
|
||||
}
|
||||
catch(RedisException &e)
|
||||
{
|
||||
fail(e.message());
|
||||
}
|
||||
|
||||
srandmember = Command::srandmember("myset", 2);
|
||||
try
|
||||
{
|
||||
Array result = _redis.execute<Array>(srandmember);
|
||||
assert(result.size() == 2);
|
||||
}
|
||||
catch(RedisException &e)
|
||||
{
|
||||
fail(e.message());
|
||||
}
|
||||
|
||||
srandmember = Command::srandmember("myset", -5);
|
||||
try
|
||||
{
|
||||
Array result = _redis.execute<Array>(srandmember);
|
||||
assert(result.size() == 5);
|
||||
}
|
||||
catch(RedisException &e)
|
||||
{
|
||||
fail(e.message());
|
||||
}
|
||||
}
|
||||
|
||||
void RedisTest::testStrlen()
|
||||
{
|
||||
if (!_connected)
|
||||
@@ -2233,6 +2394,8 @@ CppUnit::Test* RedisTest::suite()
|
||||
CppUnit_addTest(pSuite, RedisTest, testSIsmember);
|
||||
CppUnit_addTest(pSuite, RedisTest, testSMembers);
|
||||
CppUnit_addTest(pSuite, RedisTest, testSMove);
|
||||
CppUnit_addTest(pSuite, RedisTest, testSPop);
|
||||
CppUnit_addTest(pSuite, RedisTest, testSRandMember);
|
||||
CppUnit_addTest(pSuite, RedisTest, testSRem);
|
||||
CppUnit_addTest(pSuite, RedisTest, testStrlen);
|
||||
CppUnit_addTest(pSuite, RedisTest, testSUnion);
|
||||
|
||||
@@ -61,6 +61,8 @@ public:
|
||||
void testSIsmember();
|
||||
void testSMembers();
|
||||
void testSMove();
|
||||
void testSPop();
|
||||
void testSRandMember();
|
||||
void testSRem();
|
||||
void testSUnion();
|
||||
void testSUnionStore();
|
||||
|
||||
Reference in New Issue
Block a user