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