test_labcomm_copy.c 9.83 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
#include <fcntl.h>
#include <assert.h>
#include <err.h>

10
11
12
13
14
15
16
#include "labcomm2014.h"
#include "labcomm2014_private.h"
#include "labcomm2014_default_error_handler.h"
#include "labcomm2014_default_memory.h"
#include "labcomm2014_default_scheduler.h"
#include "labcomm2014_fd_writer.h"
#include "labcomm2014_fd_reader.h"
17
#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

#define DATA_FILE "copy_test.dat"

static void handle_s1(generated_encoding_S1 *v, void *context)
{
25
  labcomm2014_copy_generated_encoding_S1(labcomm2014_default_memory, context, v);
26
27
28
29
}

static void handle_b(generated_encoding_B *v, void *context)
{
30
  labcomm2014_copy_generated_encoding_B(labcomm2014_default_memory, context, v);
31
32
33
34
}

static void handle_i(generated_encoding_I *v, void *context)
{
35
  labcomm2014_copy_generated_encoding_I(labcomm2014_default_memory, context, v);
36
37
38
39
}

static void handle_p(generated_encoding_P *v, void *context)
{
40
  labcomm2014_copy_generated_encoding_P(labcomm2014_default_memory, context, v);
41
42
}

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

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

53
54
static void handle_a(more_types_A *v, void *context)
{
55
  labcomm2014_copy_more_types_A(labcomm2014_default_memory, context, v);
56
57
58
59
}

static void handle_s(more_types_S *v, void *context)
{
60
  labcomm2014_copy_more_types_S(labcomm2014_default_memory, context, v);
61
62
63
64
}

static void handle_ns(more_types_NS *v, void *context)
{
65
  labcomm2014_copy_more_types_NS(labcomm2014_default_memory, context, v);
66
67
}

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

73
74
int main(int argc, char **argv)
{
75
76
  struct labcomm2014_encoder *encoder;
  struct labcomm2014_decoder *decoder;
77
78
79
80
81
82
83
84
85
  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

  fd = open(DATA_FILE, O_RDWR | O_CREAT | O_TRUNC, 0644);
  if (fd == -1)
    err(1, "open()");
  encoder =
103
104
105
106
    labcomm2014_encoder_new(labcomm2014_fd_writer_new(labcomm2014_default_memory, fd, 0),
			labcomm2014_default_error_handler,
			labcomm2014_default_memory,
			labcomm2014_default_scheduler);
107

108
  labcomm2014_encoder_register_generated_encoding_S1(encoder);
109
  s1.i = 1;
110
  labcomm2014_encode_generated_encoding_S1(encoder, &s1);
111

112
  labcomm2014_encoder_register_generated_encoding_B(encoder);
113
  b = 2;
114
  labcomm2014_encode_generated_encoding_B(encoder, &b);
115

116
  labcomm2014_encoder_register_generated_encoding_I(encoder);
117
118
119
120
121
  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;
122
  labcomm2014_encode_generated_encoding_I(encoder, &I);
123

124
  labcomm2014_encoder_register_generated_encoding_P(encoder);
125
126
127
128
  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;
129
  labcomm2014_encode_generated_encoding_P(encoder, &p);
130

131
  labcomm2014_encoder_register_test_sample_test_var(encoder);
132
  test_var.n_0 = 2;
133
134
  test_var.n_2 = 7;
  test_var.a = calloc(test_var.n_0 * 2 * test_var.n_2, sizeof(*test_var.a));
135
  for (int i = 0; i < test_var.n_0; i++)
136
137
138
139
    for (int j = 0; j < 2; j++)
      for (int k = 0; k < test_var.n_2; k++) {
        test_var.a[(((i) * 2 + j) * test_var.n_2) + k] = 100 * i + 10 * j + k;
      }
140
  labcomm2014_encode_test_sample_test_var(encoder, &test_var);
141

142
  labcomm2014_encoder_register_more_types_A(encoder);
143
144
  for (int i = 0; i < sizeof(a.a) / sizeof(a.a[0]); i++)
    a.a[i] = i;
145
  labcomm2014_encode_more_types_A(encoder, &a);
146

147
  labcomm2014_encoder_register_more_types_S(encoder);
148
  s = "this is a string";
149
  labcomm2014_encode_more_types_S(encoder, &s);
150

151
  labcomm2014_encoder_register_more_types_NS(encoder);
152
153
  ns.s1 = "this is a string";
  ns.s2 = "this is a another string";
154
  labcomm2014_encode_more_types_NS(encoder, &ns);
155

156
  labcomm2014_encoder_register_more_types_AS(encoder);
157
158
159
160
161
  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";
162
163
164
165
166
167
168
  labcomm2014_encode_more_types_AS(encoder, &as);

  labcomm2014_encoder_register_generated_encoding_R(encoder);
  labcomm2014_encoder_sample_ref_register(encoder, labcomm2014_signature_generated_encoding_V);
  labcomm2014_encoder_sample_ref_register(encoder, 
                                      labcomm2014_signature_generated_encoding_UnusedD);
  labcomm2014_encoder_sample_ref_register(encoder, labcomm2014_signature_generated_encoding_R);
169
170
171
172
173
174
175
176
  r.a[0] = labcomm2014_encoder_get_sample_ref(
    encoder, labcomm2014_signature_generated_encoding_V);
  r.a[1] = labcomm2014_encoder_get_sample_ref(
    encoder, labcomm2014_signature_generated_encoding_UnusedE);
  r.a[2] = labcomm2014_encoder_get_sample_ref(
    encoder, labcomm2014_signature_generated_encoding_UnusedD);
  r.a[3] = labcomm2014_encoder_get_sample_ref(
    encoder, labcomm2014_signature_generated_encoding_R);
177
178
179
  labcomm2014_encode_generated_encoding_R(encoder, &r);

  labcomm2014_encoder_free(encoder);
180
181
182
  encoder = NULL;
  lseek(fd, 0, SEEK_SET);
  decoder =
183
184
185
186
187
188
189
190
191
192
    labcomm2014_decoder_new(labcomm2014_fd_reader_new(labcomm2014_default_memory, fd, 0),
			labcomm2014_default_error_handler,
			labcomm2014_default_memory,
			labcomm2014_default_scheduler);

  labcomm2014_decoder_register_generated_encoding_S1(decoder, handle_s1, &cache_s1);
  labcomm2014_decoder_register_generated_encoding_B(decoder, handle_b, &cache_b);
  labcomm2014_decoder_register_generated_encoding_I(decoder, handle_i, &cache_I);
  labcomm2014_decoder_register_generated_encoding_P(decoder, handle_p, &cache_p);
  labcomm2014_decoder_register_test_sample_test_var(decoder, handle_test_var,
193
						&cache_test_var);
194
195
196
197
198
199
200
201
202
203
204
  labcomm2014_decoder_register_more_types_A(decoder, handle_a, &cache_a);
  labcomm2014_decoder_register_more_types_S(decoder, handle_s, &cache_s);
  labcomm2014_decoder_register_more_types_NS(decoder, handle_ns, &cache_ns);
  labcomm2014_decoder_register_more_types_AS(decoder, handle_as, &cache_as);
  labcomm2014_decoder_register_generated_encoding_R(decoder, handle_r, &cache_r);
  labcomm2014_decoder_sample_ref_register(decoder, labcomm2014_signature_generated_encoding_V);
  labcomm2014_decoder_sample_ref_register(decoder, 
                                      labcomm2014_signature_generated_encoding_UnusedE);
  labcomm2014_decoder_sample_ref_register(decoder, labcomm2014_signature_generated_encoding_R);

  while (labcomm2014_decoder_decode_one(decoder) > 0) ;
205

206
  printf("cache_s1.i = %d, s1.i = %d\n", cache_s1.i, s1.i);
207
  assert(cache_s1.i == s1.i);
208
  puts("S1 copied ok");
209

210
  assert(cache_b == b);
211
  puts("B copied ok");
212

213
214
215
216
217
  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);
218
  puts("I copied ok");
219

220
  assert(cache_p.n_0 == p.n_0);
221
222
223
  for (int i = 0; i < p.n_0; i++)
    assert(cache_p.a[i].i == p.a[i].i);
  free(p.a);
224
  puts("P copied ok");
225

226
  assert(cache_test_var.n_0 == test_var.n_0);
227
  assert(cache_test_var.n_2 == test_var.n_2);
228
  for (int i = 0; i < test_var.n_0; i++)
229
230
231
232
233
234
235
    for (int j = 0; j < 2; j++)
      for (int k = 0; k < test_var.n_2; k++) {
        assert(cache_test_var.a[(((i) * 2 + j) * test_var.n_2) + k] == 
                     test_var.a[(((i) * 2 + j) * test_var.n_2) + k]);
        assert(cache_test_var.a[(((i) * 2 + j) * test_var.n_2) + k] == 
               100 * i + 10 * j + k);
      }
236
237
238
  free(test_var.a);
  puts("test_var copied ok");

239
240
241
242
243
244
245
  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");

246
247
  assert(!strcmp(cache_ns.s1, ns.s1));
  assert(!strcmp(cache_ns.s2, ns.s2));
248
249
  puts("NS copied ok");

250
251
252
253
254
  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");

255
256
257
258
259
260
261
262
263
264
  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");

265
  labcomm2014_decoder_free(decoder);
266
267
  close(fd);
  unlink(DATA_FILE);
268

269
  labcomm2014_copy_free_generated_encoding_S1(labcomm2014_default_memory, &cache_s1);
270
  puts("S1 deallocated ok");
271
  labcomm2014_copy_free_generated_encoding_B(labcomm2014_default_memory, &cache_b);
272
  puts("B deallocated ok");
273
  labcomm2014_copy_free_generated_encoding_I(labcomm2014_default_memory, &cache_I);
274
  puts("I deallocated ok");
275
  labcomm2014_copy_free_generated_encoding_P(labcomm2014_default_memory, &cache_p);
276
  puts("P deallocated ok");
277
  labcomm2014_copy_free_test_sample_test_var(labcomm2014_default_memory, &cache_test_var);
278
  puts("test_var deallocated ok");
279
  labcomm2014_copy_free_more_types_A(labcomm2014_default_memory, &cache_a);
280
  puts("A deallocated ok");
281
  labcomm2014_copy_free_more_types_S(labcomm2014_default_memory, &cache_s);
282
  puts("S deallocated ok");
283
  labcomm2014_copy_free_more_types_NS(labcomm2014_default_memory, &cache_ns);
284
  puts("NS deallocated ok");
285
  labcomm2014_copy_free_more_types_AS(labcomm2014_default_memory, &cache_as);
286
  puts("AS deallocated ok");
287
}