+#include <string>
+
+using namespace std;
+
+extern "C" {
+ test_return basic_test(memcached_st *memc);
+ test_return increment_test(memcached_st *memc);
+ test_return basic_master_key_test(memcached_st *memc);
+ test_return mget_result_function(memcached_st *memc);
+ test_return mget_test(memcached_st *memc);
+ memcached_return callback_counter(memcached_st *,
+ memcached_result_st *,
+ void *context);
+ void *world_create(void);
+ void world_destroy(void *p);
+}
+
+static void populate_vector(vector<char> &vec, const string &str)
+{
+ vec.reserve(str.length());
+ memcpy(&*vec.begin(), str.c_str(), str.length());
+}
+
+static void copy_vec_to_string(vector<char> &vec, string &str)
+{
+ str.clear();
+ char *tmp= static_cast<char *>(malloc(vec.size() * sizeof(char)));
+ if (!vec.empty())
+ {
+ memcpy(tmp, &vec[0], vec.size());
+ str.assign(tmp);
+ }
+}
+
+test_return basic_test(memcached_st *memc)
+{
+ Memcached foo(memc);
+ const string value_set("This is some data");
+ std::vector<char> value;
+ std::vector<char> test_value;
+
+ populate_vector(value, value_set);
+
+ foo.set("mine", value, 0, 0);
+ test_value= foo.get("mine", test_value);
+
+ assert((memcmp(&test_value[0], &value[0], test_value.size()) == 0));
+
+ return TEST_SUCCESS;
+}
+
+test_return increment_test(memcached_st *memc)
+{
+ Memcached mcach(memc);
+ bool rc;
+ const string key("blah");
+ const string inc_value("1");
+ std::vector<char> inc_val;
+ vector<char> ret_value;
+ string ret_string;
+ uint64_t int_inc_value;
+ uint64_t int_ret_value;
+
+ populate_vector(inc_val, inc_value);
+
+ rc= mcach.set(key, inc_val, 0, 0);
+ if (rc == false)
+ {
+ return TEST_FAILURE;
+ }
+ ret_value= mcach.get(key, ret_value);
+ if (ret_value.empty())
+ {
+ return TEST_FAILURE;
+ }
+ copy_vec_to_string(ret_value, ret_string);
+
+ printf("string is: %s\n", ret_string.c_str());
+
+ int_inc_value= uint64_t(atol(inc_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);
+ assert(rc == true);
+ assert(int_ret_value == 2);
+
+ rc= mcach.increment(key, 1, &int_ret_value);
+ assert(rc == true);
+ assert(int_ret_value == 3);
+
+ rc= mcach.increment(key, 5, &int_ret_value);
+ assert(rc == true);
+ assert(int_ret_value == 8);
+
+ return TEST_SUCCESS;
+}
+
+test_return basic_master_key_test(memcached_st *memc)
+{
+ Memcached foo(memc);
+ const string value_set("Data for server A");
+ vector<char> value;
+ vector<char> test_value;
+ const string master_key_a("server-a");
+ const string master_key_b("server-b");
+ const string key("xyz");
+
+ populate_vector(value, value_set);
+
+ foo.setByKey(master_key_a, key, value, 0, 0);
+ test_value= foo.getByKey(master_key_a, key, test_value);
+
+ assert((memcmp(&value[0], &test_value[0], value.size()) == 0));
+
+ test_value.clear();
+
+ test_value= foo.getByKey(master_key_b, key, test_value);
+ assert((memcmp(&value[0], &test_value[0], value.size()) == 0));
+
+ return TEST_SUCCESS;
+}
+
+/* Count the results */
+memcached_return callback_counter(memcached_st *,
+ memcached_result_st *,
+ void *context)
+{
+ unsigned int *counter= static_cast<unsigned int *>(context);
+
+ *counter= *counter + 1;
+
+ return MEMCACHED_SUCCESS;
+}
+
+test_return mget_result_function(memcached_st *memc)
+{
+ Memcached mc(memc);
+ bool rc;
+ string key1("fudge");
+ string key2("son");
+ string key3("food");
+ vector<string> keys;
+ vector< vector<char> > values;
+ vector<char> val1;
+ vector<char> val2;
+ vector<char> 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.setAll(keys, values, 50, 9);
+ assert(rc == true);
+
+ rc= mc.mget(keys);
+ assert(rc == true);
+
+ callbacks[0]= &callback_counter;
+ counter= 0;
+ rc= mc.fetchExecute(callbacks, static_cast<void *>(&counter), 1);
+
+ assert(counter == 3);
+
+ return TEST_SUCCESS;
+}
+
+test_return mget_test(memcached_st *memc)