LTP GCOV extension - code coverage report
Current view: directory - tds/unittests - iconv_fread.c
Test: FreeTDS coverage
Date: 2008-09-03 Instrumented lines: 34
Code covered: 76.5 % Executed lines: 26

       1                 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2                 :  * Copyright (C) 2006   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 <tdsiconv.h>
      22                 : 
      23                 : #if HAVE_UNISTD_H
      24                 : #undef getpid
      25                 : #include <unistd.h>
      26                 : #endif /* HAVE_UNISTD_H */
      27                 : 
      28                 : #include <assert.h>
      29                 : 
      30                 : /* test tds_iconv_fread */
      31                 : 
      32                 : static char software_version[] = "$Id: iconv_fread.c,v 1.4 2007/11/26 14:44:37 jklowden Exp $";
      33                 : static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
      34                 : 
      35                 : int
      36                 : main(int argc, char **argv)
      37               3 : {
      38               3 :         iconv_t cd = tds_sys_iconv_open("ISO-8859-1", "UTF-8");
      39                 :         static const char out_file[] = "iconv_fread.out";
      40                 :         char buf[256];
      41                 :         int i;
      42                 :         FILE *f;
      43                 : 
      44               3 :         if (cd == (iconv_t) - 1) {
      45               0 :                 fprintf(stderr, "Error creating conversion, giving up!\n");
      46               0 :                 return 0;
      47                 :         }
      48                 : 
      49               3 :         f = fopen(out_file, "w+b");
      50               3 :         if (!f) {
      51               0 :                 fprintf(stderr, "Error opening file!\n");
      52               0 :                 return 1;
      53                 :         }
      54                 : 
      55              99 :         for (i = 0; i < 32; ++i) {
      56                 :                 TDS_CHAR out[512];
      57              96 :                 size_t out_len = sizeof(out), res;
      58              96 :                 const unsigned char x = 0x90;
      59                 : 
      60                 :                 /* write test string to file */
      61              96 :                 if (fseek(f, 0L, SEEK_SET)) {
      62               0 :                         fprintf(stderr, "Error seeking!\n");
      63               0 :                         return 1;
      64                 :                 }
      65              96 :                 memset(buf, 'a', i);
      66              96 :                 buf[i] = 0xC0 + (x >> 6);
      67              96 :                 buf[i+1] = 0x80 + (x & 0x3f);
      68                 : 
      69              96 :                 fwrite(buf, 1, i+2, f);
      70              96 :                 if (fseek(f, 0L, SEEK_SET)) {
      71               0 :                         fprintf(stderr, "Error seeking!\n");
      72               0 :                         return 1;
      73                 :                 }
      74                 : 
      75                 :                 /* convert it */
      76              96 :                 memset(out, 'x', sizeof(out));
      77              96 :                 res = tds_iconv_fread(cd, f, i+2, 0, out, &out_len);
      78              96 :                 printf("res %u out_len %u\n", (unsigned int) res, (unsigned int) out_len);
      79                 : 
      80                 :                 /* test */
      81              96 :                 memset(buf, 'a', i);
      82              96 :                 buf[i] = 0x90;
      83              96 :                 assert(res == 0);
      84              96 :                 assert(sizeof(out) - out_len == i+1);
      85              96 :                 assert(memcmp(out, buf, i+1) == 0);
      86                 :         }
      87               3 :         fclose(f);
      88               3 :         unlink(out_file);
      89                 : 
      90               3 :         tds_sys_iconv_close(cd);
      91               3 :         return 0;
      92                 : }

Generated by: LTP GCOV extension version 1.6