Add thread queue tests

This commit is contained in:
Nils O. Selåsdal
2012-11-12 22:18:04 +01:00
parent a4c80d1c76
commit 1eda2f8a53
3 changed files with 172 additions and 1 deletions
+2
View File
@@ -10,6 +10,7 @@ extern Suite *container_of_suite(void);
extern Suite *read_file_suite(void);
extern Suite *pack_suite(void);
extern Suite *logging_suite(void);
extern Suite *threadqueue_suite(void);
int
main (int argc, char *argv[])
@@ -29,6 +30,7 @@ main (int argc, char *argv[])
srunner_add_suite(sr, read_file_suite());
srunner_add_suite(sr, pack_suite());
srunner_add_suite(sr, logging_suite());
srunner_add_suite(sr, threadqueue_suite());
srunner_run_all (sr, CK_VERBOSE);
+169
View File
@@ -0,0 +1,169 @@
#include <check.h>
#include <stdio.h>
#include <sched.h>
#include <stdlib.h>
#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;
}