test_labcomm_copy.c 9.15 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
  labcomm_encoder_register_test_sample_test_var(encoder);
  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 141
  labcomm_encode_test_sample_test_var(encoder, &test_var);

142 143 144 145 146 147 148 149 150 151
  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);
152 153
  ns.s1 = "this is a string";
  ns.s2 = "this is a another string";
154 155
  labcomm_encode_more_types_NS(encoder, &ns);

156 157 158 159 160 161 162 163
  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);

164 165 166 167 168 169 170 171 172 173 174
  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);

175 176 177 178 179 180 181 182 183 184 185 186
  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);
187
  labcomm_decoder_register_generated_encoding_P(decoder, handle_p, &cache_p);
188 189
  labcomm_decoder_register_test_sample_test_var(decoder, handle_test_var,
						&cache_test_var);
190 191 192
  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);
193
  labcomm_decoder_register_more_types_AS(decoder, handle_as, &cache_as);
194 195 196 197 198
  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);
199 200

  while (labcomm_decoder_decode_one(decoder) > 0) ;
201 202

  assert(cache_s1.i == s1.i);
203
  puts("S1 copied ok");
204

205
  assert(cache_b == b);
206
  puts("B copied ok");
207

208 209 210 211 212
  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);
213
  puts("I copied ok");
214

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

221
  assert(cache_test_var.n_0 == test_var.n_0);
222
  assert(cache_test_var.n_2 == test_var.n_2);
223
  for (int i = 0; i < test_var.n_0; i++)
224 225 226 227 228 229 230
    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);
      }
231 232 233
  free(test_var.a);
  puts("test_var copied ok");

234 235 236 237 238 239 240
  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");

241 242
  assert(!strcmp(cache_ns.s1, ns.s1));
  assert(!strcmp(cache_ns.s2, ns.s2));
243 244
  puts("NS copied ok");

245 246 247 248 249
  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");

250 251 252 253 254 255 256 257 258 259
  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");

260 261 262
  labcomm_decoder_free(decoder);
  close(fd);
  unlink(DATA_FILE);
263 264 265 266 267 268 269 270 271

  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");
272 273
  labcomm_copy_free_test_sample_test_var(labcomm_default_memory, &cache_test_var);
  puts("test_var deallocated ok");
274 275 276 277 278 279
  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");
280 281
  labcomm_copy_free_more_types_AS(labcomm_default_memory, &cache_as);
  puts("AS deallocated ok");
282
}