More type casts for char signed/unsigned and xmlChar. Using
[yaz-moved-to-github.git] / test / tstodr.c
1 /*
2  * Copyright (C) 1995-2005, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: tstodr.c,v 1.7 2005-08-22 20:34:23 adam Exp $
6  *
7  */
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <yaz/odr.h>
11 #include <yaz/oid.h>
12 #include "tstodrcodec.h"
13
14 #define MYOID  "1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19"
15
16 void tst_MySequence1(ODR encode, ODR decode)
17 {
18     char *ber_buf;
19     int ber_len;
20     Yc_MySequence *s = odr_malloc(encode, sizeof(*s));
21     Yc_MySequence *t;
22
23     s->first = odr_intdup(encode, 12345);
24     s->second = odr_malloc(encode, sizeof(*s->second));
25     s->second->buf = (unsigned char *) "hello";
26     s->second->len = 5;
27     s->second->size = 0;
28     s->third = odr_intdup(encode, 1);
29     s->fourth = odr_nullval();
30     s->fifth = odr_intdup(encode, YC_MySequence_enum1);
31     
32     s->myoid = odr_getoidbystr(decode, MYOID);
33
34     if (!yc_MySequence(encode, &s, 0, 0))
35         exit(1);
36     
37     ber_buf = odr_getbuf(encode, &ber_len, 0);
38
39     odr_setbuf(decode, ber_buf, ber_len, 0);
40
41     if (!yc_MySequence(decode, &t, 0, 0))
42         exit(2);
43     if (!t->first || *t->first != 12345)
44         exit(3);
45     if (!t->second || !t->second->buf || t->second->len != 5)
46         exit(4);
47     if (memcmp(t->second->buf, "hello", t->second->len))
48         exit(5);
49     if (!t->third || *t->third != 1)
50         exit(6);
51     if (!t->fourth)
52         exit(7);
53     if (!t->fifth || *t->fifth != YC_MySequence_enum1)
54         exit(8);
55     if (!t->myoid)
56         exit(9);
57     else
58     {
59         int *myoid = odr_getoidbystr(decode, MYOID);
60         struct oident *oident;
61
62         if (oid_oidcmp(myoid, t->myoid))
63             exit(10);
64         oident = oid_getentbyoid(t->myoid);
65     }
66 }
67
68 void tst_MySequence2(ODR encode, ODR decode)
69 {
70     Yc_MySequence *s = odr_malloc(encode, sizeof(*s));
71
72     s->first = 0;  /* deliberately miss this .. */
73     s->second = odr_malloc(encode, sizeof(*s->second));
74     s->second->buf = (unsigned char *) "hello";
75     s->second->len = 5;
76     s->second->size = 0;
77     s->third = odr_intdup(encode, 1);
78     s->fourth = odr_nullval();
79     s->fifth = odr_intdup(encode, YC_MySequence_enum1);
80     s->myoid = odr_getoidbystr(encode, MYOID);
81
82     if (yc_MySequence(encode, &s, 0, 0)) /* should fail */
83         exit(9);
84     if (odr_geterror(encode) != OREQUIRED)
85         exit(10);
86     if (strcmp(odr_getelement(encode), "first"))
87         exit(11);
88     odr_reset(encode);
89
90     if (odr_geterror(encode) != ONONE)
91         exit(12);
92     if (strcmp(odr_getelement(encode), ""))
93         exit(13);
94 }
95
96 void tst_MySequence3(ODR encode, ODR decode)
97 {
98     char buf[40];
99     int i;
100     Yc_MySequence *t;
101
102     srand(123);
103     for (i = 0; i<1000; i++)
104     {
105         int j;
106         for (j = 0; j<sizeof(buf); j++)
107             buf[j] = rand();
108
109         for (j = 1; j<sizeof(buf); j++)
110         {
111             odr_setbuf(decode, buf, j, 0);
112             yc_MySequence(decode, &t, 0, 0);
113             odr_reset(decode);
114         }
115     }
116 }
117
118 int main(int argc, char **argv)
119 {
120     ODR odr_encode = odr_createmem(ODR_ENCODE);
121     ODR odr_decode = odr_createmem(ODR_DECODE);
122
123     tst_MySequence1(odr_encode, odr_decode);
124     tst_MySequence2(odr_encode, odr_decode);
125     tst_MySequence3(odr_encode, odr_decode);
126
127     odr_destroy(odr_encode);
128     odr_destroy(odr_decode);
129     exit(0);
130 }
131 /*
132  * Local variables:
133  * c-basic-offset: 4
134  * indent-tabs-mode: nil
135  * End:
136  * vim: shiftwidth=4 tabstop=8 expandtab
137  */
138