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
27 2 : main(void)
28 : {
29 2 : char *text="error\n";
30 : char *endptr;
31 : numeric *num, *nin;
32 : decimal *dec;
33 : long l;
34 2 : int i, j, k, q, r, count = 0;
35 : double d;
36 2 : numeric **numarr = (numeric **) calloc(1, sizeof(numeric));
37 :
38 2 : ECPGdebug(1, stderr);
39 :
40 34 : for (i = 0; nums[i]; i++)
41 : {
42 32 : num = PGTYPESnumeric_from_asc(nums[i], &endptr);
43 32 : if (!num) check_errno();
44 32 : if (endptr != NULL)
45 : {
46 32 : printf("endptr of %d is not NULL\n", i);
47 32 : if (*endptr != '\0')
48 2 : printf("*endptr of %d is not \\0\n", i);
49 : }
50 32 : if (!num) continue;
51 :
52 30 : numarr = realloc(numarr, sizeof(numeric *) * (count + 1));
53 30 : numarr[count++] = num;
54 :
55 30 : text = PGTYPESnumeric_to_asc(num, -1);
56 30 : if (!text) check_errno();
57 30 : printf("num[%d,1]: %s\n", i, text); PGTYPESchar_free(text);
58 30 : text = PGTYPESnumeric_to_asc(num, 0);
59 30 : if (!text) check_errno();
60 30 : printf("num[%d,2]: %s\n", i, text); PGTYPESchar_free(text);
61 30 : text = PGTYPESnumeric_to_asc(num, 1);
62 30 : if (!text) check_errno();
63 30 : printf("num[%d,3]: %s\n", i, text); PGTYPESchar_free(text);
64 30 : text = PGTYPESnumeric_to_asc(num, 2);
65 30 : if (!text) check_errno();
66 30 : printf("num[%d,4]: %s\n", i, text); PGTYPESchar_free(text);
67 :
68 30 : nin = PGTYPESnumeric_new();
69 30 : text = PGTYPESnumeric_to_asc(nin, 2);
70 30 : if (!text) check_errno();
71 30 : printf("num[%d,5]: %s\n", i, text); PGTYPESchar_free(text);
72 :
73 30 : r = PGTYPESnumeric_to_long(num, &l);
74 30 : if (r) check_errno();
75 30 : printf("num[%d,6]: %ld (r: %d)\n", i, r?0L:l, r);
76 30 : if (r == 0)
77 : {
78 22 : r = PGTYPESnumeric_from_long(l, nin);
79 22 : if (r) check_errno();
80 22 : text = PGTYPESnumeric_to_asc(nin, 2);
81 22 : q = PGTYPESnumeric_cmp(num, nin);
82 22 : printf("num[%d,7]: %s (r: %d - cmp: %d)\n", i, text, r, q);
83 22 : PGTYPESchar_free(text);
84 : }
85 :
86 30 : r = PGTYPESnumeric_to_int(num, &k);
87 30 : if (r) check_errno();
88 30 : printf("num[%d,8]: %d (r: %d)\n", i, r?0:k, r);
89 30 : if (r == 0)
90 : {
91 22 : r = PGTYPESnumeric_from_int(k, nin);
92 22 : if (r) check_errno();
93 22 : text = PGTYPESnumeric_to_asc(nin, 2);
94 22 : q = PGTYPESnumeric_cmp(num, nin);
95 22 : printf("num[%d,9]: %s (r: %d - cmp: %d)\n", i, text, r, q);
96 22 : PGTYPESchar_free(text);
97 : }
98 :
99 30 : 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 28 : r = PGTYPESnumeric_to_double(num, &d);
105 28 : if (r) check_errno();
106 28 : printf("num[%d,10]: ", i);
107 28 : print_double(r ? 0.0 : d);
108 28 : 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 :
116 30 : dec = PGTYPESdecimal_new();
117 30 : r = PGTYPESnumeric_to_decimal(num, dec);
118 30 : if (r) check_errno();
119 : /* we have no special routine for outputting decimal, it would
120 : * convert to a numeric anyway */
121 30 : printf("num[%d,11]: - (r: %d)\n", i, r);
122 30 : if (r == 0)
123 : {
124 28 : r = PGTYPESnumeric_from_decimal(dec, nin);
125 28 : if (r) check_errno();
126 28 : text = PGTYPESnumeric_to_asc(nin, 2);
127 28 : q = PGTYPESnumeric_cmp(num, nin);
128 28 : printf("num[%d,12]: %s (r: %d - cmp: %d)\n", i, text, r, q);
129 28 : PGTYPESchar_free(text);
130 : }
131 :
132 30 : PGTYPESdecimal_free(dec);
133 30 : PGTYPESnumeric_free(nin);
134 30 : printf("\n");
135 : }
136 :
137 32 : for (i = 0; i < count; i++)
138 : {
139 480 : for (j = 0; j < count; j++)
140 : {
141 450 : numeric* a = PGTYPESnumeric_new();
142 450 : numeric* s = PGTYPESnumeric_new();
143 450 : numeric* m = PGTYPESnumeric_new();
144 450 : numeric* d = PGTYPESnumeric_new();
145 450 : r = PGTYPESnumeric_add(numarr[i], numarr[j], a);
146 450 : if (r)
147 : {
148 0 : check_errno();
149 0 : printf("r: %d\n", r);
150 : }
151 : else
152 : {
153 450 : text = PGTYPESnumeric_to_asc(a, 10);
154 450 : printf("num[a,%d,%d]: %s\n", i, j, text);
155 450 : PGTYPESchar_free(text);
156 : }
157 450 : r = PGTYPESnumeric_sub(numarr[i], numarr[j], s);
158 450 : if (r)
159 : {
160 0 : check_errno();
161 0 : printf("r: %d\n", r);
162 : }
163 : else
164 : {
165 450 : text = PGTYPESnumeric_to_asc(s, 10);
166 450 : printf("num[s,%d,%d]: %s\n", i, j, text);
167 450 : PGTYPESchar_free(text);
168 : }
169 450 : r = PGTYPESnumeric_mul(numarr[i], numarr[j], m);
170 450 : if (r)
171 : {
172 0 : check_errno();
173 0 : printf("r: %d\n", r);
174 : }
175 : else
176 : {
177 450 : text = PGTYPESnumeric_to_asc(m, 10);
178 450 : printf("num[m,%d,%d]: %s\n", i, j, text);
179 450 : PGTYPESchar_free(text);
180 : }
181 450 : r = PGTYPESnumeric_div(numarr[i], numarr[j], d);
182 450 : if (r)
183 : {
184 30 : check_errno();
185 30 : printf("r: %d\n", r);
186 : }
187 : else
188 : {
189 420 : text = PGTYPESnumeric_to_asc(d, 10);
190 420 : printf("num[d,%d,%d]: %s\n", i, j, text);
191 420 : PGTYPESchar_free(text);
192 : }
193 :
194 450 : PGTYPESnumeric_free(a);
195 450 : PGTYPESnumeric_free(s);
196 450 : PGTYPESnumeric_free(m);
197 450 : PGTYPESnumeric_free(d);
198 : }
199 : }
200 :
201 32 : for (i = 0; i < count; i++)
202 : {
203 30 : text = PGTYPESnumeric_to_asc(numarr[i], -1);
204 30 : printf("%d: %s\n", i, text);
205 30 : PGTYPESchar_free(text);
206 30 : PGTYPESnumeric_free(numarr[i]);
207 : }
208 2 : free(numarr);
209 :
210 2 : return 0;
211 : }
212 :
213 : static void
214 52 : check_errno(void)
215 : {
216 52 : switch(errno)
217 : {
218 0 : case 0:
219 0 : printf("(no errno set) - ");
220 0 : break;
221 20 : case PGTYPES_NUM_OVERFLOW:
222 20 : printf("(errno == PGTYPES_NUM_OVERFLOW) - ");
223 20 : break;
224 0 : case PGTYPES_NUM_UNDERFLOW:
225 0 : printf("(errno == PGTYPES_NUM_UNDERFLOW) - ");
226 0 : break;
227 2 : case PGTYPES_NUM_BAD_NUMERIC:
228 2 : printf("(errno == PGTYPES_NUM_BAD_NUMERIC) - ");
229 2 : break;
230 30 : case PGTYPES_NUM_DIVIDE_ZERO:
231 30 : printf("(errno == PGTYPES_NUM_DIVIDE_ZERO) - ");
232 30 : break;
233 0 : default:
234 0 : printf("(unknown errno (%d))\n", errno);
235 0 : printf("(libc: (%s)) ", strerror(errno));
236 0 : break;
237 : }
238 :
239 52 : }
|