Update for test system.
[awesomized/libmemcached] / tests / plus.cpp
index 2fb0e83da94a896157bfade6ec76a6b8a63891df..be6cde5098cfad6d1cd2e39a4799cfd7c3dbaf37 100644 (file)
@@ -1,8 +1,9 @@
 /*
   C++ interface test
 */
+#include "libmemcached/memcached.hpp"
+
 #include <assert.h>
-#include <memcached.hh>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include "test.h"
 
-uint8_t basic_test(memcached_st *memc)
+#include <string>
+#include <iostream>
+
+using namespace std;
+using namespace memcache;
+
+extern "C" {
+   test_return_t basic_test(memcached_st *memc);
+   test_return_t increment_test(memcached_st *memc);
+   test_return_t basic_master_key_test(memcached_st *memc);
+   test_return_t mget_result_function(memcached_st *memc);
+   test_return_t basic_behavior(memcached_st *memc);
+   test_return_t mget_test(memcached_st *memc);
+   memcached_return_t callback_counter(memcached_st *,
+                                       memcached_result_st *,
+                                       void *context);
+}
+
+static void populate_vector(vector<char> &vec, const string &str)
 {
-  Memcached foo;
-  char *value_set= "This is some data";
-  char *value;
-  size_t value_length;
+  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_t 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);
 
-  foo.set("mine", value_set, strlen(value_set));
-  value= foo.get("mine", &value_length);
+  assert((memcmp(&test_value[0], &value[0], test_value.size()) == 0));
 
-  assert((memcmp(value, value_set, value_length) == 0));
+  /*
+   * Simple test of the exceptions here...this should throw an exception
+   * saying that the key is empty.
+   */
+  try
+  {
+    foo.set("", value, 0, 0);
+  }
+  catch (Error &err)
+  {
+    return TEST_SUCCESS;
+  }
 
-  return 0;
+  return TEST_FAILURE;
 }
-uint8_t increment_test(memcached_st *memc)
+
+test_return_t increment_test(memcached_st *memc)
 {
-  Memcached mcach;
-  memcached_return rc;
-  char *key= "inctest";
-  char *inc_value= "1";
-  char *ret_value;
+  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;
-  size_t value_length;
 
-  mcach.set(key, inc_value, strlen(inc_value));
-  ret_value= mcach.get(key, &value_length);
-  printf("\nretvalue %s\n",ret_value);
-  int_inc_value= atoi(inc_value);
-  int_ret_value= atoi(ret_value);
-  assert(int_ret_value == int_inc_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 == MEMCACHED_SUCCESS);
+  assert(rc == true);
   assert(int_ret_value == 2);
 
   rc= mcach.increment(key, 1, &int_ret_value);
-  assert(rc == MEMCACHED_SUCCESS);
+  assert(rc == true);
   assert(int_ret_value == 3);
 
   rc= mcach.increment(key, 5, &int_ret_value);
-  assert(rc == MEMCACHED_SUCCESS);
+  assert(rc == true);
   assert(int_ret_value == 8);
 
-  return 0;
+  return TEST_SUCCESS;
 }
 
-uint8_t basic_master_key_test(memcached_st *memc)
+test_return_t basic_master_key_test(memcached_st *memc)
 {
-  Memcached foo;
-  char *value_set= "Data for server A";
-  char *master_key_a= "server-a";
-  char *master_key_b= "server-b";
-  char *key= "xyz";
-  char *value;
-  size_t value_length;
+  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");
 
-  foo.set_by_key(master_key_a, key, value_set, strlen(value_set));
-  value= foo.get_by_key(master_key_a, key, &value_length);
+  populate_vector(value, value_set);
 
-  assert((memcmp(value, value_set, value_length) == 0));
+  foo.setByKey(master_key_a, key, value, 0, 0);
+  foo.getByKey(master_key_a, key, test_value);
 
-  value= foo.get_by_key(master_key_b, key, &value_length);
-  assert((memcmp(value, value_set, value_length) == 0));
+  assert((memcmp(&value[0], &test_value[0], value.size()) == 0));
 
-  return 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_t callback_counter(memcached_st *,
+                                  memcached_result_st *,
+                                  void *context)
+{
+  unsigned int *counter= static_cast<unsigned int *>(context);
 
-test_st tests[] ={
-  {"basic", 0, basic_test },
-  {"basic", 0, increment_test },
-  {"basic_master_key", 0, basic_master_key_test },
-  {0, 0, 0}
-};
+  *counter= *counter + 1;
 
-collection_st collection[] ={
-  {"block", 0, 0, tests},
-  {0, 0, 0, 0}
-};
+  return MEMCACHED_SUCCESS;
+}
+
+test_return_t mget_result_function(memcached_st *memc)
+{
+  Memcache 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_fn 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);
 
-#define SERVERS_TO_CREATE 1
+  assert(counter == 3);
 
-void *world_create(void)
+  return TEST_SUCCESS;
+}
+
+test_return_t mget_test(memcached_st *memc)
 {
-  unsigned int x;
-  memcached_server_st *servers;
-  server_startup_st *construct;
+  Memcache mc(memc);
+  bool rc;
+  memcached_return_t mc_rc;
+  vector<string> keys;
+  vector< vector<char> *> values;
+  keys.reserve(3);
+  keys.push_back("fudge");
+  keys.push_back("son");
+  keys.push_back("food");
+  vector<char> val1;
+  vector<char> val2;
+  vector<char> val3;
+  populate_vector(val1, "fudge");
+  populate_vector(val2, "son");
+  populate_vector(val3, "food");
+  values.reserve(3);
+  values.push_back(&val1);
+  values.push_back(&val2);
+  values.push_back(&val3);
+
+  string return_key;
+  vector<char> return_value;
+
+  /* We need to empty the server before we continue the test */
+  rc= mc.flush(0);
+  assert(rc == true);
+
+  rc= mc.mget(keys);
+  assert(rc == true);
+
+  while ((mc_rc= mc.fetch(return_key, return_value)) != MEMCACHED_END)
+  {
+    assert(return_value.size() != 0);
+    return_value.clear();
+  }
+  assert(mc_rc == MEMCACHED_END);
 
-  construct= (server_startup_st *)malloc(sizeof(server_startup_st));
-  memset(construct, 0, sizeof(server_startup_st));
+  rc= mc.setAll(keys, values, 50, 9);
+  assert(rc == true);
 
-  construct->count= SERVERS_TO_CREATE;
-  server_startup(construct);
+  rc= mc.mget(keys);
+  assert(rc == true);
 
-  return construct;
+  while ((mc_rc= mc.fetch(return_key, return_value)) != MEMCACHED_END)
+  {
+    assert(return_key.length() == return_value.size());
+    assert(!memcmp(&return_value[0], return_key.c_str(), return_value.size()));
+  }
+
+  return TEST_SUCCESS;
 }
 
-void world_destroy(void *p)
+test_return_t basic_behavior(memcached_st *memc)
 {
-  server_startup_st *construct= (server_startup_st *)p;
-  memcached_server_st *servers= (memcached_server_st *)construct->servers;
-  memcached_server_list_free(servers);
+  Memcache mc(memc);
+  bool rc;
+  uint64_t value = 1;
+  rc = mc.setBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY, value);
+  assert(rc);
+  uint64_t behavior = mc.getBehavior(MEMCACHED_BEHAVIOR_VERIFY_KEY);
+  assert(behavior == value);
 
-  server_shutdown(construct);
-  free(construct);
+  return TEST_SUCCESS;
 }
 
+test_st tests[] ={
+  { "basic", 0,
+    reinterpret_cast<test_callback_fn>(basic_test) },
+  { "basic_master_key", 0,
+    reinterpret_cast<test_callback_fn>(basic_master_key_test) },
+  { "increment_test", 0,
+    reinterpret_cast<test_callback_fn>(increment_test) },
+  { "mget", 1,
+    reinterpret_cast<test_callback_fn>(mget_test) },
+  { "mget_result_function", 1,
+    reinterpret_cast<test_callback_fn>(mget_result_function) },
+  { "basic_behavior", 0,
+    reinterpret_cast<test_callback_fn>(basic_behavior) },
+  {0, 0, 0}
+};
+
+collection_st collection[] ={
+  {"block", 0, 0, tests},
+  {0, 0, 0, 0}
+};
+
+#define SERVERS_TO_CREATE 5
+
+#include "libmemcached_world.h"
+
 void get_world(world_st *world)
 {
   world->collections= collection;
-  world->create= world_create;
-  world->destroy= world_destroy;
+  world->collection_startup= reinterpret_cast<test_callback_fn>(world_collection_startup);
+  world->flush= reinterpret_cast<test_callback_fn>(world_flush);
+  world->pre_run= reinterpret_cast<test_callback_fn>(world_pre_run);
+  world->create= reinterpret_cast<test_callback_create_fn>(world_create);
+  world->post_run= reinterpret_cast<test_callback_fn>(world_post_run);
+  world->on_error= reinterpret_cast<test_callback_error_fn>(world_on_error);
+  world->destroy= reinterpret_cast<test_callback_fn>(world_destroy);
+  world->runner= &defualt_libmemcached_runner;
 }