Header And Logo

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

pgtypeslib-num_test2.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 /* End of automatic include section */
00007 #define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))
00008 
00009 #line 1 "num_test2.pgc"
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include <pgtypes_numeric.h>
00013 #include <pgtypes_error.h>
00014 #include <decimal.h>
00015 
00016 
00017 #line 1 "regression.h"
00018 
00019 
00020 
00021 
00022 
00023 
00024 #line 7 "num_test2.pgc"
00025 
00026 
00027 
00028 /*
00029 
00030 NOTE: This file has a different expect file for regression tests on MinGW32
00031 
00032 */
00033 
00034 
00035 char* nums[] = { "2E394", "-2", ".794", "3.44", "592.49E21", "-32.84e4",
00036                  "2E-394", ".1E-2", "+.0", "-592.49E-07", "+32.84e-4",
00037                  ".500001", "-.5000001",
00038                  "1234567890123456789012345678.91", /* 30 digits should fit
00039                                                        into decimal */
00040                  "1234567890123456789012345678.921", /* 31 digits should NOT
00041                                                         fit into decimal */
00042                  "not a number",
00043                  NULL};
00044 
00045 
00046 static void
00047 check_errno(void);
00048 
00049 int
00050 main(void)
00051 {
00052     char *text="error\n";
00053     char *endptr;
00054     numeric *num, *nin;
00055     decimal *dec;
00056     long l;
00057     int i, j, k, q, r, count = 0;
00058     double d;
00059     numeric **numarr = (numeric **) calloc(1, sizeof(numeric));
00060 
00061     ECPGdebug(1, stderr);
00062 
00063     for (i = 0; nums[i]; i++)
00064     {
00065         num = PGTYPESnumeric_from_asc(nums[i], &endptr);
00066         if (!num) check_errno();
00067         if (endptr != NULL)
00068         {
00069             printf("endptr of %d is not NULL\n", i);
00070             if (*endptr != '\0')
00071                 printf("*endptr of %d is not \\0\n", i);
00072         }
00073         if (!num) continue;
00074 
00075         numarr = realloc(numarr, sizeof(numeric *) * (count + 1));
00076         numarr[count++] = num;
00077 
00078         text = PGTYPESnumeric_to_asc(num, -1);
00079         if (!text) check_errno();
00080         printf("num[%d,1]: %s\n", i, text); free(text);
00081         text = PGTYPESnumeric_to_asc(num, 0);
00082         if (!text) check_errno();
00083         printf("num[%d,2]: %s\n", i, text); free(text);
00084         text = PGTYPESnumeric_to_asc(num, 1);
00085         if (!text) check_errno();
00086         printf("num[%d,3]: %s\n", i, text); free(text);
00087         text = PGTYPESnumeric_to_asc(num, 2);
00088         if (!text) check_errno();
00089         printf("num[%d,4]: %s\n", i, text); free(text);
00090 
00091         nin = PGTYPESnumeric_new();
00092         text = PGTYPESnumeric_to_asc(nin, 2);
00093         if (!text) check_errno();
00094         printf("num[%d,5]: %s\n", i, text); free(text);
00095 
00096         r = PGTYPESnumeric_to_long(num, &l);
00097         if (r) check_errno();
00098         printf("num[%d,6]: %ld (r: %d)\n", i, r?0L:l, r);
00099         if (r == 0)
00100         {
00101             r = PGTYPESnumeric_from_long(l, nin);
00102             if (r) check_errno();
00103             text = PGTYPESnumeric_to_asc(nin, 2);
00104             q = PGTYPESnumeric_cmp(num, nin);
00105             printf("num[%d,7]: %s (r: %d - cmp: %d)\n", i, text, r, q);
00106             free(text);
00107         }
00108 
00109         r = PGTYPESnumeric_to_int(num, &k);
00110         if (r) check_errno();
00111         printf("num[%d,8]: %d (r: %d)\n", i, r?0:k, r);
00112         if (r == 0)
00113         {
00114             r = PGTYPESnumeric_from_int(k, nin);
00115             if (r) check_errno();
00116             text = PGTYPESnumeric_to_asc(nin, 2);
00117             q = PGTYPESnumeric_cmp(num, nin);
00118             printf("num[%d,9]: %s (r: %d - cmp: %d)\n", i, text, r, q);
00119             free(text);
00120         }
00121 
00122         if (i != 6)
00123         {
00124             /* underflow does not work reliable on several archs, so not testing it here */
00125             /* this is a libc problem since we only call strtod() */
00126 
00127             r = PGTYPESnumeric_to_double(num, &d);
00128             if (r) check_errno();
00129             printf("num[%d,10]: %g (r: %d)\n", i, r?0.0:d, r);
00130         }
00131 
00132         /* do not test double to numeric because
00133          * - extra digits are different on different architectures
00134          * - PGTYPESnumeric_from_double internally calls PGTYPESnumeric_from_asc anyway
00135          */
00136 
00137         dec = PGTYPESdecimal_new();
00138         r = PGTYPESnumeric_to_decimal(num, dec);
00139         if (r) check_errno();
00140         /* we have no special routine for outputting decimal, it would
00141          * convert to a numeric anyway */
00142         printf("num[%d,11]: - (r: %d)\n", i, r);
00143         if (r == 0)
00144         {
00145             r = PGTYPESnumeric_from_decimal(dec, nin);
00146             if (r) check_errno();
00147             text = PGTYPESnumeric_to_asc(nin, 2);
00148             q = PGTYPESnumeric_cmp(num, nin);
00149             printf("num[%d,12]: %s (r: %d - cmp: %d)\n", i, text, r, q);
00150             free(text);
00151         }
00152 
00153         PGTYPESdecimal_free(dec);
00154         PGTYPESnumeric_free(nin);
00155         printf("\n");
00156     }
00157 
00158     for (i = 0; i < count; i++)
00159     {
00160         for (j = 0; j < count; j++)
00161         {
00162             numeric* a = PGTYPESnumeric_new();
00163             numeric* s = PGTYPESnumeric_new();
00164             numeric* m = PGTYPESnumeric_new();
00165             numeric* d = PGTYPESnumeric_new();
00166             r = PGTYPESnumeric_add(numarr[i], numarr[j], a);
00167             if (r)
00168             {
00169                 check_errno();
00170                 printf("r: %d\n", r);
00171             }
00172             else
00173             {
00174                 text = PGTYPESnumeric_to_asc(a, 10);
00175                 printf("num[a,%d,%d]: %s\n", i, j, text);
00176                 free(text);
00177             }
00178             r = PGTYPESnumeric_sub(numarr[i], numarr[j], s);
00179             if (r)
00180             {
00181                 check_errno();
00182                 printf("r: %d\n", r);
00183             }
00184             else
00185             {
00186                 text = PGTYPESnumeric_to_asc(s, 10);
00187                 printf("num[s,%d,%d]: %s\n", i, j, text);
00188                 free(text);
00189             }
00190             r = PGTYPESnumeric_mul(numarr[i], numarr[j], m);
00191             if (r)
00192             {
00193                 check_errno();
00194                 printf("r: %d\n", r);
00195             }
00196             else
00197             {
00198                 text = PGTYPESnumeric_to_asc(m, 10);
00199                 printf("num[m,%d,%d]: %s\n", i, j, text);
00200                 free(text);
00201             }
00202             r = PGTYPESnumeric_div(numarr[i], numarr[j], d);
00203             if (r)
00204             {
00205                 check_errno();
00206                 printf("r: %d\n", r);
00207             }
00208             else
00209             {
00210                 text = PGTYPESnumeric_to_asc(d, 10);
00211                 printf("num[d,%d,%d]: %s\n", i, j, text);
00212                 free(text);
00213             }
00214 
00215             PGTYPESnumeric_free(a);
00216             PGTYPESnumeric_free(s);
00217             PGTYPESnumeric_free(m);
00218             PGTYPESnumeric_free(d);
00219         }
00220     }
00221 
00222     for (i = 0; i < count; i++)
00223     {
00224         text = PGTYPESnumeric_to_asc(numarr[i], -1);
00225         printf("%d: %s\n", i, text);
00226         free(text);
00227         PGTYPESnumeric_free(numarr[i]);
00228     }
00229     free(numarr);
00230 
00231     return (0);
00232 }
00233 
00234 static void
00235 check_errno(void)
00236 {
00237     switch(errno)
00238     {
00239         case 0:
00240             printf("(no errno set) - ");
00241             break;
00242         case PGTYPES_NUM_OVERFLOW:
00243             printf("(errno == PGTYPES_NUM_OVERFLOW) - ");
00244             break;
00245         case PGTYPES_NUM_UNDERFLOW:
00246             printf("(errno == PGTYPES_NUM_UNDERFLOW) - ");
00247             break;
00248         case PGTYPES_NUM_BAD_NUMERIC:
00249             printf("(errno == PGTYPES_NUM_BAD_NUMERIC) - ");
00250             break;
00251         case PGTYPES_NUM_DIVIDE_ZERO:
00252             printf("(errno == PGTYPES_NUM_DIVIDE_ZERO) - ");
00253             break;
00254         default:
00255             printf("(unknown errno (%d))\n", errno);
00256             printf("(libc: (%s)) ", strerror(errno));
00257             break;
00258     }
00259 
00260 }