Age Owner Branch data TLA Line data Source code
1 : : #include <stdio.h>
2 : : #include <stdlib.h>
3 : : #include <pgtypes_numeric.h>
4 : : #include <pgtypes_error.h>
5 : : #include <decimal.h>
6 : :
7 : : exec sql include ../regression;
8 : :
9 : : exec sql include ../printf_hack;
10 : :
11 : :
12 : : char* nums[] = { "2E394", "-2", ".794", "3.44", "592.49E21", "-32.84e4",
13 : : "2E-394", ".1E-2", "+.0", "-592.49E-07", "+32.84e-4",
14 : : ".500001", "-.5000001",
15 : : "1234567890123456789012345678.91", /* 30 digits should fit
16 : : into decimal */
17 : : "1234567890123456789012345678.921", /* 31 digits should NOT
18 : : fit into decimal */
19 : : "not a number",
20 : : NULL};
21 : :
22 : :
23 : : static void
24 : : check_errno(void);
25 : :
26 : : int
7349 meskes@postgresql.or 27 :CBC 1 : main(void)
28 : : {
29 : 1 : char *text="error\n";
30 : : char *endptr;
31 : : numeric *num, *nin;
32 : : decimal *dec;
33 : : long l;
7341 34 : 1 : int i, j, k, q, r, count = 0;
35 : 1 : numeric **numarr = (numeric **) calloc(1, sizeof(numeric));
36 : :
7349 37 : 1 : ECPGdebug(1, stderr);
38 : :
39 [ + + ]: 17 : for (i = 0; nums[i]; i++)
40 : : {
41 : 16 : num = PGTYPESnumeric_from_asc(nums[i], &endptr);
7341 42 [ + + ]: 16 : if (!num) check_errno();
7349 43 [ + - ]: 16 : if (endptr != NULL)
44 : : {
45 : 16 : printf("endptr of %d is not NULL\n", i);
7347 46 [ + + ]: 16 : if (*endptr != '\0')
47 : 1 : printf("*endptr of %d is not \\0\n", i);
48 : : }
7341 49 [ + + ]: 16 : if (!num) continue;
50 : :
51 : 15 : numarr = realloc(numarr, sizeof(numeric *) * (count + 1));
52 : 15 : numarr[count++] = num;
53 : :
7349 54 : 15 : text = PGTYPESnumeric_to_asc(num, -1);
7341 55 [ - + ]: 15 : if (!text) check_errno();
3016 tmunro@postgresql.or 56 : 15 : printf("num[%d,1]: %s\n", i, text); PGTYPESchar_free(text);
7349 meskes@postgresql.or 57 : 15 : text = PGTYPESnumeric_to_asc(num, 0);
7341 58 [ - + ]: 15 : if (!text) check_errno();
3016 tmunro@postgresql.or 59 : 15 : printf("num[%d,2]: %s\n", i, text); PGTYPESchar_free(text);
7349 meskes@postgresql.or 60 : 15 : text = PGTYPESnumeric_to_asc(num, 1);
7341 61 [ - + ]: 15 : if (!text) check_errno();
3016 tmunro@postgresql.or 62 : 15 : printf("num[%d,3]: %s\n", i, text); PGTYPESchar_free(text);
7349 meskes@postgresql.or 63 : 15 : text = PGTYPESnumeric_to_asc(num, 2);
7341 64 [ - + ]: 15 : if (!text) check_errno();
3016 tmunro@postgresql.or 65 : 15 : printf("num[%d,4]: %s\n", i, text); PGTYPESchar_free(text);
66 : :
7349 meskes@postgresql.or 67 : 15 : nin = PGTYPESnumeric_new();
68 : 15 : text = PGTYPESnumeric_to_asc(nin, 2);
7341 69 [ - + ]: 15 : if (!text) check_errno();
3016 tmunro@postgresql.or 70 : 15 : printf("num[%d,5]: %s\n", i, text); PGTYPESchar_free(text);
71 : :
7349 meskes@postgresql.or 72 : 15 : r = PGTYPESnumeric_to_long(num, &l);
7341 73 [ + + ]: 15 : if (r) check_errno();
7349 74 [ + + ]: 15 : printf("num[%d,6]: %ld (r: %d)\n", i, r?0L:l, r);
75 [ + + ]: 15 : if (r == 0)
76 : : {
77 : 11 : r = PGTYPESnumeric_from_long(l, nin);
7341 78 [ - + ]: 11 : if (r) check_errno();
7349 79 : 11 : text = PGTYPESnumeric_to_asc(nin, 2);
7347 80 : 11 : q = PGTYPESnumeric_cmp(num, nin);
81 : 11 : printf("num[%d,7]: %s (r: %d - cmp: %d)\n", i, text, r, q);
3016 tmunro@postgresql.or 82 : 11 : PGTYPESchar_free(text);
83 : : }
84 : :
7349 meskes@postgresql.or 85 : 15 : r = PGTYPESnumeric_to_int(num, &k);
7341 86 [ + + ]: 15 : if (r) check_errno();
7349 87 [ + + ]: 15 : printf("num[%d,8]: %d (r: %d)\n", i, r?0:k, r);
88 [ + + ]: 15 : if (r == 0)
89 : : {
90 : 11 : r = PGTYPESnumeric_from_int(k, nin);
7341 91 [ - + ]: 11 : if (r) check_errno();
7349 92 : 11 : text = PGTYPESnumeric_to_asc(nin, 2);
7347 93 : 11 : q = PGTYPESnumeric_cmp(num, nin);
94 : 11 : printf("num[%d,9]: %s (r: %d - cmp: %d)\n", i, text, r, q);
3016 tmunro@postgresql.or 95 : 11 : PGTYPESchar_free(text);
96 : : }
97 : :
7178 meskes@postgresql.or 98 [ + + ]: 15 : if (i != 6)
99 : : {
100 : : /* underflow does not work reliable on several archs, so not testing it here */
101 : : /* this is a libc problem since we only call strtod() */
102 : :
103 : : double d;
104 : :
105 : 14 : r = PGTYPESnumeric_to_double(num, &d);
106 [ + + ]: 14 : if (r) check_errno();
2900 tgl@sss.pgh.pa.us 107 : 14 : printf("num[%d,10]: ", i);
108 [ + + ]: 14 : print_double(r ? 0.0 : d);
109 : 14 : printf(" (r: %d)\n", r);
110 : : }
111 : :
112 : : /* do not test double to numeric because
113 : : * - extra digits are different on different architectures
114 : : * - PGTYPESnumeric_from_double internally calls PGTYPESnumeric_from_asc anyway
115 : : */
116 : :
7347 meskes@postgresql.or 117 : 15 : dec = PGTYPESdecimal_new();
118 : 15 : r = PGTYPESnumeric_to_decimal(num, dec);
7341 119 [ + + ]: 15 : if (r) check_errno();
120 : : /* we have no special routine for outputting decimal, it would
121 : : * convert to a numeric anyway */
7333 122 : 15 : printf("num[%d,11]: - (r: %d)\n", i, r);
7347 123 [ + + ]: 15 : if (r == 0)
124 : : {
125 : 14 : r = PGTYPESnumeric_from_decimal(dec, nin);
7341 126 [ - + ]: 14 : if (r) check_errno();
7347 127 : 14 : text = PGTYPESnumeric_to_asc(nin, 2);
128 : 14 : q = PGTYPESnumeric_cmp(num, nin);
7333 129 : 14 : printf("num[%d,12]: %s (r: %d - cmp: %d)\n", i, text, r, q);
3016 tmunro@postgresql.or 130 : 14 : PGTYPESchar_free(text);
131 : : }
132 : :
7347 meskes@postgresql.or 133 : 15 : PGTYPESdecimal_free(dec);
7349 134 : 15 : PGTYPESnumeric_free(nin);
135 : 15 : printf("\n");
136 : : }
137 : :
7341 138 [ + + ]: 16 : for (i = 0; i < count; i++)
139 : : {
140 [ + + ]: 240 : for (j = 0; j < count; j++)
141 : : {
142 : 225 : numeric* a = PGTYPESnumeric_new();
143 : 225 : numeric* s = PGTYPESnumeric_new();
144 : 225 : numeric* m = PGTYPESnumeric_new();
145 : 225 : numeric* d = PGTYPESnumeric_new();
146 : 225 : r = PGTYPESnumeric_add(numarr[i], numarr[j], a);
147 [ - + ]: 225 : if (r)
148 : : {
7341 meskes@postgresql.or 149 :UBC 0 : check_errno();
150 : 0 : printf("r: %d\n", r);
151 : : }
152 : : else
153 : : {
7341 meskes@postgresql.or 154 :CBC 225 : text = PGTYPESnumeric_to_asc(a, 10);
155 : 225 : printf("num[a,%d,%d]: %s\n", i, j, text);
3016 tmunro@postgresql.or 156 : 225 : PGTYPESchar_free(text);
157 : : }
7341 meskes@postgresql.or 158 : 225 : r = PGTYPESnumeric_sub(numarr[i], numarr[j], s);
159 [ - + ]: 225 : if (r)
160 : : {
7341 meskes@postgresql.or 161 :UBC 0 : check_errno();
162 : 0 : printf("r: %d\n", r);
163 : : }
164 : : else
165 : : {
7341 meskes@postgresql.or 166 :CBC 225 : text = PGTYPESnumeric_to_asc(s, 10);
167 : 225 : printf("num[s,%d,%d]: %s\n", i, j, text);
3016 tmunro@postgresql.or 168 : 225 : PGTYPESchar_free(text);
169 : : }
7341 meskes@postgresql.or 170 : 225 : r = PGTYPESnumeric_mul(numarr[i], numarr[j], m);
171 [ - + ]: 225 : if (r)
172 : : {
7341 meskes@postgresql.or 173 :UBC 0 : check_errno();
174 : 0 : printf("r: %d\n", r);
175 : : }
176 : : else
177 : : {
7341 meskes@postgresql.or 178 :CBC 225 : text = PGTYPESnumeric_to_asc(m, 10);
179 : 225 : printf("num[m,%d,%d]: %s\n", i, j, text);
3016 tmunro@postgresql.or 180 : 225 : PGTYPESchar_free(text);
181 : : }
7341 meskes@postgresql.or 182 : 225 : r = PGTYPESnumeric_div(numarr[i], numarr[j], d);
183 [ + + ]: 225 : if (r)
184 : : {
185 : 15 : check_errno();
186 : 15 : printf("r: %d\n", r);
187 : : }
188 : : else
189 : : {
190 : 210 : text = PGTYPESnumeric_to_asc(d, 10);
191 : 210 : printf("num[d,%d,%d]: %s\n", i, j, text);
3016 tmunro@postgresql.or 192 : 210 : PGTYPESchar_free(text);
193 : : }
194 : :
5878 meskes@postgresql.or 195 : 225 : PGTYPESnumeric_free(a);
196 : 225 : PGTYPESnumeric_free(s);
197 : 225 : PGTYPESnumeric_free(m);
198 : 225 : PGTYPESnumeric_free(d);
199 : : }
200 : : }
201 : :
7341 202 [ + + ]: 16 : for (i = 0; i < count; i++)
203 : : {
204 : 15 : text = PGTYPESnumeric_to_asc(numarr[i], -1);
205 : 15 : printf("%d: %s\n", i, text);
3016 tmunro@postgresql.or 206 : 15 : PGTYPESchar_free(text);
5878 meskes@postgresql.or 207 : 15 : PGTYPESnumeric_free(numarr[i]);
208 : : }
209 : 1 : free(numarr);
210 : :
3321 peter_e@gmx.net 211 : 1 : return 0;
212 : : }
213 : :
214 : : static void
7349 meskes@postgresql.or 215 : 26 : check_errno(void)
216 : : {
217 [ - + - + : 26 : switch(errno)
+ - ]
218 : : {
7349 meskes@postgresql.or 219 :UBC 0 : case 0:
220 : 0 : printf("(no errno set) - ");
221 : 0 : break;
7349 meskes@postgresql.or 222 :CBC 10 : case PGTYPES_NUM_OVERFLOW:
223 : 10 : printf("(errno == PGTYPES_NUM_OVERFLOW) - ");
224 : 10 : break;
7341 meskes@postgresql.or 225 :UBC 0 : case PGTYPES_NUM_UNDERFLOW:
226 : 0 : printf("(errno == PGTYPES_NUM_UNDERFLOW) - ");
227 : 0 : break;
7349 meskes@postgresql.or 228 :CBC 1 : case PGTYPES_NUM_BAD_NUMERIC:
229 : 1 : printf("(errno == PGTYPES_NUM_BAD_NUMERIC) - ");
230 : 1 : break;
7341 231 : 15 : case PGTYPES_NUM_DIVIDE_ZERO:
232 : 15 : printf("(errno == PGTYPES_NUM_DIVIDE_ZERO) - ");
233 : 15 : break;
7349 meskes@postgresql.or 234 :UBC 0 : default:
235 : 0 : printf("(unknown errno (%d))\n", errno);
236 : 0 : printf("(libc: (%s)) ", strerror(errno));
237 : 0 : break;
238 : : }
239 : :
7349 meskes@postgresql.or 240 :CBC 26 : }
|