55
66#include "../../queue.h"
77
8- static int tests_run = 0 ;
8+ static int tests_run = 0 ;
99static int tests_failed = 0 ;
1010
11- #define RUN_TEST (fn ) \
12- do { \
13- tests_run++; \
14- printf("Running %s...\n", #fn); \
15- fn(); \
16- printf("[DONE] %s\n\n", #fn); \
11+ #define RUN_TEST (fn ) \
12+ do { \
13+ tests_run++; \
14+ printf("Running %s...\n", #fn); \
15+ fn(); \
16+ printf("[DONE] %s\n\n", #fn); \
1717 } while (0)
1818
19- #define CHECK (cond ) \
20- do { \
21- if (!(cond)) { \
22- tests_failed++; \
23- fprintf(stderr, "[FAIL] %s:%d: %s\n", __FILE__, __LINE__, #cond); \
24- return; \
25- } \
19+ #define CHECK (cond ) \
20+ do { \
21+ if (!(cond)) { \
22+ tests_failed++; \
23+ fprintf(stderr, "[FAIL] %s:%d: %s\n", __FILE__, __LINE__, #cond); \
24+ return; \
25+ } \
2626 } while (0)
2727
2828/* For use inside pthread entry functions (which must return void *). */
29- #define CHECK_THREAD (cond ) \
30- do { \
31- if (!(cond)) { \
32- tests_failed++; \
33- fprintf(stderr, "[FAIL] %s:%d: %s\n", __FILE__, __LINE__, #cond); \
34- return NULL; \
35- } \
29+ #define CHECK_THREAD (cond ) \
30+ do { \
31+ if (!(cond)) { \
32+ tests_failed++; \
33+ fprintf(stderr, "[FAIL] %s:%d: %s\n", __FILE__, __LINE__, #cond); \
34+ return NULL; \
35+ } \
3636 } while (0)
3737
38- static void test_queue_new_and_delete (void ) {
38+ static void test_queue_new_and_delete (void )
39+ {
3940 queue_t * q = queue_new (4 );
4041 CHECK (q != NULL );
4142
4243 queue_delete (& q );
4344 CHECK (q == NULL );
4445}
4546
46- static void test_queue_push_pop_basic (void ) {
47+ static void test_queue_push_pop_basic (void )
48+ {
4749 queue_t * q = queue_new (4 );
4850 CHECK (q != NULL );
4951
@@ -57,15 +59,15 @@ static void test_queue_push_pop_basic(void) {
5759
5860 int * out = NULL ;
5961
60- CHECK (queue_pop (q , (void * * ) & out ) == true);
62+ CHECK (queue_pop (q , (void * * )& out ) == true);
6163 CHECK (out == & a );
6264 CHECK (* out == 1 );
6365
64- CHECK (queue_pop (q , (void * * ) & out ) == true);
66+ CHECK (queue_pop (q , (void * * )& out ) == true);
6567 CHECK (out == & b );
6668 CHECK (* out == 2 );
6769
68- CHECK (queue_pop (q , (void * * ) & out ) == true);
70+ CHECK (queue_pop (q , (void * * )& out ) == true);
6971 CHECK (out == & c );
7072 CHECK (* out == 3 );
7173
@@ -75,10 +77,11 @@ static void test_queue_push_pop_basic(void) {
7577
7678typedef struct {
7779 queue_t * q ;
78- int count ;
80+ int count ;
7981} pc_args_t ;
8082
81- static void * producer_thread (void * arg ) {
83+ static void * producer_thread (void * arg )
84+ {
8285 pc_args_t * pc = arg ;
8386 for (int i = 0 ; i < pc -> count ; i ++ ) {
8487 int * value = malloc (sizeof (int ));
@@ -89,28 +92,30 @@ static void *producer_thread(void *arg) {
8992 return NULL ;
9093}
9194
92- static void * consumer_thread (void * arg ) {
95+ static void * consumer_thread (void * arg )
96+ {
9397 pc_args_t * pc = arg ;
9498 for (int i = 0 ; i < pc -> count ; i ++ ) {
9599 int * value = NULL ;
96- CHECK_THREAD (queue_pop (pc -> q , (void * * ) & value ) == true);
100+ CHECK_THREAD (queue_pop (pc -> q , (void * * )& value ) == true);
97101 CHECK_THREAD (value != NULL );
98102 CHECK_THREAD (* value == i );
99103 free (value );
100104 }
101105 return NULL ;
102106}
103107
104- static void test_queue_blocking_producer_consumer (void ) {
108+ static void test_queue_blocking_producer_consumer (void )
109+ {
105110 const int capacity = 4 ;
106- const int nitems = 20 ;
111+ const int nitems = 20 ;
107112
108113 queue_t * q = queue_new (capacity );
109114 CHECK (q != NULL );
110115
111116 pc_args_t pc = {
112- . q = q ,
113- .count = nitems ,
117+ . q = q ,
118+ .count = nitems ,
114119 };
115120
116121 pthread_t prod , cons ;
@@ -130,14 +135,16 @@ static void test_queue_blocking_producer_consumer(void) {
130135 CHECK (q == NULL );
131136}
132137
133- static void test_queue_null_behavior (void ) {
138+ static void test_queue_null_behavior (void )
139+ {
134140 CHECK (queue_push (NULL , NULL ) == false);
135141
136- void * out = (void * ) 0xDEADBEEF ;
142+ void * out = (void * )0xDEADBEEF ;
137143 CHECK (queue_pop (NULL , & out ) == false);
138144}
139145
140- int main (void ) {
146+ int main (void )
147+ {
141148 RUN_TEST (test_queue_new_and_delete );
142149 RUN_TEST (test_queue_push_pop_basic );
143150 RUN_TEST (test_queue_blocking_producer_consumer );
0 commit comments