LCOV - code coverage report
Current view: top level - src/interfaces/ecpg/test/pgtypeslib - num_test2.pgc (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 88.4 % 138 122
Test Date: 2026-09-25 23:15:50 Functions: 100.0 % 2 2
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 77.9 % 68 53

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

Generated by: LCOV version 2.0-1