+ 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;
+}
+
+static test_return 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;
+}
+
+static test_return 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;
+ uint32_t 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;
+}
+
+static test_return 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;
+ uint32_t 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 */
+static test_return 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;
+}
+
+static test_return 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;
+}
+
+static test_return 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;
+}
+
+static test_return 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;
+}
+
+static test_return 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;
+}
+
+static test_return 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 */
+static memcached_return callback_counter(memcached_st *ptr __attribute__((unused)),
+ memcached_result_st *result __attribute__((unused)),
+ void *context)
+{
+ unsigned int *counter= (unsigned int *)context;
+
+ *counter= *counter + 1;
+
+ return MEMCACHED_SUCCESS;
+}
+
+static test_return mget_result_function(memcached_st *memc)
+{
+ memcached_return rc;
+ char *keys[]= {"fudge", "son", "food"};
+ size_t key_length[]= {5, 3, 4};
+ unsigned int x;
+ unsigned int counter;
+ memcached_execute_function callbacks[1];
+
+ /* We need to empty the server before continueing test */
+ rc= memcached_flush(memc, 0);
+ 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);
+
+ callbacks[0]= &callback_counter;
+ counter= 0;
+ rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
+
+ assert(counter == 3);
+
+ return 0;
+}
+
+static test_return mget_test(memcached_st *memc)
+{
+ memcached_return rc;
+ char *keys[]= {"fudge", "son", "food"};
+ size_t key_length[]= {5, 3, 4};
+ unsigned int x;
+ uint32_t flags;
+
+ char return_key[MEMCACHED_MAX_KEY];
+ size_t return_key_length;
+ char *return_value;
+ size_t return_value_length;
+
+ /* 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 ((return_value= memcached_fetch(memc, return_key, &return_key_length,
+ &return_value_length, &flags, &rc)) != NULL)
+ {
+ assert(return_value);
+ }
+ assert(!return_value);
+ assert(return_value_length == 0);
+ 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 ((return_value= memcached_fetch(memc, return_key, &return_key_length,
+ &return_value_length, &flags, &rc)))
+ {
+ assert(return_value);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(return_key_length == return_value_length);
+ assert(!memcmp(return_value, return_key, return_value_length));
+ free(return_value);
+ x++;
+ }
+
+ return 0;
+}
+
+static test_return get_stats_keys(memcached_st *memc)
+{
+ char **list;
+ char **ptr;
+ memcached_stat_st stat;
+ memcached_return rc;
+
+ list= memcached_stat_get_keys(memc, &stat, &rc);
+ assert(rc == MEMCACHED_SUCCESS);
+ for (ptr= list; *ptr; ptr++)
+ assert(*ptr);
+ fflush(stdout);
+
+ free(list);
+
+ return 0;
+}
+
+static test_return version_string_test(memcached_st *memc __attribute__((unused)))
+{
+ const char *version_string;
+
+ version_string= memcached_lib_version();
+
+ assert(!strcmp(version_string, LIBMEMCACHED_VERSION_STRING));
+
+ return 0;
+}
+
+static test_return get_stats(memcached_st *memc)
+{
+ unsigned int x;
+ char **list;
+ char **ptr;
+ memcached_return rc;
+ memcached_stat_st *stat;
+
+ stat= memcached_stat(memc, NULL, &rc);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(stat);
+
+ for (x= 0; x < memcached_server_count(memc); x++)
+ {
+ list= memcached_stat_get_keys(memc, stat+x, &rc);
+ assert(rc == MEMCACHED_SUCCESS);
+ for (ptr= list; *ptr; ptr++);
+
+ free(list);
+ }
+
+ memcached_stat_free(NULL, stat);
+
+ return 0;
+}
+
+static test_return add_host_test(memcached_st *memc)
+{
+ unsigned int x;
+ memcached_server_st *servers;
+ memcached_return rc;
+ char servername[]= "0.example.com";
+
+ servers= memcached_server_list_append_with_weight(NULL, servername, 400, 0, &rc);
+ assert(servers);
+ assert(1 == memcached_server_list_count(servers));
+
+ for (x= 2; x < 20; x++)
+ {
+ char buffer[SMALL_STRING_LEN];
+
+ snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x);
+ servers= memcached_server_list_append_with_weight(servers, buffer, 401, 0,
+ &rc);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(x == memcached_server_list_count(servers));
+ }
+
+ rc= memcached_server_push(memc, servers);
+ assert(rc == MEMCACHED_SUCCESS);
+ rc= memcached_server_push(memc, servers);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ memcached_server_list_free(servers);
+
+ return 0;
+}
+
+static memcached_return clone_test_callback(memcached_st *parent __attribute__((unused)), memcached_st *clone __attribute__((unused)))
+{
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return cleanup_test_callback(memcached_st *ptr __attribute__((unused)))
+{
+ return MEMCACHED_SUCCESS;
+}
+
+static test_return callback_test(memcached_st *memc)
+{
+ /* Test User Data */
+ {
+ int x= 5;
+ int *test_ptr;
+ memcached_return rc;
+
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_USER_DATA, &x);
+ assert(rc == MEMCACHED_SUCCESS);
+ test_ptr= (int *)memcached_callback_get(memc, MEMCACHED_CALLBACK_USER_DATA, &rc);
+ assert(*test_ptr == x);
+ }
+
+ /* Test Clone Callback */
+ {
+ memcached_clone_func temp_function;
+ memcached_return rc;
+
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, clone_test_callback);
+ assert(rc == MEMCACHED_SUCCESS);
+ temp_function= (memcached_clone_func)memcached_callback_get(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, &rc);
+ assert(temp_function == clone_test_callback);
+ }
+
+ /* Test Cleanup Callback */
+ {
+ memcached_cleanup_func temp_function;
+ memcached_return rc;
+
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, cleanup_test_callback);
+ assert(rc == MEMCACHED_SUCCESS);
+ temp_function= (memcached_cleanup_func)memcached_callback_get(memc, MEMCACHED_CALLBACK_CLONE_FUNCTION, &rc);
+ assert(temp_function == cleanup_test_callback);
+ }
+
+ return 0;
+}
+
+/* We don't test the behavior itself, we test the switches */
+static test_return behavior_test(memcached_st *memc)
+{
+ uint64_t value;
+ uint32_t set= 1;
+
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
+ value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
+ assert(value == 1);
+
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
+ value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
+ assert(value == 1);
+
+ set= MEMCACHED_HASH_MD5;
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
+ value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
+ assert(value == MEMCACHED_HASH_MD5);
+
+ set= 0;
+
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
+ value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
+ assert(value == 0);
+
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
+ value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY);
+ assert(value == 0);
+
+ set= MEMCACHED_HASH_DEFAULT;
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
+ value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
+ assert(value == MEMCACHED_HASH_DEFAULT);
+
+ set= MEMCACHED_HASH_CRC;
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
+ value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
+ assert(value == MEMCACHED_HASH_CRC);
+
+ value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
+ assert(value > 0);
+
+ value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
+ assert(value > 0);
+
+ return 0;
+}
+
+/* Test case provided by Cal Haldenbrand */
+static test_return user_supplied_bug1(memcached_st *memc)
+{
+ unsigned int setter= 1;
+ unsigned int x;
+
+ unsigned long long total= 0;
+ uint32_t size= 0;
+ char key[10];
+ char randomstuff[6 * 1024];
+ memcached_return rc;
+
+ memset(randomstuff, 0, 6 * 1024);
+
+ /* We just keep looking at the same values over and over */
+ srandom(10);
+
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, setter);
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
+
+
+ /* add key */
+ for (x= 0 ; total < 20 * 1024576 ; x++ )
+ {
+ unsigned int j= 0;
+
+ size= (rand() % ( 5 * 1024 ) ) + 400;
+ memset(randomstuff, 0, 6 * 1024);
+ assert(size < 6 * 1024); /* Being safe here */
+
+ for (j= 0 ; j < size ;j++)
+ randomstuff[j] = (char) (rand() % 26) + 97;
+
+ total += size;
+ sprintf(key, "%d", x);
+ rc = memcached_set(memc, key, strlen(key),
+ randomstuff, strlen(randomstuff), 10, 0);
+ WATCHPOINT_ERROR(rc);
+ assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+ /* If we fail, lets try again */
+ if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_BUFFERED)
+ rc = memcached_set(memc, key, strlen(key),
+ randomstuff, strlen(randomstuff), 10, 0);
+ WATCHPOINT_ERROR(rc);
+ assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+ }
+
+ return 0;
+}
+
+/* Test case provided by Cal Haldenbrand */
+static test_return user_supplied_bug2(memcached_st *memc)
+{
+ int errors;
+ unsigned int setter;
+ unsigned int x;
+ unsigned long long total;
+
+ setter= 1;
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, setter);
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
+#ifdef NOT_YET
+ setter = 20 * 1024576;
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, setter);
+ setter = 20 * 1024576;
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, setter);
+ getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
+ getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
+
+ for (x= 0, errors= 0, total= 0 ; total < 20 * 1024576 ; x++)
+#endif
+
+ for (x= 0, errors= 0, total= 0 ; total < 24576 ; x++)
+ {
+ memcached_return rc= MEMCACHED_SUCCESS;
+ char buffer[SMALL_STRING_LEN];
+ uint32_t flags= 0;
+ size_t val_len= 0;
+ char *getval;
+
+ memset(buffer, 0, SMALL_STRING_LEN);
+
+ snprintf(buffer, SMALL_STRING_LEN, "%u", x);
+ getval= memcached_get(memc, buffer, strlen(buffer),
+ &val_len, &flags, &rc);
+ if (rc != MEMCACHED_SUCCESS)
+ {
+ if (rc == MEMCACHED_NOTFOUND)
+ errors++;
+ else
+ {
+ WATCHPOINT_ERROR(rc);
+ assert(0);
+ }
+
+ continue;
+ }
+ total+= val_len;
+ errors= 0;
+ free(getval);
+ }
+
+ return 0;
+}
+
+/* Do a large mget() over all the keys we think exist */
+#define KEY_COUNT 3000 // * 1024576
+static test_return user_supplied_bug3(memcached_st *memc)
+{
+ memcached_return rc;
+ unsigned int setter;
+ unsigned int x;
+ char **keys;
+ size_t key_lengths[KEY_COUNT];
+
+ setter= 1;
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, setter);
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, setter);
+#ifdef NOT_YET
+ setter = 20 * 1024576;
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, setter);
+ setter = 20 * 1024576;
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, setter);
+ getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE);
+ getter = memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE);
+#endif
+
+ keys= (char **)malloc(sizeof(char *) * KEY_COUNT);
+ assert(keys);
+ memset(keys, 0, (sizeof(char *) * KEY_COUNT));
+ for (x= 0; x < KEY_COUNT; x++)
+ {
+ char buffer[30];
+
+ snprintf(buffer, 30, "%u", x);
+ keys[x]= strdup(buffer);
+ key_lengths[x]= strlen(keys[x]);
+ }
+
+ rc= memcached_mget(memc, keys, key_lengths, KEY_COUNT);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ /* Turn this into a help function */
+ {
+ char return_key[MEMCACHED_MAX_KEY];
+ size_t return_key_length;
+ char *return_value;
+ size_t return_value_length;
+ uint32_t flags;
+
+ while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
+ &return_value_length, &flags, &rc)))
+ {
+ assert(return_value);
+ assert(rc == MEMCACHED_SUCCESS);
+ free(return_value);
+ }
+ }
+
+ for (x= 0; x < KEY_COUNT; x++)
+ free(keys[x]);
+ free(keys);
+
+ return 0;
+}
+
+/* Make sure we behave properly if server list has no values */
+static test_return user_supplied_bug4(memcached_st *memc)
+{
+ memcached_return rc;
+ char *keys[]= {"fudge", "son", "food"};
+ size_t key_length[]= {5, 3, 4};
+ unsigned int x;
+ uint32_t flags;
+ char return_key[MEMCACHED_MAX_KEY];
+ size_t return_key_length;
+ char *return_value;
+ size_t return_value_length;
+
+ /* Here we free everything before running a bunch of mget tests */
+ {
+ memcached_server_list_free(memc->hosts);
+ memc->hosts= NULL;
+ memc->number_of_hosts= 0;
+ }
+
+
+ /* We need to empty the server before continueing test */
+ rc= memcached_flush(memc, 0);
+ assert(rc == MEMCACHED_NO_SERVERS);
+
+ rc= memcached_mget(memc, keys, key_length, 3);
+ assert(rc == MEMCACHED_NO_SERVERS);
+
+ while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
+ &return_value_length, &flags, &rc)) != NULL)
+ {
+ assert(return_value);
+ }
+ assert(!return_value);
+ assert(return_value_length == 0);
+ assert(rc == MEMCACHED_NO_SERVERS);
+
+ 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_NO_SERVERS);
+ }
+
+ rc= memcached_mget(memc, keys, key_length, 3);
+ assert(rc == MEMCACHED_NO_SERVERS);
+
+ x= 0;
+ while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
+ &return_value_length, &flags, &rc)))
+ {
+ assert(return_value);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(return_key_length == return_value_length);
+ assert(!memcmp(return_value, return_key, return_value_length));
+ free(return_value);
+ x++;
+ }
+
+ return 0;
+}
+
+#define VALUE_SIZE_BUG5 1048064
+static test_return user_supplied_bug5(memcached_st *memc)
+{
+ memcached_return rc;
+ char *keys[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
+ size_t key_length[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
+ char return_key[MEMCACHED_MAX_KEY];
+ size_t return_key_length;
+ char *value;
+ size_t value_length;
+ uint32_t flags;
+ unsigned int count;
+ unsigned int x;
+ char insert_data[VALUE_SIZE_BUG5];
+
+ for (x= 0; x < VALUE_SIZE_BUG5; x++)
+ insert_data[x]= rand();
+
+ memcached_flush(memc, 0);
+ value= memcached_get(memc, keys[0], key_length[0],
+ &value_length, &flags, &rc);
+ assert(value == NULL);
+ rc= memcached_mget(memc, keys, key_length, 4);
+
+ count= 0;
+ while ((value= memcached_fetch(memc, return_key, &return_key_length,
+ &value_length, &flags, &rc)))
+ count++;
+ assert(count == 0);
+
+ for (x= 0; x < 4; x++)
+ {
+ rc= memcached_set(memc, keys[x], key_length[x],
+ insert_data, VALUE_SIZE_BUG5,
+ (time_t)0, (uint32_t)0);
+ assert(rc == MEMCACHED_SUCCESS);
+ }
+
+ for (x= 0; x < 10; x++)
+ {
+ value= memcached_get(memc, keys[0], key_length[0],
+ &value_length, &flags, &rc);
+ assert(value);
+ free(value);
+
+ rc= memcached_mget(memc, keys, key_length, 4);
+ count= 0;
+ while ((value= memcached_fetch(memc, return_key, &return_key_length,
+ &value_length, &flags, &rc)))
+ {
+ count++;
+ free(value);
+ }
+ assert(count == 4);
+ }
+
+ return 0;
+}
+
+static test_return user_supplied_bug6(memcached_st *memc)
+{
+ memcached_return rc;
+ char *keys[]= {"036790384900", "036790384902", "036790384904", "036790384906"};
+ size_t key_length[]= {strlen("036790384900"), strlen("036790384902"), strlen("036790384904"), strlen("036790384906")};
+ char return_key[MEMCACHED_MAX_KEY];
+ size_t return_key_length;
+ char *value;
+ size_t value_length;
+ uint32_t flags;
+ unsigned int count;
+ unsigned int x;
+ char insert_data[VALUE_SIZE_BUG5];
+
+ for (x= 0; x < VALUE_SIZE_BUG5; x++)
+ insert_data[x]= rand();
+
+ memcached_flush(memc, 0);
+ value= memcached_get(memc, keys[0], key_length[0],
+ &value_length, &flags, &rc);
+ assert(value == NULL);
+ assert(rc == MEMCACHED_NOTFOUND);
+ rc= memcached_mget(memc, keys, key_length, 4);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ count= 0;
+ while ((value= memcached_fetch(memc, return_key, &return_key_length,
+ &value_length, &flags, &rc)))
+ count++;
+ assert(count == 0);
+ assert(rc == MEMCACHED_END);
+
+ for (x= 0; x < 4; x++)
+ {
+ rc= memcached_set(memc, keys[x], key_length[x],
+ insert_data, VALUE_SIZE_BUG5,
+ (time_t)0, (uint32_t)0);
+ assert(rc == MEMCACHED_SUCCESS);
+ }
+
+ for (x= 0; x < 2; x++)
+ {
+ value= memcached_get(memc, keys[0], key_length[0],
+ &value_length, &flags, &rc);
+ assert(value);
+ free(value);
+
+ rc= memcached_mget(memc, keys, key_length, 4);
+ assert(rc == MEMCACHED_SUCCESS);
+ count= 3;
+ /* We test for purge of partial complete fetches */
+ for (count= 3; count; count--)
+ {
+ value= memcached_fetch(memc, return_key, &return_key_length,
+ &value_length, &flags, &rc);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(!(memcmp(value, insert_data, value_length)));
+ assert(value_length);
+ free(value);
+ }
+ }
+
+ return 0;
+}
+
+static test_return user_supplied_bug8(memcached_st *memc __attribute__((unused)))
+{
+ memcached_return rc;
+ memcached_st *mine;
+ memcached_st *clone;
+