- thread_pool test added
[strongswan.git] / Source / charon / tester.c
1 /**
2 * @file tester.c
3 *
4 * @brief Test module for automatic testing
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
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>.
16 *
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
20 * for more details.
21 */
22
23
24 #include <stdlib.h>
25 #include <freeswan.h>
26 #include <pluto/constants.h>
27 #include <pluto/defs.h>
28 #include <string.h>
29
30 #include "tester.h"
31 #include "linked_list.h"
32 #include "thread_pool.h"
33
34 /**
35 * @brief Private Variables and Functions of tester class
36 *
37 */
38 typedef struct private_tester_s private_tester_t;
39
40 struct private_tester_s {
41 tester_t tester;
42
43 /* Private values */
44 FILE* output;
45 int tests_count;
46 int failed_tests_count;
47 int failed_asserts_count;
48
49 /* Private functions */
50 /**
51 * @brief is called in a testcase to check a specific situation
52 *
53 * @param this tester object
54 * @param to_be_true assert which has to be true
55 * @param Name of the assertion
56 */
57 void (*assert_true) (private_tester_t *this, bool to_be_true, char *assert_name);
58
59 /**
60 * @brief run a specific test case
61 *
62 * @param this tester object
63 * @param test_function implements the test case
64 * @param Name of the Test
65 */
66 void (*run_test) (private_tester_t *this, void (*test_function) (private_tester_t * tester), char * test_name);
67 };
68
69 /**
70 * @brief Test function to test the linked list class
71 */
72 static void test_linked_list(private_tester_t *this)
73 {
74 void *test_value = NULL;
75
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");
82
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");
85
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");
90
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");
93
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");
96
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");
99
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");
102
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");
105
106 this->assert_true(this,(linked_list->destroy(linked_list) == SUCCESS), "destroy call check");
107 }
108
109 /**
110 * @brief Test function to test the thread pool class
111 */
112 static void test_thread_pool(private_tester_t *this)
113 {
114 size_t pool_size;
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");
119 pool->destroy(pool);
120 }
121
122 /**
123 * @brief Testing of all registered tests
124 *
125 * New tests have to be added in this function
126 */
127 static status_t test_all(tester_t *tester)
128 {
129 private_tester_t *this =(private_tester_t*) tester;
130 fprintf(this->output,"Start testing\n");
131
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");
135
136 fprintf(this->output,"End testing. %d tests failed of %d tests\n",this->failed_tests_count,this->tests_count);
137
138 #ifdef LEAK_DETECTIVE
139 /* Leaks are reported in log file */
140 report_leaks();
141 #endif
142 return SUCCESS;
143 }
144
145
146 /**
147 * @brief implements the private run_test-Function
148 *
149 */
150 static void run_test(private_tester_t *tester, void (*test_function) (private_tester_t * tester), char * test_name)
151 {
152 private_tester_t *this = tester;
153 this->tests_count++;
154 this->failed_asserts_count = 0;
155 fprintf(this->output,"Start Test '%s'\n", test_name);
156 test_function(this);
157 fprintf(this->output,"End Test '%s'\n", test_name);
158 if (this->failed_asserts_count > 0)
159 {
160 this->failed_tests_count++;
161 }
162 }
163
164 /**
165 * @brief implements the private assert_true-Function
166 *
167 */
168 static void assert_true(private_tester_t *tester, bool to_be_true,char * assert_name)
169 {
170 private_tester_t *this = tester;
171
172 if (assert_name == NULL)
173 {
174 assert_name = "unknown";
175 }
176
177 if (!to_be_true)
178 {
179 this->failed_asserts_count++;
180 fprintf(this->output," Assert '%s' failed!\n", assert_name);
181 }else
182 {
183 fprintf(this->output," Assert '%s' succeeded\n", assert_name);
184 }
185 }
186
187 /**
188 * Implements the destroy function
189 *
190 */
191 static status_t destroy(tester_t *this)
192 {
193 pfree(this);
194 return SUCCESS;
195 }
196
197 tester_t *tester_create(FILE *output)
198 {
199 private_tester_t *this = alloc_thing(private_tester_t, "private_tester_t");
200
201 this->tester.destroy = destroy;
202 this->tester.test_all = test_all;
203 this->run_test = run_test;
204 this->assert_true = assert_true;
205
206 this->failed_tests_count = 0;
207 this->tests_count = 0;
208 this->output = output;
209
210 return &(this->tester);
211 }