2 * \file lib/src/Framework/CUError.c
6 * \author Bjoern Doebel <doebel@os.inf.tu-dresden.de
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.
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
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.
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.
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
36 * Error handling code used by CUnit
38 * 16-Jul-2004 Created access functions for error code, error action
39 * functions, messages for new error codes. (JDS)
43 * Error handling functions (implementation).
45 /** @addtogroup Framework
53 #include "dde_support.h"
59 * Global/Static Definitions
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;
66 /* Private function forward declarations */
67 static const char* get_error_desc(CU_ErrorCode error);
69 #ifdef CUNIT_DO_NOT_DEFINE_UNLESS_BUILDING_TESTS
70 void test_exit(int status);
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.
82 * @see CU_get_error_msg()
85 void CU_set_error(CU_ErrorCode error)
87 if ((error != CUE_SUCCESS) && (g_error_action == CUEA_ABORT)) {
88 #ifndef CUNIT_DO_NOT_DEFINE_UNLESS_BUILDING_TESTS
90 fprintf(stderr, "\nAborting due to error #%d: %s\n",
92 get_error_desc(error));
95 printk("Aborting due to error #%d: %s\n", (int)error,
96 get_error_desc(error));
98 #endif /* DDE_LINUX */
104 g_error_number = error;
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()
118 CU_ErrorCode CU_get_error(void)
120 return g_error_number;
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()
135 const char* CU_get_error_msg(void)
137 return get_error_desc(g_error_number);
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
153 void CU_set_error_action(CU_ErrorAction action)
155 g_error_action = action;
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
165 CU_ErrorAction CU_get_error_action(void)
167 return g_error_action;
171 * Private static function definitions
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
177 * @param iError CU_ErrorCode to look up.
178 * @return Pointer to a string containing the error message.
179 * @see CU_get_error_msg()
181 static const char* get_error_desc(CU_ErrorCode iError)
185 static const char* ErrorDescription[] = {
186 "No Error", /* CUE_SUCCESS - 0 */
187 "Memory allocation failed.", /* CUE_NOMEMORY - 1 */
196 "Test registry does not exist.", /* CUE_NOREGISTRY - 10 */
197 "Registry already exists.", /* CUE_REGISTRY_EXISTS - 11 */
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 */
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 */
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 */
233 iMaxIndex = (int)(sizeof(ErrorDescription)/sizeof(char *) - 1);
234 if ((int)iError < 0) {
235 return ErrorDescription[0];
237 else if ((int)iError > iMaxIndex) {
238 return ErrorDescription[iMaxIndex];
241 return ErrorDescription[(int)iError];
247 #ifdef CUNIT_BUILD_TESTS
248 #include "test_cunit.h"
250 void test_cunit_CUError(void)
252 CU_ErrorCode old_err = CU_get_error();
253 CU_ErrorAction old_action = CU_get_error_action();
255 test_cunit_start_tests("CUError.c");
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);
262 CU_set_error(CUE_NOREGISTRY);
263 TEST(CU_get_error() != CUE_SUCCESS);
264 TEST(CU_get_error() == CUE_NOREGISTRY);
266 /* CU_get_error_msg() */
267 CU_set_error(CUE_SUCCESS);
268 TEST(!strcmp(CU_get_error_msg(), get_error_desc(CUE_SUCCESS)));
270 CU_set_error(CUE_NOTEST);
271 TEST(!strcmp(CU_get_error_msg(), get_error_desc(CUE_NOTEST)));
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)));
277 TEST(!strcmp(get_error_desc(100), "Undefined Error"));
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);
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);
291 CU_set_error(old_err);
292 CU_set_error_action(old_action);
294 test_cunit_end_tests();
297 #endif /* CUNIT_BUILD_TESTS */