libdispatch update
[platform/upstream/gcd.git] / dispatch-1.0 / testing / dispatch_test.c
index 859d5a2..ce92ae0 100644 (file)
-#include "config/config.h"
-
-#include <sys/types.h>
-#include <sys/wait.h>
-
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <errno.h>
-#include <sys/errno.h>
-#include <string.h>
+/*
+ * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
 
 #include "dispatch_test.h"
+#include "bsdtests.h"
 
-#define _test_print(_file, _line, _desc, \
-       _expr, _fmt1, _val1, _fmt2, _val2) do { \
-       const char* _exprstr = _expr ? "PASS" : "FAIL"; \
-       char _linestr[BUFSIZ]; \
-       if (!_expr) { \
-               snprintf(_linestr, sizeof(_linestr), \
-                       " (%s:%ld)", _file, _line); \
-       } else { \
-               _linestr[0] = 0; \
-       } \
-       if (_fmt2 == 0) { \
-               printf("\tValue: " _fmt1 "\n"           \
-                       "[%s] %s%s\n",                  \
-                       _val1,                          \
-                       _exprstr,                       \
-                       _desc,                          \
-                       _linestr);                      \
-       } else { \
-               printf("\tActual: " _fmt1 "\n"     \
-                       "\tExpected: " _fmt2 "\n"  \
-                       "[%s] %s%s\n",             \
-                       _val1,                     \
-                       _val2,                     \
-                       _exprstr,                  \
-                       _desc,                     \
-                       _linestr);                 \
-       } \
-       if (!_expr) { \
-               printf("\t%s:%ld\n", _file, _line); \
-       } \
-       fflush(stdout); \
-} while (0);
-
-void
-test_start(const char* desc) {
-       printf("\n==================================================\n");
-       printf("[TEST] %s\n", desc);
-       printf("[PID] %d\n", getpid());
-       printf("==================================================\n\n");
-       usleep(100000); // give 'gdb --waitfor=' a chance to find this proc
-}
-
-#define test_ptr_null(a,b) _test_ptr_null(__FILE__, __LINE__, a, b)
-void
-_test_ptr_null(const char* file, long line, const char* desc, const void* ptr) {
-       _test_print(file, line, desc,
-               (ptr == NULL), "%p", ptr, "%p", (void*)0);
-}
-
-#define test_ptr_notnull(a,b) _test_ptr_notnull(__FILE__, __LINE__, a, b)
-void
-_test_ptr_notnull(const char* file, long line, const char* desc, const void* ptr) {
-       _test_print(file, line, desc,
-               (ptr != NULL), "%p", ptr, "%p", ptr ?: (void*)~0);
-}
-
-#define test_ptr(a,b,c) _test_ptr(__FILE__, __LINE__, a, b, c)
-void
-_test_ptr(const char* file, long line, const char* desc, const void* actual, const void* expected) {
-       _test_print(file, line, desc,
-               (actual == expected), "%p", actual, "%p", expected);
-}
-
-#define test_long(a,b,c) _test_long(__FILE__, __LINE__, a, b, c)
-void
-_test_long(const char* file, long line, const char* desc, long actual, long expected) {
-       _test_print(file, line, desc,
-               (actual == expected), "%ld", actual, "%ld", expected);
-}
+#ifdef __OBJC_GC__
+#include <objc/objc-auto.h>
+#endif
 
-#define test_long_less_than(a, b, c) _test_long_less_than(__FILE__, __LINE__, a, b, c)
-void
-_test_long_less_than(const char* file, long line, const char* desc, long actual, long expected_max) {
-       _test_print(file, line, desc, (actual < expected_max), "%ld", actual, "<%ld", expected_max);
-}
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/event.h>
+#include <assert.h>
 
-#define test_double_less_than(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m)
-void
-_test_double_less_than(const char* file, long line, const char* desc, double val, double max_expected) {
-       _test_print(file, line, desc, (val < max_expected), "%f", val, "<%f", max_expected);
-}
+#include <dispatch/dispatch.h>
 
-#define test_double_less_than_or_equal(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m)
-void
-_test_double_less_than_or_equal(const char* file, long line, const char* desc, double val, double max_expected) {
-       _test_print(file, line, desc, (val <= max_expected), "%f", val, "<%f", max_expected);
-}
+void test_start(const char* desc);
 
-#define test_errno(a,b,c) _test_errno(__FILE__, __LINE__, a, b, c)
 void
-_test_errno(const char* file, long line, const char* desc, long actual, long expected) {
-       char* actual_str;
-       char* expected_str;
-       asprintf(&actual_str, "%ld\t%s", actual, actual ? strerror(actual) : "");
-       asprintf(&expected_str, "%ld\t%s", expected, expected ? strerror(expected) : "");
-       _test_print(file, line, desc,
-               (actual == expected), "%s", actual_str, "%s", expected_str);
-       free(actual_str);
-       free(expected_str);
+dispatch_test_start(const char* desc)
+{
+#if defined(__OBJC_GC__) && MAC_OS_X_VERSION_MIN_REQUIRED < 1070
+       objc_startCollectorThread();
+#endif
+       test_start(desc);
 }
 
-#include <spawn.h>
-
-extern char **environ;
-
-void
-test_stop(void) {
-       test_stop_after_delay((void *)(intptr_t)0);
+bool
+dispatch_test_check_evfilt_read_for_fd(int fd)
+{
+       int kq = kqueue();
+       assert(kq != -1);
+       struct kevent ke = {
+               .ident = fd,
+               .filter = EVFILT_READ,
+               .flags = EV_ADD|EV_ENABLE,
+       };
+       struct timespec t = {
+               .tv_sec = 1,
+       };
+       int r = kevent(kq, &ke, 1, &ke, 1, &t);
+       close(kq);
+       return r > 0;
 }
 
 void
-test_stop_after_delay(void *delay) {
-#if HAVE_LEAKS
-       int res;
-       pid_t pid;
-       char pidstr[10];
-#endif
-
-       if (delay != NULL) {
-               sleep((int)(intptr_t)delay);
-       }
-
-#if HAVE_LEAKS
-       if (getenv("NOLEAKS")) _exit(EXIT_SUCCESS);
-
-       /* leaks doesn't work against debug variant malloc */
-       if (getenv("DYLD_IMAGE_SUFFIX")) _exit(EXIT_SUCCESS);
-       
-       snprintf(pidstr, sizeof(pidstr), "%d", getpid());
-       char* args[] = { "./leaks-wrapper", pidstr, NULL };
-       res = posix_spawnp(&pid, args[0], NULL, NULL, args, environ);
-       if (res == 0 && pid > 0) {
-               int status;
-               waitpid(pid, &status, 0);
-               test_long("Leaks", status, 0);
-       } else {
-               perror(args[0]);
-       }
-#endif
-       _exit(EXIT_SUCCESS);
+_dispatch_test_current(const char* file, long line, const char* desc, dispatch_queue_t expected)
+{
+       dispatch_queue_t actual = dispatch_get_current_queue();
+       _test_ptr(file, line, desc, actual, expected);
 }