From 6f3043515a1746dabd01f2e4000a0c7dafa3938e Mon Sep 17 00:00:00 2001 From: mquinson Date: Sun, 23 Oct 2005 11:13:56 +0000 Subject: [PATCH] Convert modules 'config', 'ex' and 'cunit' to unit testing git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@1806 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- testsuite/xbt/config_usage.c | 123 ------------------- testsuite/xbt/ex_test.c | 231 ----------------------------------- 2 files changed, 354 deletions(-) delete mode 100644 testsuite/xbt/config_usage.c delete mode 100644 testsuite/xbt/ex_test.c diff --git a/testsuite/xbt/config_usage.c b/testsuite/xbt/config_usage.c deleted file mode 100644 index 95a82f5750..0000000000 --- a/testsuite/xbt/config_usage.c +++ /dev/null @@ -1,123 +0,0 @@ -/* $Id$ */ - -/* test config - test code to the config set */ - -/* Copyright (c) 2004 Martin Quinson. All rights reserved. */ - -/* This program is free software; you can redistribute it and/or modify it - * under the terms of the license (GNU LGPL) which comes with this package. */ - -#include -#include "gras.h" - -XBT_LOG_NEW_CATEGORY(test,"Logging for this test"); - -/*====[ Code ]===============================================================*/ -static xbt_cfg_t make_set(){ - xbt_cfg_t set=NULL; - - set = xbt_cfg_new(); - xbt_cfg_register_str(set,"speed:1_to_2_int"); - xbt_cfg_register_str(set,"hostname:1_to_1_string"); - xbt_cfg_register_str(set,"user:1_to_10_string"); - - return set; -} /* end_of_make_set */ - -int main(int argc, char **argv) { - xbt_ex_t e; - xbt_cfg_t set; - - char *str; - - xbt_init(&argc,argv); - - fprintf(stderr,"==== Alloc and free a config set.\n"); - set=make_set(); - xbt_cfg_set_parse(set, "hostname:veloce user:mquinson\nuser:oaumage\tuser:alegrand"); - xbt_cfg_dump("test set","",set); - xbt_cfg_free(&set); - xbt_cfg_free(&set); - - fprintf(stderr, "==== Validation test with too few values of 'speed'\n"); - set=make_set(); - xbt_cfg_set_parse(set, "hostname:veloce user:mquinson\nuser:oaumage\tuser:alegrand"); - TRY { - xbt_cfg_check(set); - } CATCH(e) { - if (e.category != mismatch_error || - strncmp(e.msg,"Config elem speed needs",strlen("Config elem speed needs"))) - RETHROW; - xbt_ex_free(e); - } - xbt_cfg_free(&set); - xbt_cfg_free(&set); - - fprintf(stderr,"==== Validation test with too much values of 'speed'\n"); - set=make_set(); - xbt_cfg_set_parse(set,"hostname:toto:42 user:alegrand"); - TRY { - xbt_cfg_set_parse(set,"speed:42 speed:24 speed:34"); - } CATCH(e) { - if (e.category != mismatch_error || - strncmp(e.msg,"Cannot add value 34 to the config elem speed", - strlen("Config elem speed needs"))) - RETHROW; - xbt_ex_free(e); - } - xbt_cfg_check(set); - xbt_cfg_free(&set); - xbt_cfg_free(&set); - - fprintf(stderr,"==== Get single value (Expected: 'speed value: 42')\n"); - { - /* get_single_value */ - int ival; - xbt_cfg_t myset=make_set(); - - xbt_cfg_set_parse(myset,"hostname:toto:42 speed:42"); - ival = xbt_cfg_get_int(myset,"speed"); - fprintf(stderr,"speed value: %d\n",ival); /* Prints: "speed value: 42" */ - xbt_cfg_free(&myset); - } - - fprintf(stderr,"==== Get multiple values (Expected: 'Count: 3; Options: mquinson;ecaron;alegrand;')\n"); - { - /* get_multiple_value */ - xbt_dynar_t dyn; - int ival; - xbt_cfg_t myset=make_set(); - - xbt_cfg_set_parse(myset, "hostname:veloce user:mquinson\nuser:oaumage\tuser:alegrand"); - xbt_cfg_set_parse(myset,"speed:42"); - xbt_cfg_check(myset); - dyn = xbt_cfg_get_dynar(myset,"user"); - fprintf(stderr,"Count: %lu; Options: ",xbt_dynar_length(dyn)); - xbt_dynar_foreach(dyn,ival,str) { - fprintf(stderr,"%s;",str); - } - fprintf(stderr,"\n"); - /* This prints: "Count: 3; Options: mquinson;ecaron;alegrand;" */ - xbt_cfg_free(&myset); - } - - fprintf(stderr,"==== Try to use an unregistered option. (ERROR EXPECTED: 'color' not registered)\n"); - { - xbt_cfg_t myset=make_set(); - TRY { - xbt_cfg_set_parse(myset,"color:blue"); - THROW1(mismatch_error,0,"Found an option which shouldn't be there (%s)","color:blue"); - } CATCH(e) { - if (e.category != not_found_error) - RETHROW; - xbt_ex_free(e); - } - /* This spits an error: 'color' not registered */ - xbt_cfg_free(&myset); - } - - fprintf(stderr,"==== Success\n"); - xbt_exit(); - return 0; -} - diff --git a/testsuite/xbt/ex_test.c b/testsuite/xbt/ex_test.c deleted file mode 100644 index 78b2f5797d..0000000000 --- a/testsuite/xbt/ex_test.c +++ /dev/null @@ -1,231 +0,0 @@ -/* -** OSSP ex - Exception Handling -** Copyright (c) 2002-2004 Ralf S. Engelschall -** Copyright (c) 2002-2004 The OSSP Project -** Copyright (c) 2002-2004 Cable & Wireless -** -** This file is part of OSSP ex, an exception handling library -** which can be found at http://www.ossp.org/pkg/lib/ex/. -** -** Permission to use, copy, modify, and distribute this software for -** any purpose with or without fee is hereby granted, provided that -** the above copyright notice and this permission notice appear in all -** copies. -** -** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED -** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. -** IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR -** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF -** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT -** OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -** SUCH DAMAGE. -** -** ex_test.c: exception handling test suite -*/ - -#include "xbt/cunit.h" -#include "xbt/ex.h" -#include "xbt/log.h" - -XBT_LOG_NEW_CATEGORY(test,"This test"); - -XBT_TEST_UNIT(test_expected_failure) { - xbt_test0("Skipped test"); - xbt_test_skip(); - - xbt_test0("EXPECTED FAILURE"); - xbt_test_expect_failure(); - xbt_test_log2("%s %s","Test","log"); - xbt_test_fail0("EXPECTED FAILURE"); -} - -XBT_TEST_UNIT(test_controlflow) { - xbt_ex_t ex; - volatile int n=1; - - xbt_test0("basic nested control flow"); - - TRY { - if (n != 1) - xbt_test_fail1("M1: n=%d (!= 1)", n); - n++; - TRY { - if (n != 2) - xbt_test_fail1("M2: n=%d (!= 2)", n); - n++; - THROW0(unknown_error,0,"something"); - } CATCH (ex) { - if (n != 3) - xbt_test_fail1("M3: n=%d (!= 1)", n); - n++; - RETHROW; - } - xbt_test_fail1("MX: n=%d (shouldn't reach this point)", n); - } - CATCH(ex) { - if (n != 4) - xbt_test_fail1("M4: n=%d (!= 4)", n); - n++; - xbt_ex_free(ex); - } - if (n != 5) - xbt_test_fail1("M5: n=%d (!= 5)", n); -} - -XBT_TEST_UNIT(test_value) { - xbt_ex_t ex; - - TRY { - THROW0(unknown_error, 2, "toto"); - } CATCH(ex) { - xbt_test0("exception value passing"); - if (ex.category != unknown_error) - xbt_test_fail1("category=%d (!= 1)", ex.category); - if (ex.value != 2) - xbt_test_fail1("value=%d (!= 2)", ex.value); - if (strcmp(ex.msg,"toto")) - xbt_test_fail1("message=%s (!= toto)", ex.msg); - xbt_ex_free(ex); - } -} - -XBT_TEST_UNIT(test_variables) { - xbt_ex_t ex; - int r1, r2; - volatile int v1, v2; - - r1 = r2 = v1 = v2 = 1234; - TRY { - r2 = 5678; - v2 = 5678; - THROW0(unknown_error, 0, "toto"); - } CATCH(ex) { - xbt_test0("variable preservation"); - if (r1 != 1234) - xbt_test_fail1("r1=%d (!= 1234)", r1); - if (v1 != 1234) - xbt_test_fail1("v1=%d (!= 1234)", v1); - /* r2 is allowed to be destroyed because not volatile */ - if (v2 != 5678) - xbt_test_fail1("v2=%d (!= 5678)", v2); - xbt_ex_free(ex); - } -} - -XBT_TEST_UNIT(test_cleanup) { - xbt_ex_t ex; - volatile int v1; - int c; - - xbt_test0("cleanup handling"); - - v1 = 1234; - c = 0; - TRY { - v1 = 5678; - THROW0(1, 2, "blah"); - } CLEANUP { - if (v1 != 5678) - xbt_test_fail1("v1 = %d (!= 5678)", v1); - c = 1; - } CATCH(ex) { - if (v1 != 5678) - xbt_test_fail1("v1 = %d (!= 5678)", v1); - if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.msg,"blah"))) - xbt_test_fail0("unexpected exception contents"); - xbt_ex_free(ex); - } - if (!c) - xbt_test_fail0("xbt_ex_free not executed"); -} - -int main(int argc, char *argv[]) { - xbt_test_suite_t suite; - - suite = xbt_test_suite_new("Testsuite Autotest"); - xbt_test_suite_push(suite, test_expected_failure, "expected failures"); - - suite = xbt_test_suite_new("Exception Handling"); - xbt_test_suite_push(suite, test_controlflow, "basic nested control flow"); - xbt_test_suite_push(suite, test_value, "exception value passing"); - xbt_test_suite_push(suite, test_variables, "variable value preservation"); - xbt_test_suite_push(suite, test_cleanup, "cleanup handling"); - - return xbt_test_run(); -} - - -/* - * The following is the example included in the documentation. It's a good - * idea to check its syntax even if we don't try to run it. - * And actually, it allows to put comments in the code despite doxygen. - */ -static char *mallocex(int size) { - return NULL; -} -#define SMALLAMOUNT 10 -#define TOOBIG 100000000 - -#if 0 /* this contains syntax errors, actually */ -static void bad_example(void) { - struct {char*first;} *globalcontext; - ex_t ex; - - /* BAD_EXAMPLE */ - TRY { - char *cp1, *cp2, *cp3; - - cp1 = mallocex(SMALLAMOUNT); - globalcontext->first = cp1; - cp2 = mallocex(TOOBIG); - cp3 = mallocex(SMALLAMOUNT); - strcpy(cp1, "foo"); - strcpy(cp2, "bar"); - } CLEANUP { - if (cp3 != NULL) free(cp3); - if (cp2 != NULL) free(cp2); - if (cp1 != NULL) free(cp1); - } CATCH(ex) { - printf("cp3=%s", cp3); - RETHROW; - } - /* end_of_bad_example */ -} -#endif - -static void good_example(void) { - struct {char*first;} *globalcontext; - xbt_ex_t ex; - - /* GOOD_EXAMPLE */ - { /*01*/ - char * volatile /*03*/ cp1 = NULL /*02*/; - char * volatile /*03*/ cp2 = NULL /*02*/; - char * volatile /*03*/ cp3 = NULL /*02*/; - TRY { - cp1 = mallocex(SMALLAMOUNT); - globalcontext->first = cp1; - cp1 = NULL /*05 give away*/; - cp2 = mallocex(TOOBIG); - cp3 = mallocex(SMALLAMOUNT); - strcpy(cp1, "foo"); - strcpy(cp2, "bar"); - } CLEANUP { /*04*/ - printf("cp3=%s", cp3 == NULL /*02*/ ? "" : cp3); - if (cp3 != NULL) - free(cp3); - if (cp2 != NULL) - free(cp2); - /*05 cp1 was given away */ - } CATCH(ex) { - /*05 global context untouched */ - RETHROW; - } - } - /* end_of_good_example */ -} -- 2.20.1