LCOV - code coverage report
Current view: top level - src/tds/unittests - t0006.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 71 88 80.7 %
Date: 2025-04-15 09:57:00 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004  Brian Bruns
       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             : #include "common.h"
      20             : #include <freetds/convert.h>
      21             : 
      22             : static TDSCONTEXT ctx;
      23             : 
      24          10 : TEST_MAIN()
      25             : {
      26             :         TDSLOGIN *login;
      27             :         TDSSOCKET *tds;
      28          10 :         int verbose = 0;
      29             :         int rc;
      30             :         int i;
      31             : 
      32             :         /* variables for conversions */
      33             :         TDSCOLUMN *curcol;
      34             :         TDSRESULTINFO *resinfo;
      35             :         unsigned char *src;
      36             : 
      37             :         CONV_RESULT cr;
      38             :         TDS_INT srctype, srclen;
      39             : 
      40          10 :         int src_id = 0;
      41             :         double src_val;
      42             :         double src_err;
      43          10 :         double tolerance = 0.000001;
      44             : 
      45             :         char sql[256];
      46          10 :         int num_sybreal = 5;
      47             :         float sybreal[5];
      48          10 :         int num_sybflt8 = 7;
      49             :         double sybflt8[7];
      50             :         int result_type;
      51             : 
      52          10 :         memset(&ctx, 0, sizeof(ctx));
      53             : 
      54          10 :         sybreal[0] = (float) 1.1;
      55          10 :         sybreal[1] = (float) 12345678;
      56          10 :         sybreal[2] = (float) 0.012345678;
      57          10 :         sybreal[3] = (float) 1.234567890e+20;
      58          10 :         sybreal[4] = (float) 1.234567890e-20;
      59             : 
      60          10 :         sybflt8[0] = 1.1;
      61          10 :         sybflt8[1] = 1234567890123456.0;
      62          10 :         sybflt8[2] = 0.01234567890123456;
      63          10 :         sybflt8[3] = 1.234567890123456e+20;
      64          10 :         sybflt8[4] = 1.234567890123456e-20;
      65          10 :         sybflt8[5] = 1.234567890123456e+200;
      66          10 :         sybflt8[6] = 1.234567890123456e-200;
      67             : 
      68          10 :         printf("%s: Test SYBREAL, SYBFLT8 values\n", __FILE__);
      69          10 :         rc = try_tds_login(&login, &tds, __FILE__, verbose);
      70          10 :         if (rc != TDS_SUCCESS) {
      71           0 :                 fprintf(stderr, "try_tds_login() failed\n");
      72           0 :                 return 1;
      73             :         }
      74             : 
      75             : 
      76             :         /*
      77             :          * SYBREAL tests
      78             :          */
      79             :         if (verbose)
      80             :                 printf("Starting SYBREAL tests\n");
      81          10 :         rc = run_query(tds, "DROP TABLE #test_table");
      82          10 :         if (rc != TDS_SUCCESS) {
      83             :                 return 1;
      84             :         }
      85          10 :         rc = run_query(tds, "CREATE TABLE #test_table (id int, val real)");
      86          10 :         if (rc != TDS_SUCCESS) {
      87             :                 return 1;
      88             :         }
      89             : 
      90          50 :         for (i = 0; i < num_sybreal; i++) {
      91          50 :                 sprintf(sql, "INSERT #test_table (id, val) VALUES (%d, %.8g)", i, sybreal[i]);
      92             :                 if (verbose)
      93             :                         printf("%s\n", sql);
      94          50 :                 rc = run_query(tds, sql);
      95          50 :                 if (rc != TDS_SUCCESS) {
      96             :                         return 1;
      97             :                 }
      98             :         }
      99             : 
     100          10 :         rc = tds_submit_query(tds, "SELECT * FROM #test_table");
     101             : 
     102          70 :         while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_RETURN_ROW|TDS_RETURN_COMPUTE)) == TDS_SUCCESS) {
     103          50 :                 switch (result_type) {
     104          50 :                 case TDS_ROW_RESULT:
     105          50 :                         resinfo = tds->res_info;
     106         150 :                         for (i = 0; i < resinfo->num_cols; i++) {
     107         100 :                                 curcol = resinfo->columns[i];
     108         100 :                                 src = curcol->column_data;
     109             :                                 if (verbose) {
     110             :                                         srctype = curcol->column_type;
     111             :                                         srclen = curcol->column_size;
     112             :                                         tds_convert(&ctx, srctype, src, srclen, SYBCHAR, &cr);
     113             :                                         printf("col %i is %s\n", i, cr.c);
     114             :                                 }
     115         100 :                                 if (i == 0) {
     116          50 :                                         src_id = *(int *) src;
     117             :                                 } else {
     118          50 :                                         src_val = *(float *) src;
     119          50 :                                         src_err = src_val - sybreal[src_id];
     120          50 :                                         if (src_err != 0.0) {
     121           0 :                                                 src_err = src_err / src_val;
     122             :                                         }
     123          50 :                                         if (src_err < -tolerance || src_err > tolerance) {
     124           0 :                                                 fprintf(stderr, "SYBREAL expected %.8g  got %.8g\n",
     125             :                                                         sybreal[src_id], src_val);
     126           0 :                                                 fprintf(stderr, "Error was %.4g%%\n", 100 * src_err);
     127           0 :                                                 return 1;
     128             :                                         }
     129             :                                 }
     130             :                         }
     131             :                 case TDS_COMPUTE_RESULT:
     132             :                         break;
     133           0 :                 default:
     134           0 :                         fprintf(stderr, "tds_process_tokens() unexpected result\n");
     135           0 :                         break;
     136             :                 }
     137             :         }
     138          10 :         if (rc != TDS_NO_MORE_RESULTS) {
     139           0 :                 fprintf(stderr, "tds_process_tokens() unexpected return\n");
     140             :         }
     141             : 
     142             : 
     143             :         /*
     144             :          * SYBFLT8 tests
     145             :          */
     146             :         if (verbose)
     147             :                 printf("Starting SYBFLT8 tests\n");
     148          10 :         rc = run_query(tds, "DROP TABLE #test_table");
     149          10 :         if (rc != TDS_SUCCESS) {
     150             :                 return 1;
     151             :         }
     152          10 :         rc = run_query(tds, "CREATE TABLE #test_table (id int, val float(48))");
     153          10 :         if (rc != TDS_SUCCESS) {
     154             :                 return 1;
     155             :         }
     156             : 
     157          70 :         for (i = 0; i < num_sybflt8; i++) {
     158          70 :                 sprintf(sql, "INSERT #test_table (id, val) VALUES (%d, %.15g)", i, sybflt8[i]);
     159             :                 if (verbose)
     160             :                         printf("%s\n", sql);
     161          70 :                 rc = run_query(tds, sql);
     162          70 :                 if (rc != TDS_SUCCESS) {
     163             :                         return 1;
     164             :                 }
     165             :         }
     166             : 
     167          10 :         rc = tds_submit_query(tds, "SELECT * FROM #test_table");
     168          90 :         while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_RETURN_ROW|TDS_RETURN_COMPUTE)) == TDS_SUCCESS) {
     169          70 :                 switch (result_type) {
     170          70 :                 case TDS_ROW_RESULT:
     171          70 :                         resinfo = tds->res_info;
     172         210 :                         for (i = 0; i < resinfo->num_cols; i++) {
     173         140 :                                 curcol = resinfo->columns[i];
     174         140 :                                 src = curcol->column_data;
     175             :                                 if (verbose) {
     176             :                                         srctype = curcol->column_type;
     177             :                                         srclen = curcol->column_size;
     178             :                                         tds_convert(&ctx, srctype, src, srclen, SYBCHAR, &cr);
     179             :                                         printf("col %i is %s\n", i, cr.c);
     180             :                                 }
     181         140 :                                 if (i == 0) {
     182          70 :                                         src_id = *(int *) src;
     183             :                                 } else {
     184          70 :                                         memcpy(&src_val, src, 8);
     185          70 :                                         src_err = src_val - sybflt8[src_id];
     186          70 :                                         if (src_err != 0.0) {
     187          60 :                                                 src_err = src_err / src_val;
     188             :                                         }
     189          70 :                                         if (src_err < -tolerance || src_err > tolerance) {
     190           0 :                                                 fprintf(stderr, "SYBFLT8 expected %.16g  got %.16g\n",
     191             :                                                         sybflt8[src_id], src_val);
     192           0 :                                                 fprintf(stderr, "Error was %.4g%%\n", 100 * src_err);
     193           0 :                                                 return 1;
     194             :                                         }
     195             :                                 }
     196             :                         }
     197             :                 case TDS_COMPUTE_RESULT:
     198             :                         break;
     199           0 :                 default:
     200           0 :                         fprintf(stderr, "tds_process_tokens() returned unexpected result\n");
     201           0 :                         break;
     202             :                 }
     203             :         }
     204          10 :         if (rc != TDS_NO_MORE_RESULTS) {
     205           0 :                 fprintf(stderr, "tds_process_tokens() unexpected return\n");
     206             :         }
     207             : 
     208          10 :         try_tds_logout(login, tds, verbose);
     209          10 :         return 0;
     210             : }

Generated by: LCOV version 1.13