r3/tests/check_queue.c

127 lines
2.6 KiB
C
Raw Normal View History

2014-05-23 22:17:54 -04:00
/*
* check_queue.c
* Copyright (C) 2014 c9s <c9s@c9smba.local>
*
* Distributed under terms of the MIT license.
*/
#include "r3_queue.h"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <check.h>
2014-05-23 22:24:57 -04:00
#include "r3.h"
#include "zmalloc.h"
2014-05-23 22:17:54 -04:00
#define number_of_threads 6
#define number_of_threads_d 5
#define number_of_iters 1000
struct _stru {
int number;
int thread_no;
queue * q;
};
2014-05-23 22:20:37 -04:00
void * producer_thread(void * arg)
2014-05-23 22:17:54 -04:00
{
struct _stru * args = (struct _stru *) arg;
int number = args->number;
//int th_number = args->thread_no;
queue * q = args->q;
int i;
srand(time(NULL));
for(i = 0; i < number; i++) {
2014-05-23 22:24:57 -04:00
char * message = zmalloc(16);
2014-05-23 22:17:54 -04:00
snprintf(message, 15, "rand: %d", rand());
2014-05-23 22:20:37 -04:00
queue_push(q, (void *)message);
2014-05-23 22:17:54 -04:00
}
return NULL;
}
2014-05-23 22:20:37 -04:00
void * consumer_thread(void * args)
2014-05-23 22:17:54 -04:00
{
queue * q = (queue *) args;
void * data;
2014-05-23 22:20:37 -04:00
while((data = queue_pop(q)) != NULL) {
2014-05-23 22:17:54 -04:00
char * string = (char *)data;
2014-05-23 22:24:57 -04:00
zfree(data);
2014-05-23 22:17:54 -04:00
}
return NULL;
}
START_TEST (test_queue)
{
2014-05-23 22:24:57 -04:00
queue * q = queue_new();
2014-05-23 22:27:47 -04:00
2014-05-23 22:29:54 -04:00
for (int i = 0 ; i < 100 ; i++ ) {
2014-05-23 22:27:47 -04:00
queue_push(q, (void*) i);
}
2014-05-23 22:29:54 -04:00
for (int i = 0 ; i < 100 ; i++ ) {
int v = (int) queue_pop(q);
2014-05-23 22:27:47 -04:00
ck_assert_int_eq(i, v);
}
queue_free(q);
2014-05-23 22:17:54 -04:00
}
END_TEST
START_TEST (test_queue_threads)
{
2014-05-23 22:24:57 -04:00
queue * q = queue_new();
2014-05-23 22:17:54 -04:00
pthread_t threads[number_of_threads];
pthread_t thread_d[number_of_threads_d];
int i;
struct _stru arg[number_of_threads];
for(i = 0; i < number_of_threads; i++) {
arg[i].number = number_of_iters;
arg[i].thread_no = i;
arg[i].q = q;
2014-05-23 22:20:37 -04:00
pthread_create(threads+i, NULL, producer_thread, (void *)&arg[i]);
2014-05-23 22:17:54 -04:00
}
for(i = 0; i < number_of_threads_d; i++) {
2014-05-23 22:20:37 -04:00
pthread_create(thread_d+i, NULL, consumer_thread, (void *)q);
2014-05-23 22:17:54 -04:00
}
for(i = 0; i < number_of_threads; i++) {
pthread_join(*(threads+i), NULL);
}
for(i = 0; i < number_of_threads_d; i++) {
pthread_join(*(thread_d+i), NULL);
}
2014-05-23 22:24:57 -04:00
queue_free(q);
2014-05-23 22:17:54 -04:00
}
END_TEST
Suite* r3_suite (void) {
Suite *suite = suite_create("queue");
TCase *tcase = tcase_create("queue_test");
tcase_add_test(tcase, test_queue_threads);
tcase_add_test(tcase, test_queue);
tcase_set_timeout(tcase, 30);
suite_add_tcase(suite, tcase);
return suite;
}
int main (int argc, char *argv[]) {
int number_failed;
Suite *suite = r3_suite();
SRunner *runner = srunner_create(suite);
srunner_run_all(runner, CK_NORMAL);
number_failed = srunner_ntests_failed(runner);
srunner_free(runner);
return number_failed;
}