Make sure test asserts are always included.

This commit is contained in:
Ali Mashtizadeh 2023-08-30 12:46:17 -04:00
parent 67578f14b7
commit 1c2848207d

View File

@ -6,6 +6,12 @@
#include <pthread.h> #include <pthread.h>
#define test_assert(_expr) \
if (!(_expr)) { \
__assert(__func__, __FILE__, __LINE__, #_expr); \
}
pthread_mutex_t mtx; pthread_mutex_t mtx;
pthread_cond_t cnd; pthread_cond_t cnd;
@ -25,10 +31,10 @@ thread_lock(void *arg)
for (i = 0; i < 100; i++) { for (i = 0; i < 100; i++) {
status = pthread_mutex_lock(&mtx); status = pthread_mutex_lock(&mtx);
assert(status == 0); test_assert(status == 0);
pthread_yield(); pthread_yield();
status = pthread_mutex_unlock(&mtx); status = pthread_mutex_unlock(&mtx);
assert(status == 0); test_assert(status == 0);
} }
return NULL; return NULL;
@ -42,9 +48,9 @@ thread_cond(void *arg)
for (i = 0; i < 100; i++) { for (i = 0; i < 100; i++) {
status = pthread_cond_wait(&cnd, NULL); status = pthread_cond_wait(&cnd, NULL);
assert(status == 0); test_assert(status == 0);
status = pthread_cond_signal(&cnd); status = pthread_cond_signal(&cnd);
assert(status == 0); test_assert(status == 0);
} }
return NULL; return NULL;
@ -61,76 +67,76 @@ main(int argc, const char *argv[])
printf("PThread Test\n"); printf("PThread Test\n");
// Simple thread Test // Simple thread Test
printf("simple test\n"); printf("simple test: ");
status = pthread_create(&thr, NULL, thread_simple, NULL); status = pthread_create(&thr, NULL, thread_simple, NULL);
assert(status == 0); test_assert(status == 0);
status = pthread_join(thr, &result); status = pthread_join(thr, &result);
assert(status == 0); test_assert(status == 0);
assert(result == NULL); test_assert(result == NULL);
printf("\n"); printf("OK\n");
// Return value Test // Return value Test
printf("return value test\n"); printf("return value test: ");
status = pthread_create(&thr, NULL, thread_simple, (void *)1); status = pthread_create(&thr, NULL, thread_simple, (void *)1);
assert(status == 0); test_assert(status == 0);
status = pthread_join(thr, &result); status = pthread_join(thr, &result);
assert(status == 0); test_assert(status == 0);
assert(result == (void *)1); test_assert(result == (void *)1);
printf("\n"); printf("OK\n");
// Mutex Test // Mutex Test
printf("simple mutex lock test\n"); printf("simple mutex lock test: ");
status = pthread_mutex_init(&mtx, NULL); status = pthread_mutex_init(&mtx, NULL);
assert(status == 0); test_assert(status == 0);
status = pthread_mutex_lock(&mtx); status = pthread_mutex_lock(&mtx);
assert(status == 0); test_assert(status == 0);
status = pthread_mutex_unlock(&mtx); status = pthread_mutex_unlock(&mtx);
assert(status == 0); test_assert(status == 0);
status = pthread_mutex_destroy(&mtx); status = pthread_mutex_destroy(&mtx);
assert(status == 0); test_assert(status == 0);
printf("\n"); printf("OK\n");
// Mutex Contention Test // Mutex Contention Test
printf("contended mutex lock test\n"); printf("contended mutex lock test: ");
pthread_mutex_init(&mtx, NULL); pthread_mutex_init(&mtx, NULL);
status = pthread_create(&thr, NULL, thread_lock, (void *)1); status = pthread_create(&thr, NULL, thread_lock, (void *)1);
assert(status == 0); test_assert(status == 0);
for (i = 0; i < 100; i++) { for (i = 0; i < 100; i++) {
status = pthread_mutex_lock(&mtx); status = pthread_mutex_lock(&mtx);
assert(status == 0); test_assert(status == 0);
pthread_yield(); pthread_yield();
pthread_mutex_unlock(&mtx); pthread_mutex_unlock(&mtx);
assert(status == 0); test_assert(status == 0);
} }
status = pthread_join(thr, &result); status = pthread_join(thr, &result);
assert(status == 0); test_assert(status == 0);
status = pthread_mutex_destroy(&mtx); status = pthread_mutex_destroy(&mtx);
assert(status == 0); test_assert(status == 0);
printf("\n"); printf("OK\n");
// Condition Variable Test // Condition Variable Test
printf("simple condition variable test\n"); printf("simple condition variable test: ");
status = pthread_cond_init(&cnd, NULL); status = pthread_cond_init(&cnd, NULL);
assert(status == 0); test_assert(status == 0);
status = pthread_cond_signal(&cnd); status = pthread_cond_signal(&cnd);
assert(status == 0); test_assert(status == 0);
status = pthread_cond_wait(&cnd, NULL); status = pthread_cond_wait(&cnd, NULL);
assert(status == 0); test_assert(status == 0);
status = pthread_cond_destroy(&cnd); status = pthread_cond_destroy(&cnd);
assert(status == 0); test_assert(status == 0);
printf("\n"); printf("OK\n");
printf("threaded condition variable test\n"); printf("threaded condition variable test: OK");
status = pthread_cond_init(&cnd, NULL); status = pthread_cond_init(&cnd, NULL);
assert(status == 0); test_assert(status == 0);
for (i = 0; i < 100; i++) { for (i = 0; i < 100; i++) {
status = pthread_cond_signal(&cnd); status = pthread_cond_signal(&cnd);
assert(status == 0); test_assert(status == 0);
status = pthread_cond_wait(&cnd, NULL); status = pthread_cond_wait(&cnd, NULL);
assert(status == 0); test_assert(status == 0);
} }
status = pthread_cond_destroy(&cnd); status = pthread_cond_destroy(&cnd);
assert(status == 0); test_assert(status == 0);
printf("\n"); printf("\n");
printf("Success!\n"); printf("Success!\n");