test_labcomm.c 5.72 KB
Newer Older
1 2
/*
  test_labcomm.c -- Various labcomm tests
Oscar Olsson's avatar
Oscar Olsson committed
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
  Copyright 2013 Anders Blomdell <anders.blomdell@control.lth.se>

  This file is part of LabComm.

  LabComm is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  LabComm is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "labcomm_private.h"
#include "labcomm_default_error_handler.h"
#include "labcomm_default_memory.h"
#include "labcomm_default_scheduler.h"
#include "test/gen/test_sample.h"

static unsigned char buffer[512];

static int writer_alloc(struct labcomm_writer *w, 
36
			struct labcomm_writer_action_context *action_context)
37 38 39 40 41 42 43 44 45
{
  w->data = buffer;
  w->data_size = sizeof(buffer);
  w->count = sizeof(buffer);
  
  return 0;
}
static int writer_start(struct labcomm_writer *w, 
			 struct labcomm_writer_action_context *action_context,
46
			 int index, const struct labcomm_signature *signature,
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
			 void *value)
{
  return 0;
}
const struct labcomm_writer_action writer_action = {
  .alloc = writer_alloc,
  .start = writer_start,
};
static struct labcomm_writer_action_context writer_action_context = {
  .next = NULL,
  .action = &writer_action,
  .context = NULL
}; 
static struct labcomm_writer writer =  {
  .action_context = &writer_action_context,
  .data = buffer,
  .data_size = sizeof(buffer),
  .count = sizeof(buffer),
  .pos = 0,
  .error = 0,
};

static int reader_alloc(struct labcomm_reader *r, 
70
			struct labcomm_reader_action_context *action_context)
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
{
  r->data = buffer;
  r->data_size = sizeof(buffer);
  r->count = 0;
  r->memory = labcomm_default_memory;
  
  return 0;
}
static int reader_fill(struct labcomm_reader *r, 
		       struct labcomm_reader_action_context *action_context)
{
  r->error = -ENOMEM;
  return r->error;
}
const struct labcomm_reader_action reader_action = {
  .alloc = reader_alloc,
  .fill = reader_fill,
};
static struct labcomm_reader_action_context reader_action_context = {
  .next = NULL,
  .action = &reader_action,
  .context = NULL
}; 
static struct labcomm_reader reader =  {
  .action_context = &reader_action_context,
  .data = buffer,
  .data_size = sizeof(buffer),
  .count = 0,
  .pos = 0,
  .error = 0,
};

Anders Blomdell's avatar
Anders Blomdell committed
103 104 105
static int32_t encoder_data[256];
static test_sample_test_var encoder_var = {
  .n_0 = 1,
106
  .n_2 = 1,
Anders Blomdell's avatar
Anders Blomdell committed
107 108 109 110 111
  .a = encoder_data,
};
static int32_t decoder_data[256];
static test_sample_test_var decoder_var = {
  .n_0 = 1,
112
  .n_2 = 1,
Anders Blomdell's avatar
Anders Blomdell committed
113 114
  .a = decoder_data,
};;
115 116 117

void handle_test_var(test_sample_test_var *v, void *ctx)
{
Anders Blomdell's avatar
Anders Blomdell committed
118
  decoder_var.a[0] = v->a[0];  
119 120 121 122 123 124 125 126 127
}

int test_decode_one(struct labcomm_decoder *decoder)
{
  int result;

  for (reader.count = 0 ; reader.count < writer.pos ; reader.count++) {
    reader.error = 0;
    reader.pos = 0;
128 129
    result = labcomm_decoder_decode_one(decoder); 
    if (result >= 0 ) {
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
      fprintf(stderr, "Got result from buffer with bogus length (%d)\n",
	      result);
      exit(1);
    }
  }
  reader.error = 0;
  reader.pos = 0;
  reader.count = writer.pos;
  result = labcomm_decoder_decode_one(decoder);
  if (result < 0) {
    fprintf(stderr, "Got result from buffer with correct length (%d)\n",
	    result);
    exit(1);
  }
  return result;
}

Anders Blomdell's avatar
Anders Blomdell committed
147 148
static void test_encode_decode(struct labcomm_encoder *encoder,
			       struct labcomm_decoder *decoder,
149
			       int expected, uint32_t n_0, uint32_t n_2)
150 151
{
  int err;
Anders Blomdell's avatar
Anders Blomdell committed
152 153 154

  writer.pos = 0;
  encoder_var.n_0 = n_0;
155
  encoder_var.n_2 = n_2;
Anders Blomdell's avatar
Anders Blomdell committed
156 157 158
  encoder_var.a[0] = 314;
  labcomm_encode_test_sample_test_var(encoder, &encoder_var);
  err = test_decode_one(decoder);
159 160
  fprintf(stderr, "decode of sample %u * 2 * %u -> size=%d err=%d\n", 
	  n_0, n_2, writer.pos, err);
Anders Blomdell's avatar
Anders Blomdell committed
161
  if (writer.pos != labcomm_sizeof_test_sample_test_var(&encoder_var)) {
162 163
    fprintf(stderr, "Incorrect sizeof %u * 2 * %u (%d != %d)\n",
	    n_0, n_2, 
Anders Blomdell's avatar
Anders Blomdell committed
164 165 166 167
	    writer.pos, labcomm_sizeof_test_sample_test_var(&encoder_var));
    exit(1);
  }
  if (writer.pos != expected) {
168 169
    fprintf(stderr, "Unexpected size %u * 2 * %u (%d != %d)\n",
	    n_0, n_2, 
Anders Blomdell's avatar
Anders Blomdell committed
170 171 172 173 174 175 176 177
	    writer.pos, expected);
    exit(1);
  }
}

int main(void)
{
  int err, i;
178 179 180 181 182 183 184 185 186 187
  struct labcomm_encoder *encoder = labcomm_encoder_new(
    &writer, 
    labcomm_default_error_handler,
    labcomm_default_memory,
    labcomm_default_scheduler);
  struct labcomm_decoder *decoder = labcomm_decoder_new(
    &reader,
    labcomm_default_error_handler,
    labcomm_default_memory,
    labcomm_default_scheduler);
188 189 190
  err = test_decode_one(decoder);
  fprintf(stderr, "decode of version -> index %d\n", err);
  writer.pos = 0;
191 192 193 194 195
  labcomm_decoder_register_test_sample_test_var(decoder,
						handle_test_var, 
						NULL);
  labcomm_encoder_register_test_sample_test_var(encoder);
  err = test_decode_one(decoder);
Anders Blomdell's avatar
Anders Blomdell committed
196
  fprintf(stderr, "decode of register -> index %d\n", err);
197
  test_encode_decode(encoder, decoder, 12, 1, 1);
Anders Blomdell's avatar
Anders Blomdell committed
198
  if (decoder_var.a[0] != encoder_var.a[0]) {
199
    fprintf(stderr, "Failed to decode correct value %d != %d\n", 
Anders Blomdell's avatar
Anders Blomdell committed
200
	    encoder_var.a[0], decoder_var.a[0]);
201 202
    exit(1);
  }
203
  test_encode_decode(encoder, decoder, 36, 2, 2);
204
  test_encode_decode(encoder, decoder, 4, 0, 0);
Anders Blomdell's avatar
Anders Blomdell committed
205
  for (i = 1 ; i <= 4 ; i++) {
206 207
    test_encode_decode(encoder, decoder, 3+i, 0, (1<<(7*i))-1);
    test_encode_decode(encoder, decoder, 4+i, 0, (1<<(7*i)));
Anders Blomdell's avatar
Anders Blomdell committed
208
  }
209
  test_encode_decode(encoder, decoder, 8, 0, 4294967295);
210 211
  return 0;
}