+#include <string>
+
+using namespace std;
+using namespace memcache;
+
+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());
+ vec.assign(str.begin(), str.end());
+}
+
+static void copy_vec_to_string(vector<char> &vec, string &str)
+{
+ str.clear();
+ if (! vec.empty())
+ {
+ str.assign(vec.begin(), vec.end());
+ }
+}
+
+test_return basic_test(memcached_st *memc)
+{
+ Memcache 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);
+ 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)
+{
+ Memcache 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;
+ }
+ 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_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)
+{
+ Memcache 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);
+ foo.getByKey(master_key_a, key, test_value);
+
+ assert((memcmp(&value[0], &test_value[0], value.size()) == 0));
+
+ test_value.clear();
+
+ 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)