LCOV - code coverage report
Current view: top level - src/tds/unittests - file_stream.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 24 24 100.0 %
Date: 2026-05-04 13:14:38 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2026 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             : /*
      21             :  * Check file streaming
      22             :  */
      23             : 
      24             : #include "common.h"
      25             : 
      26             : #include <assert.h>
      27             : 
      28             : #ifdef HAVE_UNISTD_H
      29             : #include <unistd.h>
      30             : #endif
      31             : 
      32             : #include <freetds/tds/stream.h>
      33             : 
      34             : static uint8_t data[1024 * 2];
      35             : 
      36          10 : TEST_MAIN()
      37             : {
      38             :         static const char terminators[3][4] = {
      39             :                 "\n", "\r\n", "\n\n"
      40             :         };
      41          10 :         size_t data_len = 0;
      42             :         int i;
      43             :         FILE *f;
      44             :         TDSFILESTREAM stream[1];
      45             :         char buf[128];
      46             : 
      47             :         /* fill some data containing different line terminators */
      48        1210 :         for (i = 0; data_len < sizeof(data) - 40; ++i)
      49        1200 :                 data_len += sprintf((char *) data + data_len, "This is line %d%s", i, terminators[i % 3]);
      50             : 
      51          10 :         f = fopen("file_stream.dat", "wb");
      52          10 :         assert(f);
      53          10 :         assert(fwrite(data, 1, data_len, f) == data_len);
      54          10 :         fclose(f);
      55             : 
      56          10 :         f = fopen("file_stream.dat", "rb");
      57          10 :         assert(f);
      58          10 :         fseek(f, 1234, SEEK_SET);
      59             : 
      60          10 :         assert(TDS_SUCCEED(tds_file_stream_init(stream, f)));
      61          10 :         assert(tds_file_stream_tell(stream) == 1234);
      62          10 :         assert(tds_file_stream_read_raw(stream, buf, 10) == 10);
      63          10 :         assert(memcmp(buf, "e 74\n\nThis", 10) == 0);
      64          10 :         assert(tds_file_stream_tell(stream) == 1244);
      65          10 :         assert(TDS_SUCCEED(tds_file_stream_seek_set(stream, 567)));
      66          10 :         assert(tds_file_stream_tell(stream) == 567);
      67          10 :         assert(tds_file_stream_read_raw(stream, buf, 20) == 20);
      68          10 :         assert(memcmp(buf, "e 34\r\nThis is line 3", 20) == 0);
      69          10 :         assert(tds_file_stream_tell(stream) == 587);
      70          10 :         assert(TDS_SUCCEED(tds_file_stream_close(stream)));
      71             : 
      72          10 :         unlink("file_stream.dat");
      73             : 
      74          10 :         return 0;
      75             : }

Generated by: LCOV version 1.13