X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=tests%2Fplus.cpp;h=6b43164433cfeafa8741918b66af8d02e3d3ee5d;hb=1b18496b3d1399d90cfba1975f23aee9c747445a;hp=dc075478c9fdec7b9c4091c6912c2cd311b7b2ce;hpb=f4ab6afe5f2a8a038d5a79afbf25eae8ad300cc6;p=m6w6%2Flibmemcached diff --git a/tests/plus.cpp b/tests/plus.cpp index dc075478..6b431644 100644 --- a/tests/plus.cpp +++ b/tests/plus.cpp @@ -1,7 +1,7 @@ /* C++ interface test */ -#include "libmemcached/memcached.hh" +#include "libmemcached/memcached.hpp" #include #include @@ -19,6 +19,7 @@ #include using namespace std; +using namespace memcache; extern "C" { test_return basic_test(memcached_st *memc); @@ -33,37 +34,66 @@ extern "C" { void world_destroy(void *p); } +static void populate_vector(vector &vec, const string &str) +{ + vec.reserve(str.length()); + vec.assign(str.begin(), str.end()); +} + +static void copy_vec_to_string(vector &vec, string &str) +{ + str.clear(); + if (! vec.empty()) + { + str.assign(vec.begin(), vec.end()); + } +} + test_return basic_test(memcached_st *memc) { - Memcached foo(memc); + Memcache foo(memc); const string value_set("This is some data"); - string value; - size_t value_length; + std::vector value; + std::vector test_value; + + populate_vector(value, value_set); - foo.set("mine", value_set, 0, 0); - value= foo.get("mine", &value_length); + foo.set("mine", value, 0, 0); + foo.get("mine", test_value); - assert((memcmp(value.c_str(), value_set.c_str(), value_length) == 0)); + assert((memcmp(&test_value[0], &value[0], test_value.size()) == 0)); return TEST_SUCCESS; } test_return increment_test(memcached_st *memc) { - Memcached mcach(memc); + Memcache mcach(memc); bool rc; - const string key("inctest"); + const string key("blah"); const string inc_value("1"); - string ret_value; + std::vector inc_val; + vector ret_value; + string ret_string; uint64_t int_inc_value; uint64_t int_ret_value; - size_t value_length; - mcach.set(key, inc_value, 0, 0); - ret_value= mcach.get(key, &value_length); - printf("\nretvalue %s\n",ret_value.c_str()); + populate_vector(inc_val, inc_value); + + rc= mcach.set(key, inc_val, 0, 0); + if (rc == false) + { + return TEST_FAILURE; + } + mcach.get(key, ret_value); + if (ret_value.empty()) + { + return TEST_FAILURE; + } + copy_vec_to_string(ret_value, ret_string); + int_inc_value= uint64_t(atol(inc_value.c_str())); - int_ret_value= uint64_t(atol(ret_value.c_str())); + int_ret_value= uint64_t(atol(ret_string.c_str())); assert(int_ret_value == int_inc_value); rc= mcach.increment(key, 1, &int_ret_value); @@ -83,21 +113,25 @@ test_return increment_test(memcached_st *memc) test_return basic_master_key_test(memcached_st *memc) { - Memcached foo(memc); + Memcache foo(memc); const string value_set("Data for server A"); + vector value; + vector test_value; const string master_key_a("server-a"); const string master_key_b("server-b"); const string key("xyz"); - string value; - size_t value_length; - foo.set_by_key(master_key_a, key, value_set, 0, 0); - value= foo.get_by_key(master_key_a, key, &value_length); + populate_vector(value, value_set); + + foo.setByKey(master_key_a, key, value, 0, 0); + foo.getByKey(master_key_a, key, test_value); + + assert((memcmp(&value[0], &test_value[0], value.size()) == 0)); - assert((memcmp(value.c_str(), value_set.c_str(), value_length) == 0)); + test_value.clear(); - value= foo.get_by_key(master_key_b, key, &value_length); - assert((memcmp(value.c_str(), value_set.c_str(), value_length) == 0)); + foo.getByKey(master_key_b, key, test_value); + assert((memcmp(&value[0], &test_value[0], value.size()) == 0)); return TEST_SUCCESS; } @@ -116,22 +150,33 @@ memcached_return callback_counter(memcached_st *, test_return mget_result_function(memcached_st *memc) { - Memcached mc(memc); + Memcache mc(memc); bool rc; string key1("fudge"); string key2("son"); string key3("food"); vector keys; + vector< vector *> values; + vector val1; + vector val2; + vector val3; + populate_vector(val1, key1); + populate_vector(val2, key2); + populate_vector(val3, key3); keys.reserve(3); keys.push_back(key1); keys.push_back(key2); keys.push_back(key3); + values.reserve(3); + values.push_back(&val1); + values.push_back(&val2); + values.push_back(&val3); unsigned int counter; memcached_execute_function callbacks[1]; /* We need to empty the server before we continue the test */ rc= mc.flush(0); - rc= mc.set_all(keys, keys, 50, 9); + rc= mc.setAll(keys, values, 50, 9); assert(rc == true); rc= mc.mget(keys); @@ -139,7 +184,7 @@ test_return mget_result_function(memcached_st *memc) callbacks[0]= &callback_counter; counter= 0; - rc= mc.fetch_execute(callbacks, static_cast(&counter), 1); + rc= mc.fetchExecute(callbacks, static_cast(&counter), 1); assert(counter == 3); @@ -148,20 +193,28 @@ test_return mget_result_function(memcached_st *memc) test_return mget_test(memcached_st *memc) { - Memcached mc(memc); + Memcache mc(memc); bool rc; memcached_return mc_rc; vector keys; + vector< vector *> values; keys.reserve(3); keys.push_back("fudge"); keys.push_back("son"); keys.push_back("food"); - uint32_t flags; + vector val1; + vector val2; + vector val3; + populate_vector(val1, "fudge"); + populate_vector(val2, "son"); + populate_vector(val3, "food"); + values.reserve(3); + values.push_back(&val1); + values.push_back(&val2); + values.push_back(&val3); string return_key; - size_t return_key_length; - string return_value; - size_t return_value_length; + vector return_value; /* We need to empty the server before we continue the test */ rc= mc.flush(0); @@ -170,27 +223,23 @@ test_return mget_test(memcached_st *memc) rc= mc.mget(keys); assert(rc == true); - while (mc.fetch(return_key, return_value, &return_key_length, - &return_value_length, &flags, &mc_rc)) + while ((mc_rc= mc.fetch(return_key, return_value)) != MEMCACHED_END) { - assert(return_value.length() != 0); + assert(return_value.size() != 0); + return_value.clear(); } - assert(return_value_length == 0); assert(mc_rc == MEMCACHED_END); - rc= mc.set_all(keys, keys, 50, 9); + rc= mc.setAll(keys, values, 50, 9); assert(rc == true); rc= mc.mget(keys); assert(rc == true); - while ((mc.fetch(return_key, return_value, &return_key_length, - &return_value_length, &flags, &mc_rc))) + while ((mc_rc= mc.fetch(return_key, return_value)) != MEMCACHED_END) { - assert(return_value.length() != 0); - assert(mc_rc == MEMCACHED_SUCCESS); - assert(return_key_length == return_value_length); - assert(!memcmp(return_value.c_str(), return_key.c_str(), return_value_length)); + assert(return_key.length() == return_value.size()); + assert(!memcmp(&return_value[0], return_key.c_str(), return_value.size())); } return TEST_SUCCESS;