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