aboutsummaryrefslogtreecommitdiffstats
path: root/lib/erl_interface/test/ei_decode_encode_SUITE_data/ei_decode_encode_test.c
blob: 406f02ecfb2d7f9c6d1930449d2cb02724765c3a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 2004-2009. All Rights Reserved.
 * 
 * The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved online at http://www.erlang.org/.
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * %CopyrightEnd%
 */

#ifdef VXWORKS
#include "reclaim.h"
#endif

#include "ei_runner.h"

/*
 * Purpose: Read pids, funs and others without real meaning on the C side 
 *          and pass it back to Erlang to test that it is still the same. 
 * Author:  [email protected]
 */

#define EI_DECODE_ENCODE(FUNC,TYPE) \
  { \
    char *buf; \
    char buf2[1024]; \
    TYPE p; \
    int size1 = 0; \
    int size2 = 0; \
    int size3 = 0; \
    int err; \
    ei_x_buff arg; \
\
    message("ei_decode_" #FUNC ", arg is type " #TYPE); \
    buf = read_packet(NULL); \
    err = ei_decode_ ## FUNC(buf+1, &size1, &p); \
    if (err != 0) { \
      if (err != -1) { \
	fail("decode returned non zero but not -1"); \
      } else { \
	fail("decode returned non zero"); \
      } \
      return; \
    } \
    if (size1 < 1) { \
      fail("size is < 1"); \
      return; \
    } \
\
    message("ei_encode_" #FUNC " buf is NULL, arg is type " #TYPE); \
    err = ei_encode_ ## FUNC(NULL, &size2, &p); \
    if (err != 0) { \
      if (err != -1) { \
	fail("size calculation returned non zero but not -1"); \
	return; \
      } else { \
	fail("size calculation returned non zero"); \
	return; \
      } \
    } \
    if (size1 != size2) { \
      message("size1 = %d, size2 = %d\n",size1,size2); \
      fail("decode and encode size differs when buf is NULL"); \
      return; \
    } \
    message("ei_encode_" #FUNC ", arg is type " #TYPE); \
    err = ei_encode_ ## FUNC(buf2, &size3, &p); \
    if (err != 0) { \
      if (err != -1) { \
	fail("returned non zero but not -1"); \
      } else { \
	fail("returned non zero"); \
      } \
      return; \
    } \
    if (size1 != size3) { \
      message("size1 = %d, size2 = %d\n",size1,size3); \
      fail("decode and encode size differs"); \
      return; \
    } \
    send_buffer(buf2, size1); \
\
    message("ei_x_encode_" #FUNC ", arg is type " #TYPE); \
    ei_x_new(&arg); \
    err = ei_x_encode_ ## FUNC(&arg, &p); \
    if (err != 0) { \
      if (err != -1) { \
	fail("returned non zero but not -1"); \
      } else { \
	fail("returned non zero"); \
      } \
      ei_x_free(&arg); \
      return; \
    } \
    if (arg.index < 1) { \
      fail("size is < 1"); \
      ei_x_free(&arg); \
      return; \
    } \
    send_buffer(arg.buff, arg.index); \
    ei_x_free(&arg); \
  }

#define EI_DECODE_ENCODE_BIG(FUNC,TYPE) \
  { \
    char *buf; \
    char buf2[2048]; \
    TYPE *p; \
    int size1 = 0; \
    int size2 = 0; \
    int size3 = 0; \
    int err, index = 0, len, type; \
    ei_x_buff arg; \
\
    message("ei_decode_" #FUNC ", arg is type " #TYPE); \
    buf = read_packet(NULL); \
    ei_get_type(buf+1, &index, &type, &len); \
    p = ei_alloc_big(len); \
    err = ei_decode_ ## FUNC(buf+1, &size1, p); \
    if (err != 0) { \
      if (err != -1) { \
	fail("decode returned non zero but not -1"); \
      } else { \
	fail("decode returned non zero"); \
      } \
      return; \
    } \
    if (size1 < 1) { \
      fail("size is < 1"); \
      return; \
    } \
\
    message("ei_encode_" #FUNC " buf is NULL, arg is type " #TYPE); \
    err = ei_encode_ ## FUNC(NULL, &size2, p); \
    if (err != 0) { \
      if (err != -1) { \
	fail("size calculation returned non zero but not -1"); \
	return; \
      } else { \
	fail("size calculation returned non zero"); \
	return; \
      } \
    } \
    if (size1 != size2) { \
      message("size1 = %d, size2 = %d\n",size1,size2); \
      fail("decode and encode size differs when buf is NULL"); \
      return; \
    } \
    message("ei_encode_" #FUNC ", arg is type " #TYPE); \
    err = ei_encode_ ## FUNC(buf2, &size3, p); \
    if (err != 0) { \
      if (err != -1) { \
	fail("returned non zero but not -1"); \
      } else { \
	fail("returned non zero"); \
      } \
      return; \
    } \
    if (size1 != size3) { \
      message("size1 = %d, size2 = %d\n",size1,size3); \
      fail("decode and encode size differs"); \
      return; \
    } \
    send_buffer(buf2, size1); \
\
    message("ei_x_encode_" #FUNC ", arg is type " #TYPE); \
    ei_x_new(&arg); \
    err = ei_x_encode_ ## FUNC(&arg, p); \
    if (err != 0) { \
      if (err != -1) { \
	fail("returned non zero but not -1"); \
      } else { \
	fail("returned non zero"); \
      } \
      ei_x_free(&arg); \
      return; \
    } \
    if (arg.index < 1) { \
      fail("size is < 1"); \
      ei_x_free(&arg); \
      return; \
    } \
    send_buffer(arg.buff, arg.index); \
    ei_x_free(&arg); \
    ei_free_big(p); \
  }




/* ******************************************************************** */

TESTCASE(test_ei_decode_encode)
{
    EI_DECODE_ENCODE(fun  , erlang_fun);
    EI_DECODE_ENCODE(pid  , erlang_pid);
    EI_DECODE_ENCODE(port , erlang_port);
    EI_DECODE_ENCODE(ref  , erlang_ref);
    EI_DECODE_ENCODE(trace, erlang_trace);

    EI_DECODE_ENCODE_BIG(big  , erlang_big);
    EI_DECODE_ENCODE_BIG(big  , erlang_big);
    EI_DECODE_ENCODE_BIG(big  , erlang_big);

    EI_DECODE_ENCODE_BIG(big  , erlang_big);
    EI_DECODE_ENCODE_BIG(big  , erlang_big);
    EI_DECODE_ENCODE_BIG(big  , erlang_big);

    /* Test large node containers... */
    EI_DECODE_ENCODE(pid  , erlang_pid);
    EI_DECODE_ENCODE(port , erlang_port);
    EI_DECODE_ENCODE(ref  , erlang_ref);
    EI_DECODE_ENCODE(pid  , erlang_pid);
    EI_DECODE_ENCODE(port , erlang_port);
    EI_DECODE_ENCODE(ref  , erlang_ref);

    report(1);
}

/* ******************************************************************** */