Source restructure. yaz-marcdump part of installation
[yaz-moved-to-github.git] / test / tstodr.c
1 /*
2  * Copyright (c) 1995-2003, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Id: tstodr.c,v 1.1 2003-10-27 12:21:38 adam Exp $
6  *
7  */
8 #include <stdio.h>
9 #include <yaz/odr.h>
10 #include "tstodrcodec.h"
11
12 void tst_MySequence1(ODR encode, ODR decode)
13 {
14     char *ber_buf;
15     int ber_len;
16     Yc_MySequence *s = odr_malloc(encode, sizeof(*s));
17     Yc_MySequence *t;
18
19     s->first = odr_intdup(encode, 12345);
20     s->second = odr_malloc(encode, sizeof(*s->second));
21     s->second->buf = "hello";
22     s->second->len = 5;
23     s->second->size = 0;
24     s->third = odr_intdup(encode, 1);
25     s->fourth = odr_nullval();
26     s->fifth = odr_intdup(encode, YC_MySequence_enum1);
27
28     if (!yc_MySequence(encode, &s, 0, 0))
29         exit(1);
30     
31     ber_buf = odr_getbuf(encode, &ber_len, 0);
32
33     odr_setbuf(decode, ber_buf, ber_len, 0);
34
35     if (!yc_MySequence(decode, &t, 0, 0))
36         exit(2);
37     if (!t->first || *t->first != 12345)
38         exit(3);
39     if (!t->second || !t->second->buf || t->second->len != 5)
40         exit(4);
41     if (memcmp(t->second->buf, "hello", t->second->len))
42         exit(5);
43     if (!t->third || *t->third != 1)
44         exit(6);
45     if (!t->fourth)
46         exit(7);
47     if (!t->fifth || *t->fifth != YC_MySequence_enum1)
48         exit(8);
49 }
50
51 void tst_MySequence2(ODR encode, ODR decode)
52 {
53     char *ber_buf;
54     int ber_len;
55     Yc_MySequence *s = odr_malloc(encode, sizeof(*s));
56     Yc_MySequence *t;
57
58     s->first = 0;  /* deliberately miss this .. */
59     s->second = odr_malloc(encode, sizeof(*s->second));
60     s->second->buf = "hello";
61     s->second->len = 5;
62     s->second->size = 0;
63     s->third = odr_intdup(encode, 1);
64     s->fourth = odr_nullval();
65     s->fifth = odr_intdup(encode, YC_MySequence_enum1);
66
67     if (yc_MySequence(encode, &s, 0, 0)) /* should fail */
68         exit(9);
69     if (odr_geterror(encode) != OREQUIRED)
70         exit(10);
71     if (strcmp(odr_getelement(encode), "first"))
72         exit(11);
73     odr_reset(encode);
74
75     if (odr_geterror(encode) != ONONE)
76         exit(12);
77     if (strcmp(odr_getelement(encode), ""))
78         exit(13);
79 }
80
81 void tst_MySequence3(ODR encode, ODR decode)
82 {
83     char buf[40];
84     int i;
85     Yc_MySequence *t;
86
87     srand(123);
88     for (i = 0; i<1000; i++)
89     {
90         int j;
91         for (j = 0; j<sizeof(buf); j++)
92             buf[j] = rand();
93
94         for (j = 1; j<sizeof(buf); j++)
95         {
96             odr_setbuf(decode, buf, j, 0);
97             yc_MySequence(decode, &t, 0, 0);
98             odr_reset(decode);
99         }
100     }
101 }
102
103 int main(int argc, char **argv)
104 {
105     ODR odr_encode = odr_createmem(ODR_ENCODE);
106     ODR odr_decode = odr_createmem(ODR_DECODE);
107
108     tst_MySequence1(odr_encode, odr_decode);
109     tst_MySequence2(odr_encode, odr_decode);
110     tst_MySequence3(odr_encode, odr_decode);
111
112     odr_destroy(odr_encode);
113     odr_destroy(odr_decode);
114     exit(0);
115 }