root/test/class/opal_pointer_array.c

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

DEFINITIONS

This source file includes following definitions.
  1. test
  2. main

   1 /* -*- Mode: C; c-basic-offset:4 ; -*- */
   2 /*
   3  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
   4  *                         University Research and Technology
   5  *                         Corporation.  All rights reserved.
   6  * Copyright (c) 2004-2017 The University of Tennessee and The University
   7  *                         of Tennessee Research Foundation.  All rights
   8  *                         reserved.
   9  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
  10  *                         University of Stuttgart.  All rights reserved.
  11  * Copyright (c) 2004-2005 The Regents of the University of California.
  12  *                         All rights reserved.
  13  * Copyright (c) 2010      Cisco Systems, Inc.  All rights reserved.
  14  * $COPYRIGHT$
  15  *
  16  * Additional copyrights may follow
  17  *
  18  * $HEADER$
  19  */
  20 
  21 /*
  22  * This test is intended to test the opal_pointer_array
  23  *   class
  24  */
  25 
  26 #include "opal_config.h"
  27 #include <stdlib.h>
  28 #include <stdio.h>
  29 #include <assert.h>
  30 #include <string.h>
  31 
  32 #include "support.h"
  33 #include "opal/class/opal_pointer_array.h"
  34 
  35 typedef union {
  36     int ivalue;
  37     char *cvalue;
  38 } value_t;
  39 
  40 static void test(bool thread_usage){
  41 
  42     /* local variables */
  43     opal_pointer_array_t *array;
  44     value_t *test_data;
  45     int len_test_data,i,test_len_in_array,error_cnt;
  46     int ele_index;
  47     int error_code;
  48     value_t value;
  49 
  50     /* initialize thread levels */
  51     opal_set_using_threads(thread_usage);
  52 
  53     array=OBJ_NEW(opal_pointer_array_t);
  54     assert(array);
  55 
  56     len_test_data=5;
  57     test_data=malloc(sizeof(value_t)*len_test_data);
  58     assert(test_data);
  59 
  60     for(i=0 ; i < len_test_data ; i++ ) {
  61         test_data[i].ivalue = (i+1);
  62     }
  63 
  64     /* add data to table */
  65     test_len_in_array=3;
  66     assert(len_test_data>=test_len_in_array);
  67     for(i=0 ; i < test_len_in_array ; i++ ) {
  68         opal_pointer_array_add(array,test_data[i].cvalue);
  69     }
  70     /* check to see that test_len_in_array are in array */
  71     if( (array->size - array->number_free) == test_len_in_array) {
  72         test_success();
  73     } else {
  74         test_failure("check on number of elements in array");
  75     }
  76 
  77     /* check order of data */
  78     error_cnt=0;
  79     for(i=0 ; i < test_len_in_array ; i++ ) {
  80         value.cvalue = array->addr[i];
  81         if( (i+1) != value.ivalue ) {
  82             error_cnt++;
  83         }
  84     }
  85     if( 0 == error_cnt ) {
  86         test_success();
  87     } else {
  88         test_failure(" data check ");
  89     }
  90 
  91     /* free 2nd element and make sure that value is reset correctly,
  92      *   and that the lowest_index is also reset correctly */
  93     ele_index=1;
  94     error_code=opal_pointer_array_set_item(array,ele_index,NULL);
  95     if( 0 == error_code ) {
  96         test_success();
  97     } else {
  98         test_failure(" opal_pointer_array_set_item ");
  99     }
 100     if( NULL == array->addr[ele_index]){
 101         test_success();
 102     } else {
 103         test_failure(" set pointer value");
 104     }
 105     if( ele_index == array->lowest_free ) {
 106         test_success();
 107     } else {
 108         test_failure(" lowest free ");
 109     }
 110 
 111     /* test opal_pointer_array_get_item */
 112     opal_pointer_array_remove_all(array);
 113     error_cnt=0;
 114     for(i=0 ; i < array->size ; i++ ) {
 115         value.ivalue = i + 2;
 116         ele_index=opal_pointer_array_add(array, value.cvalue);
 117         if( i != ele_index ) {
 118             error_cnt++;
 119         }
 120     }
 121     if( 0 == error_cnt ) {
 122         test_success();
 123     } else {
 124         test_failure(" opal_pointer_array_add 2nd ");
 125     }
 126 
 127     error_cnt=0;
 128     for(i=0 ; i < array->size ; i++ ) {
 129         value.cvalue = opal_pointer_array_get_item(array,i);
 130         if( (i+2) != value.ivalue ) {
 131             error_cnt++;
 132         }
 133     }
 134     if( 0 == error_cnt ) {
 135         test_success();
 136     } else {
 137         test_failure(" data check - 2nd ");
 138     }
 139 
 140     OBJ_RELEASE(array);
 141     assert(NULL == array);
 142 
 143     array=OBJ_NEW(opal_pointer_array_t);
 144     assert(array);
 145     opal_pointer_array_init(array, 0, 4, 2);
 146     for( i = 0; i < 4; i++ ) {
 147         value.ivalue = i + 1;
 148         if( 0 > opal_pointer_array_add( array, value.cvalue ) ) {
 149             test_failure("Add/Remove: failure during initial data_add ");
 150         }
 151     }
 152     for( i = i-1; i >= 0; i-- ) {
 153         if( i % 2 )
 154             if( 0 != opal_pointer_array_set_item(array, i, NULL) )
 155                 test_failure("Add/Remove: failure during item removal ");
 156     }
 157     for( i = 0; i < 4; i++ ) {
 158         if( !opal_pointer_array_add( array, (void*)(uintptr_t)(i+1) ) ) {
 159             if( i != 2 ) {
 160                 test_failure("Add/Remove: failure during the readd ");
 161                 break;
 162             }
 163         }
 164     }
 165     opal_pointer_array_remove_all(array);
 166     OBJ_RELEASE(array);
 167     assert(NULL == array);
 168 
 169     free(test_data);
 170 }
 171 
 172 
 173 int main(int argc, char **argv)
 174 {
 175     test_init("opal_pointer_array");
 176 
 177     /* run through tests with thread usage set to false */
 178     test(false);
 179 
 180     /* run through tests with thread usage set to true */
 181     test(true);
 182 
 183     return test_finalize();
 184 }

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