LCOV - code coverage report
Current view: top level - src/tds/unittests - numeric.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 60 65 92.3 %
Date: 2026-11-25 04:57:11 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2005   Frediano Ziglio
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : 
      20             : #include "common.h"
      21             : #include <freetds/tds/convert.h>
      22             : #include <assert.h>
      23             : 
      24             : /* test numeric scale */
      25             : 
      26             : static int g_result = 0;
      27             : static TDSCONTEXT ctx;
      28             : 
      29             : static void
      30         890 : test0(const char *src, int prec, int scale, int prec2, unsigned char scale2)
      31             : {
      32             :         int i;
      33             :         char buf[256];
      34             :         char result[256];
      35             :         char *p;
      36             :         CONV_RESULT cr;
      37             :         TDS_NUMERIC num;
      38             : 
      39             :         /* get initial number */
      40         890 :         memset(&cr.n, 0, sizeof(cr.n));
      41         890 :         cr.n.precision = prec;
      42         890 :         cr.n.scale = scale;
      43         890 :         if (tds_convert(&ctx, SYBVARCHAR, src, (TDS_UINT)strlen(src), SYBNUMERIC, &cr) < 0) {
      44           0 :                 fprintf(stderr, "Error getting numeric %s(%d,%d)\n", src, prec, scale);
      45           0 :                 exit(1);
      46             :         }
      47         890 :         num = cr.n;
      48             : 
      49             :         /* change scale with string */
      50         890 :         tds_numeric_to_string(&num, buf);
      51        2270 :         while ((p = strchr(buf, '.')) != NULL)
      52         490 :                 memmove(p, p+1, strlen(p));
      53             : 
      54        3020 :         for (i = 0; i < (scale2 - scale); ++i)
      55        3020 :                 strcat(buf, "0");
      56        2030 :         for (i = 0; i < (scale - scale2); ++i) {
      57        2030 :                 assert(strlen(buf) > 1);
      58        2030 :                 buf[strlen(buf)-1] = 0;
      59             :         }
      60         890 :         if (scale2) {
      61         790 :                 size_t len = strlen(buf);
      62         790 :                 assert(len > scale2);
      63         790 :                 memmove(buf + len - scale2 + 1, buf + len - scale2, scale2 + 1);
      64         790 :                 buf[len-scale2] = '.';
      65             :         }
      66         890 :         cr.n.precision = prec2;
      67         890 :         cr.n.scale = scale2;
      68         890 :         if (tds_convert(&ctx, SYBVARCHAR, src, (TDS_UINT)strlen(src), SYBNUMERIC, &cr) < 0)
      69         260 :                 strcpy(buf, "error");
      70             : 
      71             :         /* change scale with function */
      72         890 :         if (tds_numeric_change_prec_scale(&num, prec2, scale2) < 0)
      73         260 :                 strcpy(result, "error");
      74             :         else
      75         630 :                 tds_numeric_to_string(&num, result);
      76             : 
      77         890 :         if (strcmp(buf, result) != 0) {
      78           0 :                 fprintf(stderr, "Failed! %s (%d,%d) -> (%d,%d)\n\tshould be %s\n\tis %s\n",
      79             :                         src, prec, scale, prec2, scale2, buf, result);
      80           0 :                 g_result = 1;
      81           0 :                 exit(1);
      82             :         } else {
      83         890 :                 printf("%s -> %s ok!\n", src, buf);
      84             :         }
      85         890 : }
      86             : 
      87             : static void
      88             : test(const char *src, int prec, int scale, int scale2)
      89             : {
      90         820 :         test0(src, prec, scale, prec, scale2);
      91             : }
      92             : 
      93          10 : TEST_MAIN()
      94             : {
      95             :         int i;
      96             :         char very_long[90];
      97             : 
      98          10 :         memset(&ctx, 0, sizeof(ctx));
      99             : 
     100             :         /* increase scale */
     101         110 :         for (i = 0; i < 10; ++i) {
     102         200 :                 test("1234", 18+i, 0, 2+i);
     103         200 :                 test("1234.1234", 18+i, 5, 3+i);
     104         200 :                 test("1234.1234", 22+i, 5+i, 12+i);
     105             :         }
     106             : 
     107             :         /* overflow */
     108          10 :         test("1234", 4, 0, 2);
     109         120 :         for (i = 1; i < 12; ++i)
     110         220 :                 test("1234", 3+i, 0, i);
     111         100 :         for (i = 2; i < 12; ++i)
     112         200 :                 test("1234", 2+i, 0, i);
     113             : 
     114             :         /* decrease scale */
     115          10 :         test("1234", 10, 4, 0);
     116         190 :         for (i = 2; i < 20; ++i)
     117         180 :                 test("1234.765", 30, 20, i);
     118             : 
     119          10 :         test0("765432.2", 30, 2, 20, 2);
     120          10 :         test0("765432.2", 30, 2, 40, 2);
     121          10 :         test0("765432.2", 30, 2, 6, 2);
     122             : 
     123             :         /* test big overflows */
     124          10 :         test0("10000000000000000000000000", 30, 0, 10, 0);
     125          10 :         test0("1000000000000000000", 30, 0, 10, 0);
     126             : 
     127          10 :         test0("10000000000000000", 30, 10, 19, 0);
     128          10 :         test0("10000000000000000", 30, 10, 12, 0);
     129             : 
     130             :         /* test potential internal buffer overflow */
     131          10 :         memset(very_long, '9', sizeof(very_long));
     132          10 :         very_long[76] = 0;
     133          10 :         test(very_long, 77, 0, 1);
     134             : 
     135             :         /* test potential internal buffer underflow */
     136          60 :         for (i = 1; i <= 5; ++i) {
     137          50 :                 test("0", i, 0, 1);
     138          50 :                 test("0", i, 1, 0);
     139             :         }
     140             : 
     141             : #if 0
     142             :         {
     143             :                 int p1, s1, p2, s2;
     144             :                 for (p1 = 1; p1 <= 77; ++p1) {
     145             :                         printf("(%d,%d) -> (%d,%d)\n", p1, s1, p2, s2);
     146             :                         for (s1 = 0; s1 < p1; ++s1)
     147             :                                 for (p2 = 1; p2 <= 77; ++p2)
     148             :                                         for (s2 = 0; s2 < p2; ++s2)
     149             :                                                 test0("9", p1, s1, p2, s2);
     150             :                 }
     151             :         }
     152             : #endif
     153             : 
     154          10 :         if (!g_result)
     155          10 :                 printf("All passed!\n");
     156             : 
     157          10 :         return g_result;
     158             : }

Generated by: LCOV version 1.13