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