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

Go to the source code of this file.

Macros

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

Functions

static void check_errno (void)
 
int main (void)
 

Variables

char * nums []
 

Macro Definition Documentation

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

Definition at line 7 of file pgtypeslib-num_test2.c.

Referenced by main().

Function Documentation

static void check_errno ( void  )
static

Definition at line 235 of file pgtypeslib-num_test2.c.

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

Referenced by main().

236 {
237  switch(errno)
238  {
239  case 0:
240  printf("(no errno set) - ");
241  break;
243  printf("(errno == PGTYPES_NUM_OVERFLOW) - ");
244  break;
246  printf("(errno == PGTYPES_NUM_UNDERFLOW) - ");
247  break;
249  printf("(errno == PGTYPES_NUM_BAD_NUMERIC) - ");
250  break;
252  printf("(errno == PGTYPES_NUM_DIVIDE_ZERO) - ");
253  break;
254  default:
255  printf("(unknown errno (%d))\n", errno);
256  printf("(libc: (%s)) ", strerror(errno));
257  break;
258  }
259 
260 }
#define PGTYPES_NUM_DIVIDE_ZERO
Definition: pgtypes_error.h:5
#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 50 of file pgtypeslib-num_test2.c.

References calloc, check_errno(), ECPGdebug, free, i, NULL, nums, PGTYPESdecimal_free(), PGTYPESdecimal_new(), PGTYPESnumeric_add(), PGTYPESnumeric_cmp(), PGTYPESnumeric_div(), PGTYPESnumeric_free(), PGTYPESnumeric_from_asc(), PGTYPESnumeric_from_decimal(), PGTYPESnumeric_from_int(), PGTYPESnumeric_from_long(), PGTYPESnumeric_mul(), PGTYPESnumeric_new(), PGTYPESnumeric_sub(), PGTYPESnumeric_to_asc(), PGTYPESnumeric_to_decimal(), PGTYPESnumeric_to_double(), PGTYPESnumeric_to_int(), PGTYPESnumeric_to_long(), and realloc.

51 {
52  char *text="error\n";
53  char *endptr;
54  numeric *num, *nin;
55  decimal *dec;
56  long l;
57  int i, j, k, q, r, count = 0;
58  double d;
59  numeric **numarr = (numeric **) calloc(1, sizeof(numeric));
60 
61  ECPGdebug(1, stderr);
62 
63  for (i = 0; nums[i]; i++)
64  {
65  num = PGTYPESnumeric_from_asc(nums[i], &endptr);
66  if (!num) check_errno();
67  if (endptr != NULL)
68  {
69  printf("endptr of %d is not NULL\n", i);
70  if (*endptr != '\0')
71  printf("*endptr of %d is not \\0\n", i);
72  }
73  if (!num) continue;
74 
75  numarr = realloc(numarr, sizeof(numeric *) * (count + 1));
76  numarr[count++] = num;
77 
78  text = PGTYPESnumeric_to_asc(num, -1);
79  if (!text) check_errno();
80  printf("num[%d,1]: %s\n", i, text); free(text);
81  text = PGTYPESnumeric_to_asc(num, 0);
82  if (!text) check_errno();
83  printf("num[%d,2]: %s\n", i, text); free(text);
84  text = PGTYPESnumeric_to_asc(num, 1);
85  if (!text) check_errno();
86  printf("num[%d,3]: %s\n", i, text); free(text);
87  text = PGTYPESnumeric_to_asc(num, 2);
88  if (!text) check_errno();
89  printf("num[%d,4]: %s\n", i, text); free(text);
90 
91  nin = PGTYPESnumeric_new();
92  text = PGTYPESnumeric_to_asc(nin, 2);
93  if (!text) check_errno();
94  printf("num[%d,5]: %s\n", i, text); free(text);
95 
96  r = PGTYPESnumeric_to_long(num, &l);
97  if (r) check_errno();
98  printf("num[%d,6]: %ld (r: %d)\n", i, r?0L:l, r);
99  if (r == 0)
100  {
101  r = PGTYPESnumeric_from_long(l, nin);
102  if (r) check_errno();
103  text = PGTYPESnumeric_to_asc(nin, 2);
104  q = PGTYPESnumeric_cmp(num, nin);
105  printf("num[%d,7]: %s (r: %d - cmp: %d)\n", i, text, r, q);
106  free(text);
107  }
108 
109  r = PGTYPESnumeric_to_int(num, &k);
110  if (r) check_errno();
111  printf("num[%d,8]: %d (r: %d)\n", i, r?0:k, r);
112  if (r == 0)
113  {
114  r = PGTYPESnumeric_from_int(k, nin);
115  if (r) check_errno();
116  text = PGTYPESnumeric_to_asc(nin, 2);
117  q = PGTYPESnumeric_cmp(num, nin);
118  printf("num[%d,9]: %s (r: %d - cmp: %d)\n", i, text, r, q);
119  free(text);
120  }
121 
122  if (i != 6)
123  {
124  /* underflow does not work reliable on several archs, so not testing it here */
125  /* this is a libc problem since we only call strtod() */
126 
127  r = PGTYPESnumeric_to_double(num, &d);
128  if (r) check_errno();
129  printf("num[%d,10]: %g (r: %d)\n", i, r?0.0:d, r);
130  }
131 
132  /* do not test double to numeric because
133  * - extra digits are different on different architectures
134  * - PGTYPESnumeric_from_double internally calls PGTYPESnumeric_from_asc anyway
135  */
136 
137  dec = PGTYPESdecimal_new();
138  r = PGTYPESnumeric_to_decimal(num, dec);
139  if (r) check_errno();
140  /* we have no special routine for outputting decimal, it would
141  * convert to a numeric anyway */
142  printf("num[%d,11]: - (r: %d)\n", i, r);
143  if (r == 0)
144  {
145  r = PGTYPESnumeric_from_decimal(dec, nin);
146  if (r) check_errno();
147  text = PGTYPESnumeric_to_asc(nin, 2);
148  q = PGTYPESnumeric_cmp(num, nin);
149  printf("num[%d,12]: %s (r: %d - cmp: %d)\n", i, text, r, q);
150  free(text);
151  }
152 
153  PGTYPESdecimal_free(dec);
154  PGTYPESnumeric_free(nin);
155  printf("\n");
156  }
157 
158  for (i = 0; i < count; i++)
159  {
160  for (j = 0; j < count; j++)
161  {
166  r = PGTYPESnumeric_add(numarr[i], numarr[j], a);
167  if (r)
168  {
169  check_errno();
170  printf("r: %d\n", r);
171  }
172  else
173  {
174  text = PGTYPESnumeric_to_asc(a, 10);
175  printf("num[a,%d,%d]: %s\n", i, j, text);
176  free(text);
177  }
178  r = PGTYPESnumeric_sub(numarr[i], numarr[j], s);
179  if (r)
180  {
181  check_errno();
182  printf("r: %d\n", r);
183  }
184  else
185  {
186  text = PGTYPESnumeric_to_asc(s, 10);
187  printf("num[s,%d,%d]: %s\n", i, j, text);
188  free(text);
189  }
190  r = PGTYPESnumeric_mul(numarr[i], numarr[j], m);
191  if (r)
192  {
193  check_errno();
194  printf("r: %d\n", r);
195  }
196  else
197  {
198  text = PGTYPESnumeric_to_asc(m, 10);
199  printf("num[m,%d,%d]: %s\n", i, j, text);
200  free(text);
201  }
202  r = PGTYPESnumeric_div(numarr[i], numarr[j], d);
203  if (r)
204  {
205  check_errno();
206  printf("r: %d\n", r);
207  }
208  else
209  {
210  text = PGTYPESnumeric_to_asc(d, 10);
211  printf("num[d,%d,%d]: %s\n", i, j, text);
212  free(text);
213  }
214 
219  }
220  }
221 
222  for (i = 0; i < count; i++)
223  {
224  text = PGTYPESnumeric_to_asc(numarr[i], -1);
225  printf("%d: %s\n", i, text);
226  free(text);
227  PGTYPESnumeric_free(numarr[i]);
228  }
229  free(numarr);
230 
231  return (0);
232 }
#define calloc(a, b)
Definition: header.h:50
int PGTYPESnumeric_from_long(signed long int, numeric *)
Definition: numeric.c:1405
int PGTYPESnumeric_from_decimal(decimal *, numeric *)
Definition: numeric.c:1652
decimal * PGTYPESdecimal_new(void)
Definition: numeric.c:144
int PGTYPESnumeric_to_decimal(numeric *, decimal *)
Definition: numeric.c:1629
char * nums[]
void PGTYPESnumeric_free(numeric *)
Definition: numeric.c:470
numeric * PGTYPESnumeric_new(void)
Definition: numeric.c:127
int PGTYPESnumeric_to_int(numeric *, int *)
Definition: numeric.c:1581
int PGTYPESnumeric_to_long(numeric *, long *)
Definition: numeric.c:1600
int PGTYPESnumeric_cmp(numeric *, numeric *)
Definition: numeric.c:1367
int PGTYPESnumeric_div(numeric *, numeric *, numeric *)
Definition: numeric.c:1138
int PGTYPESnumeric_from_int(signed int, numeric *)
Definition: numeric.c:1396
char * PGTYPESnumeric_to_asc(numeric *, int)
Definition: numeric.c:428
static void check_errno(void)
numeric * PGTYPESnumeric_from_asc(char *, char **)
Definition: numeric.c:406
#define free(a)
Definition: header.h:60
#define NULL
Definition: c.h:226
int PGTYPESnumeric_to_double(numeric *, double *)
Definition: numeric.c:1570
void PGTYPESdecimal_free(decimal *)
Definition: numeric.c:477
int PGTYPESnumeric_add(numeric *, numeric *, numeric *)
Definition: numeric.c:722
#define realloc(a, b)
Definition: header.h:55
int i
Definition: c.h:434
int PGTYPESnumeric_mul(numeric *, numeric *, numeric *)
Definition: numeric.c:981
int PGTYPESnumeric_sub(numeric *, numeric *, numeric *)
Definition: numeric.c:850
#define ECPGdebug(X, Y)

Variable Documentation

char* nums[]
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:226

Definition at line 35 of file pgtypeslib-num_test2.c.

Referenced by main().