diff options
author | Jan Hutter <jhutter@hsr.ch> | 2005-11-07 08:57:57 +0000 |
---|---|---|
committer | Jan Hutter <jhutter@hsr.ch> | 2005-11-07 08:57:57 +0000 |
commit | e15f7292ec4663977c1ee0414634ca738949a087 (patch) | |
tree | 59ccfa72d85a0a46543e4260c2e9934d01b9fcde | |
parent | fd9cabd4e68d85877a442ae2fff53c87e687f42b (diff) | |
download | strongswan-e15f7292ec4663977c1ee0414634ca738949a087.tar.bz2 strongswan-e15f7292ec4663977c1ee0414634ca738949a087.tar.xz |
- tester_t class rewritten
- test for send_queue_t added
-rw-r--r-- | Source/charon/tester.c | 28 | ||||
-rw-r--r-- | Source/charon/tester.h | 27 | ||||
-rw-r--r-- | Source/charon/tests.c | 2 | ||||
-rw-r--r-- | Source/charon/tests/send_queue_test.c | 143 | ||||
-rw-r--r-- | Source/charon/tests/send_queue_test.h | 38 |
5 files changed, 217 insertions, 21 deletions
diff --git a/Source/charon/tester.c b/Source/charon/tester.c index 0c6fb0680..20b07ec65 100644 --- a/Source/charon/tester.c +++ b/Source/charon/tester.c @@ -43,6 +43,11 @@ typedef struct private_tester_s private_tester_t; struct private_tester_s { tester_t public; + + /* Private functions */ + void (*run_test) (tester_t *tester, void (*test_function) (tester_t * tester), char * test_name); + + /* Private values */ FILE* output; int tests_count; @@ -53,9 +58,9 @@ struct private_tester_s { }; /* - * Implementation of function test_all + * Implementation of function perform_tests */ -static status_t test_all(tester_t *tester,test_t **tests) +static status_t perform_tests(tester_t *tester,test_t **tests) { private_tester_t *this =(private_tester_t*) tester; int current_test = 0; @@ -63,7 +68,7 @@ static status_t test_all(tester_t *tester,test_t **tests) while (tests[current_test] != NULL) { - tester->run_test(tester,tests[current_test]->test_function,tests[current_test]->test_name); + this->run_test(tester,tests[current_test]->test_function,tests[current_test]->test_name); current_test++; } @@ -72,6 +77,15 @@ static status_t test_all(tester_t *tester,test_t **tests) return SUCCESS; } +/* + * Implementation of function perform_test + */ +static status_t perform_test(tester_t *tester, test_t *test) +{ + test_t *tests[] = {test, NULL}; + return (perform_tests(tester,tests)); +} + /** * Returns the difference of to timeval structs in microseconds * @@ -168,11 +182,13 @@ tester_t *tester_create(FILE *output, bool display_succeeded_asserts) private_tester_t *this = alloc_thing(private_tester_t, "private_tester_t"); this->public.destroy = destroy; - this->public.test_all = test_all; - this->public.run_test = run_test; + this->public.perform_tests = perform_tests; + this->public.perform_test = perform_test; this->public.assert_true = assert_true; this->public.assert_false = assert_false; - + + + this->run_test = run_test; this->display_succeeded_asserts = display_succeeded_asserts; this->failed_tests_count = 0; this->tests_count = 0; diff --git a/Source/charon/tester.h b/Source/charon/tester.h index 4567660a7..8df6423c1 100644 --- a/Source/charon/tester.h +++ b/Source/charon/tester.h @@ -48,14 +48,23 @@ struct test_s{ struct tester_s { /** - * @brief Tests all testcases of specific tester object + * @brief Tests all testcases in array tests with specific tester object * * @param tester tester object - * @param pointer to a list of tests to perform. - * the last list item has to be NULL. + * @param pointer to a array of test_t-pointers. + * the last item has to be NULL. * @return SUCCESSFUL if succeeded, FAILED otherwise */ - status_t (*test_all) (tester_t *tester,test_t **tests); + status_t (*perform_tests) (tester_t *tester,test_t **tests); + + /** + * @brief run a specific test case + * + * @param this tester object + * @param test pointer to a test_t-object which will be performed + * @param Name of the Test + */ + status_t (*perform_test) (tester_t *tester, test_t *test); /** * @brief is called in a testcase to check a specific situation for TRUE @@ -83,16 +92,6 @@ struct tester_s { */ void (*assert_false) (tester_t *tester, bool to_be_false, char *assert_name); - - /** - * @brief run a specific test case - * - * @param this tester object - * @param test_function implements the test case - * @param Name of the Test - */ - void (*run_test) (tester_t *this, void (*test_function) (tester_t * tester), char * test_name); - /** * @brief Destroys a tester object * diff --git a/Source/charon/tests.c b/Source/charon/tests.c index 74dec6bd5..b778e9b0c 100644 --- a/Source/charon/tests.c +++ b/Source/charon/tests.c @@ -98,7 +98,7 @@ job_queue_t *job_queue; tester_t *tester = tester_create(test_output, TRUE); - tester->test_all(tester,all_tests); + tester->perform_tests(tester,all_tests); tester->destroy(tester); diff --git a/Source/charon/tests/send_queue_test.c b/Source/charon/tests/send_queue_test.c new file mode 100644 index 000000000..0298899a9 --- /dev/null +++ b/Source/charon/tests/send_queue_test.c @@ -0,0 +1,143 @@ +/** + * @file send_queue_test.c + * + * @brief Tests to test the Send-Queue type send_queue_t + * + */ + +/* + * Copyright (C) 2005 Jan Hutter, Martin Willi + * Hochschule fuer Technik Rapperswil + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include <pthread.h> + +#include "send_queue_test.h" +#include "../tester.h" +#include "../send_queue.h" + + +/** + * @brief Informations for the involved test-thread used in this test + * + */ +typedef struct send_queue_test_s send_queue_test_t; + + +struct send_queue_test_s{ + /** + * Associated tester_t-Object + */ + tester_t *tester; + + /** + * Queue to test + */ + send_queue_t *send_queue; + + /** + * number of items to be inserted in the send-queue by each thread + */ + int insert_item_count; + + /** + * number of items to be removed by each + * receiver thread from the send-queue + */ + int remove_item_count; +}; + +/** + * @brief sender thread used in the the send_queue test function + * + * @param testinfo informations for the specific thread. + */ +static void test_send_queue_sender(send_queue_test_t * testinfo) +{ + int i; + for (i = 0; i < testinfo->insert_item_count; i++) + { + packet_t *packet = packet_create(); + testinfo->tester->assert_true(testinfo->tester,(packet != NULL), "create packet call check"); + testinfo->tester->assert_true(testinfo->tester,(testinfo->send_queue->add(testinfo->send_queue,packet) == SUCCESS), "add packet call check"); + } +} + +/** + * @brief receiver thread used in the the send_queue test function + * + * @param testinfo informations for the specific thread. + */ +static void test_send_queue_receiver(send_queue_test_t * testinfo) +{ + int i; + for (i = 0; i < testinfo->remove_item_count; i++) + { + packet_t *packet; + testinfo->tester->assert_true(testinfo->tester,(testinfo->send_queue->get(testinfo->send_queue,&packet) == SUCCESS), "get packet call check"); + + testinfo->tester->assert_true(testinfo->tester,( packet != NULL), "packet not NULL call check"); + + testinfo->tester->assert_true(testinfo->tester,( packet->destroy(packet) == SUCCESS), "packet destroy call check"); + } +} + +/* + * description is in header file + */ +void test_send_queue(tester_t *tester) +{ + int value, desired_value, i; + int sender_count = 10; + int receiver_count = 2; + pthread_t sender_threads[sender_count]; + pthread_t receiver_threads[receiver_count]; + send_queue_t *send_queue = send_queue_create(); + send_queue_test_t test_infos; + + test_infos.tester = tester; + test_infos.send_queue = send_queue; + test_infos.insert_item_count = 10000; + test_infos.remove_item_count = 10000; + + + desired_value = test_infos.insert_item_count * sender_count - + test_infos.remove_item_count * receiver_count; + + for (i = 0; i < receiver_count;i++) + { + pthread_create( &receiver_threads[i], NULL,(void*(*)(void*)) &test_send_queue_receiver, (void*) &test_infos); + } + + for (i = 0; i < sender_count;i++) + { + pthread_create( &sender_threads[i], NULL,(void*(*)(void*)) &test_send_queue_sender, (void*) &test_infos); + } + + + /* Wait for all threads */ + for (i = 0; i < sender_count;i++) + { + pthread_join(sender_threads[i], NULL); + } + for (i = 0; i < receiver_count;i++) + { + pthread_join(receiver_threads[i], NULL); + } + + + /* the send-queue has to have diserd_value count entries*/ + tester->assert_true(tester,(send_queue->get_count(send_queue,&value) == SUCCESS), "get count call check"); + tester->assert_true(tester,(value == desired_value), "count value check"); + tester->assert_true(tester,(send_queue->destroy(send_queue) == SUCCESS), "destroy call check"); +} diff --git a/Source/charon/tests/send_queue_test.h b/Source/charon/tests/send_queue_test.h new file mode 100644 index 000000000..60b9306c0 --- /dev/null +++ b/Source/charon/tests/send_queue_test.h @@ -0,0 +1,38 @@ +/** + * @file send_queue_test.h + * + * @brief Tests to test the Send-Queue type send_queue_t + * + */ + +/* + * Copyright (C) 2005 Jan Hutter, Martin Willi + * Hochschule fuer Technik Rapperswil + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#ifndef SEND_QUEUE_TEST_H_ +#define SEND_QUEUE_TEST_H_ + +#include "../tester.h" + +/** + * @brief Test function used to test the send_queue functionality + * + * Tests are performed using different threads to test the multi-threaded + * features of the send_queue_t. + * + * @param tester associated tester object + */ +void test_send_queue(tester_t *tester); + +#endif /*SEND_QUEUE_TEST_H_*/ |