summaryrefslogtreecommitdiff
path: root/src/c/auxiliaryFunctions/sign/testSign.c
blob: 7f23f8f38a6c8875c9002cef32ba52e0248931d6 (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
/*
 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 *  Copyright (C) 2008-2008 - INRIA - Bruno JOFRET
 *
 *  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
 *
 */

/*
** FIXME : Add some array tests !
*/

#include "testSign.h"

int ssignsTest() {

  float positive = 0.123f;
  float negative = -0.123f;
  float zero = 0.0f;

  printf(">> Float \n");
  assert(ssigns(positive) == 1);
  assert(ssigns(negative) == -1);
  assert(ssigns(zero) == 0);
  return 0;
}

int dsignsTest() {

  double positive = 0.123;
  double negative = -0.123;
  double zero = 0;

  printf(">> Double\n");
  assert(dsigns(positive) == 1);
  assert(dsigns(negative) == -1);
  assert(dsigns(zero) == 0);
  return 0;
}

int csignsTest() {
  floatComplex zero = FloatComplex(0., 0.);
  floatComplex i = FloatComplex(0., 1.);
  floatComplex oneAndI = FloatComplex(1., 1.);
  floatComplex sign_i = csigns(i);
  floatComplex sign_oneAndI =  csigns(oneAndI);
  floatComplex sign_zero = csigns(zero);

  printf(">> Float Complex\n");
  /* sign(%i) = %i */
  assert(creals(sign_i) == 0);
  assert(cimags(sign_i) == 1);
  /* sign(1+%i) = (1+%i) / sqrt(2) */
  assert(creals(sign_oneAndI) == 1 / sqrtf(2));
  assert(cimags(sign_oneAndI) == 1 / sqrtf(2));
  /* sign(0) = 0 */
  assert(creals(sign_zero) == 0);
  assert(cimags(sign_zero) == 0);

  return 0;
}

int zsignsTest() {
  floatComplex zero = FloatComplex(0., 0.);
  doubleComplex i = DoubleComplex(0., 1.);
  doubleComplex oneAndI = DoubleComplex(1., 1.);
  doubleComplex sign_i = zsigns(i);
  doubleComplex sign_oneAndI =  zsigns(oneAndI);
  floatComplex sign_zero = csigns(zero);

  printf(">> Double Complex\n");
  /* sign(%i) = %i */
  assert(zreals(sign_i) == 0);
  assert(zimags(sign_i) == 1);
  /* sign(1+%i) = (1+%i) / sqrt(2) */
  assert ( fabs ( zreals(sign_oneAndI) - (sqrt(2.)/2.) ) / fabs(  zreals(sign_oneAndI)) < 1e-15) ;
  assert ( fabs ( zimags(sign_oneAndI) - 1 / sqrt(2.) ) / fabs(  zimags(sign_oneAndI)) < 1e-15) ;
  /* sign(0) = 0 */
  assert(creals(sign_zero) == 0);
  assert(cimags(sign_zero) == 0);

  return 0;
}


int testSign() {
  int ssignsTestStatus, dsignsTestStatus = 0;
  int csignsTestStatus, zsignsTestStatus = 0;
  printf("\n>>>> Sign Tests\n");
  ssignsTestStatus = ssignsTest();
  dsignsTestStatus = dsignsTest();
  csignsTestStatus = csignsTest();
  zsignsTestStatus = zsignsTest();

  return (ssignsTestStatus + dsignsTestStatus +
	  csignsTestStatus + zsignsTestStatus);
}

int main(void) {
  assert(testSign() == 0);
  return 0;
}