Test for Linked List written
[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
33 /**
34 * @brief Private Variables and Functions of tester class
35 *
36 */
37 typedef struct private_tester_s private_tester_t;
38
39 struct private_tester_s {
40 tester_t tester;
41
42 /* Private values */
43 FILE* output;
44 int tests_count;
45 int failed_tests_count;
46 int failed_asserts_count;
47
48 /* Private functions */
49 /**
50 * @brief is called in a testcase to check a specific situation
51 *
52 * @param this tester object
53 * @param to_be_true assert which has to be true
54 * @param Name of the assertion
55 */
56 void (*assert_true) (private_tester_t *this, bool to_be_true, char *assert_name);
57
58 /**
59 * @brief run a specific test case
60 *
61 * @param this tester object
62 * @param test_function implements the test case
63 * @param Name of the Test
64 */
65 void (*run_test) (private_tester_t *this, void (*test_function) (private_tester_t * tester), char * test_name);
66 };
67
68 /**
69 * @brief Test function to test the linked list class
70 */
71 static void test_linked_list(private_tester_t * this){
72 void *test_value = NULL;
73
74 linked_list_t *linked_list = linked_list_create();
75 linked_list->insert_first(linked_list,"one");
76 linked_list->insert_first(linked_list,"two");
77 linked_list->insert_first(linked_list,"three");
78 linked_list->insert_first(linked_list,"four");
79 linked_list->insert_first(linked_list,"five");
80
81 this->assert_true(this,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check");
82 this->assert_true(this,(strcmp((char *) test_value,"five") == 0), "get_first value check");
83
84 this->assert_true(this,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check");
85 this->assert_true(this,(strcmp((char *) test_value,"one") == 0), "get_last value check");
86
87 this->assert_true(this,(linked_list->remove_first(linked_list,&test_value) == SUCCESS), "remove_first call check");
88 this->assert_true(this,(strcmp((char *) test_value,"five") == 0), "remove_first value check");
89
90 this->assert_true(this,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check");
91 this->assert_true(this,(strcmp((char *) test_value,"four") == 0), "get_first value check");
92
93 this->assert_true(this,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check");
94 this->assert_true(this,(strcmp((char *) test_value,"one") == 0), "get_last value check");
95
96 this->assert_true(this,(linked_list->remove_last(linked_list,&test_value) == SUCCESS), "remove_last call check");
97 this->assert_true(this,(strcmp((char *) test_value,"one") == 0), "remove_last value check");
98
99 this->assert_true(this,(linked_list->get_last(linked_list,&test_value) == SUCCESS), "get_last call check");
100 this->assert_true(this,(strcmp((char *) test_value,"two") == 0), "get_last value check");
101
102 this->assert_true(this,(linked_list->get_first(linked_list,&test_value) == SUCCESS), "get_first call check");
103 this->assert_true(this,(strcmp((char *) test_value,"four") == 0), "get_first value check");
104
105 this->assert_true(this,(linked_list->destroy(linked_list) == SUCCESS), "destroy call check");
106 }
107
108 /**
109 * @brief Testing of all registered tests
110 *
111 * New tests have to be added in this function
112 */
113 static status_t test_all(tester_t *tester)
114 {
115 private_tester_t *this =(private_tester_t*) tester;
116 fprintf(this->output,"Start testing\n");
117
118 /* Add new Tests here! */
119 this->run_test(this,test_linked_list,"Linked List");
120
121 fprintf(this->output,"End testing. %d tests failed of %d tests\n",this->failed_tests_count,this->tests_count);
122
123 #ifdef LEAK_DETECTIVE
124 /* Leaks are reported in log file */
125 report_leaks();
126 #endif
127 return SUCCESS;
128 }
129
130
131 /**
132 * @brief implements the private run_test-Function
133 *
134 */
135 static void run_test(private_tester_t *tester, void (*test_function) (private_tester_t * tester), char * test_name)
136 {
137 private_tester_t *this = tester;
138 this->tests_count++;
139 this->failed_asserts_count = 0;
140 fprintf(this->output,"Start Test '%s'\n", test_name);
141 test_function(this);
142 fprintf(this->output,"End Test '%s'\n", test_name);
143 if (this->failed_asserts_count > 0)
144 {
145 this->failed_tests_count++;
146 }
147 }
148
149 /**
150 * @brief implements the private assert_true-Function
151 *
152 */
153 static void assert_true(private_tester_t *tester, bool to_be_true,char * assert_name)
154 {
155 private_tester_t *this = tester;
156
157 if (assert_name == NULL)
158 {
159 assert_name = "unknown";
160 }
161
162 if (!to_be_true)
163 {
164 this->failed_asserts_count++;
165 fprintf(this->output," Assert '%s' failed!\n", assert_name);
166 }else
167 {
168 fprintf(this->output," Assert '%s' succeeded\n", assert_name);
169 }
170 }
171
172 /**
173 * Implements the destroy function
174 *
175 */
176 static status_t destroy(tester_t *this)
177 {
178 pfree(this);
179 return SUCCESS;
180 }
181
182 tester_t *tester_create(FILE *output)
183 {
184 private_tester_t *this = alloc_thing(private_tester_t, "private_tester_t");
185
186 this->tester.destroy = destroy;
187 this->tester.test_all = test_all;
188 this->run_test = run_test;
189 this->assert_true = assert_true;
190
191 this->failed_tests_count = 0;
192 this->tests_count = 0;
193 this->output = output;
194
195 return &(this->tester);
196 }