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