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