summaryrefslogtreecommitdiff
path: root/src/c/elementaryFunctions/fix/testFloatFix.c
blob: 44d5596cd94995013e17adb645af61be03c9849f (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 *  Copyright (C) 2008 - INRIA - Arnaud TORSET
 *
 *  This file must be used under the terms of the CeCILL.
 *  This source file is licensed as described in the file COPYING, which
 *  you should have received as part of this distribution.  The terms
 *  are also available at
 *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
 *
 */

#include <assert.h>
#include <math.h>
#include <stdio.h>
#include "fix.h"


static void sfixsTest(void) {
	assert ((fabs( sfixs(-2) - (-2))/fabs(sfixs(-2)))<1e-16);	
	assert ((fabs( sfixs(-1.9f) - (-1))/fabs(sfixs(-1.9f)))<1e-16);		
	assert ((fabs( sfixs(-1.8f) - (-1))/fabs(sfixs(-1.8f)))<1e-16);	
	assert ((fabs( sfixs(-1.7f) - (-1))/fabs(sfixs(-1.7f)))<1e-16);	
	assert ((fabs( sfixs(-1.6f) - (-1))/fabs(sfixs(-1.6f)))<1e-16);	
	assert ((fabs( sfixs(-1.5f) - (-1))/fabs(sfixs(-1.5f)))<1e-16);	
	assert ((fabs( sfixs(-1.4f) - (-1))/fabs(sfixs(-1.4f)))<1e-16);	
	assert ((fabs( sfixs(-1.3f) - (-1))/fabs(sfixs(-1.3f)))<1e-16);	
	assert ((fabs( sfixs(-1.2f) - (-1))/fabs(sfixs(-1.2f)))<1e-16);	
	assert ((fabs( sfixs(-1.1f) - (-1))/fabs(sfixs(-1.1f)))<1e-16);	
	assert ((fabs( sfixs(-1.0f) - (-1))/fabs(sfixs(-1.0f)))<1e-16);		
	assert (fabs( sfixs(-.9f))<1e-16);	
	assert ((fabs( sfixs(1.0f) - (1))/fabs(sfixs(1.0f)))<1e-16);	
	assert ((fabs( sfixs(1.1f) - (1))/fabs(sfixs(1.1f)))<1e-16);	
	assert ((fabs( sfixs(1.2f) - (1))/fabs(sfixs(1.2f)))<1e-16);	
	assert ((fabs( sfixs(1.3f) - (1))/fabs(sfixs(1.3f)))<1e-16);	
	assert ((fabs( sfixs(1.4f) - (1))/fabs(sfixs(1.4f)))<1e-16);	
	assert ((fabs( sfixs(1.5f) - (1))/fabs(sfixs(1.5f)))<1e-16);	
	assert ((fabs( sfixs(1.6f) - (1))/fabs(sfixs(1.6f)))<1e-16);	
	assert ((fabs( sfixs(1.7f) - (1))/fabs(sfixs(1.7f)))<1e-16);	
	assert ((fabs( sfixs(1.8f) - (1))/fabs(sfixs(1.8f)))<1e-16);	
	assert ((fabs( sfixs(1.9f) - (1))/fabs(sfixs(1.9f)))<1e-16);	
	assert ((fabs( sfixs(2.0f) - (2))/fabs(sfixs(2.0f)))<1e-16);
}

static void cfixsTest(void) {
	floatComplex in, out;
	
	
	/* tests allant de -2 + i a -1 + 2*i, les reels décroissants de 0.1, les imaginaires croissant de 0.1
	+ 1 test supplementaire : -0.9 + 0.9*i*/
	
	in=FloatComplex(-2,1);
	out = cfixs(in);
	assert ((fabs( creals(out) - (-2))/fabs(creals(out)))<1e-16);	
	assert ((fabs( cimags(out) - (1))/fabs(cimags(out)))<1e-16);
	in=FloatComplex(-1.9f,1.1f);
	out = cfixs(in);	
	assert ((fabs( creals(out) - (-1))/fabs(creals(out)))<1e-16);	
	assert ((fabs( cimags(out) - (1))/fabs(cimags(out)))<1e-16);	
	in=FloatComplex(-1.8f,1.2f);
	out = cfixs(in);
	assert ((fabs( creals(out) - (-1))/fabs(creals(out)))<1e-16);
	assert ((fabs( cimags(out) - (1))/fabs(cimags(out)))<1e-16);
	in=FloatComplex(-1.7f,1.3f);
	out = cfixs(in);		
	assert ((fabs( creals(out) - (-1))/fabs(creals(out)))<1e-16);	
	assert ((fabs( cimags(out) - (1))/fabs(cimags(out)))<1e-16);
	in=FloatComplex(-1.6f,1.4f);
	out = cfixs(in);
	assert ((fabs( creals(out) - (-1))/fabs(creals(out)))<1e-16);		
	assert ((fabs( cimags(out) - (1))/fabs(cimags(out)))<1e-16);
	in=FloatComplex(-1.5f,1.5f);
	out = cfixs(in);	
	assert ((fabs( creals(out) - (-1))/fabs(creals(out)))<1e-16);		
	assert ((fabs( cimags(out) - (1))/fabs(cimags(out)))<1e-16);
	in=FloatComplex(-1.4f,1.6f);
	out = cfixs(in);	
	assert ((fabs( creals(out) - (-1))/fabs(creals(out)))<1e-16);	
	assert ((fabs( cimags(out) - (1))/fabs(cimags(out)))<1e-16);
	in=FloatComplex(-1.3f,1.7f);
	out = cfixs(in);
	assert ((fabs( creals(out) - (-1))/fabs(creals(out)))<1e-16);	
	assert ((fabs( cimags(out) - (1))/fabs(cimags(out)))<1e-16);
	in=FloatComplex(-1.2f,1.8f);
	out = cfixs(in);
	assert ((fabs( creals(out) - (-1))/fabs(creals(out)))<1e-16);	
	assert ((fabs( cimags(out) - (1))/fabs(cimags(out)))<1e-16);
	in=FloatComplex(-1.1f,1.9f);
	out = cfixs(in);
	assert ((fabs( creals(out) - (-1))/fabs(creals(out)))<1e-16);	
	assert ((fabs( cimags(out) - (1))/fabs(cimags(out)))<1e-16);
	in=FloatComplex(-1.0f,2.0f);
	out = cfixs(in);
	assert ((fabs( creals(out) - (-1))/fabs(creals(out)))<1e-16);	
	assert ((fabs( cimags(out) - (2))/fabs(cimags(out)))<1e-16);
	in=FloatComplex(-.9f,.9f);
	out = cfixs(in);	
	assert (fabs( creals(out))<1e-16);		
	assert (fabs( cimags(out))<1e-16);	
}

static void sfixaTest(void) {
	int i;
	float in[10]={1.2f, 2.0f,-2.0f,-3.1f,-4.5f,6.1f,4.1f,4.9f,-0.7f, 0.5f};
	float res[10]={1,2,-2,-3,-4,6,4,4,0,0};
	float out[10];
	
	sfixa(in,10,out);
	
	for (i=0;i<10;i++)
		if (out[i]!=0) assert(fabs(out[i]-res[i])/fabs(out[i]) <1e-16);
		else 	assert(fabs(out[i]-res[i]) <1e-16);
}

static void cfixaTest(void) {
	int i;
	float inR[10]={1.2f, 2.0f,-2.0f,-3.1f,-4.5f,6.1f,4.1f,4.9f,-0.7f, 0.5f};
	float inI[10]={5.4f,1.8f,1.4f,-4.0f,-2.7f,-0.3f,1.5f,1.4f,7.3f,-4.6f};
	float resR[10]={1,2,-2,-3,-4,6,4,4,0,0};
	float resI[10]={5,1,1,-4,-2,0,1,1,7,-4};
	floatComplex out[10],*in;
	
	in=FloatComplexMatrix(inR,inI,10);
	cfixa(in,10,out);
	
	for (i=0;i<10;i++){
		if (creals(out[i])!=0)	assert(fabs(creals(out[i])-resR[i])/fabs(creals(out[i])) <1e-16);
		else assert(fabs(creals(out[i])-resR[i]) <1e-16);
		if (cimags(out[i])!=0)	assert(fabs(cimags(out[i])-resI[i])/fabs(cimags(out[i])) <1e-16);
		else assert(fabs(cimags(out[i])-resI[i]) <1e-16);
	 }

}

static int testFix() {
  printf("\n>>>> Float Fix Tests\n");
  sfixsTest();
  cfixsTest();
  sfixaTest();
  cfixaTest();

  return 0;
}

int main() {
  assert(testFix() == 0);
  return 0;
}