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