diff options
-rw-r--r-- | test/ring_test.c | 17 | ||||
-rw-r--r-- | test/sem_test.c | 1 | ||||
-rw-r--r-- | test/sorted_array_test.c | 20 | ||||
-rw-r--r-- | test/tree_test.c | 32 | ||||
-rw-r--r-- | wscript | 5 | ||||
-rw-r--r-- | zix/sem.h | 43 | ||||
-rw-r--r-- | zix/thread.h | 37 |
7 files changed, 110 insertions, 45 deletions
diff --git a/test/ring_test.c b/test/ring_test.c index b7d181c..6cbe4fd 100644 --- a/test/ring_test.c +++ b/test/ring_test.c @@ -16,7 +16,6 @@ #include <limits.h> #include <stdarg.h> -#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -69,15 +68,15 @@ reader(void* arg) { printf("Reader starting\n"); - int ref_msg[MSG_SIZE]; // Reference generated for comparison - int read_msg[MSG_SIZE]; // Read from ring - size_t count = 0; - int start = gen_msg(ref_msg, 0); + int ref_msg[MSG_SIZE]; // Reference generated for comparison + int read_msg[MSG_SIZE]; // Read from ring + unsigned count = 0; + int start = gen_msg(ref_msg, 0); for (size_t i = 0; i < n_writes; ++i) { if (zix_ring_read_space(ring) >= MSG_SIZE * sizeof(int)) { if (zix_ring_read(ring, read_msg, MSG_SIZE * sizeof(int))) { if (!cmp_msg(ref_msg, read_msg)) { - printf("FAIL: Message %zu is corrupt\n", count); + printf("FAIL: Message %u is corrupt\n", count); read_error = true; return NULL; } @@ -118,7 +117,7 @@ main(int argc, char** argv) return 1; } - int size = 1024; + unsigned size = 1024; if (argc > 1) { size = atoi(argv[1]); } @@ -128,7 +127,7 @@ main(int argc, char** argv) n_writes = atoi(argv[2]); } - printf("Testing %zu writes of %d ints to a %d int ring...\n", + printf("Testing %u writes of %d ints to a %d int ring...\n", n_writes, MSG_SIZE, size); ring = zix_ring_new(size); @@ -216,7 +215,7 @@ main(int argc, char** argv) return failure("Successful underrun read\n"); } - char* big_buf = calloc(size, 1); + char* big_buf = (char*)calloc(size, 1); n = zix_ring_write(ring, big_buf, size - 1); if (n != (uint32_t)size - 1) { return failure("Maximum size write failed (wrote %u)\n", n); diff --git a/test/sem_test.c b/test/sem_test.c index e5c7b55..b48ad4c 100644 --- a/test/sem_test.c +++ b/test/sem_test.c @@ -15,7 +15,6 @@ */ #include <limits.h> -#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> diff --git a/test/sorted_array_test.c b/test/sorted_array_test.c index d33151a..3b0a090 100644 --- a/test/sorted_array_test.c +++ b/test/sorted_array_test.c @@ -14,14 +14,15 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include <limits.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> -#include <limits.h> +#include <time.h> -#include <sys/time.h> - -#ifndef _MSC_VER +#ifdef _MSC_VER +# define PRIdPTR "Id" +#else # include <inttypes.h> #endif @@ -77,8 +78,7 @@ stress(int test_num, unsigned n_elems) return test_fail(); } if (*(intptr_t*)zix_sorted_array_get_data(ti) != r) { - fprintf(stderr, "Data corrupt (saw %" PRIdPTR - ", expected %" PRIdPTR ")\n", + fprintf(stderr, "Data corrupt (%" PRIdPTR " != %" PRIdPTR ")\n", *(intptr_t*)zix_sorted_array_get_data(ti), r); return test_fail(); } @@ -94,8 +94,7 @@ stress(int test_num, unsigned n_elems) return test_fail(); } if (*(intptr_t*)zix_sorted_array_get_data(ti) != r) { - fprintf(stderr, "Data corrupt (saw %" PRIdPTR - ", expected %" PRIdPTR ")\n", + fprintf(stderr, "Data corrupt (%" PRIdPTR " != %" PRIdPTR ")\n", *(intptr_t*)zix_sorted_array_get_data(ti), r); return test_fail(); } @@ -150,9 +149,6 @@ main(int argc, char** argv) const unsigned n_tests = 3; unsigned n_elems = 0; - struct timeval time; - gettimeofday(&time, NULL); - if (argc == 1) { n_elems = 4096; } else { @@ -160,7 +156,7 @@ main(int argc, char** argv) if (argc > 2) { seed = atol(argv[2]); } else { - seed = time.tv_sec + time.tv_usec; + seed = time(NULL); } } diff --git a/test/tree_test.c b/test/tree_test.c index dc91ff8..6f25f6c 100644 --- a/test/tree_test.c +++ b/test/tree_test.c @@ -14,14 +14,15 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include <limits.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> -#include <limits.h> +#include <time.h> -#include <sys/time.h> - -#ifndef _MSC_VER +#ifdef _MSC_VER +# define PRIdPTR "Id" +#else # include <inttypes.h> #endif @@ -76,7 +77,7 @@ stress(int test_num, size_t n_elems) return test_fail(); } if ((intptr_t)zix_tree_get(ti) != r) { - fprintf(stderr, "Data corrupt (saw %" PRIdPTR ", expected %zu)\n", + fprintf(stderr, "Data corrupt (%" PRIdPTR" != %" PRIdPTR ")\n", (intptr_t)zix_tree_get(ti), r); return test_fail(); } @@ -98,7 +99,7 @@ stress(int test_num, size_t n_elems) return test_fail(); } if ((intptr_t)zix_tree_get(ti) != r) { - fprintf(stderr, "Data corrupt (saw %" PRIdPTR ", expected %zu)\n", + fprintf(stderr, "Data corrupt (%" PRIdPTR " != %" PRIdPTR ")\n", (intptr_t)zix_tree_get(ti), r); return test_fail(); } @@ -115,7 +116,7 @@ stress(int test_num, size_t n_elems) r = ith_elem(test_num, n_elems, i); const intptr_t iter_data = (intptr_t)zix_tree_get(iter); if (iter_data < last) { - fprintf(stderr, "Iter corrupt (%" PRIdPTR " < %zu)\n", + fprintf(stderr, "Iter corrupt (%" PRIdPTR " < %" PRIdPTR ")\n", iter_data, last); return test_fail(); } @@ -133,7 +134,7 @@ stress(int test_num, size_t n_elems) r = ith_elem(test_num, n_elems, i); const intptr_t iter_data = (intptr_t)zix_tree_get(iter); if (iter_data > last) { - fprintf(stderr, "Iter corrupt (%" PRIdPTR " < %zu)\n", + fprintf(stderr, "Iter corrupt (%" PRIdPTR " < %" PRIdPTR ")\n", iter_data, last); return test_fail(); } @@ -172,7 +173,7 @@ stress(int test_num, size_t n_elems) return test_fail(); } if ((intptr_t)zix_tree_get(ti) != r) { - fprintf(stderr, "Data corrupt (saw %" PRIdPTR ", expected %zu)\n", + fprintf(stderr, "Data corrupt (%" PRIdPTR " != %" PRIdPTR ")\n", (intptr_t)zix_tree_get(ti), r); return test_fail(); } @@ -192,11 +193,8 @@ stress(int test_num, size_t n_elems) int main(int argc, char** argv) { - const size_t n_tests = 3; - size_t n_elems = 0; - - struct timeval time; - gettimeofday(&time, NULL); + const unsigned n_tests = 3; + unsigned n_elems = 0; if (argc == 1) { n_elems = 4096; @@ -205,7 +203,7 @@ main(int argc, char** argv) if (argc > 2) { seed = atol(argv[2]); } else { - seed = time.tv_sec + time.tv_usec; + seed = time(NULL); } } @@ -214,10 +212,10 @@ main(int argc, char** argv) return 1; } - printf("Running %zu tests with %zu elements (seed %d)", + printf("Running %u tests with %u elements (seed %d)", n_tests, n_elems, seed); - for (size_t i = 0; i < n_tests; ++i) { + for (unsigned i = 0; i < n_tests; ++i) { printf("."); fflush(stdout); if (stress(i, n_elems)) { @@ -125,6 +125,8 @@ def build(bld): if bld.env['BUILD_TESTS']: test_libs = ['pthread'] test_cflags = [] + if bld.env['MSVC_COMPILER']: + test_libs = [] if bld.is_defined('HAVE_GCOV'): test_libs += ['gcov'] test_cflags += ['-fprofile-arcs', '-ftest-coverage'] @@ -209,6 +211,7 @@ def upload_docs(ctx): def test(ctx): autowaf.pre_test(ctx, APPNAME) + os.chdir('test') for i in tests: - autowaf.run_tests(ctx, APPNAME, ['test/%s' % i], dirs=['./src','./test']) + autowaf.run_tests(ctx, APPNAME, ['%s' % i], dirs=['./src','./test']) autowaf.post_test(ctx, APPNAME) @@ -17,10 +17,10 @@ #ifndef ZIX_SEM_H #define ZIX_SEM_H -#include <stdbool.h> - #ifdef __APPLE__ # include <mach/mach.h> +#elif defined(_WIN32) +# include <windows.h> #else # include <semaphore.h> #endif @@ -129,7 +129,44 @@ zix_sem_try_wait(ZixSem* sem) return semaphore_timedwait(sem->sem, zero) == KERN_SUCCESS; } -#else /* !defined(__APPLE__) */ +#elif defined(_WIN32) + +struct ZixSemImpl { + HANDLE sem; +}; + +static inline ZixStatus +zix_sem_init(ZixSem* sem, unsigned initial) +{ + sem->sem = CreateSemaphore(NULL, initial, LONG_MAX, NULL); + return (sem->sem) ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS; +} + +static inline void +zix_sem_destroy(ZixSem* sem) +{ + CloseHandle(sem->sem); +} + +static inline void +zix_sem_post(ZixSem* sem) +{ + ReleaseSemaphore(sem->sem, 1, NULL); +} + +static inline void +zix_sem_wait(ZixSem* sem) +{ + WaitForSingleObject(sem->sem, INFINITE); +} + +static inline bool +zix_sem_try_wait(ZixSem* sem) +{ + WaitForSingleObject(sem->sem, 0); +} + +#else /* !defined(__APPLE__) && !defined(_WIN32) */ struct ZixSemImpl { sem_t sem; diff --git a/zix/thread.h b/zix/thread.h index b1fcf97..ff5a727 100644 --- a/zix/thread.h +++ b/zix/thread.h @@ -17,8 +17,12 @@ #ifndef ZIX_THREAD_H #define ZIX_THREAD_H -#include <errno.h> -#include <pthread.h> +#ifdef _WIN32 +# include <windows.h> +#else +# include <errno.h> +# include <pthread.h> +#endif #include "zix/common.h" @@ -35,7 +39,11 @@ extern "C" { @{ */ +#ifdef _WIN32 +typedef HANDLE ZixThread; +#else typedef pthread_t ZixThread; +#endif /** Initialize @c thread to a new thread. @@ -55,6 +63,29 @@ zix_thread_create(ZixThread* thread, static inline ZixStatus zix_thread_join(ZixThread thread, void** retval); +#ifdef _WIN32 + +static inline ZixStatus +zix_thread_create(ZixThread* thread, + size_t stack_size, + void* (*function)(void*), + void* arg) +{ + *thread = CreateThread(NULL, stack_size, + (LPTHREAD_START_ROUTINE)function, arg, + 0, NULL); + return *thread ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR; +} + +static inline ZixStatus +zix_thread_join(ZixThread thread, void** retval) +{ + return WaitForSingleObject(thread, INFINITE) + ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR; +} + +#else /* !defined(_WIN32) */ + static inline ZixStatus zix_thread_create(ZixThread* thread, size_t stack_size, @@ -88,6 +119,8 @@ zix_thread_join(ZixThread thread, void** retval) ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS; } +#endif + /** @} @} |