Merge in code for C++ compiling of libmemcached.
[m6w6/libmemcached] / libtest / test.h
index cc0fd78aaaf21c8e5614c0776fdf4631ea355d76..656be1698967f36f92c0d50b6aefbb5b99e4cee0 100644 (file)
@@ -1,19 +1,49 @@
-/* uTest
- * Copyright (C) 2006-2009 Brian Aker
- * All rights reserved.
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ * 
+ *  Gearmand client and server library.
+ *
+ *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *  Copyright (C) 2006-2010 Brian Aker
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ *
+ *      * Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ *      * Redistributions in binary form must reproduce the above
+ *  copyright notice, this list of conditions and the following disclaimer
+ *  in the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ *      * The names of its contributors may not be used to endorse or
+ *  promote products derived from this software without specific prior
+ *  written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
- * Use and distribution licensed under the BSD license.  See
- * the COPYING file in the parent directory for full text.
  */
 
+#pragma once
+
+#include <libtest/visibility.h>
+
 /*
   Structures for generic tests.
 */
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
@@ -39,9 +69,6 @@ typedef test_return_t (*test_callback_fn)(void *);
 typedef test_return_t (*test_callback_runner_fn)(test_callback_fn, void *);
 typedef test_return_t (*test_callback_error_fn)(test_return_t, void *);
 
-/* Help function for use with gettimeofday() */
-long int timedif(struct timeval a, struct timeval b);
-
 /**
   A structure describing the test case.
 */
@@ -139,14 +166,25 @@ typedef struct {
   uint32_t total;
 } world_stats_st;
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Help function for use with gettimeofday() */
+LIBTEST_API
+long int timedif(struct timeval a, struct timeval b);
+
 /* How we make all of this work :) */
+LIBTEST_API
 void get_world(world_st *world);
 
+LIBTEST_INTERNAL_API
 void create_core(void);
 
 /**
   @note Friendly print function for errors.
 */
+LIBTEST_API
 const char *test_strerror(test_return_t code);
 
 #define test_fail(A) \
@@ -179,6 +217,17 @@ do \
   } \
 } while (0)
 
+#define test_compare(A,B) \
+do \
+{ \
+  if ((A) != (B)) \
+  { \
+    fprintf(stderr, "\n%s:%d: Expected %lu == %lu\n", __FILE__, __LINE__, (unsigned long)(A), (unsigned long)(B)); \
+    create_core(); \
+    return TEST_FAILURE; \
+  } \
+} while (0)
+
 #define test_false(A) \
 do \
 { \
@@ -189,17 +238,54 @@ do \
   } \
 } while (0)
 
+#define test_false_with(A,B) \
+do \
+{ \
+  if ((A)) { \
+    fprintf(stderr, "\nAssertion failed at %s:%d: %s with %s\n", __FILE__, __LINE__, #A, (B));\
+    create_core(); \
+    return TEST_FAILURE; \
+  } \
+} while (0)
+
 #define test_strcmp(A,B) \
 do \
 { \
   if (strcmp((A), (B))) \
   { \
-    fprintf(stderr, "\n%s:%d: %s -> %s\n", __FILE__, __LINE__, (A), (B)); \
+    fprintf(stderr, "\n%s:%d: `%s` -> `%s`\n", __FILE__, __LINE__, (A), (B)); \
     create_core(); \
     return TEST_FAILURE; \
   } \
 } while (0)
 
+#define test_memcmp(A,B,C) \
+do \
+{ \
+  if (memcmp((A), (B), (C))) \
+  { \
+    fprintf(stderr, "\n%s:%d: %.*s -> %.*s\n", __FILE__, __LINE__, (int)(C), (char *)(A), (int)(C), (char *)(B)); \
+    create_core(); \
+    return TEST_FAILURE; \
+  } \
+} while (0)
+
+#define STRINGIFY(x) #x
+#define TOSTRING(x) STRINGIFY(x)
+#define AT __FILE__ ":" TOSTRING(__LINE__)
+
+#ifdef __cplusplus
+#define STRING_WITH_LEN(X) (X), (static_cast<size_t>((sizeof(X) - 1)))
+#else
+#define STRING_WITH_LEN(X) (X), ((size_t)((sizeof(X) - 1)))
+#endif
+
+#ifdef __cplusplus
+#define STRING_PARAM_WITH_LEN(X) X, static_cast<size_t>(sizeof(X) - 1)
+#else
+#define STRING_PARAM_WITH_LEN(X) X, (size_t)((sizeof(X) - 1))
+#endif
+
 #ifdef __cplusplus
 }
 #endif