summaryrefslogtreecommitdiffstats
path: root/lib/test.c
blob: d022f6ed272385fb3e8eb97d39896e481948e20c (plain)
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
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include "ts.h"


void _ts_assert_approximate( fp_t y0, fp_t y1, fp_t e )
{
	assert( ( fabs( y0 - y1 ) <= e ) );
}


bool _ts_is_approximate( fp_t y0, fp_t y1, fp_t e )
{
    return ( fabs( y0 - y1 ) <= e );
}


#define _ts_test_print( FUNCTION )                        \
                                                          \
    fputs( #FUNCTION, stdout );                           \
    fputs( ": ", stdout )


void ts_test_all( void )
{
	ts_test_absolute_value();
	ts_test_addition();
	ts_test_ceiling();
	
}

bool ts_test(
	int ( *ts_function )(
        fp_t * const *y,
	    uint_t y_n,
	    fp_t const * const *x,
	    uint_t x_n,
		uint_t n,
		ts_constant_t const *c
    ),
    uint_t ( *ts_offset )(
        ts_constant_t const *c
    ),
	fp_t const * const *x,
	fp_t const * const *e,
	uint_t n,
	ts_constant_t const *c,
    ts_parameter_t const *m )
{
	if ( !ts_function || !ts_offset || !m || !e || !x ) return false;

	uint_t offset = ts_offset( c );
	if ( offset >= n ) return false;
	uint_t y_len = n - offset;
    bool result = true;

	fp_t *y[m->n_outputs];

    for( uint_t i = 0; i < m->n_outputs; i++ )
    {
        fp_t *tmp = calloc( y_len, sizeof( fp_t ) );
        if ( !tmp )
        {
            for ( uint_t j = 0; j < i; j++ ) free( y[j] );
            return false;
        }
        y[i] = tmp;
    }

	if ( ts_function( y, m->n_outputs, x, m->n_inputs, n, c ) != 0 )
	{
		for ( uint_t i = 0; i < m->n_outputs; i++ ) free( y[i] );
		return false;
	}

    for( uint_t i = 0; i < m->n_outputs; ++i )
    {
    	for( uint_t j = 0; j < y_len; ++j )
	    {
		    result &= _ts_is_approximate( y[i][j], e[i][j], TS_TEST_EPSILON );
	    }

        free( y[i] );
    }

    return result;
}