LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/test/pgtypeslib - num_test2.pgc (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 122 138 88.4 %
Date: 2024-04-26 00:11:43 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14