#include #include #include #include #include "ucore_threadqueue.h" struct thr_msg { struct uc_threadmsg msg; int a,b; char data[32]; }; START_TEST (test_thread_queue_length) struct uc_threadqueue queue; int i; fail_if(uc_thread_queue_init(&queue, 100) != 0); for(i = 0; i < 51; i++) { struct thr_msg *msg = malloc(sizeof *msg); fail_if(msg == NULL); fail_if(uc_thread_queue_add(&queue, &msg->msg) != 0); } fail_if(uc_thread_queue_length(&queue) != 51); END_TEST START_TEST (test_thread_queue_one_thread) struct uc_threadqueue queue; int i; fail_if(uc_thread_queue_init(&queue, 100) != 0); for(i = 0; i < 51; i++) { struct thr_msg *msg = malloc(sizeof *msg); fail_if(msg == NULL); msg->a = i; fail_if(uc_thread_queue_add(&queue, &msg->msg) != 0); } for(i = 0; i < 51; i++) { struct uc_threadmsg *msg; struct thr_msg *my_msg; fail_if(uc_thread_queue_get(&queue, NULL, &msg) != 0); my_msg = (struct thr_msg *)msg; fail_if(my_msg->a != i); free(my_msg); } fail_if(uc_thread_queue_destroy(&queue, NULL) != 0); END_TEST //pretty ugly.. static int test_thread_queue_walk_global; static void test_thread_queue_walk_func(struct uc_threadmsg *msg) { struct thr_msg *my_msg = (struct thr_msg *)msg; fail_if(my_msg->a != test_thread_queue_walk_global); test_thread_queue_walk_global++; } START_TEST (test_thread_queue_walk) struct uc_threadqueue queue; int i; fail_if(uc_thread_queue_init(&queue, 100) != 0); for(i = 0; i < 51; i++) { struct thr_msg *msg = malloc(sizeof *msg); fail_if(msg == NULL); msg->a = i; fail_if(uc_thread_queue_add(&queue, &msg->msg) != 0); } test_thread_queue_walk_global = 0; uc_thread_queue_walk(&queue, test_thread_queue_walk_func); END_TEST #define READER_WRITER_NUM_MSG 20000 static void *test_thread_queue_reader_writer_func(void *arg) { struct uc_threadqueue *queue = arg; int i; for(i = 0; i < READER_WRITER_NUM_MSG; i++) { struct uc_threadmsg *msg; struct thr_msg *my_msg; fail_if(uc_thread_queue_get(queue, NULL, &msg) != 0); my_msg = (struct thr_msg *)msg; fail_if(my_msg->a != i); free(my_msg); } } START_TEST (test_thread_queue_reader_writer) struct uc_threadqueue queue; int i; pthread_t tid; fail_if(uc_thread_queue_init(&queue, 1000) != 0); fail_if(pthread_create(&tid, NULL, test_thread_queue_reader_writer_func, &queue) != 0); sched_yield(); for(i = 0; i < READER_WRITER_NUM_MSG; i++) { struct thr_msg *msg = malloc(sizeof *msg); fail_if(msg == NULL); msg->a = i; fail_if(uc_thread_queue_add(&queue, &msg->msg) != 0); } pthread_join(tid, NULL); fail_if(uc_thread_queue_destroy(&queue, NULL) != 0); END_TEST START_TEST (test_thread_queue_reader_writer_len_1) struct uc_threadqueue queue; int i; pthread_t tid; fail_if(uc_thread_queue_init(&queue, 1) != 0); fail_if(pthread_create(&tid, NULL, test_thread_queue_reader_writer_func, &queue) != 0); sched_yield(); for(i = 0; i < READER_WRITER_NUM_MSG; i++) { struct thr_msg *msg = malloc(sizeof *msg); fail_if(msg == NULL); msg->a = i; fail_if(uc_thread_queue_add(&queue, &msg->msg) != 0); } pthread_join(tid, NULL); END_TEST START_TEST (test_thread_queue_zero_len) struct uc_threadqueue queue; fail_if(uc_thread_queue_init(&queue, 0) == 0); //thread_queue_init should fail END_TEST Suite *threadqueue_suite(void) { Suite *s = suite_create("treadqueue"); TCase *tc1 = tcase_create("thread queue"); tcase_add_test(tc1, test_thread_queue_length); tcase_add_test(tc1, test_thread_queue_one_thread); tcase_add_test(tc1, test_thread_queue_walk); tcase_add_test(tc1, test_thread_queue_reader_writer); tcase_add_test(tc1, test_thread_queue_reader_writer_len_1); tcase_add_test(tc1, test_thread_queue_zero_len); suite_add_tcase(s, tc1); return s; }