root/test/support/support.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. test_init
  2. test_success
  3. test_failure
  4. test_verify_str
  5. test_verify_int
  6. test_finalize
  7. test_comment
  8. test_fail_stop

   1 /*
   2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
   3  *                         University Research and Technology
   4  *                         Corporation.  All rights reserved.
   5  * Copyright (c) 2004-2005 The University of Tennessee and The University
   6  *                         of Tennessee Research Foundation.  All rights
   7  *                         reserved.
   8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
   9  *                         University of Stuttgart.  All rights reserved.
  10  * Copyright (c) 2004-2005 The Regents of the University of California.
  11  *                         All rights reserved.
  12  * Copyright (c) 2010      Cisco Systems, Inc.  All rights reserved.
  13  * $COPYRIGHT$
  14  *
  15  * Additional copyrights may follow
  16  *
  17  * $HEADER$
  18  */
  19 
  20 #include "opal_config.h"
  21 #include <stdlib.h>
  22 #include <string.h>
  23 #include <assert.h>
  24 #include <stdio.h>
  25 
  26 #include "support.h"
  27 
  28 /**
  29  * A testing support library to provide uniform reporting output
  30  */
  31 
  32 static int opal_n_tests;
  33 static int opal_n_success;
  34 static int opal_n_failures;
  35 static char *opal_description;
  36 
  37 void test_init(const char *a)
  38 {
  39     /* local variables */
  40     size_t len;
  41 
  42     /* save the descriptive string */
  43     len = strlen(a);
  44     opal_description = (char *) malloc(len + 1);
  45     assert(opal_description);
  46 
  47     strcpy(opal_description, a);
  48 
  49     /* initialize counters */
  50     opal_n_tests = 0;
  51     opal_n_success = 0;
  52     opal_n_failures = 0;
  53 
  54     return;
  55 
  56 }
  57 
  58 
  59 void test_success(void)
  60 {
  61     opal_n_tests++;
  62     opal_n_success++;
  63 }
  64 
  65 
  66 void test_failure(const char *a)
  67 {
  68     opal_n_tests++;
  69     opal_n_failures++;
  70 
  71     fprintf(stderr, " Failure : ");
  72     fprintf(stderr, "%s", a);
  73     fprintf(stderr, "\n");
  74     fflush(stderr);
  75 }
  76 
  77 
  78 int test_verify_str(const char *expected_result, const char *test_result)
  79 {
  80     size_t len_expect, len_result;
  81     int return_value;
  82 
  83     return_value = 1;
  84     len_expect = expected_result ? strlen(expected_result) : 0;
  85     len_result = test_result ? strlen(test_result) : 0;
  86 
  87     if ((!(len_expect == len_result)) ||
  88         (0 != strcmp(expected_result, test_result))) {
  89         test_failure("Comparison failure");
  90         fprintf(stderr, " Expected result: %s\n", expected_result);
  91         fprintf(stderr, " Test result: %s\n", test_result);
  92         fflush(stderr);
  93         return_value = 0;
  94     } else {
  95         test_success();
  96     }
  97 
  98     return return_value;
  99 }
 100 
 101 
 102 int test_verify_int(int expected_result, int test_result)
 103 {
 104     int return_value;
 105 
 106     return_value = 1;
 107     if (expected_result != test_result) {
 108         test_failure("Comparison failure");
 109         fprintf(stderr, " Expected result: %d\n", expected_result);
 110         fprintf(stderr, " Test result: %d\n", test_result);
 111         fflush(stderr);
 112         return_value = 0;
 113     } else {
 114         test_success();
 115     }
 116 
 117     return return_value;
 118 }
 119 
 120 
 121 int test_finalize(void)
 122 {
 123     int return_value;
 124 
 125     return_value = 0;
 126 
 127     if (opal_n_tests == opal_n_success) {
 128         fprintf(stderr, "SUPPORT: OMPI Test Passed: %s: (%d tests)\n",
 129                 opal_description, opal_n_tests);
 130         fflush(stderr);
 131     } else {
 132         fprintf(stderr,
 133                 "SUPPORT: OMPI Test failed: %s (%d of %d failed)\n",
 134                 opal_description, opal_n_failures, opal_n_tests);
 135         fflush(stderr);
 136         return_value = 1;
 137     }
 138 
 139     if (NULL != opal_description)
 140         free(opal_description);
 141 
 142     return return_value;
 143 }
 144 
 145 
 146 /* note this is for additional output that does NOT go to STDERR but STDOUT */
 147 void test_comment (const char* userstr)
 148 {
 149         fprintf(stdout, "%s:%s\n", opal_description, userstr);
 150 }
 151 
 152 
 153 void test_fail_stop(const char *msg, int status)
 154 {
 155     test_failure(msg);
 156     test_finalize();
 157     exit(status);
 158 }

/* [<][>][^][v][top][bottom][index][help] */