| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <pgtypes_numeric.h> |
| 4 |
#include <pgtypes_error.h> |
| 5 |
#include <decimal.h> |
| 6 |
#include <sqltypes.h> |
| 7 |
|
| 8 |
exec sql include ../regression; |
| 9 |
|
| 10 |
exec sql include ../printf_hack; |
| 11 |
|
| 12 |
|
| 13 |
/* |
| 14 |
TODO: |
| 15 |
deccmp => DECUNKNOWN |
| 16 |
decimal point: , and/or . ? |
| 17 |
ECPG_INFORMIX_BAD_EXPONENT ? |
| 18 |
*/ |
| 19 |
|
| 20 |
char* decs[] = { "2E394", "-2", ".794", "3.44", "592.49E21", "-32.84e4", |
| 21 |
"2E-394", ".1E-2", "+.0", "-592.49E-07", "+32.84e-4", |
| 22 |
".500001", "-.5000001", |
| 23 |
"1234567890123456789012345678.91", /* 30 digits should fit |
| 24 |
into decimal */ |
| 25 |
"1234567890123456789012345678.921", /* 31 digits should NOT |
| 26 |
fit into decimal */ |
| 27 |
"not a number", |
| 28 |
NULL}; |
| 29 |
|
| 30 |
|
| 31 |
static void |
| 32 |
check_errno(void); |
| 33 |
|
| 34 |
#define BUFSIZE 200 |
| 35 |
|
| 36 |
int |
| 37 |
main(void) |
| 38 |
{ |
| 39 |
decimal *dec, *din; |
| 40 |
char buf[BUFSIZE]; |
| 41 |
long l; |
| 42 |
int i, j, k, q, r, count = 0; |
| 43 |
double dbl; |
| 44 |
decimal **decarr = (decimal **) calloc(1, sizeof(decimal)); |
| 45 |
|
| 46 |
ECPGdebug(1, stderr); |
| 47 |
|
| 48 |
for (i = 0; decs[i]; i++) |
| 49 |
{ |
| 50 |
dec = PGTYPESdecimal_new(); |
| 51 |
r = deccvasc(decs[i], strlen(decs[i]), dec); |
| 52 |
if (r) |
| 53 |
{ |
| 54 |
check_errno(); |
| 55 |
printf("dec[%d,0]: r: %d\n", i, r); |
| 56 |
PGTYPESdecimal_free(dec); |
| 57 |
continue; |
| 58 |
} |
| 59 |
decarr = realloc(decarr, sizeof(decimal *) * (count + 1)); |
| 60 |
decarr[count++] = dec; |
| 61 |
|
| 62 |
r = dectoasc(dec, buf, BUFSIZE-1, -1); |
| 63 |
if (r < 0) check_errno(); |
| 64 |
printf("dec[%d,1]: r: %d, %s\n", i, r, buf); |
| 65 |
|
| 66 |
r = dectoasc(dec, buf, BUFSIZE-1, 0); |
| 67 |
if (r < 0) check_errno(); |
| 68 |
printf("dec[%d,2]: r: %d, %s\n", i, r, buf); |
| 69 |
r = dectoasc(dec, buf, BUFSIZE-1, 1); |
| 70 |
if (r < 0) check_errno(); |
| 71 |
printf("dec[%d,3]: r: %d, %s\n", i, r, buf); |
| 72 |
r = dectoasc(dec, buf, BUFSIZE-1, 2); |
| 73 |
if (r < 0) check_errno(); |
| 74 |
printf("dec[%d,4]: r: %d, %s\n", i, r, buf); |
| 75 |
|
| 76 |
din = PGTYPESdecimal_new(); |
| 77 |
r = dectoasc(din, buf, BUFSIZE-1, 2); |
| 78 |
if (r < 0) check_errno(); |
| 79 |
printf("dec[%d,5]: r: %d, %s\n", i, r, buf); |
| 80 |
|
| 81 |
r = dectolong(dec, &l); |
| 82 |
if (r) check_errno(); |
| 83 |
printf("dec[%d,6]: %ld (r: %d)\n", i, r?0L:l, r); |
| 84 |
if (r == 0) |
| 85 |
{ |
| 86 |
r = deccvlong(l, din); |
| 87 |
if (r) check_errno(); |
| 88 |
dectoasc(din, buf, BUFSIZE-1, 2); |
| 89 |
q = deccmp(dec, din); |
| 90 |
printf("dec[%d,7]: %s (r: %d - cmp: %d)\n", i, buf, r, q); |
| 91 |
} |
| 92 |
|
| 93 |
r = dectoint(dec, &k); |
| 94 |
if (r) check_errno(); |
| 95 |
printf("dec[%d,8]: %d (r: %d)\n", i, r?0:k, r); |
| 96 |
if (r == 0) |
| 97 |
{ |
| 98 |
r = deccvint(k, din); |
| 99 |
if (r) check_errno(); |
| 100 |
dectoasc(din, buf, BUFSIZE-1, 2); |
| 101 |
q = deccmp(dec, din); |
| 102 |
printf("dec[%d,9]: %s (r: %d - cmp: %d)\n", i, buf, r, q); |
| 103 |
} |
| 104 |
|
| 105 |
if (i != 6) |
| 106 |
{ |
| 107 |
/* underflow does not work reliable on several archs, so not testing it here */ |
| 108 |
/* this is a libc problem since we only call strtod() */ |
| 109 |
r = dectodbl(dec, &dbl); |
| 110 |
if (r) check_errno(); |
| 111 |
printf("dec[%d,10]: ", i); |
| 112 |
print_double(r ? 0.0 : dbl); |
| 113 |
printf(" (r: %d)\n", r); |
| 114 |
} |
| 115 |
|
| 116 |
PGTYPESdecimal_free(din); |
| 117 |
printf("\n"); |
| 118 |
} |
| 119 |
|
| 120 |
/* add a NULL value */ |
| 121 |
dec = PGTYPESdecimal_new(); |
| 122 |
decarr = realloc(decarr, sizeof(decimal *) * (count + 1)); |
| 123 |
decarr[count++] = dec; |
| 124 |
|
| 125 |
rsetnull(CDECIMALTYPE, (char *) decarr[count-1]); |
| 126 |
printf("dec[%d]: %sNULL\n", count-1, |
| 127 |
risnull(CDECIMALTYPE, (char *) decarr[count-1]) ? "" : "NOT "); |
| 128 |
printf("dec[0]: %sNULL\n", |
| 129 |
risnull(CDECIMALTYPE, (char *) decarr[0]) ? "" : "NOT "); |
| 130 |
|
| 131 |
r = dectoasc(decarr[3], buf, -1, -1); |
| 132 |
check_errno(); printf("dectoasc with len == -1: r: %d\n", r); |
| 133 |
r = dectoasc(decarr[3], buf, 0, -1); |
| 134 |
check_errno(); printf("dectoasc with len == 0: r: %d\n", r); |
| 135 |
|
| 136 |
for (i = 0; i < count; i++) |
| 137 |
{ |
| 138 |
for (j = 0; j < count; j++) |
| 139 |
{ |
| 140 |
decimal a, s, m, d; |
| 141 |
int c; |
| 142 |
c = deccmp(decarr[i], decarr[j]); |
| 143 |
printf("dec[c,%d,%d]: %d\n", i, j, c); |
| 144 |
|
| 145 |
/* |
| 146 |
* decarr[count-1] is risnull(), which makes these functions |
| 147 |
* return 0 without changing the output parameter. Make that |
| 148 |
* clear by initializing each output parameter. |
| 149 |
*/ |
| 150 |
deccvint(7654321, &a); |
| 151 |
deccvint(7654321, &s); |
| 152 |
deccvint(7654321, &m); |
| 153 |
deccvint(7654321, &d); |
| 154 |
|
| 155 |
r = decadd(decarr[i], decarr[j], &a); |
| 156 |
if (r) |
| 157 |
{ |
| 158 |
check_errno(); |
| 159 |
printf("r: %d\n", r); |
| 160 |
} |
| 161 |
else |
| 162 |
{ |
| 163 |
dectoasc(&a, buf, BUFSIZE-1, -1); |
| 164 |
printf("dec[a,%d,%d]: %s\n", i, j, buf); |
| 165 |
} |
| 166 |
|
| 167 |
r = decsub(decarr[i], decarr[j], &s); |
| 168 |
if (r) |
| 169 |
{ |
| 170 |
check_errno(); |
| 171 |
printf("r: %d\n", r); |
| 172 |
} |
| 173 |
else |
| 174 |
{ |
| 175 |
dectoasc(&s, buf, BUFSIZE-1, -1); |
| 176 |
printf("dec[s,%d,%d]: %s\n", i, j, buf); |
| 177 |
} |
| 178 |
|
| 179 |
r = decmul(decarr[i], decarr[j], &m); |
| 180 |
if (r) |
| 181 |
{ |
| 182 |
check_errno(); |
| 183 |
printf("r: %d\n", r); |
| 184 |
} |
| 185 |
else |
| 186 |
{ |
| 187 |
dectoasc(&m, buf, BUFSIZE-1, -1); |
| 188 |
printf("dec[m,%d,%d]: %s\n", i, j, buf); |
| 189 |
} |
| 190 |
|
| 191 |
r = decdiv(decarr[i], decarr[j], &d); |
| 192 |
if (r) |
| 193 |
{ |
| 194 |
check_errno(); |
| 195 |
printf("r: %d\n", r); |
| 196 |
} |
| 197 |
else |
| 198 |
{ |
| 199 |
dectoasc(&d, buf, BUFSIZE-1, -1); |
| 200 |
printf("dec[d,%d,%d]: %s\n", i, j, buf); |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
for (i = 0; i < count; i++) |
| 206 |
{ |
| 207 |
dectoasc(decarr[i], buf, BUFSIZE-1, -1); |
| 208 |
printf("%d: %s\n", i, buf); |
| 209 |
|
| 210 |
PGTYPESdecimal_free(decarr[i]); |
| 211 |
} |
| 212 |
free(decarr); |
| 213 |
|
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
static void |
| 218 |
check_errno(void) |
| 219 |
{ |
| 220 |
switch(errno) |
| 221 |
{ |
| 222 |
case 0: |
| 223 |
printf("(no errno set) - "); |
| 224 |
break; |
| 225 |
case ECPG_INFORMIX_NUM_OVERFLOW: |
| 226 |
printf("(errno == ECPG_INFORMIX_NUM_OVERFLOW) - "); |
| 227 |
break; |
| 228 |
case ECPG_INFORMIX_NUM_UNDERFLOW: |
| 229 |
printf("(errno == ECPG_INFORMIX_NUM_UNDERFLOW) - "); |
| 230 |
break; |
| 231 |
case PGTYPES_NUM_OVERFLOW: |
| 232 |
printf("(errno == PGTYPES_NUM_OVERFLOW) - "); |
| 233 |
break; |
| 234 |
case PGTYPES_NUM_UNDERFLOW: |
| 235 |
printf("(errno == PGTYPES_NUM_UNDERFLOW) - "); |
| 236 |
break; |
| 237 |
case PGTYPES_NUM_BAD_NUMERIC: |
| 238 |
printf("(errno == PGTYPES_NUM_BAD_NUMERIC) - "); |
| 239 |
break; |
| 240 |
case PGTYPES_NUM_DIVIDE_ZERO: |
| 241 |
printf("(errno == PGTYPES_NUM_DIVIDE_ZERO) - "); |
| 242 |
break; |
| 243 |
default: |
| 244 |
printf("(unknown errno (%d))\n", errno); |
| 245 |
printf("(libc: (%s)) ", strerror(errno)); |
| 246 |
break; |
| 247 |
} |
| 248 |
} |