+uint8_t replace_test(memcached_st *memc)
+{
+ memcached_return rc;
+ char *key= "foo";
+ char *value= "when we sanitize";
+ char *original= "first we insert some data";
+
+ rc= memcached_set(memc, key, strlen(key),
+ original, strlen(original),
+ (time_t)0, (uint32_t)0);
+ assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+
+ rc= memcached_replace(memc, key, strlen(key),
+ value, strlen(value),
+ (time_t)0, (uint32_t)0);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ return 0;
+}
+
+uint8_t delete_test(memcached_st *memc)
+{
+ memcached_return rc;
+ char *key= "foo";
+ char *value= "when we sanitize";
+
+ rc= memcached_set(memc, key, strlen(key),
+ value, strlen(value),
+ (time_t)0, (uint32_t)0);
+ assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+
+ rc= memcached_delete(memc, key, strlen(key), (time_t)0);
+ assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+
+ return 0;
+}
+
+uint8_t flush_test(memcached_st *memc)
+{
+ memcached_return rc;
+
+ rc= memcached_flush(memc, 0);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ return 0;
+}
+
+memcached_return server_function(memcached_st *ptr, memcached_server_st *server, void *context)
+{
+ /* Do Nothing */
+
+ return MEMCACHED_SUCCESS;
+}
+
+uint8_t memcached_server_cursor_test(memcached_st *memc)
+{
+ char *context= "foo bad";
+ memcached_server_function callbacks[1];
+
+ callbacks[0]= server_function;
+ memcached_server_cursor(memc, callbacks, context, 1);
+
+ return 0;
+}
+
+uint8_t bad_key_test(memcached_st *memc)
+{
+ memcached_return rc;
+ char *key= "foo bad";
+ char *string;
+ size_t string_length;
+ uint32_t flags;
+ memcached_st *clone;
+ unsigned int set= 1;
+
+ clone= memcached_clone(NULL, memc);
+ assert(clone);
+
+ (void)memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
+
+ string= memcached_get(clone, key, strlen(key),
+ &string_length, &flags, &rc);
+ assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
+ assert(string_length == 0);
+ assert(!string);
+
+ set= 0;
+ (void)memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set);
+ string= memcached_get(clone, key, strlen(key),
+ &string_length, &flags, &rc);
+ assert(rc == MEMCACHED_NOTFOUND);
+ assert(string_length == 0);
+ assert(!string);
+
+ memcached_free(clone);
+
+ return 0;
+}
+
+#define READ_THROUGH_VALUE "set for me"
+memcached_return read_through_trigger(memcached_st *memc,
+ char *key, size_t key_length,
+ memcached_result_st *result)
+{
+
+ return memcached_result_set_value(result, READ_THROUGH_VALUE, strlen(READ_THROUGH_VALUE));
+}
+
+uint8_t read_through(memcached_st *memc)
+{
+ memcached_return rc;
+ char *key= "foo";
+ char *string;
+ size_t string_length;
+ uint32_t flags;
+
+ string= memcached_get(memc, key, strlen(key),
+ &string_length, &flags, &rc);
+
+ assert(rc == MEMCACHED_NOTFOUND);
+ assert(string_length == 0);
+ assert(!string);
+
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_GET_FAILURE, read_through_trigger);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ string= memcached_get(memc, key, strlen(key),
+ &string_length, &flags, &rc);
+
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(string_length == strlen(READ_THROUGH_VALUE));
+ assert(!strcmp(READ_THROUGH_VALUE, string));
+ free(string);
+
+ string= memcached_get(memc, key, strlen(key),
+ &string_length, &flags, &rc);
+
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(string_length == strlen(READ_THROUGH_VALUE));
+ assert(!strcmp(READ_THROUGH_VALUE, string));
+ free(string);
+
+ return 0;
+}
+
+memcached_return delete_trigger(memcached_st *ptr, char *key, size_t key_length)
+{
+ assert(key);
+
+ return MEMCACHED_SUCCESS;
+}
+
+uint8_t delete_through(memcached_st *memc)
+{
+ memcached_trigger_delete_key callback;
+ memcached_return rc;
+
+ callback= delete_trigger;
+
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_DELETE_TRIGGER, callback);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ return 0;
+}
+
+uint8_t get_test(memcached_st *memc)
+{
+ memcached_return rc;
+ char *key= "foo";
+ char *string;
+ size_t string_length;
+ uint32_t flags;
+
+ rc= memcached_delete(memc, key, strlen(key), (time_t)0);
+ assert(rc == MEMCACHED_BUFFERED || rc == MEMCACHED_NOTFOUND);
+
+ string= memcached_get(memc, key, strlen(key),
+ &string_length, &flags, &rc);
+
+ assert(rc == MEMCACHED_NOTFOUND);
+ assert(string_length == 0);
+ assert(!string);
+
+ return 0;
+}
+
+uint8_t get_test2(memcached_st *memc)
+{
+ memcached_return rc;
+ char *key= "foo";
+ char *value= "when we sanitize";
+ char *string;
+ size_t string_length;
+ uint32_t flags;
+
+ rc= memcached_set(memc, key, strlen(key),
+ value, strlen(value),
+ (time_t)0, (uint32_t)0);
+ assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+
+ string= memcached_get(memc, key, strlen(key),
+ &string_length, &flags, &rc);
+
+ assert(string);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(string_length == strlen(value));
+ assert(!memcmp(string, value, string_length));
+
+ free(string);
+
+ return 0;
+}
+
+uint8_t set_test2(memcached_st *memc)
+{
+ memcached_return rc;
+ char *key= "foo";
+ char *value= "train in the brain";
+ size_t value_length= strlen(value);
+ unsigned int x;
+
+ for (x= 0; x < 10; x++)
+ {
+ rc= memcached_set(memc, key, strlen(key),
+ value, value_length,
+ (time_t)0, (uint32_t)0);
+ assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+ }
+
+ return 0;
+}
+
+uint8_t set_test3(memcached_st *memc)
+{
+ memcached_return rc;
+ char *key= "foo";
+ char *value;
+ size_t value_length= 8191;
+ unsigned int x;
+
+ value = (char*)malloc(value_length);
+ assert(value);
+
+ for (x= 0; x < value_length; x++)
+ value[x] = (char) (x % 127);
+
+ for (x= 0; x < 1; x++)
+ {
+ rc= memcached_set(memc, key, strlen(key),
+ value, value_length,
+ (time_t)0, (uint32_t)0);
+ assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+ }
+
+ free(value);
+
+ return 0;
+}
+
+uint8_t get_test3(memcached_st *memc)
+{
+ memcached_return rc;
+ char *key= "foo";
+ char *value;
+ size_t value_length= 8191;
+ char *string;
+ size_t string_length;
+ uint32_t flags;
+ int x;
+
+ value = (char*)malloc(value_length);
+ assert(value);
+
+ for (x= 0; x < value_length; x++)
+ value[x] = (char) (x % 127);
+
+ rc= memcached_set(memc, key, strlen(key),
+ value, value_length,
+ (time_t)0, (uint32_t)0);
+ assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+
+ string= memcached_get(memc, key, strlen(key),
+ &string_length, &flags, &rc);
+
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(string);
+ assert(string_length == value_length);
+ assert(!memcmp(string, value, string_length));
+
+ free(string);
+ free(value);
+
+ return 0;
+}
+
+uint8_t get_test4(memcached_st *memc)
+{
+ memcached_return rc;
+ char *key= "foo";
+ char *value;
+ size_t value_length= 8191;
+ char *string;
+ size_t string_length;
+ uint32_t flags;
+ int x;
+
+ value = (char*)malloc(value_length);
+ assert(value);
+
+ for (x= 0; x < value_length; x++)
+ value[x] = (char) (x % 127);
+
+ rc= memcached_set(memc, key, strlen(key),
+ value, value_length,
+ (time_t)0, (uint32_t)0);
+ assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+
+ for (x= 0; x < 10; x++)
+ {
+ string= memcached_get(memc, key, strlen(key),
+ &string_length, &flags, &rc);
+
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(string);
+ assert(string_length == value_length);
+ assert(!memcmp(string, value, string_length));
+ free(string);
+ }
+
+ free(value);
+
+ return 0;
+}
+
+/* Do not copy the style of this code, I just access hosts to testthis function */
+uint8_t stats_servername_test(memcached_st *memc)
+{
+ memcached_return rc;
+ memcached_stat_st stat;
+ rc= memcached_stat_servername(&stat, NULL,
+ memc->hosts[0].hostname,
+ memc->hosts[0].port);
+
+ return 0;
+}
+
+uint8_t increment_test(memcached_st *memc)
+{
+ uint64_t new_number;
+ memcached_return rc;
+ char *key= "number";
+ char *value= "0";
+
+ rc= memcached_set(memc, key, strlen(key),
+ value, strlen(value),
+ (time_t)0, (uint32_t)0);
+ assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+
+ rc= memcached_increment(memc, key, strlen(key),
+ 1, &new_number);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(new_number == 1);
+
+ rc= memcached_increment(memc, key, strlen(key),
+ 1, &new_number);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(new_number == 2);
+
+ return 0;
+}
+
+uint8_t decrement_test(memcached_st *memc)
+{
+ uint64_t new_number;
+ memcached_return rc;
+ char *key= "number";
+ char *value= "3";
+
+ rc= memcached_set(memc, key, strlen(key),
+ value, strlen(value),
+ (time_t)0, (uint32_t)0);
+ assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+
+ rc= memcached_decrement(memc, key, strlen(key),
+ 1, &new_number);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(new_number == 2);
+
+ rc= memcached_decrement(memc, key, strlen(key),
+ 1, &new_number);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(new_number == 1);
+
+ return 0;
+}
+
+uint8_t quit_test(memcached_st *memc)
+{
+ memcached_return rc;
+ char *key= "fudge";
+ char *value= "sanford and sun";
+
+ rc= memcached_set(memc, key, strlen(key),
+ value, strlen(value),
+ (time_t)10, (uint32_t)3);
+ assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+ memcached_quit(memc);
+
+ rc= memcached_set(memc, key, strlen(key),
+ value, strlen(value),
+ (time_t)50, (uint32_t)9);
+ assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+
+ return 0;
+}
+
+uint8_t mget_result_test(memcached_st *memc)
+{
+ memcached_return rc;
+ char *keys[]= {"fudge", "son", "food"};
+ size_t key_length[]= {5, 3, 4};
+ unsigned int x;
+
+ memcached_result_st results_obj;
+ memcached_result_st *results;
+
+ results= memcached_result_create(memc, &results_obj);
+ assert(results);
+ assert(&results_obj == results);
+
+ /* We need to empty the server before continueing test */
+ rc= memcached_flush(memc, 0);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ rc= memcached_mget(memc, keys, key_length, 3);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ while ((results= memcached_fetch_result(memc, &results_obj, &rc)) != NULL)
+ {
+ assert(results);
+ }
+
+ while ((results= memcached_fetch_result(memc, &results_obj, &rc)) != NULL)
+ assert(!results);
+ assert(rc == MEMCACHED_END);
+
+ for (x= 0; x < 3; x++)
+ {
+ rc= memcached_set(memc, keys[x], key_length[x],
+ keys[x], key_length[x],
+ (time_t)50, (uint32_t)9);
+ assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+ }
+
+ rc= memcached_mget(memc, keys, key_length, 3);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
+ {
+ assert(results);
+ assert(&results_obj == results);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(memcached_result_key_length(results) == memcached_result_length(results));
+ assert(!memcmp(memcached_result_key_value(results),
+ memcached_result_value(results),
+ memcached_result_length(results)));
+ }
+
+ memcached_result_free(&results_obj);
+
+ return 0;
+}
+
+uint8_t mget_result_alloc_test(memcached_st *memc)
+{
+ memcached_return rc;
+ char *keys[]= {"fudge", "son", "food"};
+ size_t key_length[]= {5, 3, 4};
+ unsigned int x;
+
+ memcached_result_st *results;
+
+ /* We need to empty the server before continueing test */
+ rc= memcached_flush(memc, 0);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ rc= memcached_mget(memc, keys, key_length, 3);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ while ((results= memcached_fetch_result(memc, NULL, &rc)) != NULL)
+ {
+ assert(results);
+ }
+ assert(!results);
+ assert(rc == MEMCACHED_END);
+
+ for (x= 0; x < 3; x++)
+ {
+ rc= memcached_set(memc, keys[x], key_length[x],
+ keys[x], key_length[x],
+ (time_t)50, (uint32_t)9);
+ assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+ }
+
+ rc= memcached_mget(memc, keys, key_length, 3);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ x= 0;
+ while ((results= memcached_fetch_result(memc, NULL, &rc)))
+ {
+ assert(results);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(memcached_result_key_length(results) == memcached_result_length(results));
+ assert(!memcmp(memcached_result_key_value(results),
+ memcached_result_value(results),
+ memcached_result_length(results)));
+ memcached_result_free(results);
+ x++;
+ }
+
+ return 0;
+}
+
+/* Count the results */
+unsigned int callback_counter(memcached_st *ptr, memcached_result_st *result, void *context)
+{
+ unsigned int *counter= (unsigned int *)context;
+
+ *counter= *counter + 1;
+
+ return 0;
+}
+
+uint8_t mget_result_function(memcached_st *memc)