Better ODR diagnostics for missing elements which includes additional
[yaz-moved-to-github.git] / odr / tstodr.c
1 /*
2  * Copyright (c) 1995-2003, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Id: tstodr.c,v 1.3 2003-05-20 19:55:30 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_getaddinfo(encode), "first"))
72         exit(11);
73     odr_reset(encode);
74
75     if (odr_geterror(encode) != ONONE)
76         exit(12);
77     if (strcmp(odr_getaddinfo(encode), ""))
78         exit(13);
79 }
80
81 int main(int argc, char **argv)
82 {
83     ODR odr_encode = odr_createmem(ODR_ENCODE);
84     ODR odr_decode = odr_createmem(ODR_DECODE);
85
86     tst_MySequence1(odr_encode, odr_decode);
87     tst_MySequence2(odr_encode, odr_decode);
88
89     odr_destroy(odr_encode);
90     odr_destroy(odr_decode);
91     exit(0);
92 }