/* $Id: cpl_error-test.c,v 1.16 2007/11/13 13:13:02 llundin Exp $ * * This file is part of the ESO Common Pipeline Library * Copyright (C) 2001-2004 European Southern Observatory * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* * $Author: llundin $ * $Date: 2007/11/13 13:13:02 $ * $Revision: 1.16 $ * $Name: $ */ /*----------------------------------------------------------------------------- Includes -----------------------------------------------------------------------------*/ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include /*----------------------------------------------------------------------------- Prototypes of private functions -----------------------------------------------------------------------------*/ static cpl_error_code cpl_error_test_set(cpl_error_code); static cpl_error_code cpl_error_test_set_where(void); static cpl_error_code cpl_error_test_set_message(cpl_error_code); static cpl_error_code cpl_error_test_set_message_empty(cpl_error_code); static cpl_error_code cpl_error_test_ensure(cpl_error_code); /*----------------------------------------------------------------------------- Main -----------------------------------------------------------------------------*/ int main(void) { const cpl_boolean has_func = strcmp(cpl_func, "main") ? CPL_FALSE : CPL_TRUE; cpl_error_code ierror; cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING); /* Insert tests here */ /* Test 1: Verify that the error state is empty on start-up */ cpl_test( CPL_ERROR_NONE == 0); cpl_test( cpl_error_get_code() == CPL_ERROR_NONE ); cpl_test( cpl_error_get_line() == 0 ); cpl_test( strlen(cpl_error_get_function()) == 0 ); cpl_test( strlen(cpl_error_get_file()) == 0 ); /* Test 1b: Verify that cpl_error_set() will not change that */ cpl_test( cpl_error_test_set(CPL_ERROR_NONE) == CPL_ERROR_NONE); cpl_test( cpl_error_get_code() == CPL_ERROR_NONE ); cpl_test( cpl_error_get_line() == 0 ); cpl_test( strlen(cpl_error_get_function()) == 0 ); cpl_test( strlen(cpl_error_get_file()) == 0 ); /* Test 1c: Verify that cpl_error_set_where() will not change that */ cpl_test( cpl_error_test_set_where() == CPL_ERROR_NONE); cpl_test( cpl_error_get_code() == CPL_ERROR_NONE ); cpl_test( cpl_error_get_line() == 0 ); cpl_test( strlen(cpl_error_get_function()) == 0 ); cpl_test( strlen(cpl_error_get_file()) == 0 ); /* Test 1d: Verify that cpl_error_set_message() will not change that */ cpl_test( cpl_error_test_set_message(CPL_ERROR_NONE) == CPL_ERROR_NONE); cpl_test( cpl_error_get_code() == CPL_ERROR_NONE ); cpl_test( cpl_error_get_line() == 0 ); cpl_test( strlen(cpl_error_get_function()) == 0 ); cpl_test( strlen(cpl_error_get_file()) == 0 ); /* Do a number of tests on all (other) error codes */ for (ierror = CPL_ERROR_NONE; ierror <= CPL_ERROR_EOL+1; ierror++) { char msg[CPL_ERROR_MAX_MESSAGE_LENGTH]; unsigned line; if (ierror == CPL_ERROR_NONE) continue; /* Test 2: Verify that cpl_error_set_message() correctly sets the error */ cpl_test( cpl_error_test_set_message(ierror) == ierror); cpl_test( cpl_error_get_code() == ierror); cpl_test( cpl_error_get_line() > __LINE__ ); cpl_test( strcmp(cpl_error_get_file(), __FILE__) == 0 ); cpl_test( strcmp(cpl_error_get_function(), "hardcoded") == 0 ); strncpy(msg, cpl_error_get_message(), CPL_ERROR_MAX_MESSAGE_LENGTH); msg[CPL_ERROR_MAX_MESSAGE_LENGTH-1] = '\0'; cpl_test( strlen(msg) > 0 ); /* Test 3: Verify that cpl_error_set_message() correctly sets the error */ cpl_test( cpl_error_test_set_message_empty(ierror) == ierror); cpl_test( cpl_error_get_code() == ierror); cpl_test( cpl_error_get_line() > __LINE__ ); cpl_test( strcmp(cpl_error_get_file(), __FILE__) == 0 ); cpl_test( strcmp(cpl_error_get_function(), "hardcoded") == 0 ); cpl_test( strcmp(cpl_error_get_message(), cpl_error_get_message_default(ierror)) == 0 ); /* Test 5: Verify that cpl_error_reset() correctly resets the error */ cpl_error_reset(); cpl_test( cpl_error_get_code() == CPL_ERROR_NONE ); cpl_test( cpl_error_get_line() == 0 ); cpl_test( strlen(cpl_error_get_function()) == 0 ); cpl_test( strlen(cpl_error_get_file()) == 0 ); /* Test 6: Verify that cpl_ensure() correctly sets the error */ cpl_test( cpl_error_test_ensure(ierror) == ierror); cpl_test( cpl_error_get_code() == ierror); cpl_test( cpl_error_get_line() > __LINE__ ); cpl_test( strcmp(cpl_error_get_file(), __FILE__) == 0 ); cpl_test( strstr(msg, cpl_error_get_message()) == msg ); cpl_test( strlen(msg) > strlen(cpl_error_get_message()) ); if (has_func) cpl_test( strcmp(cpl_error_get_function(), "cpl_error_test_ensure") == 0 ); line = cpl_error_get_line(); /* Test 7: Verify that cpl_error_set_where() propagates correctly */ cpl_test( cpl_error_test_set_where() == ierror); cpl_test( cpl_error_get_code() == ierror); cpl_test( cpl_error_get_line() > __LINE__ ); cpl_test( cpl_error_get_line() != line ); cpl_test( strcmp(cpl_error_get_file(), __FILE__) == 0 ); cpl_test( strstr(msg, cpl_error_get_message()) == msg ); cpl_test( strlen(msg) > strlen(cpl_error_get_message()) ); if (has_func) cpl_test( strcmp(cpl_error_get_function(), "where") == 0 ); /* Test 8: Verify that cpl_error_set() correctly sets the error */ cpl_test( cpl_error_test_set(ierror) == ierror); cpl_test( cpl_error_get_code() == ierror); cpl_test( cpl_error_get_line() > __LINE__ ); cpl_test( strcmp(cpl_error_get_file(), __FILE__) == 0 ); cpl_test( strstr(msg, cpl_error_get_message()) == msg ); cpl_test( strlen(msg) > strlen(cpl_error_get_message()) ); if (has_func) cpl_test( strcmp(cpl_error_get_function(), "set") == 0 ); /* Test 9: Verify that cpl_error_reset() correctly resets the error */ cpl_error_reset(); cpl_test( cpl_error_get_code() == CPL_ERROR_NONE ); cpl_test( cpl_error_get_line() == 0 ); cpl_test( strlen(cpl_error_get_function()) == 0 ); cpl_test( strlen(cpl_error_get_file()) == 0 ); } /* End of actual test code */ return cpl_test_end(0); } /*----------------------------------------------------------------------------*/ /** @internal @brief Set the error using the supplied code and cpl_error_set() @param code The error code to set @return The error code */ /*----------------------------------------------------------------------------*/ static cpl_error_code cpl_error_test_set(cpl_error_code code) { return cpl_error_set("set", code); } /*----------------------------------------------------------------------------*/ /** @internal @brief Propagate the CPL error @return The error code */ /*----------------------------------------------------------------------------*/ static cpl_error_code cpl_error_test_set_where(void) { return cpl_error_set_where("where"); } /*----------------------------------------------------------------------------*/ /** @internal @brief Set the error using the supplied code and cpl_error_set() @param code The error code to set @return The error code */ /*----------------------------------------------------------------------------*/ static cpl_error_code cpl_error_test_set_message(cpl_error_code code) { return cpl_error_set_message("hardcoded", code, "Error-code=%u", (unsigned)code); } /*----------------------------------------------------------------------------*/ /** @internal @brief Set the error using the supplied code and cpl_error_set() @param code The error code to set @return The error code */ /*----------------------------------------------------------------------------*/ static cpl_error_code cpl_error_test_set_message_empty(cpl_error_code code) { return cpl_error_set_message("hardcoded", code, " "); } /*----------------------------------------------------------------------------*/ /** @internal @brief Set the error using the supplied code and cpl_error_ensure() @param code The error code to set @return The error code */ /*----------------------------------------------------------------------------*/ static cpl_error_code cpl_error_test_ensure(cpl_error_code code) { cpl_ensure(0, code, code); return CPL_ERROR_NONE; }