LTP GCOV extension - code coverage report
Current view: directory - dblib/unittests - t0006.c
Test: FreeTDS coverage
Date: 2008-09-05 Instrumented lines: 100
Code covered: 85.0 % Executed lines: 85

       1                 : /* 
       2                 :  * Purpose: Test fetching 50 rows in two result sets with a 5000-row buffer.
       3                 :  * Functions: dbbind dbcmd dbnextrow dbnumcols dbresults dbsetopt dbsqlexec
       4                 :  */
       5                 : 
       6                 : #if HAVE_CONFIG_H
       7                 : #include <config.h>
       8                 : #endif /* HAVE_CONFIG_H */
       9                 : 
      10                 : #include <stdio.h>
      11                 : 
      12                 : #if HAVE_STDLIB_H
      13                 : #include <stdlib.h>
      14                 : #endif /* HAVE_STDLIB_H */
      15                 : 
      16                 : #if HAVE_STRING_H
      17                 : #include <string.h>
      18                 : #endif /* HAVE_STRING_H */
      19                 : 
      20                 : #include <sqlfront.h>
      21                 : #include <sqldb.h>
      22                 : 
      23                 : #include "common.h"
      24                 : 
      25                 : 
      26                 : static char software_version[] = "$Id: t0006.c,v 1.14 2005/05/23 08:06:25 freddy77 Exp $";
      27                 : static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
      28                 : 
      29                 : 
      30                 : static char teststr[1024];
      31                 : static DBINT testint;
      32                 : 
      33                 : static int failed = 0;
      34                 : 
      35                 : 
      36                 : static void
      37                 : get_results(DBPROCESS * dbproc, int start)
      38               4 : {
      39               4 :         int current = start - 1;
      40                 : 
      41             156 :         while (REG_ROW == dbnextrow(dbproc)) {
      42                 :         char expected[1024];
      43                 : 
      44             148 :                 current++;
      45             148 :                 sprintf(expected, "row %04d", current);
      46                 : 
      47             148 :                 add_bread_crumb();
      48                 : 
      49             148 :                 if (testint != current) {
      50               0 :                         fprintf(stderr, "Failed.  Expected i to be %d, was %d\n", current, (int) testint);
      51               0 :                         abort();
      52                 :                 }
      53             148 :                 if (0 != strncmp(teststr, expected, strlen(expected))) {
      54               0 :                         fprintf(stdout, "Failed.  Expected s to be |%s|, was |%s|\n", expected, teststr);
      55               0 :                         abort();
      56                 :                 }
      57             148 :                 printf("Read a row of data -> %d %s\n", (int) testint, teststr);
      58                 :         }
      59               4 : }
      60                 : 
      61                 : 
      62                 : int
      63                 : main(int argc, char **argv)
      64               2 : {
      65                 :         RETCODE rc;
      66               2 :         const int rows_to_add = 50;
      67                 :         LOGINREC *login;
      68                 :         DBPROCESS *dbproc;
      69                 :         int i;
      70                 : 
      71               2 :         set_malloc_options();
      72                 : 
      73               2 :         read_login_info(argc, argv);
      74               2 :         fprintf(stdout, "Start\n");
      75               2 :         add_bread_crumb();
      76                 : 
      77                 :         /* Fortify_EnterScope(); */
      78               2 :         dbinit();
      79                 : 
      80               2 :         add_bread_crumb();
      81               2 :         dberrhandle(syb_err_handler);
      82               2 :         dbmsghandle(syb_msg_handler);
      83                 : 
      84               2 :         fprintf(stdout, "About to logon\n");
      85                 : 
      86               2 :         add_bread_crumb();
      87               2 :         login = dblogin();
      88               2 :         DBSETLPWD(login, PASSWORD);
      89               2 :         DBSETLUSER(login, USER);
      90               2 :         DBSETLAPP(login, "t0006");
      91               2 :         DBSETLHOST(login, "ntbox.dntis.ro");
      92                 : 
      93               2 :         fprintf(stdout, "About to open\n");
      94                 : 
      95               2 :         add_bread_crumb();
      96               2 :         dbproc = dbopen(login, SERVER);
      97               2 :         if (strlen(DATABASE))
      98               2 :                 dbuse(dbproc, DATABASE);
      99               2 :         add_bread_crumb();
     100               2 :         dbloginfree(login);
     101               2 :         add_bread_crumb();
     102                 : 
     103                 : #ifdef MICROSOFT_DBLIB
     104                 :         dbsetopt(dbproc, DBBUFFER, "5000");
     105                 : #else
     106               2 :         dbsetopt(dbproc, DBBUFFER, "5000", 0);
     107                 : #endif
     108                 : 
     109               2 :         add_bread_crumb();
     110                 : 
     111               2 :         fprintf(stdout, "creating table\n");
     112               2 :         dbcmd(dbproc, "create table #dblib0006 (i int not null, s char(10) not null)");
     113               2 :         dbsqlexec(dbproc);
     114               4 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
     115                 :                 /* nop */
     116                 :         }
     117                 : 
     118               2 :         fprintf(stdout, "insert\n");
     119             100 :         for (i = 1; i < rows_to_add; i++) {
     120                 :         char cmd[1024];
     121                 : 
     122              98 :                 sprintf(cmd, "insert into #dblib0006 values (%d, 'row %04d')", i, i);
     123              98 :                 dbcmd(dbproc, cmd);
     124              98 :                 dbsqlexec(dbproc);
     125             196 :                 while (dbresults(dbproc) != NO_MORE_RESULTS) {
     126                 :                         /* nop */
     127                 :                 }
     128                 :         }
     129                 : 
     130               2 :         fprintf(stdout, "first select\n");
     131               2 :         if (SUCCEED != dbcmd(dbproc, "select * from #dblib0006 where i<50 order by i")) {
     132               0 :                 fprintf(stderr, "%s:%d: dbcmd failed\n", __FILE__, __LINE__);
     133               0 :                 failed = 1;
     134                 :         }
     135               2 :         if (SUCCEED != dbsqlexec(dbproc)) {
     136               0 :                 fprintf(stderr, "%s:%d: dbsqlexec failed\n", __FILE__, __LINE__);
     137               0 :                 failed = 1;
     138                 :         }
     139               2 :         add_bread_crumb();
     140                 : 
     141                 : 
     142               2 :         if (dbresults(dbproc) != SUCCEED) {
     143               0 :                 add_bread_crumb();
     144               0 :                 fprintf(stdout, "%s:%d: Was expecting a result set.", __FILE__, __LINE__);
     145               0 :                 failed = 1;
     146               0 :                 exit(1);
     147                 :         }
     148               2 :         add_bread_crumb();
     149                 : 
     150               6 :         for (i = 1; i <= dbnumcols(dbproc); i++) {
     151               4 :                 add_bread_crumb();
     152               4 :                 printf("col %d is %s\n", i, dbcolname(dbproc, i));
     153               4 :                 add_bread_crumb();
     154                 :         }
     155                 : 
     156               2 :         add_bread_crumb();
     157               2 :         dbbind(dbproc, 1, INTBIND, -1, (BYTE *) & testint);
     158               2 :         add_bread_crumb();
     159               2 :         dbbind(dbproc, 2, STRINGBIND, -1, (BYTE *) teststr);
     160               2 :         add_bread_crumb();
     161                 : 
     162               2 :         get_results(dbproc, 1);
     163               2 :         add_bread_crumb();
     164                 : 
     165               2 :         testint = -1;
     166               2 :         strcpy(teststr, "bogus");
     167               2 :         fprintf(stdout, "second select\n");
     168               2 :         dbcmd(dbproc, "select * from #dblib0006 where i>=25 order by i");
     169               2 :         dbsqlexec(dbproc);
     170               2 :         add_bread_crumb();
     171                 : 
     172                 : 
     173               2 :         if ((rc = dbresults(dbproc)) != SUCCEED) {
     174               0 :                 add_bread_crumb();
     175               0 :                 fprintf(stdout, "%s:%d: Was expecting a result set. (rc=%d)\n", __FILE__, __LINE__, rc);
     176               0 :                 failed = 1;
     177                 :         }
     178                 : 
     179               2 :         if (!failed) {
     180               2 :                 add_bread_crumb();
     181                 : 
     182               2 :                 add_bread_crumb();
     183               2 :                 dbbind(dbproc, 1, INTBIND, -1, (BYTE *) & testint);
     184               2 :                 add_bread_crumb();
     185               2 :                 dbbind(dbproc, 2, STRINGBIND, -1, (BYTE *) teststr);
     186               2 :                 add_bread_crumb();
     187                 : 
     188               2 :                 get_results(dbproc, 25);
     189                 :         }
     190               2 :         add_bread_crumb();
     191               2 :         dbexit();
     192               2 :         add_bread_crumb();
     193                 : 
     194               2 :         fprintf(stdout, "dblib %s on %s\n", (failed ? "failed!" : "okay"), __FILE__);
     195               2 :         free_bread_crumb();
     196               2 :         return failed ? 1 : 0;
     197                 : }

Generated by: LTP GCOV extension version 1.6