#include #include #include "ts.h" bool ts_test_adx( void ) { uint_t n = 14; uint_t k = 3; /* Monotonically rising data: +DM=1 each bar, -DM=0, TR=3 */ fp_t *x[4]; x[0] = malloc( n * sizeof( fp_t ) ); x[1] = malloc( n * sizeof( fp_t ) ); x[2] = malloc( n * sizeof( fp_t ) ); x[3] = malloc( n * sizeof( fp_t ) ); for ( uint_t i = 0; i < n; ++i ) { x[0][i] = 10 + i; x[1][i] = 12 + i; x[2][i] = 9 + i; x[3][i] = 11 + i; } /* Expected: +DI=100/3, -DI=0, DX=100, ADX=100 (converged EMAs) */ fp_t edp[] = { 100.0 / 3, 100.0 / 3, 100.0 / 3, 100.0 / 3, 100.0 / 3, 100.0 / 3, 100.0 / 3, 100.0 / 3, 100.0 / 3, 100.0 / 3 }; fp_t edm[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; fp_t edx[] = { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 }; fp_t ead[] = { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 }; fp_t *e[4] = { edp, edm, edx, ead }; uint_t offset = k + 1; uint_t y_n = n - offset; /* Allocate full-size output since the function writes to absolute * indices [offset..n-1], not compacted [0..y_n-1] */ fp_t *y[4]; for ( uint_t i = 0; i < 4; ++i ) y[i] = calloc( n, sizeof( fp_t ) ); uint8_t c[TS_CONSTANT_SIZE] = { 0 }; *( uint_t * )c = k; ts_adx(y, ts_adx_p.n_outputs, (fp_t const * const *)x, ts_adx_p.n_inputs, n, (ts_constant_t const *)c); bool result = true; for ( uint_t i = 0; i < 4; ++i ) { for ( uint_t j = 0; j < y_n; ++j ) { if ( fabs( y[i][offset + j] - e[i][j] ) > TS_TEST_EPSILON ) result = false; } } for ( uint_t i = 0; i < 4; ++i ) free( y[i] ); free( x[0] ); free( x[1] ); free( x[2] ); free( x[3] ); return result; }