3
#include <pgtypes_numeric.h>
4
#include <pgtypes_error.h>
7
exec sql include ../regression;
12
NOTE: This file has a different expect file for regression tests on MinGW32
17
char* nums[] = { "2E394", "-2", ".794", "3.44", "592.49E21", "-32.84e4",
18
"2E-394", ".1E-2", "+.0", "-592.49E-07", "+32.84e-4",
19
".500001", "-.5000001",
20
"1234567890123456789012345678.91", /* 30 digits should fit
22
"1234567890123456789012345678.921", /* 31 digits should NOT
39
int i, j, k, q, r, count = 0;
41
numeric **numarr = (numeric **) calloc(1, sizeof(numeric));
45
for (i = 0; nums[i]; i++)
47
num = PGTYPESnumeric_from_asc(nums[i], &endptr);
48
if (!num) check_errno();
51
printf("endptr of %d is not NULL\n", i);
53
printf("*endptr of %d is not \\0\n", i);
57
numarr = realloc(numarr, sizeof(numeric *) * (count + 1));
58
numarr[count++] = num;
60
text = PGTYPESnumeric_to_asc(num, -1);
61
if (!text) check_errno();
62
printf("num[%d,1]: %s\n", i, text); free(text);
63
text = PGTYPESnumeric_to_asc(num, 0);
64
if (!text) check_errno();
65
printf("num[%d,2]: %s\n", i, text); free(text);
66
text = PGTYPESnumeric_to_asc(num, 1);
67
if (!text) check_errno();
68
printf("num[%d,3]: %s\n", i, text); free(text);
69
text = PGTYPESnumeric_to_asc(num, 2);
70
if (!text) check_errno();
71
printf("num[%d,4]: %s\n", i, text); free(text);
73
nin = PGTYPESnumeric_new();
74
text = PGTYPESnumeric_to_asc(nin, 2);
75
if (!text) check_errno();
76
printf("num[%d,5]: %s\n", i, text); free(text);
78
r = PGTYPESnumeric_to_long(num, &l);
80
printf("num[%d,6]: %ld (r: %d)\n", i, r?0L:l, r);
83
r = PGTYPESnumeric_from_long(l, nin);
85
text = PGTYPESnumeric_to_asc(nin, 2);
86
q = PGTYPESnumeric_cmp(num, nin);
87
printf("num[%d,7]: %s (r: %d - cmp: %d)\n", i, text, r, q);
91
r = PGTYPESnumeric_to_int(num, &k);
93
printf("num[%d,8]: %d (r: %d)\n", i, r?0:k, r);
96
r = PGTYPESnumeric_from_int(k, nin);
98
text = PGTYPESnumeric_to_asc(nin, 2);
99
q = PGTYPESnumeric_cmp(num, nin);
100
printf("num[%d,9]: %s (r: %d - cmp: %d)\n", i, text, r, q);
106
/* underflow does not work reliable on several archs, so not testing it here */
107
/* this is a libc problem since we only call strtod() */
109
r = PGTYPESnumeric_to_double(num, &d);
110
if (r) check_errno();
111
printf("num[%d,10]: %g (r: %d)\n", i, r?0.0:d, r);
114
/* do not test double to numeric because
115
* - extra digits are different on different architectures
116
* - PGTYPESnumeric_from_double internally calls PGTYPESnumeric_from_asc anyway
119
dec = PGTYPESdecimal_new();
120
r = PGTYPESnumeric_to_decimal(num, dec);
121
if (r) check_errno();
122
/* we have no special routine for outputting decimal, it would
123
* convert to a numeric anyway */
124
printf("num[%d,11]: - (r: %d)\n", i, r);
127
r = PGTYPESnumeric_from_decimal(dec, nin);
128
if (r) check_errno();
129
text = PGTYPESnumeric_to_asc(nin, 2);
130
q = PGTYPESnumeric_cmp(num, nin);
131
printf("num[%d,12]: %s (r: %d - cmp: %d)\n", i, text, r, q);
135
PGTYPESdecimal_free(dec);
136
PGTYPESnumeric_free(nin);
140
for (i = 0; i < count; i++)
142
for (j = 0; j < count; j++)
144
numeric* a = PGTYPESnumeric_new();
145
numeric* s = PGTYPESnumeric_new();
146
numeric* m = PGTYPESnumeric_new();
147
numeric* d = PGTYPESnumeric_new();
148
r = PGTYPESnumeric_add(numarr[i], numarr[j], a);
152
printf("r: %d\n", r);
156
text = PGTYPESnumeric_to_asc(a, 10);
157
printf("num[a,%d,%d]: %s\n", i, j, text);
160
r = PGTYPESnumeric_sub(numarr[i], numarr[j], s);
164
printf("r: %d\n", r);
168
text = PGTYPESnumeric_to_asc(s, 10);
169
printf("num[s,%d,%d]: %s\n", i, j, text);
172
r = PGTYPESnumeric_mul(numarr[i], numarr[j], m);
176
printf("r: %d\n", r);
180
text = PGTYPESnumeric_to_asc(m, 10);
181
printf("num[m,%d,%d]: %s\n", i, j, text);
184
r = PGTYPESnumeric_div(numarr[i], numarr[j], d);
188
printf("r: %d\n", r);
192
text = PGTYPESnumeric_to_asc(d, 10);
193
printf("num[d,%d,%d]: %s\n", i, j, text);
199
for (i = 0; i < count; i++)
201
text = PGTYPESnumeric_to_asc(numarr[i], -1);
202
printf("%d: %s\n", i, text);
215
printf("(no errno set) - ");
217
case PGTYPES_NUM_OVERFLOW:
218
printf("(errno == PGTYPES_NUM_OVERFLOW) - ");
220
case PGTYPES_NUM_UNDERFLOW:
221
printf("(errno == PGTYPES_NUM_UNDERFLOW) - ");
223
case PGTYPES_NUM_BAD_NUMERIC:
224
printf("(errno == PGTYPES_NUM_BAD_NUMERIC) - ");
226
case PGTYPES_NUM_DIVIDE_ZERO:
227
printf("(errno == PGTYPES_NUM_DIVIDE_ZERO) - ");
230
printf("(unknown errno (%d))\n", errno);
231
printf("(libc: (%s)) ", strerror(errno));