~ubuntu-branches/ubuntu/hardy/postgresql-8.4/hardy-backports

« back to all changes in this revision

Viewing changes to src/interfaces/ecpg/test/pgtypeslib/num_test2.pgc

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-03-20 12:00:13 UTC
  • Revision ID: james.westby@ubuntu.com-20090320120013-hogj7egc5mjncc5g
Tags: upstream-8.4~0cvs20090328
ImportĀ upstreamĀ versionĀ 8.4~0cvs20090328

Show diffs side-by-side

added added

removed removed

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