1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
/* $Id: cpl_errorstate-test.c,v 1.20 2010/11/16 08:47:14 llundin Exp $
*
* This file is part of the ESO Common Pipeline Library
* Copyright (C) 2001-2008 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* $Author: llundin $
* $Date: 2010/11/16 08:47:14 $
* $Revision: 1.20 $
* $Name: cpl-6_1_1 $
*/
/*-----------------------------------------------------------------------------
Includes
-----------------------------------------------------------------------------*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cpl_error.h>
#include <cpl_msg.h>
#include <cpl_test.h>
#include <cpl_memory.h>
#include <cpl_error_impl.h>
#include <cpl_errorstate.h>
#include <string.h>
/*-----------------------------------------------------------------------------
Private functions and their variables
-----------------------------------------------------------------------------*/
static void my_error_counter(unsigned, unsigned, unsigned);
static unsigned nerrors;
static cpl_boolean did_dump;
/*-----------------------------------------------------------------------------
Main
-----------------------------------------------------------------------------*/
int main(void)
{
cpl_error_code ierror;
cpl_errorstate state_none;
cpl_errorstate state, state1;
/* In order to produce a sequence of unique CPL errors,
each call to cpl_error_set() is done with its own error code which
is guatanteed not to originate from within CPL itself. */
unsigned icode, ncode = (unsigned)CPL_ERROR_EOL;
unsigned i;
cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_OFF);
state_none = cpl_errorstate_get();
/* Insert tests here */
/* This is a test for DFS05408 */
/* Set some errors */
for (i=0; i < CPL_ERROR_HISTORY_SIZE/2; i++) {
cpl_error_set(cpl_func, i + ncode);
}
/* Be able to recover to have only these errors */
state1 = cpl_errorstate_get();
/* Overflow the error history */
for (i=0; i < 2 * CPL_ERROR_HISTORY_SIZE; i++) {
cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
}
/* Recover to the original errors - the details of which have been lost */
cpl_errorstate_set(state1);
/* The dump should provide details only for the last error */
cpl_errorstate_dump(state_none, CPL_FALSE, NULL);
cpl_test_eq(cpl_error_get_code(), CPL_ERROR_HISTORY_LOST);
/* Append one new error */
cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT);
/* The dump should provide details only for the last error */
cpl_errorstate_dump(state_none, CPL_FALSE, NULL);
cpl_test_eq(cpl_error_get_code(), CPL_ERROR_ILLEGAL_OUTPUT);
cpl_errorstate_set(state1);
/* The dump should provide no details */
cpl_errorstate_dump(state_none, CPL_FALSE, NULL);
cpl_test_eq(cpl_error_get_code(), CPL_ERROR_HISTORY_LOST);
cpl_errorstate_set(state_none);
/* Test 1: Verify that the error state is empty on start-up */
cpl_test( cpl_errorstate_is_equal(state_none) );
/* Test 1a: Verify that the current error cannot be used to
modify the error state*/
cpl_errorstate_set(state_none);
cpl_test( cpl_errorstate_is_equal(state_none) );
cpl_errorstate_dump(state_none, CPL_FALSE, NULL);
nerrors = 0; did_dump = CPL_FALSE;
cpl_errorstate_dump(state_none, CPL_FALSE, my_error_counter);
cpl_test_eq( nerrors, 1 );
cpl_test_eq( did_dump, CPL_FALSE );
/* Test 2a: Detect a change in error state */
cpl_error_set_message(cpl_func, ++ncode, "Hello, world");
cpl_test_zero( cpl_errorstate_is_equal(state_none) );
cpl_errorstate_dump(state_none, CPL_FALSE, NULL);
nerrors = 0; did_dump = CPL_FALSE;
cpl_errorstate_dump(state_none, CPL_FALSE, my_error_counter);
cpl_test_eq( nerrors, 1 );
cpl_test_eq( did_dump, CPL_TRUE );
/* Test 2b: Try to dump zero errors with an existing errorstate */
cpl_errorstate_dump(cpl_errorstate_get(), CPL_FALSE, NULL);
nerrors = 0; did_dump = CPL_FALSE;
cpl_errorstate_dump(cpl_errorstate_get(), CPL_FALSE, my_error_counter);
cpl_test_eq( nerrors, 1 );
cpl_test_eq( did_dump, CPL_FALSE );
/* Add some errors without filling up the error history */
for (ierror=1; ierror < CPL_ERROR_HISTORY_SIZE/2; ierror++) {
++ncode;
cpl_error_set_message(cpl_func, ncode, "Error-code=%u",
(unsigned)ncode);
cpl_test_eq( ncode, cpl_error_get_code() );
}
/* Preserve that error for later tests */
state = cpl_errorstate_get();
icode = ncode;
cpl_errorstate_dump(state_none, CPL_FALSE, NULL);
nerrors = 0; did_dump = CPL_FALSE;
cpl_errorstate_dump(state_none, CPL_FALSE, my_error_counter);
cpl_test_eq( nerrors, CPL_ERROR_HISTORY_SIZE/2 );
cpl_test_eq( did_dump, CPL_TRUE );
/* Test 3: Fill up the error history and check the 1st error */
for (ierror=1; ierror < CPL_ERROR_HISTORY_SIZE; ierror++) {
++ncode;
cpl_error_set_message(cpl_func, ncode, "Error-code=%u",
(unsigned)ncode);
cpl_test_eq( ncode, cpl_error_get_code() );
}
cpl_test_eq( ncode + 1, icode + CPL_ERROR_HISTORY_SIZE );
/* - error state is still non-empty */
cpl_test_zero( cpl_errorstate_is_equal(state_none) );
cpl_errorstate_dump(state_none, CPL_FALSE, NULL);
nerrors = 0; did_dump = CPL_FALSE;
cpl_errorstate_dump(state_none, CPL_FALSE, my_error_counter);
cpl_test_eq( nerrors, CPL_ERROR_HISTORY_SIZE/2
+ CPL_ERROR_HISTORY_SIZE - 1);
cpl_test_eq( did_dump, CPL_TRUE );
/* - different from the 1st error */
cpl_test_zero( cpl_errorstate_is_equal(state) );
cpl_errorstate_dump(state, CPL_FALSE, NULL);
nerrors = 0; did_dump = CPL_FALSE;
cpl_errorstate_dump(state, CPL_FALSE, my_error_counter);
cpl_test_eq( nerrors, CPL_ERROR_HISTORY_SIZE - 1);
cpl_test_eq( did_dump, CPL_TRUE );
cpl_errorstate_set(state);
cpl_errorstate_dump(state, CPL_FALSE, NULL);
nerrors = 0; did_dump = CPL_FALSE;
cpl_errorstate_dump(state, CPL_FALSE, my_error_counter);
cpl_test_eq( nerrors, 1);
cpl_test_eq( did_dump, CPL_FALSE );
/* - the details of that 1st error are preserved */
cpl_test_eq( cpl_error_get_code(), icode );
cpl_test( cpl_error_get_line() != 0 );
cpl_test_zero( strcmp(cpl_error_get_file(), __FILE__) );
cpl_test_zero( strcmp(cpl_error_get_function(), cpl_func) );
/* Set two new, 2nd and 3rd errors, discarding all errors set
after 'state'. */
++ncode;
cpl_error_set_message(cpl_func, ncode, "Error-code=%u", (unsigned)ncode);
state1 = cpl_errorstate_get();
++ncode;
cpl_error_set_message(cpl_func, ncode, "Error-code=%u", (unsigned)ncode);
nerrors = 0; did_dump = CPL_FALSE;
cpl_errorstate_dump(state, CPL_FALSE, my_error_counter);
cpl_test_eq( nerrors, 2);
cpl_test_eq( did_dump, CPL_TRUE );
/* Preserve that error for later tests */
state = cpl_errorstate_get();
icode = ncode;
cpl_errorstate_dump(state_none, CPL_FALSE, NULL);
nerrors = 0; did_dump = CPL_FALSE;
cpl_errorstate_dump(state_none, CPL_FALSE, my_error_counter);
cpl_test_eq( nerrors, CPL_ERROR_HISTORY_SIZE/2 + 2);
cpl_test_eq( did_dump, CPL_TRUE );
/* Test 4: Fill up the error history and check that error */
for (ierror=1; ierror < CPL_ERROR_HISTORY_SIZE; ierror++) {
++ncode;
cpl_error_set_message(cpl_func, ncode, "Error-code=%u",
(unsigned)ncode);
cpl_test_eq( ncode, cpl_error_get_code() );
}
cpl_test_eq( ncode + 1, icode + CPL_ERROR_HISTORY_SIZE );
/* - error state is still non-empty */
cpl_test_zero( cpl_errorstate_is_equal(state_none) );
/* - different from the 3rd error */
cpl_test_zero( cpl_errorstate_is_equal(state) );
cpl_errorstate_dump(state_none, CPL_FALSE, NULL);
nerrors = 0; did_dump = CPL_FALSE;
cpl_errorstate_dump(state_none, CPL_FALSE, my_error_counter);
cpl_test_eq( nerrors, CPL_ERROR_HISTORY_SIZE/2
+ CPL_ERROR_HISTORY_SIZE + 1);
cpl_test_eq( did_dump, CPL_TRUE );
cpl_errorstate_set(state);
/* - the details of that 3rd error are preserved */
cpl_test_eq( cpl_error_get_code(), icode );
cpl_test( cpl_error_get_line() != 0 );
cpl_test_zero( strcmp(cpl_error_get_file(), __FILE__) );
cpl_test_zero( strcmp(cpl_error_get_function(), cpl_func) );
/* Test 5: Fill the error history and check the 3rd error */
for (ierror=1; ierror < CPL_ERROR_HISTORY_SIZE; ierror++) {
++ncode;
cpl_error_set_message(cpl_func, ncode, "Error-code=%u",
(unsigned)ncode);
cpl_test_eq( ncode, cpl_error_get_code() );
}
cpl_test_eq( ncode + 2, icode + 2 * CPL_ERROR_HISTORY_SIZE );
/* - and error state is still non-empty */
cpl_test_zero( cpl_errorstate_is_equal(state_none) );
/* - and different from the 3rd error */
cpl_test_zero( cpl_errorstate_is_equal(state) );
cpl_errorstate_dump(state_none, CPL_TRUE, NULL);
nerrors = 0; did_dump = CPL_FALSE;
cpl_errorstate_dump(state_none, CPL_TRUE, my_error_counter);
cpl_test_eq( nerrors, CPL_ERROR_HISTORY_SIZE/2
+ CPL_ERROR_HISTORY_SIZE + 1);
cpl_test_eq( did_dump, CPL_TRUE );
/* Test 6: Set the error state back to that 3rd error */
cpl_errorstate_set(state);
/* - error state is still non-empty */
cpl_test_zero( cpl_errorstate_is_equal(state_none) );
/* - and distinguiable from the 2nd error */
cpl_test_zero( cpl_errorstate_is_equal(state1) );
/* - and the details of that 3rd error are preserved */
cpl_test_eq( cpl_error_get_code(), icode );
cpl_test( cpl_error_get_line() != 0);
cpl_test_zero( strcmp(cpl_error_get_file(), __FILE__) );
cpl_test_zero( strcmp(cpl_error_get_function(), cpl_func) );
/* Test 7: Verify that the details of the previous, 2nd error have
been lost */
cpl_errorstate_set(state1);
/* - error state is still non-empty */
cpl_test_zero( cpl_errorstate_is_equal(state_none) );
/* - and distinguiable from the 3rd error */
cpl_test_zero( cpl_errorstate_is_equal(state) );
/* - but the details of that 2nd error has been lost */
cpl_test_eq( cpl_error_get_code(), CPL_ERROR_HISTORY_LOST );
cpl_test_zero( cpl_error_get_line());
cpl_test_zero( strlen(cpl_error_get_file()) );
cpl_test_zero( strlen(cpl_error_get_function()) );
/* Test 8: Verify that error state cannot be advanced */
cpl_errorstate_set(state);
cpl_test( cpl_errorstate_is_equal(state1) );
/* Test 8: Verify that error state can be cleared regardless */
cpl_errorstate_set(state_none);
cpl_test( cpl_errorstate_is_equal(state_none) );
/* All tests are finished */
return cpl_test_end(0);
}
/*----------------------------------------------------------------------------*/
/**
@internal
@brief Count the number of errors dumped
@param self The number of the current error to be dumped
@param first The number of the first error to be dumped
@param last The number of the last error to be dumped
@note Verifies also that the dumped errors are consequtive.
Tries also to modify the CPL error state; if that were possible
the counting should be corrupted.
*/
/*----------------------------------------------------------------------------*/
static void my_error_counter(unsigned self, unsigned first, unsigned last) {
static cpl_errorstate prevstate; /* Don't try this at home */
static unsigned preverror;
if (self != 0) {
if (self != first) {
/* Error dump must be in steps of 1 */
if (first < last) {
cpl_test_eq( self, preverror + 1 );
} else {
cpl_test_eq( self, preverror - 1 );
}
/* Verify that the CPL error state cannot be modified from here */
cpl_errorstate_set(prevstate);
}
preverror = self;
prevstate = cpl_errorstate_get();
}
/* Verify that the CPL error state cannot be modified from here */
cpl_error_reset();
(void)cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT);
cpl_error_set_where(cpl_func);
nerrors++;
if (first != 0 || last != 0) did_dump = CPL_TRUE;
}
|