PostgreSQL Source Code  git master
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 print_double (double x)
 
static void check_errno (void)
 
int main (void)
 

Variables

char * nums []
 

Macro Definition Documentation

◆ ECPGdebug

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

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

Referenced by main().

Function Documentation

◆ check_errno()

static void check_errno ( void  )
static

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

References PGTYPES_NUM_BAD_NUMERIC, PGTYPES_NUM_DIVIDE_ZERO, PGTYPES_NUM_OVERFLOW, PGTYPES_NUM_UNDERFLOW, printf, and strerror.

Referenced by main().

266 {
267  switch(errno)
268  {
269  case 0:
270  printf("(no errno set) - ");
271  break;
273  printf("(errno == PGTYPES_NUM_OVERFLOW) - ");
274  break;
276  printf("(errno == PGTYPES_NUM_UNDERFLOW) - ");
277  break;
279  printf("(errno == PGTYPES_NUM_BAD_NUMERIC) - ");
280  break;
282  printf("(errno == PGTYPES_NUM_DIVIDE_ZERO) - ");
283  break;
284  default:
285  printf("(unknown errno (%d))\n", errno);
286  printf("(libc: (%s)) ", strerror(errno));
287  break;
288  }
289 
290 }
#define PGTYPES_NUM_DIVIDE_ZERO
Definition: pgtypes_error.h:5
#define printf(...)
Definition: port.h:198
#define PGTYPES_NUM_OVERFLOW
Definition: pgtypes_error.h:3
#define PGTYPES_NUM_UNDERFLOW
Definition: pgtypes_error.h:6
#define strerror
Definition: port.h:205
#define PGTYPES_NUM_BAD_NUMERIC
Definition: pgtypes_error.h:4

◆ main()

int main ( void  )

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

References calloc, check_errno(), ECPGdebug, free, i, nums, PGTYPESchar_free(), 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(), print_double(), printf, and realloc.

79 {
80  char *text="error\n";
81  char *endptr;
82  numeric *num, *nin;
83  decimal *dec;
84  long l;
85  int i, j, k, q, r, count = 0;
86  double d;
87  numeric **numarr = (numeric **) calloc(1, sizeof(numeric));
88 
89  ECPGdebug(1, stderr);
90 
91  for (i = 0; nums[i]; i++)
92  {
93  num = PGTYPESnumeric_from_asc(nums[i], &endptr);
94  if (!num) check_errno();
95  if (endptr != NULL)
96  {
97  printf("endptr of %d is not NULL\n", i);
98  if (*endptr != '\0')
99  printf("*endptr of %d is not \\0\n", i);
100  }
101  if (!num) continue;
102 
103  numarr = realloc(numarr, sizeof(numeric *) * (count + 1));
104  numarr[count++] = num;
105 
106  text = PGTYPESnumeric_to_asc(num, -1);
107  if (!text) check_errno();
108  printf("num[%d,1]: %s\n", i, text); PGTYPESchar_free(text);
109  text = PGTYPESnumeric_to_asc(num, 0);
110  if (!text) check_errno();
111  printf("num[%d,2]: %s\n", i, text); PGTYPESchar_free(text);
112  text = PGTYPESnumeric_to_asc(num, 1);
113  if (!text) check_errno();
114  printf("num[%d,3]: %s\n", i, text); PGTYPESchar_free(text);
115  text = PGTYPESnumeric_to_asc(num, 2);
116  if (!text) check_errno();
117  printf("num[%d,4]: %s\n", i, text); PGTYPESchar_free(text);
118 
119  nin = PGTYPESnumeric_new();
120  text = PGTYPESnumeric_to_asc(nin, 2);
121  if (!text) check_errno();
122  printf("num[%d,5]: %s\n", i, text); PGTYPESchar_free(text);
123 
124  r = PGTYPESnumeric_to_long(num, &l);
125  if (r) check_errno();
126  printf("num[%d,6]: %ld (r: %d)\n", i, r?0L:l, r);
127  if (r == 0)
128  {
129  r = PGTYPESnumeric_from_long(l, nin);
130  if (r) check_errno();
131  text = PGTYPESnumeric_to_asc(nin, 2);
132  q = PGTYPESnumeric_cmp(num, nin);
133  printf("num[%d,7]: %s (r: %d - cmp: %d)\n", i, text, r, q);
134  PGTYPESchar_free(text);
135  }
136 
137  r = PGTYPESnumeric_to_int(num, &k);
138  if (r) check_errno();
139  printf("num[%d,8]: %d (r: %d)\n", i, r?0:k, r);
140  if (r == 0)
141  {
142  r = PGTYPESnumeric_from_int(k, nin);
143  if (r) check_errno();
144  text = PGTYPESnumeric_to_asc(nin, 2);
145  q = PGTYPESnumeric_cmp(num, nin);
146  printf("num[%d,9]: %s (r: %d - cmp: %d)\n", i, text, r, q);
147  PGTYPESchar_free(text);
148  }
149 
150  if (i != 6)
151  {
152  /* underflow does not work reliable on several archs, so not testing it here */
153  /* this is a libc problem since we only call strtod() */
154 
155  r = PGTYPESnumeric_to_double(num, &d);
156  if (r) check_errno();
157  printf("num[%d,10]: ", i);
158  print_double(r ? 0.0 : d);
159  printf(" (r: %d)\n", r);
160  }
161 
162  /* do not test double to numeric because
163  * - extra digits are different on different architectures
164  * - PGTYPESnumeric_from_double internally calls PGTYPESnumeric_from_asc anyway
165  */
166 
167  dec = PGTYPESdecimal_new();
168  r = PGTYPESnumeric_to_decimal(num, dec);
169  if (r) check_errno();
170  /* we have no special routine for outputting decimal, it would
171  * convert to a numeric anyway */
172  printf("num[%d,11]: - (r: %d)\n", i, r);
173  if (r == 0)
174  {
175  r = PGTYPESnumeric_from_decimal(dec, nin);
176  if (r) check_errno();
177  text = PGTYPESnumeric_to_asc(nin, 2);
178  q = PGTYPESnumeric_cmp(num, nin);
179  printf("num[%d,12]: %s (r: %d - cmp: %d)\n", i, text, r, q);
180  PGTYPESchar_free(text);
181  }
182 
183  PGTYPESdecimal_free(dec);
184  PGTYPESnumeric_free(nin);
185  printf("\n");
186  }
187 
188  for (i = 0; i < count; i++)
189  {
190  for (j = 0; j < count; j++)
191  {
196  r = PGTYPESnumeric_add(numarr[i], numarr[j], a);
197  if (r)
198  {
199  check_errno();
200  printf("r: %d\n", r);
201  }
202  else
203  {
204  text = PGTYPESnumeric_to_asc(a, 10);
205  printf("num[a,%d,%d]: %s\n", i, j, text);
206  PGTYPESchar_free(text);
207  }
208  r = PGTYPESnumeric_sub(numarr[i], numarr[j], s);
209  if (r)
210  {
211  check_errno();
212  printf("r: %d\n", r);
213  }
214  else
215  {
216  text = PGTYPESnumeric_to_asc(s, 10);
217  printf("num[s,%d,%d]: %s\n", i, j, text);
218  PGTYPESchar_free(text);
219  }
220  r = PGTYPESnumeric_mul(numarr[i], numarr[j], m);
221  if (r)
222  {
223  check_errno();
224  printf("r: %d\n", r);
225  }
226  else
227  {
228  text = PGTYPESnumeric_to_asc(m, 10);
229  printf("num[m,%d,%d]: %s\n", i, j, text);
230  PGTYPESchar_free(text);
231  }
232  r = PGTYPESnumeric_div(numarr[i], numarr[j], d);
233  if (r)
234  {
235  check_errno();
236  printf("r: %d\n", r);
237  }
238  else
239  {
240  text = PGTYPESnumeric_to_asc(d, 10);
241  printf("num[d,%d,%d]: %s\n", i, j, text);
242  PGTYPESchar_free(text);
243  }
244 
249  }
250  }
251 
252  for (i = 0; i < count; i++)
253  {
254  text = PGTYPESnumeric_to_asc(numarr[i], -1);
255  printf("%d: %s\n", i, text);
256  PGTYPESchar_free(text);
257  PGTYPESnumeric_free(numarr[i]);
258  }
259  free(numarr);
260 
261  return 0;
262 }
#define calloc(a, b)
Definition: header.h:55
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
#define printf(...)
Definition: port.h:198
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
static void print_double(double x)
int PGTYPESnumeric_from_int(signed int, numeric *)
Definition: numeric.c:1396
void PGTYPESchar_free(char *ptr)
Definition: common.c:145
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:65
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:60
int i
Definition: c.h:549
int PGTYPESnumeric_mul(numeric *, numeric *, numeric *)
Definition: numeric.c:981
int PGTYPESnumeric_sub(numeric *, numeric *, numeric *)
Definition: numeric.c:850
#define ECPGdebug(X, Y)

◆ print_double()

static void print_double ( double  x)
static

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

References convert(), printf, and sprintf.

Referenced by main().

35 {
36 #ifdef WIN32
37  /* Change Windows' 3-digit exponents to look like everyone else's */
38  char convert[128];
39  int vallen;
40 
41  sprintf(convert, "%g", x);
42  vallen = strlen(convert);
43 
44  if (vallen >= 6 &&
45  convert[vallen - 5] == 'e' &&
46  convert[vallen - 3] == '0')
47  {
48  convert[vallen - 3] = convert[vallen - 2];
49  convert[vallen - 2] = convert[vallen - 1];
50  convert[vallen - 1] = '\0';
51  }
52 
53  printf("%s", convert);
54 #else
55  printf("%g", x);
56 #endif
57 }
#define printf(...)
Definition: port.h:198
#define sprintf
Definition: port.h:194
static void convert(const int32 val, char *const buf)
Definition: zic.c:1959

Variable Documentation

◆ nums

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",
NULL}

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

Referenced by main().