LCOV - code coverage report
Current view: top level - src/dblib/unittests - t0003.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 50 66 75.8 %
Date: 2024-04-18 21:21:48 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* 
       2             :  * Purpose: Test buffering
       3             :  * Functions: dbclrbuf dbgetrow dbsetopt 
       4             :  */
       5             : 
       6             : #include "common.h"
       7             : 
       8             : int failed = 0;
       9             : 
      10             : 
      11             : int
      12           8 : main(int argc, char **argv)
      13             : {
      14             :         RETCODE rc;
      15           8 :         const int rows_to_add = 50;
      16             :         LOGINREC *login;
      17             :         DBPROCESS *dbproc;
      18             :         int i;
      19             :         char teststr[1024];
      20             :         DBINT testint;
      21             : 
      22           8 :         set_malloc_options();
      23             : 
      24           8 :         read_login_info(argc, argv);
      25             : 
      26           8 :         printf("Starting %s\n", argv[0]);
      27             : 
      28             :         /* Fortify_EnterScope(); */
      29           8 :         dbinit();
      30             : 
      31           8 :         dberrhandle(syb_err_handler);
      32           8 :         dbmsghandle(syb_msg_handler);
      33             : 
      34           8 :         printf("About to logon\n");
      35             : 
      36           8 :         login = dblogin();
      37           8 :         DBSETLPWD(login, PASSWORD);
      38           8 :         DBSETLUSER(login, USER);
      39           8 :         DBSETLAPP(login, "t0003");
      40           8 :         DBSETLHOST(login, "ntbox.dntis.ro");
      41             : 
      42           8 :         printf("About to open\n");
      43             : 
      44           8 :         dbproc = dbopen(login, SERVER);
      45           8 :         if (strlen(DATABASE))
      46           8 :                 dbuse(dbproc, DATABASE);
      47           8 :         dbloginfree(login);
      48             : 
      49             : #ifdef MICROSOFT_DBLIB
      50             :         dbsetopt(dbproc, DBBUFFER, "100");
      51             : #else
      52           8 :         dbsetopt(dbproc, DBBUFFER, "100", 0);
      53             : #endif
      54             : 
      55           8 :         printf("creating table\n");
      56           8 :         sql_cmd(dbproc);
      57           8 :         dbsqlexec(dbproc);
      58           8 :         while (dbresults(dbproc) != NO_MORE_RESULTS) {
      59             :                 /* nop */
      60             :         }
      61             : 
      62           8 :         printf("insert\n");
      63         400 :         for (i = 1; i < rows_to_add; i++) {
      64         392 :                 sql_cmd(dbproc);
      65         392 :                 dbsqlexec(dbproc);
      66         392 :                 while (dbresults(dbproc) != NO_MORE_RESULTS) {
      67             :                         /* nop */
      68             :                 }
      69             :         }
      70             : 
      71           8 :         printf("select\n");
      72           8 :         sql_cmd(dbproc);
      73           8 :         dbsqlexec(dbproc);
      74             : 
      75           8 :         if (dbresults(dbproc) != SUCCEED) {
      76           0 :                 failed = 1;
      77           0 :                 printf("Was expecting a result set.");
      78           0 :                 exit(1);
      79             :         }
      80             : 
      81          16 :         for (i = 1; i <= dbnumcols(dbproc); i++)
      82          16 :                 printf("col %d is %s\n", i, dbcolname(dbproc, i));
      83             : 
      84           8 :         dbbind(dbproc, 1, INTBIND, 0, (BYTE *) & testint);
      85           8 :         dbbind(dbproc, 2, STRINGBIND, 0, (BYTE *) teststr);
      86             : 
      87         400 :         for (i = 1; i < rows_to_add; i++) {
      88             :         char expected[1024];
      89             : 
      90         392 :                 sprintf(expected, "row %03d", i);
      91             : 
      92         392 :                 if (i % 100 == 0) {
      93           0 :                         dbclrbuf(dbproc, 100);
      94             :                 }
      95             : 
      96         392 :                 if (REG_ROW != dbnextrow(dbproc)) {
      97           0 :                         failed = 1;
      98           0 :                         fprintf(stderr, "Failed.  Expected a row\n");
      99           0 :                         exit(1);
     100             :                 }
     101         392 :                 if (testint != i) {
     102           0 :                         failed = 1;
     103           0 :                         fprintf(stderr, "Failed.  Expected i to be %d, was %d\n", i, (int) testint);
     104           0 :                         abort();
     105             :                 }
     106         392 :                 if (0 != strncmp(teststr, expected, strlen(expected))) {
     107           0 :                         failed = 1;
     108           0 :                         printf("Failed.  Expected s to be |%s|, was |%s|\n", expected, teststr);
     109           0 :                         abort();
     110             :                 }
     111         392 :                 printf("Read a row of data -> %d %s\n", (int) testint, teststr);
     112             :         }
     113             : 
     114           8 :         dbclrbuf(dbproc, 1);
     115           8 :         rc = dbnextrow(dbproc);
     116           8 :         if (rc != NO_MORE_ROWS) {
     117           0 :                 failed = 1;
     118           0 :                 fprintf(stderr, "Was expecting no more rows.  (rc=%d)\n", rc);
     119           0 :                 exit(1);
     120             :         }
     121             : 
     122           8 :         dbexit();
     123             : 
     124           8 :         printf("%s %s\n", __FILE__, (failed ? "failed!" : "OK"));
     125           8 :         return failed ? 1 : 0;
     126             : }

Generated by: LCOV version 1.13