+ string= memcached_string_create(memc, NULL, 100);
+ assert(string);
+
+ for (x= 0; x < 1024; x++)
+ {
+ memcached_return rc;
+ rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
+ assert(rc == MEMCACHED_SUCCESS);
+ }
+ memcached_string_free(string);
+
+ return 0;
+}
+
+static test_return string_alloc_append_toobig(memcached_st *memc)
+{
+ memcached_return rc;
+ unsigned int x;
+ char buffer[SMALL_STRING_LEN];
+ memcached_string_st *string;
+
+ /* Ring the bell! */
+ memset(buffer, 6, SMALL_STRING_LEN);
+
+ string= memcached_string_create(memc, NULL, 100);
+ assert(string);
+
+ for (x= 0; x < 1024; x++)
+ {
+ rc= memcached_string_append(string, buffer, SMALL_STRING_LEN);
+ assert(rc == MEMCACHED_SUCCESS);
+ }
+ rc= memcached_string_append(string, buffer, INT64_MAX);
+ assert(rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE);
+ memcached_string_free(string);
+
+ return 0;
+}
+
+static test_return cleanup_pairs(memcached_st *memc __attribute__((unused)))
+{
+ pairs_free(global_pairs);
+
+ return 0;
+}
+
+static test_return generate_pairs(memcached_st *memc __attribute__((unused)))
+{
+ unsigned long long x;
+ global_pairs= pairs_generate(GLOBAL_COUNT, 400);
+ global_count= GLOBAL_COUNT;
+
+ for (x= 0; x < global_count; x++)
+ {
+ global_keys[x]= global_pairs[x].key;
+ global_keys_length[x]= global_pairs[x].key_length;
+ }
+
+ return 0;
+}
+
+static test_return generate_large_pairs(memcached_st *memc __attribute__((unused)))
+{
+ unsigned long long x;
+ global_pairs= pairs_generate(GLOBAL2_COUNT, MEMCACHED_MAX_BUFFER+10);
+ global_count= GLOBAL2_COUNT;
+
+ for (x= 0; x < global_count; x++)
+ {
+ global_keys[x]= global_pairs[x].key;
+ global_keys_length[x]= global_pairs[x].key_length;
+ }
+
+ return 0;
+}
+
+static test_return generate_data(memcached_st *memc)
+{
+ execute_set(memc, global_pairs, global_count);
+
+ return 0;
+}
+
+static test_return generate_data_with_stats(memcached_st *memc)
+{
+ memcached_stat_st *stat_p;
+ memcached_return rc;
+ uint32_t host_index= 0;
+ execute_set(memc, global_pairs, global_count);
+
+ //TODO: hosts used size stats
+ stat_p= memcached_stat(memc, NULL, &rc);
+ assert(stat_p);
+
+ for (host_index= 0; host_index < SERVERS_TO_CREATE; host_index++)
+ {
+ printf("\nserver %u|%s|%u bytes: %llu\n", host_index, (memc->hosts)[host_index].hostname, (memc->hosts)[host_index].port, (unsigned long long)(stat_p + host_index)->bytes);
+ }
+
+ memcached_stat_free(NULL, stat_p);
+
+ return 0;
+}
+static test_return generate_buffer_data(memcached_st *memc)
+{
+ int latch= 0;
+
+ latch= 1;
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, latch);
+ generate_data(memc);
+
+ return 0;
+}
+
+static test_return get_read_count(memcached_st *memc)
+{
+ unsigned int x;
+ memcached_return rc;
+ memcached_st *clone;
+
+ clone= memcached_clone(NULL, memc);
+ assert(clone);
+
+ memcached_server_add_with_weight(clone, "localhost", 6666, 0);
+
+ {
+ char *return_value;
+ size_t return_value_length;
+ uint32_t flags;
+ uint32_t count;
+
+ for (x= count= 0; x < global_count; x++)
+ {
+ return_value= memcached_get(clone, global_keys[x], global_keys_length[x],
+ &return_value_length, &flags, &rc);
+ if (rc == MEMCACHED_SUCCESS)
+ {
+ count++;
+ if (return_value)
+ free(return_value);
+ }
+ }
+ fprintf(stderr, "\t%u -> %u", global_count, count);
+ }
+
+ memcached_free(clone);
+
+ return 0;
+}
+
+static test_return get_read(memcached_st *memc)
+{
+ unsigned int x;
+ memcached_return rc;
+
+ {
+ char *return_value;
+ size_t return_value_length;
+ uint32_t flags;
+
+ for (x= 0; x < global_count; x++)
+ {
+ return_value= memcached_get(memc, global_keys[x], global_keys_length[x],
+ &return_value_length, &flags, &rc);
+ /*
+ assert(return_value);
+ assert(rc == MEMCACHED_SUCCESS);
+ */
+ if (rc == MEMCACHED_SUCCESS && return_value)
+ free(return_value);
+ }
+ }
+
+ return 0;
+}
+
+static test_return mget_read(memcached_st *memc)
+{
+ memcached_return rc;
+
+ rc= memcached_mget(memc, global_keys, global_keys_length, global_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);
+ }
+ }
+
+ return 0;
+}
+
+static test_return mget_read_result(memcached_st *memc)
+{
+ memcached_return rc;
+
+ rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
+ assert(rc == MEMCACHED_SUCCESS);
+ /* Turn this into a help function */
+ {
+ memcached_result_st results_obj;
+ memcached_result_st *results;
+
+ results= memcached_result_create(memc, &results_obj);
+
+ while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
+ {
+ assert(results);
+ assert(rc == MEMCACHED_SUCCESS);
+ }
+
+ memcached_result_free(&results_obj);
+ }
+
+ return 0;
+}
+
+static test_return mget_read_function(memcached_st *memc)
+{
+ memcached_return rc;
+ unsigned int counter;
+ memcached_execute_function callbacks[1];
+
+ rc= memcached_mget(memc, global_keys, global_keys_length, global_count);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ callbacks[0]= &callback_counter;
+ counter= 0;
+ rc= memcached_fetch_execute(memc, callbacks, (void *)&counter, 1);
+
+ return 0;
+}
+
+static test_return delete_generate(memcached_st *memc)
+{
+ unsigned int x;
+
+ for (x= 0; x < global_count; x++)
+ {
+ (void)memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0);
+ }
+
+ return 0;
+}
+
+static test_return delete_buffer_generate(memcached_st *memc)
+{
+ int latch= 0;
+ unsigned int x;
+
+ latch= 1;
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, latch);
+
+ for (x= 0; x < global_count; x++)
+ {
+ (void)memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0);
+ }
+
+ return 0;
+}
+
+static test_return free_data(memcached_st *memc __attribute__((unused)))
+{
+ pairs_free(global_pairs);
+
+ return 0;
+}
+
+static test_return add_host_test1(memcached_st *memc)
+{
+ unsigned int x;
+ memcached_return rc;
+ char servername[]= "0.example.com";
+ memcached_server_st *servers;
+
+ 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 pre_nonblock(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_nonblock_binary(memcached_st *memc)
+{
+ memcached_return rc= MEMCACHED_FAILURE;
+ memcached_st *clone;
+
+ clone= memcached_clone(NULL, memc);
+ assert(clone);
+ // The memcached_version needs to be done on a clone, because the server
+ // will not toggle protocol on an connection.
+ memcached_version(clone);
+
+ if (clone->hosts[0].major_version >= 1 && clone->hosts[0].minor_version > 2)
+ {
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
+ rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1);
+ }
+
+ memcached_free(clone);
+ return rc;
+}
+
+static memcached_return pre_murmur(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MURMUR);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_jenkins(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_JENKINS);
+
+ return MEMCACHED_SUCCESS;
+}
+
+
+static memcached_return pre_md5(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MD5);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_crc(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_CRC);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_hsieh(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_HSIEH);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_hash_fnv1_64(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1_64);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_hash_fnv1a_64(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_64);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_hash_fnv1_32(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1_32);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_hash_fnv1a_32(memcached_st *memc)
+{
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_FNV1A_32);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_behavior_ketama(memcached_st *memc)
+{
+ memcached_return rc;
+ uint64_t value;
+
+ rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA, 1);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA);
+ assert(value == 1);
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_behavior_ketama_weighted(memcached_st *memc)
+{
+ memcached_return rc;
+ uint64_t value;
+
+ rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
+ assert(value == 1);
+
+ rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH);
+ assert(value == MEMCACHED_HASH_MD5);
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return pre_binary(memcached_st *memc)
+{
+ memcached_return rc= MEMCACHED_FAILURE;
+ memcached_st *clone;
+
+ clone= memcached_clone(NULL, memc);
+ assert(clone);
+ // The memcached_version needs to be done on a clone, because the server
+ // will not toggle protocol on an connection.
+ memcached_version(clone);
+
+ if (clone->hosts[0].major_version >= 1 && clone->hosts[0].minor_version > 2)
+ {
+ rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1);
+ }
+
+ memcached_free(clone);
+ return rc;
+}
+
+static void my_free(memcached_st *ptr __attribute__((unused)), void *mem)
+{
+ free(mem);
+}
+
+static void *my_malloc(memcached_st *ptr __attribute__((unused)), const size_t size)
+{
+ return malloc(size);
+}
+
+static void *my_realloc(memcached_st *ptr __attribute__((unused)), void *mem, const size_t size)
+{
+ return realloc(mem, size);
+}
+
+static memcached_return set_prefix(memcached_st *memc)
+{
+ memcached_return rc;
+ const char *key= "mine";
+ char *value;
+
+ /* Make sure be default none exists */
+ value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
+ assert(rc == MEMCACHED_FAILURE);
+
+ /* Test a clean set */
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
+ assert(memcmp(value, key, 4) == 0);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ /* Test that we can turn it off */
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
+ assert(rc == MEMCACHED_FAILURE);
+
+ /* Now setup for main test */
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, (void *)key);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(memcmp(value, key, 4) == 0);
+
+ /* Set to Zero, and then Set to something too large */
+ {
+ char *long_key;
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, NULL);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ value= memcached_callback_get(memc, MEMCACHED_CALLBACK_PREFIX_KEY, &rc);
+ assert(rc == MEMCACHED_FAILURE);
+ assert(value == NULL);
+
+ /* Test a long key for failure */
+ /* TODO, extend test to determine based on setting, what result should be */
+ long_key= "Thisismorethentheallottednumberofcharacters";
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
+ //assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ /* Now test a key with spaces (which will fail from long key, since bad key is not set) */
+ long_key= "This is more then the allotted number of characters";
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
+ assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
+
+ /* Test for a bad prefix, but with a short key */
+ rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_VERIFY_KEY, 1);
+ assert(rc == MEMCACHED_SUCCESS);
+
+ long_key= "dog cat";
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key);
+ assert(rc == MEMCACHED_BAD_KEY_PROVIDED);
+ }
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return set_memory_alloc(memcached_st *memc)
+{
+ {
+ memcached_malloc_function test_ptr;
+ memcached_return rc;
+
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, (void*)&my_malloc);
+ assert(rc == MEMCACHED_SUCCESS);
+ test_ptr= (memcached_malloc_function)memcached_callback_get(memc, MEMCACHED_CALLBACK_MALLOC_FUNCTION, &rc);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(test_ptr == my_malloc);
+ }
+
+ {
+ memcached_realloc_function test_ptr;
+ memcached_return rc;
+
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, (void*)&my_realloc);
+ assert(rc == MEMCACHED_SUCCESS);
+ test_ptr= (memcached_realloc_function)memcached_callback_get(memc, MEMCACHED_CALLBACK_REALLOC_FUNCTION, &rc);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(test_ptr == my_realloc);
+ }
+
+ {
+ memcached_free_function test_ptr;
+ memcached_return rc;
+
+ rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, (void*)my_free);
+ assert(rc == MEMCACHED_SUCCESS);
+ test_ptr= (memcached_free_function)memcached_callback_get(memc, MEMCACHED_CALLBACK_FREE_FUNCTION, &rc);
+ assert(rc == MEMCACHED_SUCCESS);
+ assert(test_ptr == my_free);
+ }
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return enable_consistent(memcached_st *memc)
+{
+ memcached_server_distribution value= MEMCACHED_DISTRIBUTION_CONSISTENT;
+ memcached_hash hash;
+ memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, value);
+ pre_hsieh(memc);
+
+ value= (memcached_server_distribution)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION);
+ assert(value == MEMCACHED_DISTRIBUTION_CONSISTENT);
+
+ hash= (memcached_hash)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
+ assert(hash == MEMCACHED_HASH_HSIEH);
+
+
+ return MEMCACHED_SUCCESS;
+}
+
+static memcached_return enable_cas(memcached_st *memc)
+{
+ unsigned int set= 1;
+
+ memcached_version(memc);