]>
git.saurik.com Git - redis.git/blob - client-libraries/cpp/test_client.cpp
1 #include "redisclient.h"
7 #define ASSERT_EQUAL(x,y) assert_equal(x, y, __LINE__)
8 #define ASSERT_NOT_EQUAL(x,y) assert_not_equal(x, y, __LINE__)
9 #define ASSERT_GT(x,y) assert_gt(x, y, __LINE__)
12 void assert_equal(const T
& actual
, const T
& expected
, int lineno
)
15 cerr
<< "assert_equal('" << expected
<< "', '" << actual
<< "')" << endl
;
18 if (expected
!= actual
)
20 cerr
<< "expected '" << expected
<< "' got '" << actual
<< "'" << endl
21 << "failing test called from line " << lineno
<< endl
;
27 cerr
<< "... OK" << endl
;
32 void assert_not_equal(const T
& a
, const T
& b
, int lineno
)
36 cerr
<< "expected inequality" << endl
37 << "failing test called from line " << lineno
<< endl
;
44 void assert_gt(const T
& a
, const T
& b
, int lineno
)
47 cerr
<< "assert_gt('" << a
<< "', '" << b
<< "')" << endl
;
52 cerr
<< "expected '" << a
<< "' > '" << b
<< "'" << endl
53 << "failing test called from line " << lineno
<< endl
;
59 cerr
<< "... OK" << endl
;
63 void test(const string
& name
)
66 cerr
<< "------------------------------" << endl
67 << "starting test: " << name
<< endl
;
71 int main(int argc
, char ** argv
)
77 // Test on high number databases
85 string
foo("foo"), bar("bar"), baz("baz"), buz("buz"), goo("goo");
89 // TODO ... needs a conf for redis-server
94 // doesn't throw? then, has valid numbers and known info-keys.
95 redis::server_info info
;
102 ASSERT_EQUAL(c
.get(foo
), bar
);
107 ASSERT_EQUAL(c
.getset(foo
, baz
), bar
);
108 ASSERT_EQUAL(c
.get(foo
), baz
);
113 string
x_val("hello"), y_val("world");
116 redis::client::string_vector keys
;
119 redis::client::string_vector vals
;
121 ASSERT_EQUAL(vals
.size(), size_t(2));
122 ASSERT_EQUAL(vals
[0], x_val
);
123 ASSERT_EQUAL(vals
[1], y_val
);
128 ASSERT_EQUAL(c
.setnx(foo
, bar
), false);
129 ASSERT_EQUAL(c
.setnx(buz
, baz
), true);
130 ASSERT_EQUAL(c
.get(buz
), baz
);
135 ASSERT_EQUAL(c
.incr("goo"), 1L);test("nonexistent (0) -> 1");
136 ASSERT_EQUAL(c
.incr("goo"), 2L);test("1->2");
141 ASSERT_EQUAL(c
.decr("goo"), 1L);test("2->1");
142 ASSERT_EQUAL(c
.decr("goo"), 0L);test("1->0");
147 ASSERT_EQUAL(c
.incrby("goo", 3), 3L);test("0->3");
148 ASSERT_EQUAL(c
.incrby("goo", 2), 5L);test("3->5");
153 ASSERT_EQUAL(c
.exists("goo"), true);
159 ASSERT_EQUAL(c
.exists("goo"), false);
162 test("type (basic)");
164 ASSERT_EQUAL(c
.type(goo
), redis::client::datatype_none
);test("we deleted it");
166 ASSERT_EQUAL(c
.type(goo
), redis::client::datatype_string
);
171 redis::client::string_vector keys
;
172 ASSERT_EQUAL(c
.keys("*oo", keys
), 2L);
173 ASSERT_EQUAL(keys
.size(), 2UL);
174 ASSERT_EQUAL(keys
[0], foo
);
175 ASSERT_EQUAL(keys
[1], goo
);
180 ASSERT_GT(c
.randomkey().size(), 0UL);
185 ASSERT_EQUAL(c
.exists("foo"), true);
186 ASSERT_EQUAL(c
.exists("doo"), false);
187 c
.rename("foo", "doo");
188 ASSERT_EQUAL(c
.exists("foo"), false);
189 ASSERT_EQUAL(c
.exists("doo"), true);
194 ASSERT_EQUAL(c
.exists("doo"), true);
195 ASSERT_EQUAL(c
.exists("foo"), false);
196 ASSERT_EQUAL(c
.renamenx("doo", "foo"), true);
197 ASSERT_EQUAL(c
.exists("doo"), false);
198 ASSERT_EQUAL(c
.exists("foo"), true);
199 ASSERT_EQUAL(c
.renamenx("goo", "foo"), false);
200 ASSERT_EQUAL(c
.exists("foo"), true);
201 ASSERT_EQUAL(c
.exists("goo"), true);
206 ASSERT_GT(c
.dbsize(), 0L);
213 cerr
<< "please wait a few seconds.." << endl
;
216 ASSERT_EQUAL(c
.exists("goo"), false);
221 ASSERT_EQUAL(c
.exists("list1"), false);
222 c
.rpush("list1", "val1");
223 ASSERT_EQUAL(c
.llen("list1"), 1L);
224 ASSERT_EQUAL(c
.type("list1"), redis::client::datatype_list
);
225 c
.rpush("list1", "val2");
226 ASSERT_EQUAL(c
.llen("list1"), 2L);
227 ASSERT_EQUAL(c
.lindex("list1", 0), string("val1"));
228 ASSERT_EQUAL(c
.lindex("list1", 1), string("val2"));
234 ASSERT_EQUAL(c
.exists("list1"), false);
235 c
.lpush("list1", "val1");
236 ASSERT_EQUAL(c
.type("list1"), redis::client::datatype_list
);
237 ASSERT_EQUAL(c
.llen("list1"), 1L);
238 c
.lpush("list1", "val2");
239 ASSERT_EQUAL(c
.llen("list1"), 2L);
240 ASSERT_EQUAL(c
.lindex("list1", 0), string("val2"));
241 ASSERT_EQUAL(c
.lindex("list1", 1), string("val1"));
247 ASSERT_EQUAL(c
.exists("list1"), false);
248 ASSERT_EQUAL(c
.llen("list1"), 0L);
249 c
.lpush("list1", "x");
250 ASSERT_EQUAL(c
.llen("list1"), 1L);
251 c
.lpush("list1", "y");
252 ASSERT_EQUAL(c
.llen("list1"), 2L);
257 ASSERT_EQUAL(c
.exists("list1"), true);
258 ASSERT_EQUAL(c
.llen("list1"), 2L);
259 redis::client::string_vector vals
;
260 ASSERT_EQUAL(c
.lrange("list1", 0, -1, vals
), 2L);
261 ASSERT_EQUAL(vals
.size(), 2UL);
262 ASSERT_EQUAL(vals
[0], string("y"));
263 ASSERT_EQUAL(vals
[1], string("x"));
266 test("lrange with subset of full list");
268 ASSERT_EQUAL(c
.exists("list1"), true);
269 ASSERT_EQUAL(c
.llen("list1"), 2L);
270 redis::client::string_vector vals
;
271 ASSERT_EQUAL(c
.lrange("list1", 0, 1, vals
), 2L); // inclusive, so entire list
272 ASSERT_EQUAL(vals
.size(), 2UL);
273 ASSERT_EQUAL(vals
[0], string("y"));
274 ASSERT_EQUAL(vals
[1], string("x"));
276 redis::client::string_vector vals2
;
277 ASSERT_EQUAL(c
.lrange("list1", 0, 0, vals2
), 1L); // inclusive, so first item
278 ASSERT_EQUAL(vals2
.size(), 1UL);
279 ASSERT_EQUAL(vals2
[0], string("y"));
281 redis::client::string_vector vals3
;
282 ASSERT_EQUAL(c
.lrange("list1", -1, -1, vals3
), 1L); // inclusive, so first item
283 ASSERT_EQUAL(vals3
.size(), 1UL);
284 ASSERT_EQUAL(vals3
[0], string("x"));
289 ASSERT_EQUAL(c
.exists("list1"), true);
290 ASSERT_EQUAL(c
.llen("list1"), 2L);
291 redis::client::string_vector vals
;
292 ASSERT_EQUAL(c
.get_list("list1", vals
), 2L);
293 ASSERT_EQUAL(vals
.size(), 2UL);
294 ASSERT_EQUAL(vals
[0], string("y"));
295 ASSERT_EQUAL(vals
[1], string("x"));
300 ASSERT_EQUAL(c
.exists("list1"), true);
301 ASSERT_EQUAL(c
.llen("list1"), 2L);
302 c
.ltrim("list1", 0, 0);
303 ASSERT_EQUAL(c
.exists("list1"), true);
304 ASSERT_EQUAL(c
.llen("list1"), 1L);
305 redis::client::string_vector vals
;
306 ASSERT_EQUAL(c
.get_list("list1", vals
), 1L);
307 ASSERT_EQUAL(vals
[0], string("y"));
312 ASSERT_EQUAL(c
.lindex("list1", 0), string("y"));
313 c
.rpush("list1", "x");
314 ASSERT_EQUAL(c
.llen("list1"), 2L);
315 ASSERT_EQUAL(c
.lindex("list1", -1), string("x"));
316 ASSERT_EQUAL(c
.lindex("list1", 1), string("x"));
321 c
.lset("list1", 1, "z");
322 ASSERT_EQUAL(c
.lindex("list1", 1), string("z"));
323 ASSERT_EQUAL(c
.llen("list1"), 2L);
328 c
.lrem("list1", 1, "z");
329 ASSERT_EQUAL(c
.llen("list1"), 1L);
330 ASSERT_EQUAL(c
.lindex("list1", 0), string("y"));
333 ASSERT_EQUAL(c
.lrem("list1", 0, "q"), 0L);
335 c
.rpush("list1", "z");
336 c
.rpush("list1", "z");
337 c
.rpush("list1", "z");
338 c
.rpush("list1", "a");
339 // list1 = [ y, z, z, z, a ]
340 ASSERT_EQUAL(c
.lrem("list1", 2, "z"), 2L);
341 // list1 = [ y, z, a ]
342 ASSERT_EQUAL(c
.llen("list1"), 3L);
343 ASSERT_EQUAL(c
.lindex("list1", 0), string("y"));
344 ASSERT_EQUAL(c
.lindex("list1", 1), string("z"));
345 ASSERT_EQUAL(c
.lindex("list1", 2), string("a"));
347 c
.rpush("list1", "z");
348 // list1 = [ y, z, a, z ]
349 ASSERT_EQUAL(c
.lrem("list1", -1, "z"), 1L); // <0 => rm R to L
350 // list1 = [ y, z, a ]
351 ASSERT_EQUAL(c
.llen("list1"), 3L);
352 ASSERT_EQUAL(c
.lindex("list1", 0), string("y"));
353 ASSERT_EQUAL(c
.lindex("list1", 1), string("z"));
354 ASSERT_EQUAL(c
.lindex("list1", 2), string("a"));
356 // list1 = [ y, z, a ]
357 // try to remove 5 'a's but there's only 1 ... no problem.
358 ASSERT_EQUAL(c
.lrem("list1", 5, "a"), 1L);
360 ASSERT_EQUAL(c
.llen("list1"), 2L);
361 ASSERT_EQUAL(c
.lindex("list1", 0), string("y"));
362 ASSERT_EQUAL(c
.lindex("list1", 1), string("z"));
369 // try to remove 5 'z's but there's only 1 ... now it's a problem.
375 c
.lrem_exact("list1", 5, "z");
377 catch (redis::value_error
& e
)
382 ASSERT_EQUAL(threw
, true);
384 // This DOES remove the one 'z' though
387 ASSERT_EQUAL(c
.llen("list1"), 1L);
388 ASSERT_EQUAL(c
.lindex("list1", 0), string("y"));
393 ASSERT_EQUAL(c
.lpop("list1"), string("y"));
395 ASSERT_EQUAL(c
.lpop("list1"), redis::client::missing_value
);
400 c
.rpush("list1", "hello");
401 c
.rpush("list1", "world");
402 ASSERT_EQUAL(c
.rpop("list1"), string("world"));
403 ASSERT_EQUAL(c
.rpop("list1"), string("hello"));
404 ASSERT_EQUAL(c
.lpop("list1"), redis::client::missing_value
);
409 c
.sadd("set1", "sval1");
410 ASSERT_EQUAL(c
.exists("set1"), true);
411 ASSERT_EQUAL(c
.type("set1"), redis::client::datatype_set
);
412 ASSERT_EQUAL(c
.sismember("set1", "sval1"), true);
417 c
.srem("set1", "sval1");
418 ASSERT_EQUAL(c
.exists("set1"), true);
419 ASSERT_EQUAL(c
.type("set1"), redis::client::datatype_set
);
420 ASSERT_EQUAL(c
.sismember("set1", "sval1"), false);
425 c
.sadd("set1", "hi");
427 ASSERT_EQUAL(c
.exists("set2"), false);
428 c
.smove("set1", "set2", "hi");
429 ASSERT_EQUAL(c
.sismember("set1", "hi"), false);
430 ASSERT_EQUAL(c
.sismember("set2", "hi"), true);
435 ASSERT_EQUAL(c
.scard("set1"), 0L);
436 ASSERT_EQUAL(c
.scard("set2"), 1L);
446 c
.sadd("set2", "bye");
447 redis::client::string_set members
;
448 ASSERT_EQUAL(c
.smembers("set2", members
), 2L);
449 ASSERT_EQUAL(members
.size(), 2UL);
450 ASSERT_NOT_EQUAL(members
.find("hi"), members
.end());
451 ASSERT_NOT_EQUAL(members
.find("bye"), members
.end());
456 c
.sadd("set3", "bye");
457 c
.sadd("set3", "bye2");
458 redis::client::string_vector keys
;
459 keys
.push_back("set2");
460 keys
.push_back("set3");
461 redis::client::string_set intersection
;
462 ASSERT_EQUAL(c
.sinter(keys
, intersection
), 1L);
463 ASSERT_EQUAL(intersection
.size(), 1UL);
464 ASSERT_NOT_EQUAL(intersection
.find("bye"), intersection
.end());
477 redis::client::string_vector keys
;
478 keys
.push_back("seta");
479 keys
.push_back("setb");
481 c
.sinterstore("setc", keys
);
483 redis::client::string_set members
;
484 ASSERT_EQUAL(c
.smembers("setc", members
), 2L);
485 ASSERT_EQUAL(members
.size(), 2UL);
486 ASSERT_NOT_EQUAL(members
.find("2"), members
.end());
487 ASSERT_NOT_EQUAL(members
.find("3"), members
.end());
494 redis::client::string_vector keys
;
495 keys
.push_back("setd");
496 keys
.push_back("sete");
497 redis::client::string_set a_union
;
498 ASSERT_EQUAL(c
.sunion(keys
, a_union
), 2L);
499 ASSERT_EQUAL(a_union
.size(), 2UL);
500 ASSERT_NOT_EQUAL(a_union
.find("1"), a_union
.end());
501 ASSERT_NOT_EQUAL(a_union
.find("2"), a_union
.end());
509 redis::client::string_vector keys
;
510 keys
.push_back("setf");
511 keys
.push_back("setg");
513 c
.sunionstore("seth", keys
);
515 redis::client::string_set members
;
516 ASSERT_EQUAL(c
.smembers("seth", members
), 2L);
517 ASSERT_EQUAL(members
.size(), 2UL);
518 ASSERT_NOT_EQUAL(members
.find("1"), members
.end());
519 ASSERT_NOT_EQUAL(members
.find("2"), members
.end());
525 ASSERT_EQUAL(c
.exists("ttt"), false);
530 ASSERT_EQUAL(c
.exists("ttt"), true);
532 ASSERT_EQUAL(c
.exists("ttt"), false);
535 test("move should fail since key exists already");
548 catch (redis::protocol_error
& e
)
553 ASSERT_EQUAL(threw
, true);
556 ASSERT_EQUAL(c
.exists("ttt"), true);
558 ASSERT_EQUAL(c
.exists("ttt"), true);
561 test("sort ascending");
563 c
.sadd("sort1", "3");
564 c
.sadd("sort1", "2");
565 c
.sadd("sort1", "1");
567 redis::client::string_vector sorted
;
568 ASSERT_EQUAL(c
.sort("sort1", sorted
), 3L);
569 ASSERT_EQUAL(sorted
.size(), 3UL);
570 ASSERT_EQUAL(sorted
[0], string("1"));
571 ASSERT_EQUAL(sorted
[1], string("2"));
572 ASSERT_EQUAL(sorted
[2], string("3"));
575 test("sort descending");
577 redis::client::string_vector sorted
;
578 ASSERT_EQUAL(c
.sort("sort1", sorted
, redis::client::sort_order_descending
), 3L);
579 ASSERT_EQUAL(sorted
.size(), 3UL);
580 ASSERT_EQUAL(sorted
[0], string("3"));
581 ASSERT_EQUAL(sorted
[1], string("2"));
582 ASSERT_EQUAL(sorted
[2], string("1"));
585 test("sort with limit");
590 test("sort lexicographically");
595 test("sort with pattern and weights");
612 ASSERT_GT(c
.lastsave(), 0L);
617 // You can test this if you really want to ...
621 catch (redis::redis_error
& e
)
623 cerr
<< "got exception: " << string(e
) << endl
<< "FAIL" << endl
;
627 cout
<< endl
<< "testing completed successfully" << endl
;