From 1c2848207de53b3eb0c0ae237a6adc0f867b67c3 Mon Sep 17 00:00:00 2001 From: Ali Mashtizadeh Date: Wed, 30 Aug 2023 12:46:17 -0400 Subject: [PATCH] Make sure test asserts are always included. --- tests/pthreadtest.c | 82 ++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/tests/pthreadtest.c b/tests/pthreadtest.c index 5fbd443..170c1f9 100644 --- a/tests/pthreadtest.c +++ b/tests/pthreadtest.c @@ -6,6 +6,12 @@ #include +#define test_assert(_expr) \ + if (!(_expr)) { \ + __assert(__func__, __FILE__, __LINE__, #_expr); \ + } + + pthread_mutex_t mtx; pthread_cond_t cnd; @@ -25,10 +31,10 @@ thread_lock(void *arg) for (i = 0; i < 100; i++) { status = pthread_mutex_lock(&mtx); - assert(status == 0); + test_assert(status == 0); pthread_yield(); status = pthread_mutex_unlock(&mtx); - assert(status == 0); + test_assert(status == 0); } return NULL; @@ -42,9 +48,9 @@ thread_cond(void *arg) for (i = 0; i < 100; i++) { status = pthread_cond_wait(&cnd, NULL); - assert(status == 0); + test_assert(status == 0); status = pthread_cond_signal(&cnd); - assert(status == 0); + test_assert(status == 0); } return NULL; @@ -61,76 +67,76 @@ main(int argc, const char *argv[]) printf("PThread Test\n"); // Simple thread Test - printf("simple test\n"); + printf("simple test: "); status = pthread_create(&thr, NULL, thread_simple, NULL); - assert(status == 0); + test_assert(status == 0); status = pthread_join(thr, &result); - assert(status == 0); - assert(result == NULL); - printf("\n"); + test_assert(status == 0); + test_assert(result == NULL); + printf("OK\n"); // Return value Test - printf("return value test\n"); + printf("return value test: "); status = pthread_create(&thr, NULL, thread_simple, (void *)1); - assert(status == 0); + test_assert(status == 0); status = pthread_join(thr, &result); - assert(status == 0); - assert(result == (void *)1); - printf("\n"); + test_assert(status == 0); + test_assert(result == (void *)1); + printf("OK\n"); // Mutex Test - printf("simple mutex lock test\n"); + printf("simple mutex lock test: "); status = pthread_mutex_init(&mtx, NULL); - assert(status == 0); + test_assert(status == 0); status = pthread_mutex_lock(&mtx); - assert(status == 0); + test_assert(status == 0); status = pthread_mutex_unlock(&mtx); - assert(status == 0); + test_assert(status == 0); status = pthread_mutex_destroy(&mtx); - assert(status == 0); - printf("\n"); + test_assert(status == 0); + printf("OK\n"); // Mutex Contention Test - printf("contended mutex lock test\n"); + printf("contended mutex lock test: "); pthread_mutex_init(&mtx, NULL); status = pthread_create(&thr, NULL, thread_lock, (void *)1); - assert(status == 0); + test_assert(status == 0); for (i = 0; i < 100; i++) { status = pthread_mutex_lock(&mtx); - assert(status == 0); + test_assert(status == 0); pthread_yield(); pthread_mutex_unlock(&mtx); - assert(status == 0); + test_assert(status == 0); } status = pthread_join(thr, &result); - assert(status == 0); + test_assert(status == 0); status = pthread_mutex_destroy(&mtx); - assert(status == 0); - printf("\n"); + test_assert(status == 0); + printf("OK\n"); // Condition Variable Test - printf("simple condition variable test\n"); + printf("simple condition variable test: "); status = pthread_cond_init(&cnd, NULL); - assert(status == 0); + test_assert(status == 0); status = pthread_cond_signal(&cnd); - assert(status == 0); + test_assert(status == 0); status = pthread_cond_wait(&cnd, NULL); - assert(status == 0); + test_assert(status == 0); status = pthread_cond_destroy(&cnd); - assert(status == 0); - printf("\n"); + test_assert(status == 0); + printf("OK\n"); - printf("threaded condition variable test\n"); + printf("threaded condition variable test: OK"); status = pthread_cond_init(&cnd, NULL); - assert(status == 0); + test_assert(status == 0); for (i = 0; i < 100; i++) { status = pthread_cond_signal(&cnd); - assert(status == 0); + test_assert(status == 0); status = pthread_cond_wait(&cnd, NULL); - assert(status == 0); + test_assert(status == 0); } status = pthread_cond_destroy(&cnd); - assert(status == 0); + test_assert(status == 0); printf("\n"); printf("Success!\n");