PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
compat_informix-dec_test.c File Reference
#include <ecpglib.h>
#include <ecpgerrno.h>
#include <sqlca.h>
#include <ecpg_informix.h>
#include <stdio.h>
#include <stdlib.h>
#include <pgtypes_numeric.h>
#include <pgtypes_error.h>
#include <decimal.h>
#include <sqltypes.h>
Include dependency graph for compat_informix-dec_test.c:

Go to the source code of this file.

Macros

#define ECPGdebug(X, Y)   ECPGdebug((X)+100,(Y))
 
#define BUFSIZE   200
 

Functions

static void check_errno (void)
 
int main (void)
 

Variables

char * decs []
 

Macro Definition Documentation

#define BUFSIZE   200

Definition at line 61 of file compat_informix-dec_test.c.

Referenced by main().

#define ECPGdebug (   X,
 
)    ECPGdebug((X)+100,(Y))

Definition at line 9 of file compat_informix-dec_test.c.

Referenced by main().

Function Documentation

static void check_errno ( void  )
static

Definition at line 233 of file compat_informix-dec_test.c.

References ECPG_INFORMIX_NUM_OVERFLOW, ECPG_INFORMIX_NUM_UNDERFLOW, PGTYPES_NUM_BAD_NUMERIC, PGTYPES_NUM_DIVIDE_ZERO, PGTYPES_NUM_OVERFLOW, PGTYPES_NUM_UNDERFLOW, and strerror().

Referenced by main().

234 {
235  switch(errno)
236  {
237  case 0:
238  printf("(no errno set) - ");
239  break;
241  printf("(errno == ECPG_INFORMIX_NUM_OVERFLOW) - ");
242  break;
244  printf("(errno == ECPG_INFORMIX_NUM_UNDERFLOW) - ");
245  break;
247  printf("(errno == PGTYPES_NUM_OVERFLOW) - ");
248  break;
250  printf("(errno == PGTYPES_NUM_UNDERFLOW) - ");
251  break;
253  printf("(errno == PGTYPES_NUM_BAD_NUMERIC) - ");
254  break;
256  printf("(errno == PGTYPES_NUM_DIVIDE_ZERO) - ");
257  break;
258  default:
259  printf("(unknown errno (%d))\n", errno);
260  printf("(libc: (%s)) ", strerror(errno));
261  break;
262  }
263 }
#define PGTYPES_NUM_DIVIDE_ZERO
Definition: pgtypes_error.h:5
#define ECPG_INFORMIX_NUM_OVERFLOW
Definition: ecpg_informix.h:16
#define ECPG_INFORMIX_NUM_UNDERFLOW
Definition: ecpg_informix.h:17
#define PGTYPES_NUM_OVERFLOW
Definition: pgtypes_error.h:3
#define PGTYPES_NUM_UNDERFLOW
Definition: pgtypes_error.h:6
#define PGTYPES_NUM_BAD_NUMERIC
Definition: pgtypes_error.h:4
const char * strerror(int errnum)
Definition: strerror.c:19
int main ( void  )

Definition at line 64 of file compat_informix-dec_test.c.

References buf, BUFSIZE, calloc, CDECIMALTYPE, check_errno(), decadd(), deccmp(), deccvasc(), deccvint(), deccvlong(), decdiv(), decmul(), decs, decsub(), dectoasc(), dectodbl(), dectoint(), dectolong(), ECPGdebug, free, i, PGTYPESdecimal_free(), PGTYPESdecimal_new(), realloc, risnull(), and rsetnull().

65 {
66  decimal *dec, *din;
67  char buf[BUFSIZE];
68  long l;
69  int i, j, k, q, r, count = 0;
70  double dbl;
71  decimal **decarr = (decimal **) calloc(1, sizeof(decimal));
72 
73  ECPGdebug(1, stderr);
74 
75  for (i = 0; decs[i]; i++)
76  {
77  dec = PGTYPESdecimal_new();
78  r = deccvasc(decs[i], strlen(decs[i]), dec);
79  if (r)
80  {
81  check_errno();
82  printf("dec[%d,0]: r: %d\n", i, r);
84  continue;
85  }
86  decarr = realloc(decarr, sizeof(decimal *) * (count + 1));
87  decarr[count++] = dec;
88 
89  r = dectoasc(dec, buf, BUFSIZE-1, -1);
90  if (r < 0) check_errno();
91  printf("dec[%d,1]: r: %d, %s\n", i, r, buf);
92 
93  r = dectoasc(dec, buf, BUFSIZE-1, 0);
94  if (r < 0) check_errno();
95  printf("dec[%d,2]: r: %d, %s\n", i, r, buf);
96  r = dectoasc(dec, buf, BUFSIZE-1, 1);
97  if (r < 0) check_errno();
98  printf("dec[%d,3]: r: %d, %s\n", i, r, buf);
99  r = dectoasc(dec, buf, BUFSIZE-1, 2);
100  if (r < 0) check_errno();
101  printf("dec[%d,4]: r: %d, %s\n", i, r, buf);
102 
103  din = PGTYPESdecimal_new();
104  r = dectoasc(din, buf, BUFSIZE-1, 2);
105  if (r < 0) check_errno();
106  printf("dec[%d,5]: r: %d, %s\n", i, r, buf);
107 
108  r = dectolong(dec, &l);
109  if (r) check_errno();
110  printf("dec[%d,6]: %ld (r: %d)\n", i, r?0L:l, r);
111  if (r == 0)
112  {
113  r = deccvlong(l, din);
114  if (r) check_errno();
115  dectoasc(din, buf, BUFSIZE-1, 2);
116  q = deccmp(dec, din);
117  printf("dec[%d,7]: %s (r: %d - cmp: %d)\n", i, buf, r, q);
118  }
119 
120  r = dectoint(dec, &k);
121  if (r) check_errno();
122  printf("dec[%d,8]: %d (r: %d)\n", i, r?0:k, r);
123  if (r == 0)
124  {
125  r = deccvint(k, din);
126  if (r) check_errno();
127  dectoasc(din, buf, BUFSIZE-1, 2);
128  q = deccmp(dec, din);
129  printf("dec[%d,9]: %s (r: %d - cmp: %d)\n", i, buf, r, q);
130  }
131 
132  if (i != 6)
133  {
134  /* underflow does not work reliable on several archs, so not testing it here */
135  /* this is a libc problem since we only call strtod() */
136  r = dectodbl(dec, &dbl);
137  if (r) check_errno();
138  printf("dec[%d,10]: %g (r: %d)\n", i, r?0.0:dbl, r);
139  }
140 
141  PGTYPESdecimal_free(din);
142  printf("\n");
143  }
144 
145  /* add a NULL value */
146  dec = PGTYPESdecimal_new();
147  decarr = realloc(decarr, sizeof(decimal *) * (count + 1));
148  decarr[count++] = dec;
149 
150  rsetnull(CDECIMALTYPE, (char *) decarr[count-1]);
151  printf("dec[%d]: %sNULL\n", count-1,
152  risnull(CDECIMALTYPE, (char *) decarr[count-1]) ? "" : "NOT ");
153  printf("dec[0]: %sNULL\n",
154  risnull(CDECIMALTYPE, (char *) decarr[0]) ? "" : "NOT ");
155 
156  r = dectoasc(decarr[3], buf, -1, -1);
157  check_errno(); printf("dectoasc with len == -1: r: %d\n", r);
158  r = dectoasc(decarr[3], buf, 0, -1);
159  check_errno(); printf("dectoasc with len == 0: r: %d\n", r);
160 
161  for (i = 0; i < count; i++)
162  {
163  for (j = 0; j < count; j++)
164  {
165  decimal a, s, m, d;
166  int c;
167  c = deccmp(decarr[i], decarr[j]);
168  printf("dec[c,%d,%d]: %d\n", i, j, c);
169 
170  r = decadd(decarr[i], decarr[j], &a);
171  if (r)
172  {
173  check_errno();
174  printf("r: %d\n", r);
175  }
176  else
177  {
178  dectoasc(&a, buf, BUFSIZE-1, -1);
179  printf("dec[a,%d,%d]: %s\n", i, j, buf);
180  }
181 
182  r = decsub(decarr[i], decarr[j], &s);
183  if (r)
184  {
185  check_errno();
186  printf("r: %d\n", r);
187  }
188  else
189  {
190  dectoasc(&s, buf, BUFSIZE-1, -1);
191  printf("dec[s,%d,%d]: %s\n", i, j, buf);
192  }
193 
194  r = decmul(decarr[i], decarr[j], &m);
195  if (r)
196  {
197  check_errno();
198  printf("r: %d\n", r);
199  }
200  else
201  {
202  dectoasc(&m, buf, BUFSIZE-1, -1);
203  printf("dec[m,%d,%d]: %s\n", i, j, buf);
204  }
205 
206  r = decdiv(decarr[i], decarr[j], &d);
207  if (r)
208  {
209  check_errno();
210  printf("r: %d\n", r);
211  }
212  else
213  {
214  dectoasc(&d, buf, BUFSIZE-1, -1);
215  printf("dec[d,%d,%d]: %s\n", i, j, buf);
216  }
217  }
218  }
219 
220  for (i = 0; i < count; i++)
221  {
222  dectoasc(decarr[i], buf, BUFSIZE-1, -1);
223  printf("%d: %s\n", i, buf);
224 
225  PGTYPESdecimal_free(decarr[i]);
226  }
227  free(decarr);
228 
229  return (0);
230 }
#define calloc(a, b)
Definition: header.h:55
int decsub(decimal *n1, decimal *n2, decimal *result)
Definition: informix.c:359
int dectolong(decimal *np, long *lngp)
Definition: informix.c:477
decimal * PGTYPESdecimal_new(void)
Definition: numeric.c:144
int deccvasc(char *cp, int len, decimal *np)
Definition: informix.c:198
int risnull(int t, char *ptr)
Definition: informix.c:1050
int dectodbl(decimal *np, double *dblp)
Definition: informix.c:432
int decdiv(decimal *n1, decimal *n2, decimal *result)
Definition: informix.c:312
char * c
static char * buf
Definition: pg_test_fsync.c:65
#define CDECIMALTYPE
Definition: sqltypes.h:12
int deccvint(int in, decimal *np)
Definition: informix.c:268
int dectoint(decimal *np, int *ip)
Definition: informix.c:453
int deccmp(decimal *arg1, decimal *arg2)
Definition: informix.c:167
#define free(a)
Definition: header.h:65
int decmul(decimal *n1, decimal *n2, decimal *result)
Definition: informix.c:337
static void check_errno(void)
int rsetnull(int t, char *ptr)
Definition: informix.c:1043
void PGTYPESdecimal_free(decimal *)
Definition: numeric.c:477
#define realloc(a, b)
Definition: header.h:60
int deccvlong(long lng, decimal *np)
Definition: informix.c:290
int dectoasc(decimal *np, char *cp, int len, int right)
Definition: informix.c:381
#define ECPGdebug(X, Y)
int i
#define BUFSIZE
int decadd(decimal *arg1, decimal *arg2, decimal *sum)
Definition: informix.c:151
char * decs[]

Variable Documentation

char* decs[]
Initial value:
= { "2E394", "-2", ".794", "3.44", "592.49E21", "-32.84e4",
"2E-394", ".1E-2", "+.0", "-592.49E-07", "+32.84e-4",
".500001", "-.5000001",
"1234567890123456789012345678.91",
"1234567890123456789012345678.921",
"not a number",
#define NULL
Definition: c.h:229

Definition at line 47 of file compat_informix-dec_test.c.

Referenced by main().