]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/cunit/lib/common/Framework/CUError.c
Update
[l4.git] / l4 / pkg / cunit / lib / common / Framework / CUError.c
1 /*!
2  * \file   lib/src/Framework/CUError.c
3  * \brief  
4  *
5  * \date   01/30/2007
6  * \author Bjoern Doebel <doebel@os.inf.tu-dresden.de
7  *
8  */
9 /*
10  * (c) 2007-2009 Technische Universität Dresden
11  * This file is part of TUD:OS and distributed under the terms of the
12  * GNU General Public License 2.
13  * Please see the COPYING-GPL-2 file for details.
14  */
15 /*
16  *  CUnit - A Unit testing framework library for C.
17  *  Copyright (C) 2001            Anil Kumar
18  *  Copyright (C) 2004,2005,2006  Anil Kumar, Jerry St.Clair
19  *
20  *  This library is free software; you can redistribute it and/or
21  *  modify it under the terms of the GNU Library General Public
22  *  License as published by the Free Software Foundation; either
23  *  version 2 of the License, or (at your option) any later version.
24  *
25  *  This library is distributed in the hope that it will be useful,
26  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
27  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28  *  Library General Public License for more details.
29  *
30  *  You should have received a copy of the GNU Library General Public
31  *  License along with this library; if not, write to the Free Software
32  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33  */
34
35 /*
36  *  Error handling code used by CUnit
37  *
38  *  16-Jul-2004   Created access functions for error code, error action 
39  *                functions, messages for new error codes. (JDS)
40  */
41
42 /** @file
43  * Error handling functions (implementation).
44  */
45 /** @addtogroup Framework
46  @{
47 */
48
49 #ifndef DDE_LINUX
50 #include <stdio.h>
51 #include <stdlib.h>
52 #else
53 #include "dde_support.h"
54 #endif
55
56 #include <CUError.h>
57
58 /*
59  *      Global/Static Definitions
60  */
61 /** Local variable holding the current error code. */
62 static CU_ErrorCode g_error_number = CUE_SUCCESS;
63 /** Local variable holding the current error action code. */
64 static CU_ErrorAction g_error_action = CUEA_IGNORE;
65
66 /* Private function forward declarations */
67 static const char* get_error_desc(CU_ErrorCode error);
68
69 #ifdef CUNIT_DO_NOT_DEFINE_UNLESS_BUILDING_TESTS
70 void test_exit(int status);
71 #endif
72
73 /*------------------------------------------------------------------------*/
74 /** Set the error code.
75  * This function is used internally by CUnit implementation functions
76  * when an error condition occurs within the framework.  It should
77  * not generally be called by user code.  NOTE that if the current
78  * error action is CUEA_ABORT, then calling this function will
79  * result in exit() being called for the current application.
80  * @param error CU_ErrorCode indicating the current error condition.
81  * @see CU_get_error()
82  * @see CU_get_error_msg()
83  * @see CU_ErrorCode
84  */
85 void CU_set_error(CU_ErrorCode error)
86 {
87   if ((error != CUE_SUCCESS) && (g_error_action == CUEA_ABORT)) {
88 #ifndef CUNIT_DO_NOT_DEFINE_UNLESS_BUILDING_TESTS
89 #ifndef DDE_LINUX
90     fprintf(stderr, "\nAborting due to error #%d: %s\n",
91             (int)error,
92             get_error_desc(error));
93     exit((int)error);
94 #else
95         printk("Aborting due to error #%d: %s\n", (int)error,
96                         get_error_desc(error));
97         do_exit((int)error);
98 #endif /* DDE_LINUX */
99 #else
100     test_exit(error);
101 #endif
102   }
103
104   g_error_number = error;
105 }
106
107 /*------------------------------------------------------------------------*/
108 /** Get the error code.
109  * CUnit implementation functions set the error code to indicate the
110  * status of the most recent operation.  In general, the CUnit functions
111  * will clear the code to CUE_SUCCESS, then reset it to a specific error
112  * code if an exception condition is encountered.  Some functions
113  * return the code, others leave it to the user to inspect if desired.
114  * @return The current error condition code.
115  * @see CU_get_error_msg()
116  * @see CU_ErrorCode
117  */
118 CU_ErrorCode CU_get_error(void)
119 {
120         return g_error_number;
121 }
122
123 /*------------------------------------------------------------------------*/
124 /** Get the message corresponding to the error code.
125  * CUnit implementation functions set the error code to indicate the
126  * of the most recent operation.  In general, the CUnit functions will
127  * clear the code to CUE_SUCCESS, then reset it to a specific error
128  * code if an exception condition is encountered.  This function allows
129  * the user to retrieve a descriptive error message corresponding to the
130  * error code set by the last operation.
131  * @return A message corresponding to the current error condition.
132  * @see CU_get_error()
133  * @see CU_ErrorCode
134  */
135 const char* CU_get_error_msg(void)
136 {
137         return get_error_desc(g_error_number);
138 }
139
140 /*------------------------------------------------------------------------*/
141 /** Set the action to take when an error condition occurs.
142  * This function should be used to specify the action to take
143  * when an error condition is encountered.  The default action is
144  * CUEA_IGNORE, which results in errors being ignored and test runs
145  * being continued (if possible).  A value of CUEA_FAIL causes test
146  * runs to stop as soon as an error condition occurs, while
147  * CU_ABORT causes the application to exit on any error.
148  * @param action CU_ErrorAction indicating the new error action.
149  * @see CU_get_error_action()
150  * @see CU_set_error()
151  * @see CU_ErrorAction
152  */
153 void CU_set_error_action(CU_ErrorAction action)
154 {
155   g_error_action = action;
156 }
157
158 /*------------------------------------------------------------------------*/
159 /** Get the current error action code.
160  * @return The current error action code.
161  * @see CU_set_error_action()
162  * @see CU_set_error()
163  * @see CU_ErrorAction
164  */
165 CU_ErrorAction CU_get_error_action(void)
166 {
167   return g_error_action;
168 }
169
170 /*
171  * Private static function definitions
172  */
173 /*------------------------------------------------------------------------*/
174 /** Internal function to look up the error message for a specified
175  * error code.  An empty string is returned if iError is not a member
176  * of CU_ErrorCode.
177  * @param iError  CU_ErrorCode to look up.
178  * @return Pointer to a string containing the error message.
179  * @see CU_get_error_msg()
180  */
181 static const char* get_error_desc(CU_ErrorCode iError)
182 {
183   int iMaxIndex;
184
185   static const char* ErrorDescription[] = {
186     "No Error",                             /* CUE_SUCCESS - 0 */
187     "Memory allocation failed.",            /* CUE_NOMEMORY - 1 */
188     "",
189     "",
190     "",
191     "",
192     "",
193     "",
194     "",
195     "",
196     "Test registry does not exist.",          /* CUE_NOREGISTRY - 10 */
197     "Registry already exists.",               /* CUE_REGISTRY_EXISTS - 11 */
198     "",
199     "",
200     "",
201     "",
202     "",
203     "",
204     "",
205     "",
206     "NULL suite not allowed.",                /* CUE_NOSUITE - 20 */
207     "Suite name cannot be NULL.",             /* CUE_NO_SUITENAME - 21 */
208     "Suite initialization function failed.",  /* CUE_SINIT_FAILED - 22 */
209     "Suite cleanup function failed.",         /* CUE_SCLEAN_FAILED - 23 */
210     "Suite having name already registered.",  /* CUE_DUP_SUITE - 24 */
211     "",
212     "",
213     "",
214     "",
215     "",
216     "NULL test not allowed.",                 /* CUE_NOTEST - 30 */
217     "Test name cannot be NULL.",              /* CUE_NO_TESTNAME - 31 */
218     "Test having this name already in suite.",/* CUE_DUP_TEST - 32 */
219     "Test not registered in specified suite.",/* CUE_TEST_NOT_IN_SUITE - 33 */
220     "",
221     "",
222     "",
223     "",
224     "",
225     "",
226     "Error opening file.",                    /* CUE_FOPEN_FAILED - 40 */
227     "Error closing file.",                    /* CUE_FCLOSE_FAILED - 41 */
228     "Bad file name.",                         /* CUE_BAD_FILENAME - 42 */
229     "Error during write to file.",            /* CUE_WRITE_ERROR - 43 */
230     "Undefined Error"
231   };
232
233   iMaxIndex = (int)(sizeof(ErrorDescription)/sizeof(char *) - 1);
234   if ((int)iError < 0) {
235     return ErrorDescription[0];
236   }
237   else if ((int)iError > iMaxIndex) {
238     return ErrorDescription[iMaxIndex];
239   }
240   else {
241     return ErrorDescription[(int)iError];
242   }
243 }
244
245 /** @} */
246
247 #ifdef CUNIT_BUILD_TESTS
248 #include "test_cunit.h"
249
250 void test_cunit_CUError(void)
251 {
252   CU_ErrorCode old_err = CU_get_error();
253   CU_ErrorAction old_action = CU_get_error_action();
254
255   test_cunit_start_tests("CUError.c");
256
257   /* CU_set_error() & CU_get_error() */
258   CU_set_error(CUE_NOMEMORY);
259   TEST(CU_get_error() != CUE_SUCCESS);
260   TEST(CU_get_error() == CUE_NOMEMORY);
261
262   CU_set_error(CUE_NOREGISTRY);
263   TEST(CU_get_error() != CUE_SUCCESS);
264   TEST(CU_get_error() == CUE_NOREGISTRY);
265
266   /* CU_get_error_msg() */
267   CU_set_error(CUE_SUCCESS);
268   TEST(!strcmp(CU_get_error_msg(), get_error_desc(CUE_SUCCESS)));
269
270   CU_set_error(CUE_NOTEST);
271   TEST(!strcmp(CU_get_error_msg(), get_error_desc(CUE_NOTEST)));
272
273   CU_set_error(CUE_NOMEMORY);
274   TEST(!strcmp(CU_get_error_msg(), get_error_desc(CUE_NOMEMORY)));
275   TEST(strcmp(CU_get_error_msg(), get_error_desc(CUE_SCLEAN_FAILED)));
276
277   TEST(!strcmp(get_error_desc(100), "Undefined Error"));
278
279   /* CU_set_error_action() & CU_get_error_action() */
280   CU_set_error_action(CUEA_FAIL);
281   TEST(CU_get_error_action() != CUEA_IGNORE);
282   TEST(CU_get_error_action() == CUEA_FAIL);
283   TEST(CU_get_error_action() != CUEA_ABORT);
284
285   CU_set_error_action(CUEA_ABORT);
286   TEST(CU_get_error_action() != CUEA_IGNORE);
287   TEST(CU_get_error_action() != CUEA_FAIL);
288   TEST(CU_get_error_action() == CUEA_ABORT);
289
290   /* reset  values */
291   CU_set_error(old_err);
292   CU_set_error_action(old_action);
293
294   test_cunit_end_tests();
295 }
296
297 #endif    /* CUNIT_BUILD_TESTS */