Header And Logo

PostgreSQL
| The world's most advanced open source database.

compat_informix-dec_test.c

Go to the documentation of this file.
00001 /* Processed by ecpg (regression mode) */
00002 /* These include files are added by the preprocessor */
00003 #include <ecpglib.h>
00004 #include <ecpgerrno.h>
00005 #include <sqlca.h>
00006 /* Needed for informix compatibility */
00007 #include <ecpg_informix.h>
00008 /* End of automatic include section */
00009 #define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))
00010 
00011 #line 1 "dec_test.pgc"
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <pgtypes_numeric.h>
00015 #include <pgtypes_error.h>
00016 #include <decimal.h>
00017 #include <sqltypes.h>
00018 
00019 
00020 #line 1 "regression.h"
00021 
00022 
00023 
00024 
00025 
00026 
00027 #line 8 "dec_test.pgc"
00028 
00029 
00030 
00031 /*
00032 
00033 NOTE: This file has a different expect file for regression tests on MinGW32
00034 
00035 */
00036 
00037 
00038 
00039 
00040 /*
00041 TODO:
00042     deccmp => DECUNKNOWN
00043     decimal point: , and/or . ?
00044     ECPG_INFORMIX_BAD_EXPONENT ?
00045 */
00046 
00047 char* decs[] = { "2E394", "-2", ".794", "3.44", "592.49E21", "-32.84e4",
00048                  "2E-394", ".1E-2", "+.0", "-592.49E-07", "+32.84e-4",
00049                  ".500001", "-.5000001",
00050                  "1234567890123456789012345678.91", /* 30 digits should fit
00051                                                        into decimal */
00052                  "1234567890123456789012345678.921", /* 31 digits should NOT
00053                                                         fit into decimal */
00054                  "not a number",
00055                  NULL};
00056 
00057 
00058 static void
00059 check_errno(void);
00060 
00061 #define BUFSIZE 200
00062 
00063 int
00064 main(void)
00065 {
00066     decimal *dec, *din;
00067     char buf[BUFSIZE];
00068     long l;
00069     int i, j, k, q, r, count = 0;
00070     double dbl;
00071     decimal **decarr = (decimal **) calloc(1, sizeof(decimal));
00072 
00073     ECPGdebug(1, stderr);
00074 
00075     for (i = 0; decs[i]; i++)
00076     {
00077         dec = PGTYPESdecimal_new();
00078         r = deccvasc(decs[i], strlen(decs[i]), dec);
00079         if (r)
00080         {
00081             check_errno();
00082             printf("dec[%d,0]: r: %d\n", i, r);
00083             PGTYPESdecimal_free(dec);
00084             continue;
00085         }
00086         decarr = realloc(decarr, sizeof(decimal *) * (count + 1));
00087         decarr[count++] = dec;
00088 
00089         r = dectoasc(dec, buf, BUFSIZE-1, -1);
00090         if (r < 0) check_errno();
00091         printf("dec[%d,1]: r: %d, %s\n", i, r, buf);
00092 
00093         r = dectoasc(dec, buf, BUFSIZE-1, 0);
00094         if (r < 0) check_errno();
00095         printf("dec[%d,2]: r: %d, %s\n", i, r, buf);
00096         r = dectoasc(dec, buf, BUFSIZE-1, 1);
00097         if (r < 0) check_errno();
00098         printf("dec[%d,3]: r: %d, %s\n", i, r, buf);
00099         r = dectoasc(dec, buf, BUFSIZE-1, 2);
00100         if (r < 0) check_errno();
00101         printf("dec[%d,4]: r: %d, %s\n", i, r, buf);
00102 
00103         din = PGTYPESdecimal_new();
00104         r = dectoasc(din, buf, BUFSIZE-1, 2);
00105         if (r < 0) check_errno();
00106         printf("dec[%d,5]: r: %d, %s\n", i, r, buf);
00107 
00108         r = dectolong(dec, &l);
00109         if (r) check_errno();
00110         printf("dec[%d,6]: %ld (r: %d)\n", i, r?0L:l, r);
00111         if (r == 0)
00112         {
00113             r = deccvlong(l, din);
00114             if (r) check_errno();
00115             dectoasc(din, buf, BUFSIZE-1, 2);
00116             q = deccmp(dec, din);
00117             printf("dec[%d,7]: %s (r: %d - cmp: %d)\n", i, buf, r, q);
00118         }
00119 
00120         r = dectoint(dec, &k);
00121         if (r) check_errno();
00122         printf("dec[%d,8]: %d (r: %d)\n", i, r?0:k, r);
00123         if (r == 0)
00124         {
00125             r = deccvint(k, din);
00126             if (r) check_errno();
00127             dectoasc(din, buf, BUFSIZE-1, 2);
00128             q = deccmp(dec, din);
00129             printf("dec[%d,9]: %s (r: %d - cmp: %d)\n", i, buf, r, q);
00130         }
00131 
00132         if (i != 6)
00133         {
00134             /* underflow does not work reliable on several archs, so not testing it here */
00135             /* this is a libc problem since we only call strtod() */
00136             r = dectodbl(dec, &dbl);
00137             if (r) check_errno();
00138             printf("dec[%d,10]: %g (r: %d)\n", i, r?0.0:dbl, r);
00139         }
00140 
00141         PGTYPESdecimal_free(din);
00142         printf("\n");
00143     }
00144 
00145     /* add a NULL value */
00146     dec = PGTYPESdecimal_new();
00147     decarr = realloc(decarr, sizeof(decimal *) * (count + 1));
00148     decarr[count++] = dec;
00149 
00150     rsetnull(CDECIMALTYPE, (char *) decarr[count-1]);
00151     printf("dec[%d]: %sNULL\n", count-1,
00152         risnull(CDECIMALTYPE, (char *) decarr[count-1]) ? "" : "NOT ");
00153     printf("dec[0]: %sNULL\n",
00154         risnull(CDECIMALTYPE, (char *) decarr[0]) ? "" : "NOT ");
00155 
00156     r = dectoasc(decarr[3], buf, -1, -1);
00157     check_errno(); printf("dectoasc with len == -1: r: %d\n", r);
00158     r = dectoasc(decarr[3], buf, 0, -1);
00159     check_errno(); printf("dectoasc with len == 0: r: %d\n", r);
00160 
00161     for (i = 0; i < count; i++)
00162     {
00163         for (j = 0; j < count; j++)
00164         {
00165             decimal a, s, m, d;
00166             int c;
00167             c = deccmp(decarr[i], decarr[j]);
00168             printf("dec[c,%d,%d]: %d\n", i, j, c);
00169 
00170             r = decadd(decarr[i], decarr[j], &a);
00171             if (r)
00172             {
00173                 check_errno();
00174                 printf("r: %d\n", r);
00175             }
00176             else
00177             {
00178                 dectoasc(&a, buf, BUFSIZE-1, -1);
00179                 printf("dec[a,%d,%d]: %s\n", i, j, buf);
00180             }
00181 
00182             r = decsub(decarr[i], decarr[j], &s);
00183             if (r)
00184             {
00185                 check_errno();
00186                 printf("r: %d\n", r);
00187             }
00188             else
00189             {
00190                 dectoasc(&s, buf, BUFSIZE-1, -1);
00191                 printf("dec[s,%d,%d]: %s\n", i, j, buf);
00192             }
00193 
00194             r = decmul(decarr[i], decarr[j], &m);
00195             if (r)
00196             {
00197                 check_errno();
00198                 printf("r: %d\n", r);
00199             }
00200             else
00201             {
00202                 dectoasc(&m, buf, BUFSIZE-1, -1);
00203                 printf("dec[m,%d,%d]: %s\n", i, j, buf);
00204             }
00205 
00206             r = decdiv(decarr[i], decarr[j], &d);
00207             if (r)
00208             {
00209                 check_errno();
00210                 printf("r: %d\n", r);
00211             }
00212             else
00213             {
00214                 dectoasc(&d, buf, BUFSIZE-1, -1);
00215                 printf("dec[d,%d,%d]: %s\n", i, j, buf);
00216             }
00217         }
00218     }
00219 
00220     for (i = 0; i < count; i++)
00221     {
00222         dectoasc(decarr[i], buf, BUFSIZE-1, -1);
00223         printf("%d: %s\n", i, buf);
00224 
00225         PGTYPESdecimal_free(decarr[i]);
00226     }
00227     free(decarr);
00228 
00229     return (0);
00230 }
00231 
00232 static void
00233 check_errno(void)
00234 {
00235     switch(errno)
00236     {
00237         case 0:
00238             printf("(no errno set) - ");
00239             break;
00240         case ECPG_INFORMIX_NUM_OVERFLOW:
00241             printf("(errno == ECPG_INFORMIX_NUM_OVERFLOW) - ");
00242             break;
00243         case ECPG_INFORMIX_NUM_UNDERFLOW:
00244             printf("(errno == ECPG_INFORMIX_NUM_UNDERFLOW) - ");
00245             break;
00246         case PGTYPES_NUM_OVERFLOW:
00247             printf("(errno == PGTYPES_NUM_OVERFLOW) - ");
00248             break;
00249         case PGTYPES_NUM_UNDERFLOW:
00250             printf("(errno == PGTYPES_NUM_UNDERFLOW) - ");
00251             break;
00252         case PGTYPES_NUM_BAD_NUMERIC:
00253             printf("(errno == PGTYPES_NUM_BAD_NUMERIC) - ");
00254             break;
00255         case PGTYPES_NUM_DIVIDE_ZERO:
00256             printf("(errno == PGTYPES_NUM_DIVIDE_ZERO) - ");
00257             break;
00258         default:
00259             printf("(unknown errno (%d))\n", errno);
00260             printf("(libc: (%s)) ", strerror(errno));
00261             break;
00262     }
00263 }