4 * @brief Test module for automatic testing
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 #include <pluto/constants.h>
27 #include <pluto/defs.h>
31 #include "linked_list.h"
32 #include "thread_pool.h"
35 * @brief Private Variables and Functions of tester class
38 typedef struct private_tester_s private_tester_t
;
40 struct private_tester_s
{
46 int failed_tests_count
;
47 int failed_asserts_count
;
49 /* Private functions */
51 * @brief is called in a testcase to check a specific situation
53 * @param this tester object
54 * @param to_be_true assert which has to be true
55 * @param Name of the assertion
57 void (*assert_true
) (private_tester_t
*this, bool to_be_true
, char *assert_name
);
60 * @brief run a specific test case
62 * @param this tester object
63 * @param test_function implements the test case
64 * @param Name of the Test
66 void (*run_test
) (private_tester_t
*this, void (*test_function
) (private_tester_t
* tester
), char * test_name
);
70 * @brief Test function to test the linked list class
72 static void test_linked_list(private_tester_t
*this)
74 void *test_value
= NULL
;
76 linked_list_t
*linked_list
= linked_list_create();
77 linked_list
->insert_first(linked_list
,"one");
78 linked_list
->insert_first(linked_list
,"two");
79 linked_list
->insert_first(linked_list
,"three");
80 linked_list
->insert_first(linked_list
,"four");
81 linked_list
->insert_first(linked_list
,"five");
83 this->assert_true(this,(linked_list
->get_first(linked_list
,&test_value
) == SUCCESS
), "get_first call check");
84 this->assert_true(this,(strcmp((char *) test_value
,"five") == 0), "get_first value check");
86 this->assert_true(this,(linked_list
->get_last(linked_list
,&test_value
) == SUCCESS
), "get_last call check");
87 this->assert_true(this,(strcmp((char *) test_value
,"one") == 0), "get_last value check");
88 this->assert_true(this,(linked_list
->remove_first(linked_list
,&test_value
) == SUCCESS
), "remove_first call check");
89 this->assert_true(this,(strcmp((char *) test_value
,"five") == 0), "remove_first value check");
91 this->assert_true(this,(linked_list
->get_first(linked_list
,&test_value
) == SUCCESS
), "get_first call check");
92 this->assert_true(this,(strcmp((char *) test_value
,"four") == 0), "get_first value check");
94 this->assert_true(this,(linked_list
->get_last(linked_list
,&test_value
) == SUCCESS
), "get_last call check");
95 this->assert_true(this,(strcmp((char *) test_value
,"one") == 0), "get_last value check");
97 this->assert_true(this,(linked_list
->remove_last(linked_list
,&test_value
) == SUCCESS
), "remove_last call check");
98 this->assert_true(this,(strcmp((char *) test_value
,"one") == 0), "remove_last value check");
100 this->assert_true(this,(linked_list
->get_last(linked_list
,&test_value
) == SUCCESS
), "get_last call check");
101 this->assert_true(this,(strcmp((char *) test_value
,"two") == 0), "get_last value check");
103 this->assert_true(this,(linked_list
->get_first(linked_list
,&test_value
) == SUCCESS
), "get_first call check");
104 this->assert_true(this,(strcmp((char *) test_value
,"four") == 0), "get_first value check");
106 this->assert_true(this,(linked_list
->destroy(linked_list
) == SUCCESS
), "destroy call check");
110 * @brief Test function to test the thread pool class
112 static void test_thread_pool(private_tester_t
*this)
115 size_t desired_pool_size
= 10;
116 thread_pool_t
*pool
= thread_pool_create(desired_pool_size
);
117 pool
->get_pool_size(pool
, &pool_size
);
118 this->assert_true(this, (desired_pool_size
== pool_size
), "thread creation");
123 * @brief Testing of all registered tests
125 * New tests have to be added in this function
127 static status_t
test_all(tester_t
*tester
)
129 private_tester_t
*this =(private_tester_t
*) tester
;
130 fprintf(this->output
,"Start testing\n");
132 /* Add new Tests here! */
133 this->run_test(this,test_linked_list
,"Linked List");
134 this->run_test(this,test_thread_pool
,"Thread Pool");
136 fprintf(this->output
,"End testing. %d tests failed of %d tests\n",this->failed_tests_count
,this->tests_count
);
138 #ifdef LEAK_DETECTIVE
139 /* Leaks are reported in log file */
147 * @brief implements the private run_test-Function
150 static void run_test(private_tester_t
*tester
, void (*test_function
) (private_tester_t
* tester
), char * test_name
)
152 private_tester_t
*this = tester
;
154 this->failed_asserts_count
= 0;
155 fprintf(this->output
,"Start Test '%s'\n", test_name
);
157 fprintf(this->output
,"End Test '%s'\n", test_name
);
158 if (this->failed_asserts_count
> 0)
160 this->failed_tests_count
++;
165 * @brief implements the private assert_true-Function
168 static void assert_true(private_tester_t
*tester
, bool to_be_true
,char * assert_name
)
170 private_tester_t
*this = tester
;
172 if (assert_name
== NULL
)
174 assert_name
= "unknown";
179 this->failed_asserts_count
++;
180 fprintf(this->output
," Assert '%s' failed!\n", assert_name
);
183 fprintf(this->output
," Assert '%s' succeeded\n", assert_name
);
188 * Implements the destroy function
191 static status_t
destroy(tester_t
*this)
197 tester_t
*tester_create(FILE *output
)
199 private_tester_t
*this = alloc_thing(private_tester_t
, "private_tester_t");
201 this->tester
.destroy
= destroy
;
202 this->tester
.test_all
= test_all
;
203 this->run_test
= run_test
;
204 this->assert_true
= assert_true
;
206 this->failed_tests_count
= 0;
207 this->tests_count
= 0;
208 this->output
= output
;
210 return &(this->tester
);