Fixed bug #687: Missing log lines. Immediate logging (i.e. flush) is no
[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.8 2006-01-29 21:59:13 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 #include <yaz/test.h>
15
16 #define MYOID  "1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19"
17
18 void tst_MySequence1(ODR encode, ODR decode)
19 {
20     int ret;
21     char *ber_buf;
22     int ber_len;
23     Yc_MySequence *s = odr_malloc(encode, sizeof(*s));
24     Yc_MySequence *t;
25
26     YAZ_CHECK(s);
27     s->first = odr_intdup(encode, 12345);
28     s->second = odr_malloc(encode, sizeof(*s->second));
29     s->second->buf = (unsigned char *) "hello";
30     s->second->len = 5;
31     s->second->size = 0;
32     s->third = odr_intdup(encode, 1);
33     s->fourth = odr_nullval();
34     s->fifth = odr_intdup(encode, YC_MySequence_enum1);
35     
36     s->myoid = odr_getoidbystr(decode, MYOID);
37
38     ret = yc_MySequence(encode, &s, 0, 0);
39     YAZ_CHECK(ret);
40     if (!ret)
41         return;
42     
43     ber_buf = odr_getbuf(encode, &ber_len, 0);
44
45     odr_setbuf(decode, ber_buf, ber_len, 0);
46
47     ret = yc_MySequence(decode, &t, 0, 0);
48     YAZ_CHECK(ret);
49     if (!ret)
50         return;
51
52     YAZ_CHECK(t);
53
54     YAZ_CHECK(t->first && *t->first == 12345);
55
56     YAZ_CHECK(t->second && t->second->buf && t->second->len == 5);
57
58     YAZ_CHECK(t->second && t->second->buf && t->second->len == 5 &&
59               memcmp(t->second->buf, "hello", t->second->len) == 0);
60
61     YAZ_CHECK(t->third && *t->third == 1);
62
63     YAZ_CHECK(t->fourth);
64
65     YAZ_CHECK(t->fifth && *t->fifth == YC_MySequence_enum1);
66
67     YAZ_CHECK(t->myoid);
68     if (t->myoid)
69     {
70         int *myoid = odr_getoidbystr(decode, MYOID);
71
72         YAZ_CHECK(oid_oidcmp(myoid, t->myoid) == 0);
73     }
74 }
75
76 void tst_MySequence2(ODR encode, ODR decode)
77 {
78     int ret;
79     Yc_MySequence *s = odr_malloc(encode, sizeof(*s));
80
81     YAZ_CHECK(s);
82     s->first = 0;  /* deliberately miss this .. */
83     s->second = odr_malloc(encode, sizeof(*s->second));
84     s->second->buf = (unsigned char *) "hello";
85     s->second->len = 5;
86     s->second->size = 0;
87     s->third = odr_intdup(encode, 1);
88     s->fourth = odr_nullval();
89     s->fifth = odr_intdup(encode, YC_MySequence_enum1);
90     s->myoid = odr_getoidbystr(encode, MYOID);
91
92     ret = yc_MySequence(encode, &s, 0, 0); /* should fail */
93     YAZ_CHECK(!ret);
94
95     YAZ_CHECK(odr_geterror(encode) == OREQUIRED);
96
97     YAZ_CHECK(strcmp(odr_getelement(encode), "first") == 0);
98     odr_reset(encode);
99
100     YAZ_CHECK(odr_geterror(encode) == ONONE);
101
102     YAZ_CHECK(strcmp(odr_getelement(encode), "") == 0);
103 }
104
105 void tst_MySequence3(ODR encode, ODR decode)
106 {
107     char buf[40];
108     int i;
109     Yc_MySequence *t;
110
111     srand(123);
112     for (i = 0; i<1000; i++)
113     {
114         int j;
115         for (j = 0; j<sizeof(buf); j++)
116             buf[j] = rand();
117
118         for (j = 1; j<sizeof(buf); j++)
119         {
120             odr_setbuf(decode, buf, j, 0);
121             yc_MySequence(decode, &t, 0, 0);
122             odr_reset(decode);
123         }
124     }
125 }
126
127 static void tst(void)
128 {
129     ODR odr_encode = odr_createmem(ODR_ENCODE);
130     ODR odr_decode = odr_createmem(ODR_DECODE);
131
132     YAZ_CHECK(odr_encode);
133     YAZ_CHECK(odr_decode);
134
135     tst_MySequence1(odr_encode, odr_decode);
136     tst_MySequence2(odr_encode, odr_decode);
137     tst_MySequence3(odr_encode, odr_decode);
138
139     odr_destroy(odr_encode);
140     odr_destroy(odr_decode);
141 }
142
143 int main(int argc, char **argv)
144 {
145     YAZ_CHECK_INIT(argc, argv);
146     tst();
147     YAZ_CHECK_TERM;
148 }
149
150 /*
151  * Local variables:
152  * c-basic-offset: 4
153  * indent-tabs-mode: nil
154  * End:
155  * vim: shiftwidth=4 tabstop=8 expandtab
156  */
157