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