tests: run memcached verbosely, catch output and show it on failure
[awesomized/libmemcached] / libtest / vchar.cc
index 24238060df834e5685ba46bd8009a1671e9fe26f..a4bddfdeda098eb8aa9d0523b90b3ad1f65114a0 100644 (file)
  *
  */
 
-#include <config.h>
+#include "libtest/yatlcon.h"
 #include <libtest/common.h>
 
+/* Use this for string generation */
+static const char ALPHANUMERICS[]=
+  "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz";
+
+#define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS)-1)
+
+static size_t get_alpha_num(void)
+{
+  return (size_t)random() % ALPHANUMERICS_SIZE;
+}
+
 namespace libtest {
 
+int random_alpha_num(void)
+{
+  return ALPHANUMERICS[get_alpha_num()];
+}
+
 static std::string printer(const char *str, size_t length)
 {
   std::ostringstream buf;
@@ -82,9 +98,55 @@ void make(libtest::vchar_t& arg)
 void make(libtest::vchar_t& arg, size_t length)
 {
   arg.reserve(length);
-  for (uint32_t x= 0; x < length; x++)
+  for (uint32_t x= 0; x < length; ++x)
   {
-    arg.push_back(char(x % 127));
+    arg.push_back(ALPHANUMERICS[get_alpha_num()]);
+  }
+}
+
+void chomp(libtest::vchar_t& arg)
+{
+  while(arg.size())
+  {
+    if (arg.back() == 0)
+    {
+      arg.pop_back();
+    }
+    else
+    {
+      break;
+    }
+  }
+}
+
+void append(libtest::vchar_ptr_t& arg, const char* ptr)
+{
+  if (ptr)
+  {
+    char* new_ptr= strdup(ptr);
+    if (new_ptr == NULL)
+    {
+      FATAL("UNABLE to allocate %s(%p)", ptr, ptr);
+    }
+
+    arg.push_back(new_ptr);
+  }
+}
+
+void append(libtest::vchar_t& arg, const char* ptr)
+{
+  if (ptr)
+  {
+    size_t length= strlen(ptr);
+    ASSERT_TRUE(length);
+    arg.reserve(length);
+    do
+    {
+      arg.push_back(*ptr);
+      ++ptr;
+    } while (*ptr);
+
+    arg.push_back(0);
   }
 }
 
@@ -98,7 +160,7 @@ void make_vector(libtest::vchar_t& arg, const char *str, size_t length)
 
 std::ostream& operator<<(std::ostream& output, const libtest::vchar_t& arg)
 {
-  std::string tmp= libtest::printer(&arg[0], arg.size());
+  std::string tmp= libtest::printer(arg.data(), arg.size());
   output << tmp <<  "[" << arg.size() << "]";
 
   return output;