NCBI C++ ToolKit
t0019.c
Go to the documentation of this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

1 /*
2  * Purpose: Test Some conversion, check trimming error and results
3  * Functions: dbconvert dberrhandle dbmsghandle dbinit dbexit
4  */
5 
6 #include "common.h"
7 #include <ctype.h>
8 
9 #include <common/test_assert.h>
10 
11 static int failure = 0;
12 
13 static const char *cur_result = "";
14 static const char *cur_test = "";
15 static int cur_line = 0;
16 
17 int test(int srctype, const void *srcdata, int srclen, int dsttype, int dstlen);
18 int err_handler(DBPROCESS * dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr);
19 
20 int
21 err_handler(DBPROCESS * dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr)
22 {
23  /*
24  * For server messages, cancel the query and rely on the
25  * message handler to spew the appropriate error messages out.
26  */
27  if (dberr == SYBESMSG)
28  return INT_CANCEL;
29 
30  if (dberr == 20049) {
31  fprintf(stderr, "OK: anticipated error %d (%s) arrived\n", dberr, dberrstr);
32  } else {
33  fprintf(stderr,
34  "DB-LIBRARY error (severity %d, dberr %d, oserr %d, dberrstr %s, oserrstr %s):\n",
35  severity, dberr, oserr, dberrstr ? dberrstr : "(null)", oserrstr ? oserrstr : "(null)");
36  }
37  fflush(stderr);
38 
39  return INT_CANCEL;
40 }
41 
42 int
43 test(int srctype, const void *srcdata, int srclen, int dsttype, int dstlen)
44 {
45  DBCHAR buf[10];
46  char s[20], *p;
47  int i, len, correct;
48  char out[256];
49 
50  memset(buf, '*', sizeof(buf));
51  len = dbconvert(NULL, srctype, (const BYTE*) srcdata, srclen, dsttype, (BYTE*) buf, dstlen);
52 
53  /* build result string */
54  sprintf(out, "len=%d", len);
55  p = s;
56  for (i = 0; i < sizeof(buf); ++i) {
57  *p++ = isprint((unsigned char) buf[i]) ? buf[i] : '.';
58  sprintf(strchr(out, 0), " %02X", (unsigned char) buf[i]);
59  }
60  *p = 0;
61 
62  correct = 0;
63  if (len == -1) {
64  if (strcmp(cur_result, "error") == 0)
65  correct = 1;
66  } else {
67  if (strcmp(cur_result, out) == 0)
68  correct = 1;
69  }
70  if (!correct) {
71  failure = 1;
72  printf("\nline: %d test: %s\n" "%s\n%s\n" "failed :( should be '%s'\n", cur_line, cur_test, s, out, cur_result);
73  }
74  return 0;
75 }
76 
77 #define TEST(s,out) \
78  { cur_result = out; cur_line = __LINE__; cur_test = #s; test s; }
79 
80 int
81 main(int argc, char *argv[])
82 {
83  if (dbinit() == FAIL)
84  return 1;
85 
88 
89  TEST((SYBBINARY, "ciao\0\0", 6, SYBBINARY, -2), "len=6 63 69 61 6F 00 00 2A 2A 2A 2A");
90  TEST((SYBCHAR, "ciao ", 6, SYBCHAR, -2), "len=6 63 69 61 6F 20 20 00 2A 2A 2A");
91  TEST((SYBCHAR, "ciao\0\0", 6, SYBCHAR, -2), "len=6 63 69 61 6F 00 00 00 2A 2A 2A");
92  TEST((SYBCHAR, "ciao ", 6, SYBCHAR, -1), "len=4 63 69 61 6F 00 2A 2A 2A 2A 2A");
93  TEST((SYBCHAR, "ciao\0\0", 6, SYBCHAR, -1), "len=6 63 69 61 6F 00 00 00 2A 2A 2A");
94  TEST((SYBCHAR, "ciao ", 6, SYBCHAR, 8), "len=6 63 69 61 6F 20 20 20 20 2A 2A");
95  TEST((SYBCHAR, "ciao\0\0", 6, SYBCHAR, 8), "len=6 63 69 61 6F 00 00 20 20 2A 2A");
96  TEST((SYBCHAR, "ciao ", 6, SYBCHAR, 4),
97  "len=4 63 69 61 6F 2A 2A 2A 2A 2A 2A" /* "error" */);
98  TEST((SYBCHAR, "ciao\0\0", 6, SYBCHAR, 4),
99  "len=4 63 69 61 6F 2A 2A 2A 2A 2A 2A" /* "error" */);
100  TEST((SYBCHAR, "ciao ", 6, SYBCHAR, 6), "len=6 63 69 61 6F 20 20 2A 2A 2A 2A");
101  TEST((SYBCHAR, "ciao\0\0", 6, SYBCHAR, 6), "len=6 63 69 61 6F 00 00 2A 2A 2A 2A");
102 
103  /* convert from NULL to BINARY */
104  TEST((SYBBINARY, "", 0, SYBBINARY, 6), "len=6 00 00 00 00 00 00 2A 2A 2A 2A");
105  TEST((SYBVARBINARY, "", 0, SYBBINARY, 6), "len=6 00 00 00 00 00 00 2A 2A 2A 2A");
106  TEST((SYBIMAGE, "", 0, SYBBINARY, 6), "len=6 00 00 00 00 00 00 2A 2A 2A 2A");
107  TEST((SYBBINARY, "", 0, SYBVARBINARY, 6), "len=6 00 00 00 00 00 00 2A 2A 2A 2A");
108  TEST((SYBVARBINARY, "", 0, SYBVARBINARY, 6), "len=6 00 00 00 00 00 00 2A 2A 2A 2A");
109  TEST((SYBIMAGE, "", 0, SYBVARBINARY, 6), "len=6 00 00 00 00 00 00 2A 2A 2A 2A");
110  TEST((SYBBINARY, "", 0, SYBIMAGE, 6), "len=6 00 00 00 00 00 00 2A 2A 2A 2A");
111  TEST((SYBVARBINARY, "", 0, SYBIMAGE, 6), "len=6 00 00 00 00 00 00 2A 2A 2A 2A");
112  TEST((SYBIMAGE, "", 0, SYBIMAGE, 6), "len=6 00 00 00 00 00 00 2A 2A 2A 2A");
113 
114  TEST((SYBBINARY, "", 0, SYBBINARY, -1), "len=0 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A");
115 
116  dbexit();
117  if (!failure)
118  printf("All tests passed!\n");
119  return failure;
120 }
int syb_msg_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate, int severity, char *msgtext, char *srvname, char *procname, int line)
Definition: common.c:334
std::ofstream out("events_result.xml")
main entry point for tests
#define NULL
Definition: ncbistd.hpp:225
DBINT dbconvert(DBPROCESS *dbproc, int srctype, const BYTE *src, DBINT srclen, int desttype, BYTE *dest, DBINT destlen)
cf.
Definition: dblib.c:2604
RETCODE dbinit(void)
Initialize db-lib.
Definition: dblib.c:674
EHANDLEFUNC dberrhandle(EHANDLEFUNC handler)
Set an error handler, for messages from db-lib.
Definition: dblib.c:5030
void dbexit()
Close server connections and free all related structures.
Definition: dblib.c:1559
MHANDLEFUNC dbmsghandle(MHANDLEFUNC handler)
Set a message handler, for messages from the server.
Definition: dblib.c:5049
char * buf
int i
int len
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
int isprint(Uchar c)
Definition: ncbictype.hpp:67
unsigned char BYTE
Definition: sybdb.h:334
#define SYBIMAGE
Definition: sybdb.h:186
char DBCHAR
Definition: sybdb.h:251
#define SYBCHAR
Definition: sybdb.h:160
#define SYBESMSG
Definition: sybdb.h:946
#define SYBBINARY
Definition: sybdb.h:196
#define FAIL
Definition: sybdb.h:586
#define SYBVARBINARY
Definition: sybdb.h:200
#define INT_CANCEL
Definition: sybdb.h:62
DBPROCESS * dbproc
Definition: t0013.c:18
static const char * cur_result
Definition: t0019.c:13
int main(int argc, char *argv[])
Definition: t0019.c:81
static int cur_line
Definition: t0019.c:15
int err_handler(DBPROCESS *dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr)
Definition: t0019.c:21
static int failure
Definition: t0019.c:11
#define TEST(s, out)
Definition: t0019.c:77
int test(int srctype, const void *srcdata, int srclen, int dsttype, int dstlen)
Definition: t0019.c:43
static const char * cur_test
Definition: t0019.c:14
Modified on Tue Apr 09 08:00:16 2024 by modify_doxy.py rev. 669887
Modified on Wed Apr 10 07:35:34 2024 by modify_doxy.py rev. 669887
Modified on Thu Apr 11 15:20:49 2024 by modify_doxy.py rev. 669887
Modified on Fri Apr 12 17:23:07 2024 by modify_doxy.py rev. 669887
Modified on Sat Apr 13 11:50:23 2024 by modify_doxy.py rev. 669887
Modified on Sun Apr 14 05:29:23 2024 by modify_doxy.py rev. 669887
Modified on Tue Apr 16 20:14:56 2024 by modify_doxy.py rev. 669887
Modified on Wed Apr 17 13:10:50 2024 by modify_doxy.py rev. 669887
Modified on Sat Apr 20 12:22:10 2024 by modify_doxy.py rev. 669887
Modified on Sun Apr 21 03:44:50 2024 by modify_doxy.py rev. 669887