~ubuntu-branches/ubuntu/precise/postgresql-9.1/precise-security

« back to all changes in this revision

Viewing changes to src/interfaces/ecpg/test/expected/pgtypeslib-num_test2.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

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