Exemple #1
0
void _SwarmVariable_ValueAtInt( void* swarmVariable, Particle_Index lParticle_I, double* value ) {
	SwarmVariable*	self     = (SwarmVariable*)swarmVariable;
	Variable*       variable = self->variable;
	Dof_Index       dofCount = self->dofCount;
	Dof_Index       dof_I;

	for ( dof_I = 0 ; dof_I < dofCount ; dof_I++ ) {
		value[ dof_I ] = (double) Variable_GetValueAtInt( variable, lParticle_I, dof_I );
	}
}
void VariableSuite_TestVariable_Int( VariableSuiteData* data ) {
    typedef int Triple[3];

    int* array;
    Triple* structArray;
    Index length = 10;

    /* List of values to test the variable with.
     * Values to test are hex 5's and a's because they are a series of 0101 and 1010 respectively so they test
     * each bit in memory to read/set.
     */
    long int testValues[] = { 0x55555555, 0xaaaaaaaa };
    Index testValueCount = 2;
    Index test_I;
    long int testValue;

    Variable* var;
    Variable* vec;
    Variable* vecVar[3];

    int i, j;

    array = Memory_Alloc_Array( int, length, "test" );
    structArray = Memory_Alloc_Array( Triple, length, "test" );

    var = Variable_NewScalar( "Int-Scalar", NULL, Variable_DataType_Int, &length, NULL, (void**)&array, data->vr );
    vec = Variable_NewVector( "Int-Three", NULL, Variable_DataType_Int, 3, &length, NULL, (void**)&structArray, data->vr, "a", "b", "c" );

    vecVar[0] = Variable_Register_GetByName( data->vr, "a" );
    vecVar[1] = Variable_Register_GetByName( data->vr, "b" );
    vecVar[2] = Variable_Register_GetByName( data->vr, "c" );

    Variable_Register_BuildAll( data->vr );

    for ( test_I = 0; test_I < testValueCount; ++test_I ) {

        testValue = testValues[test_I];

        for ( i = 0; i < length; ++i ) {
            Variable_SetValueInt( var, i, testValue );

            Variable_SetValueAtInt( vec, i, 0, testValue );
            Variable_SetValueAtInt( vec, i, 1, testValue );
            Variable_SetValueAtInt( vec, i, 2, testValue );
        }

        /*~~~Scalar~~~*/
        for ( i = 0; i < length; ++i ) {
            pcu_check_true( Variable_GetValueInt( var, i ) == (int)(int)testValue );
            pcu_check_true( Variable_GetValueIntAsChar( var, i ) == (char)(int)testValue );
            pcu_check_true( Variable_GetValueIntAsShort( var, i ) == (short)(int)testValue );
            pcu_check_true( fabsf(Variable_GetValueIntAsFloat( var, i ) - (float)(int)testValue) < fabsf(0.00001*testValue) );
            pcu_check_true( fabs(Variable_GetValueIntAsDouble( var, i ) - (double)(int)testValue) < fabs(0.00001*testValue) );
        }

        /*~~~Vector~~~*/
        for ( i = 0; i < length; ++i ) {
            pcu_check_true( Variable_GetValueAtInt( vec, i, 0 ) == (int)(int)testValue );
            pcu_check_true( Variable_GetValueAtIntAsChar( vec, i, 0 ) == (char)(int)testValue );
            pcu_check_true( Variable_GetValueAtIntAsShort( vec, i, 0 ) == (short)(int)testValue );
            pcu_check_true( fabsf(Variable_GetValueAtIntAsFloat( vec, i, 0 ) - (float)(int)testValue) < fabsf(0.00001*testValue) );
            pcu_check_true( fabs(Variable_GetValueAtIntAsDouble( vec, i, 0 ) - (double)(int)testValue) < fabs(0.00001*testValue));
            pcu_check_true( Variable_GetPtrAtInt( vec, i, 0 ) == &structArray[i][0] );

            pcu_check_true( Variable_GetValueAtInt( vec, i, 1 ) == (int)(int)testValue );
            pcu_check_true( Variable_GetValueAtIntAsChar( vec, i, 1 ) == (char)(int)testValue );
            pcu_check_true( Variable_GetValueAtIntAsShort( vec, i, 1 ) == (short)(int)testValue );
            pcu_check_true( fabsf(Variable_GetValueAtIntAsFloat( vec, i, 1 ) - (float)(int)testValue) < fabsf(0.00001*testValue) );
            pcu_check_true( fabs(Variable_GetValueAtIntAsDouble( vec, i, 1 ) - (double)(int)testValue) < fabs(0.00001*testValue));
            pcu_check_true( Variable_GetPtrAtInt( vec, i, 1 ) == &structArray[i][1] );

            pcu_check_true( Variable_GetValueAtInt( vec, i, 2 ) == (int)(int)testValue );
            pcu_check_true( Variable_GetValueAtIntAsChar( vec, i, 2 ) == (char)(int)testValue );
            pcu_check_true( Variable_GetValueAtIntAsShort( vec, i, 2 ) == (short)(int)testValue );
            pcu_check_true( fabsf(Variable_GetValueAtIntAsFloat( vec, i, 2 ) - (float)(int)testValue) < fabsf(0.00001*testValue));
            pcu_check_true( fabs(Variable_GetValueAtIntAsDouble( vec, i, 2 ) - (double)(int)testValue) < fabs(0.0001*testValue));
            pcu_check_true( Variable_GetPtrAtInt( vec, i, 2 ) == &structArray[i][2] );
        }

        /*~~~Vector: Sub-Variable~~~*/
        for ( i = 0; i < length; ++i ) {
            for ( j = 0; j < 3; ++j ) {
                pcu_check_true( Variable_GetStructPtr( vecVar[j], i ) == &structArray[i][j] );
            }
        }
    }
}