test_labcomm_copy.c 8.85 KB
Newer Older
1
2
3
4
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
5
#include <unistd.h>
6
7
8
9
10
11
12
13
14
15
16
17
#include <fcntl.h>
#include <assert.h>
#include <err.h>

#include "labcomm.h"
#include "labcomm_private.h"
#include "labcomm_default_error_handler.h"
#include "labcomm_default_memory.h"
#include "labcomm_default_scheduler.h"
#include "labcomm_fd_writer.h"
#include "labcomm_fd_reader.h"
#include "test/gen/generated_encoding.h"
18
#include "test/gen/test_sample.h"
19
#include "test/gen/more_types.h"
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

#define DATA_FILE "copy_test.dat"

static void handle_s1(generated_encoding_S1 *v, void *context)
{
  labcomm_copy_generated_encoding_S1(labcomm_default_memory, context, v);
}

static void handle_b(generated_encoding_B *v, void *context)
{
  labcomm_copy_generated_encoding_B(labcomm_default_memory, context, v);
}

static void handle_i(generated_encoding_I *v, void *context)
{
  labcomm_copy_generated_encoding_I(labcomm_default_memory, context, v);
}

static void handle_p(generated_encoding_P *v, void *context)
{
  labcomm_copy_generated_encoding_P(labcomm_default_memory, context, v);
}

43
44
45
46
47
static void handle_r(generated_encoding_R *v, void *context)
{
  labcomm_copy_generated_encoding_R(labcomm_default_memory, context, v);
}

48
49
50
51
52
static void handle_test_var(test_sample_test_var *v, void *context)
{
  labcomm_copy_test_sample_test_var(labcomm_default_memory, context, v);
}

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
static void handle_a(more_types_A *v, void *context)
{
  labcomm_copy_more_types_A(labcomm_default_memory, context, v);
}

static void handle_s(more_types_S *v, void *context)
{
  labcomm_copy_more_types_S(labcomm_default_memory, context, v);
}

static void handle_ns(more_types_NS *v, void *context)
{
  labcomm_copy_more_types_NS(labcomm_default_memory, context, v);
}

68
69
70
71
72
static void handle_as(more_types_AS *v, void *context)
{
  labcomm_copy_more_types_AS(labcomm_default_memory, context, v);
}

73
74
75
76
77
78
79
80
81
82
83
84
85
int main(int argc, char **argv)
{
  struct labcomm_encoder *encoder;
  struct labcomm_decoder *decoder;
  int fd;
  generated_encoding_S1 s1;
  generated_encoding_S1 cache_s1;
  generated_encoding_B b;
  generated_encoding_B cache_b;
  generated_encoding_I I;
  generated_encoding_I cache_I;
  generated_encoding_P p;
  generated_encoding_P cache_p;
86
87
  test_sample_test_var test_var;
  test_sample_test_var cache_test_var;
88
89
90
91
92
93
  more_types_A a;
  more_types_A cache_a;
  more_types_S s;
  more_types_S cache_s = NULL;
  more_types_NS ns;
  more_types_NS cache_ns;
94
95
  more_types_AS as;
  more_types_AS cache_as;
96
97
  generated_encoding_R r;
  generated_encoding_R cache_r;
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

  fd = open(DATA_FILE, O_RDWR | O_CREAT | O_TRUNC, 0644);
  if (fd == -1)
    err(1, "open()");
  encoder =
    labcomm_encoder_new(labcomm_fd_writer_new(labcomm_default_memory, fd, 0),
			labcomm_default_error_handler,
			labcomm_default_memory,
			labcomm_default_scheduler);

  labcomm_encoder_register_generated_encoding_S1(encoder);
  s1.i = 1;
  labcomm_encode_generated_encoding_S1(encoder, &s1);

  labcomm_encoder_register_generated_encoding_B(encoder);
  b = 2;
  labcomm_encode_generated_encoding_B(encoder, &b);

  labcomm_encoder_register_generated_encoding_I(encoder);
  I.n_0 = 3;
  I.a = calloc(I.n_0, sizeof(I.a[0]));
  I.a[0] = 4;
  I.a[1] = 5;
  I.a[2] = 6;
  labcomm_encode_generated_encoding_I(encoder, &I);

  labcomm_encoder_register_generated_encoding_P(encoder);
  p.n_0 = 7;
  p.a = calloc(p.n_0, sizeof(p.a[0]));
  for (int i = 0; i < p.n_0; i++)
    p.a[i].i = 8 + i;
  labcomm_encode_generated_encoding_P(encoder, &p);

131
132
133
134
135
136
137
138
139
  labcomm_encoder_register_test_sample_test_var(encoder);
  test_var.n_0 = 2;
  test_var.n_1 = 7;
  test_var.a = calloc(test_var.n_0 * test_var.n_1, sizeof(*test_var.a));
  for (int i = 0; i < test_var.n_0; i++)
    for (int j = 0; j < test_var.n_1; j++)
      test_var.a[i] = 10 * i + j;
  labcomm_encode_test_sample_test_var(encoder, &test_var);

140
141
142
143
144
145
146
147
148
149
  labcomm_encoder_register_more_types_A(encoder);
  for (int i = 0; i < sizeof(a.a) / sizeof(a.a[0]); i++)
    a.a[i] = i;
  labcomm_encode_more_types_A(encoder, &a);

  labcomm_encoder_register_more_types_S(encoder);
  s = "this is a string";
  labcomm_encode_more_types_S(encoder, &s);

  labcomm_encoder_register_more_types_NS(encoder);
150
151
  ns.s1 = "this is a string";
  ns.s2 = "this is a another string";
152
153
  labcomm_encode_more_types_NS(encoder, &ns);

154
155
156
157
158
159
160
161
  labcomm_encoder_register_more_types_AS(encoder);
  as.n_0 = 3;
  as.a = calloc(as.n_0, sizeof(as.a[0]));
  as.a[0] = "string 0";
  as.a[1] = "string 1";
  as.a[2] = "string 2";
  labcomm_encode_more_types_AS(encoder, &as);

162
163
164
165
166
167
168
169
170
171
172
  labcomm_encoder_register_generated_encoding_R(encoder);
  labcomm_encoder_sample_ref_register(encoder, labcomm_signature_generated_encoding_V);
  labcomm_encoder_sample_ref_register(encoder, 
                                      labcomm_signature_generated_encoding_UnusedD);
  labcomm_encoder_sample_ref_register(encoder, labcomm_signature_generated_encoding_R);
  r.a[0] = labcomm_signature_generated_encoding_V;
  r.a[1] = labcomm_signature_generated_encoding_UnusedE;
  r.a[2] = labcomm_signature_generated_encoding_UnusedD;
  r.a[3] = labcomm_signature_generated_encoding_R;
  labcomm_encode_generated_encoding_R(encoder, &r);

173
174
175
176
177
178
179
180
181
182
183
184
  labcomm_encoder_free(encoder);
  encoder = NULL;
  lseek(fd, 0, SEEK_SET);
  decoder =
    labcomm_decoder_new(labcomm_fd_reader_new(labcomm_default_memory, fd, 0),
			labcomm_default_error_handler,
			labcomm_default_memory,
			labcomm_default_scheduler);

  labcomm_decoder_register_generated_encoding_S1(decoder, handle_s1, &cache_s1);
  labcomm_decoder_register_generated_encoding_B(decoder, handle_b, &cache_b);
  labcomm_decoder_register_generated_encoding_I(decoder, handle_i, &cache_I);
185
  labcomm_decoder_register_generated_encoding_P(decoder, handle_p, &cache_p);
186
187
  labcomm_decoder_register_test_sample_test_var(decoder, handle_test_var,
						&cache_test_var);
188
189
190
  labcomm_decoder_register_more_types_A(decoder, handle_a, &cache_a);
  labcomm_decoder_register_more_types_S(decoder, handle_s, &cache_s);
  labcomm_decoder_register_more_types_NS(decoder, handle_ns, &cache_ns);
191
  labcomm_decoder_register_more_types_AS(decoder, handle_as, &cache_as);
192
193
194
195
196
  labcomm_decoder_register_generated_encoding_R(decoder, handle_r, &cache_r);
  labcomm_decoder_sample_ref_register(decoder, labcomm_signature_generated_encoding_V);
  labcomm_decoder_sample_ref_register(decoder, 
                                      labcomm_signature_generated_encoding_UnusedE);
  labcomm_decoder_sample_ref_register(decoder, labcomm_signature_generated_encoding_R);
197
198

  while (labcomm_decoder_decode_one(decoder) > 0) ;
199
200

  assert(cache_s1.i == s1.i);
201
  puts("S1 copied ok");
202

203
  assert(cache_b == b);
204
  puts("B copied ok");
205

206
207
208
209
210
  assert(cache_I.n_0 == I.n_0);
  assert(cache_I.a[0] == I.a[0]);
  assert(cache_I.a[1] == I.a[1]);
  assert(cache_I.a[2] == I.a[2]);
  free(I.a);
211
  puts("I copied ok");
212

213
  assert(cache_p.n_0 == p.n_0);
214
215
216
  for (int i = 0; i < p.n_0; i++)
    assert(cache_p.a[i].i == p.a[i].i);
  free(p.a);
217
  puts("P copied ok");
218

219
220
221
222
223
224
225
226
  assert(cache_test_var.n_0 == test_var.n_0);
  assert(cache_test_var.n_1 == test_var.n_1);
  for (int i = 0; i < test_var.n_0; i++)
    for (int j = 0; j < test_var.n_1; j++)
      assert(cache_test_var.a[p.n_0 * i + j] == test_var.a[p.n_0 * i + j]);
  free(test_var.a);
  puts("test_var copied ok");

227
228
229
230
231
232
233
  for (int i = 0; i < sizeof(a.a) / sizeof(a.a[0]); i++)
    assert(cache_a.a[i] == a.a[i]);
  puts("A copied ok");

  assert(!strcmp(cache_s, s));
  puts("S copied ok");

234
235
  assert(!strcmp(cache_ns.s1, ns.s1));
  assert(!strcmp(cache_ns.s2, ns.s2));
236
237
  puts("NS copied ok");

238
239
240
241
242
  for (int i = 0; i < as.n_0; i++)
    assert(!strcmp(cache_as.a[i], as.a[i]));
  free(as.a);
  puts("AS copied ok");

243
244
245
246
247
248
249
250
251
252
  fprintf(stderr, "%p %p\n", r.a[0], cache_r.a[0]);
  fprintf(stderr, "%p %p\n", r.a[1], cache_r.a[1]);
  fprintf(stderr, "%p %p\n", r.a[2], cache_r.a[2]);
  fprintf(stderr, "%p %p\n", r.a[3], cache_r.a[3]);
  assert(cache_r.a[0] == r.a[0]);
  assert(cache_r.a[1] == NULL); /* UnusedE */
  assert(cache_r.a[2] == NULL); /* UnusedD */
  assert(cache_r.a[3] == r.a[3]);
  puts("R copied ok");

253
254
255
  labcomm_decoder_free(decoder);
  close(fd);
  unlink(DATA_FILE);
256
257
258
259
260
261
262
263
264

  labcomm_copy_free_generated_encoding_S1(labcomm_default_memory, &cache_s1);
  puts("S1 deallocated ok");
  labcomm_copy_free_generated_encoding_B(labcomm_default_memory, &cache_b);
  puts("B deallocated ok");
  labcomm_copy_free_generated_encoding_I(labcomm_default_memory, &cache_I);
  puts("I deallocated ok");
  labcomm_copy_free_generated_encoding_P(labcomm_default_memory, &cache_p);
  puts("P deallocated ok");
265
266
  labcomm_copy_free_test_sample_test_var(labcomm_default_memory, &cache_test_var);
  puts("test_var deallocated ok");
267
268
269
270
271
272
  labcomm_copy_free_more_types_A(labcomm_default_memory, &cache_a);
  puts("A deallocated ok");
  labcomm_copy_free_more_types_S(labcomm_default_memory, &cache_s);
  puts("S deallocated ok");
  labcomm_copy_free_more_types_NS(labcomm_default_memory, &cache_ns);
  puts("NS deallocated ok");
273
274
  labcomm_copy_free_more_types_AS(labcomm_default_memory, &cache_as);
  puts("AS deallocated ok");
275
}