Style cleanup
[m6w6/libmemcached] / tests / test.c
index 6c6bea645c4da1376069b70ef387e48a97447ccf..7a5e3132a238c5a5d693574e86a1d6b039d2fe44 100644 (file)
@@ -2,7 +2,6 @@
   Sample test application.
 */
 #include <assert.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/time.h>
 
 #include "test.h"
 
-long int timedif(struct timeval a, struct timeval b)
+static long int timedif(struct timeval a, struct timeval b)
 {
   register int us, s;
 
-  us = a.tv_usec - b.tv_usec;
+  us = (int)(a.tv_usec - b.tv_usec);
   us /= 1000;
-  s = a.tv_sec - b.tv_sec;
+  s = (int)(a.tv_sec - b.tv_sec);
   s *= 1000;
   return s + us;
 }
 
+static const char *test_strerror(test_return_t code)
+{
+  switch (code) {
+  case TEST_SUCCESS:
+    return "ok";
+  case TEST_FAILURE:
+    return "failed";
+  case TEST_MEMORY_ALLOCATION_FAILURE:
+    return "memory allocation";
+  case TEST_SKIPPED:
+    return "skipped";
+  case TEST_MAXIMUM_RETURN:
+  default:
+    fprintf(stderr, "Unknown return value\n");
+    abort();
+  }
+
+}
+
+void create_core(void)
+{
+   if (getenv("LIBMEMCACHED_NO_COREDUMP") == NULL && fork() == 0)
+     abort();
+}
+
 int main(int argc, char *argv[])
 {
+  test_return_t failed;
   unsigned int x;
   char *collection_to_run= NULL;
   char *wildcard= NULL;
@@ -36,7 +61,6 @@ int main(int argc, char *argv[])
   world_st world;
   collection_st *collection;
   collection_st *next;
-  uint8_t failed;
   void *world_ptr;
 
   memset(&world, 0, sizeof(world_st));
@@ -45,6 +69,8 @@ int main(int argc, char *argv[])
 
   if (world.create)
     world_ptr= world.create();
+  else
+    world_ptr= NULL;
 
   startup_ptr= (server_startup_st *)world_ptr;
   servers= (memcached_server_st *)startup_ptr->servers;
@@ -69,7 +95,7 @@ int main(int argc, char *argv[])
     {
       unsigned int loop;
       memcached_st *memc;
-      memcached_return rc;
+      memcached_return_t rc;
       struct timeval start_time, end_time;
       long int load_time;
 
@@ -79,10 +105,10 @@ int main(int argc, char *argv[])
       fprintf(stderr, "Testing %s", run->name);
 
       memc= memcached_create(NULL);
-      assert(memc);
+      test_truth(memc);
 
       rc= memcached_server_push(memc, servers);
-      assert(rc == MEMCACHED_SUCCESS);
+      test_truth(rc == MEMCACHED_SUCCESS);
 
       if (run->requires_flush)
       {
@@ -92,13 +118,12 @@ int main(int argc, char *argv[])
 
       for (loop= 0; loop < memcached_server_list_count(servers); loop++)
       {
-        assert(memc->hosts[loop].fd == -1);
-        assert(memc->hosts[loop].cursor_active == 0);
+        test_truth(memc->hosts[loop].fd == -1);
+        test_truth(memc->hosts[loop].cursor_active == 0);
       }
 
       if (next->pre)
       {
-        memcached_return rc;
         rc= next->pre(memc);
 
         if (rc != MEMCACHED_SUCCESS)
@@ -112,17 +137,14 @@ int main(int argc, char *argv[])
       failed= run->function(memc);
       gettimeofday(&end_time, NULL);
       load_time= timedif(end_time, start_time);
-      if (failed)
-        fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ failed ]\n", load_time / 1000, 
-                load_time % 1000);
-      else
-        fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time / 1000, 
-                load_time % 1000);
+
+      fprintf(stderr, "\t\t\t\t\t %ld.%03ld [ %s ]\n", load_time / 1000,
+              load_time % 1000, test_strerror(failed));
 
       if (next->post)
         (void)next->post(memc);
 
-      assert(memc);
+      test_truth(memc);
 error:
       memcached_free(memc);
     }