test_labcomm_generated_encoding.c 9.23 KB
Newer Older
1
/*
2
  test_labcomm2014_generated_encoding.c -- LabComm tests of generated encoding
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

  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/>.
*/

22
#include <stdint.h>
23
#include <stdlib.h>
24
25
#include <string.h>
#include <errno.h>
26
27
28
29
#include "labcomm2014_private.h"
#include "labcomm2014_default_error_handler.h"
#include "labcomm2014_default_memory.h"
#include "labcomm2014_pthread_scheduler.h"
30
31
32
33
34
#include "test/gen/generated_encoding.h"

#define IOCTL_WRITER_ASSERT_BYTES 4096
#define IOCTL_WRITER_RESET 4097

35
#define EXPECT(...)							\
36
  {                                                                     \
37
    int expected[] = __VA_ARGS__;					\
38
    labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_ASSERT_BYTES,       \
39
40
41
42
43
44
45
46
			  __LINE__,					\
			  sizeof(expected)/sizeof(expected[0]),		\
			  expected);					\
  }

#define VARIABLE(i) -(i + 1)
#define IS_VARIABLE(i) (i < 0)

47
static unsigned char buffer[128];
48
struct labcomm2014_writer *writer;
49
static int seen_variable[1024];
50

51
static int buf_writer_alloc(
52
53
  struct labcomm2014_writer *w, 
  struct labcomm2014_writer_action_context *action_context)
54
{
55
  writer = w; /* Hack */
56
57
58
59
60
61
62
63
  w->data_size = sizeof(buffer);
  w->count = w->data_size;
  w->data = buffer;
  w->pos = 0;
  
  return 0;
}

64
static int buf_writer_free(
65
66
  struct labcomm2014_writer *w, 
  struct labcomm2014_writer_action_context *action_context)
67
68
69
70
{
  return 0;
}

71
static int buf_writer_start(
72
73
  struct labcomm2014_writer *w,
  struct labcomm2014_writer_action_context *action_context,
74
  int index,
75
  const struct labcomm2014_signature *signature,
76
  void *value)
77
78
79
80
{
  return 0;
}

81
static int buf_writer_end(
82
83
  struct labcomm2014_writer *w, 
  struct labcomm2014_writer_action_context *action_context)
84
85
86
87
{
  return 0;
}

88
static int buf_writer_flush(
89
90
  struct labcomm2014_writer *w, 
  struct labcomm2014_writer_action_context *action_context)
91
92
93
94
95
96
97
98
{
  fprintf(stderr, "Should not come here %s:%d\n", __FILE__, __LINE__);
  exit(1);
  
  return 0;
}

static int buf_writer_ioctl(
99
100
101
  struct labcomm2014_writer *w, 
  struct labcomm2014_writer_action_context *action_context,
  int signature_index, const struct labcomm2014_signature *signature,
102
  uint32_t action, va_list arg)
103
104
105
106
107
108
109
110
111
{
  int result = -ENOTSUP;
  switch (action) {
    case IOCTL_WRITER_ASSERT_BYTES: {
      int line = va_arg(arg, int);
      int count = va_arg(arg, int);
      int *expected = va_arg(arg, int *);
      int i, mismatch;

112
      mismatch = 0;
113
      if (w->pos != count) {
114
	fprintf(stderr, "Invalid length detected %d != %d (%s:%d)\n", 
115
116
117
		w->pos, count, __FILE__, line);
	mismatch = 1;
      } 
118
119
120
121
122
123
124
125
126
127
128
      for (i = 0 ; i < count ; i++) {
        if (IS_VARIABLE(expected[i])) {
          if (seen_variable[VARIABLE(expected[i])] == -1) {
            seen_variable[VARIABLE(expected[i])] = buffer[i];
          }
          if (seen_variable[VARIABLE(expected[i])] != buffer[i]) {
            fprintf(stderr, "Unexpected variable (%d:  != %d)\n",
                    seen_variable[VARIABLE(expected[i])], buffer[i]);
            mismatch = 1;
          }
        } else if (expected[i] != buffer[i]) {
129
130
131
132
133
134
135
136
137
138
139
140
141
	  mismatch = 1;
	}
      }
      if (mismatch) {
	fprintf(stderr, "Encoder mismatch (%s:%d)\n",
		__FILE__, line);

	for (i = 0 ; i < w->pos ; i++) {
	  printf("%2.2x ", w->data[i]);
	}
	printf("\n");
	for (i = 0 ; i < count ; i++) {
	  if (expected[i] < 0) {
142
	    printf("v%d ", VARIABLE(expected[i]));
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
	  } else {
	    printf("%2.2x ", expected[i] );
	  }
	}
	printf("\n");
	exit(1);
      }
      result = 0;
    } break;
    case IOCTL_WRITER_RESET: {
      w->pos = 0;
      result = 0;
    }
  }
  return result;
}

160
const struct labcomm2014_writer_action writer_action = {
161
162
163
164
165
166
167
  .alloc = buf_writer_alloc,
  .free = buf_writer_free,
  .start = buf_writer_start,
  .end = buf_writer_end,
  .flush = buf_writer_flush,
  .ioctl = buf_writer_ioctl
};
168

169
static struct labcomm2014_writer_action_context action_context = {
170
171
172
173
  .next = NULL,
  .action = &writer_action,
  .context = NULL
}; 
174
static struct labcomm2014_writer buffer_writer = {
175
  .action_context = &action_context,
176
177
178
179
180
181
182
  .data = buffer,
  .data_size = sizeof(buffer),
  .count = sizeof(buffer),
  .pos = 0,
  .error = 0,
};

183
void dump_encoder(struct labcomm2014_encoder *encoder)
184
185
186
{
  int i;
  
187
188
  for (i = 0 ; i < writer->pos ; i++) {
    printf("%2.2x ", writer->data[i]);
189
190
191
192
193
194
195
  }
  printf("\n");
}

int main(void)
{
  generated_encoding_B B = 1;
196
  generated_encoding_R R;
197
  struct labcomm2014_encoder *encoder;
198
  int i;
199

200
201
202
203
  for (i = 0 ; i < sizeof(seen_variable)/sizeof(seen_variable[0]) ; i++) {
    seen_variable[i] = -1;
  }

204
  encoder = labcomm2014_encoder_new(
Anders Blomdell's avatar
Anders Blomdell committed
205
    &buffer_writer, 
206
207
208
    labcomm2014_default_error_handler,
    labcomm2014_default_memory,
    labcomm2014_pthread_scheduler_new(labcomm2014_default_memory));
Anders Blomdell's avatar
Anders Blomdell committed
209
210
  EXPECT({ 0x01, 0x0c, 0x0b, 
           'L', 'a', 'b', 'C', 'o', 'm', 'm', '2','0', '1', '4' });
211

212
  labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
213
  /* Register twice to make sure that only one registration gets encoded */
214
215
  labcomm2014_encoder_register_generated_encoding_V(encoder);
  labcomm2014_encoder_register_generated_encoding_V(encoder);
216
  EXPECT({ 0x02, 0x08, VARIABLE(0), 0x01, 0x00, 0x01, 'V', 0x02, 0x11, 0x00,
217
218
           0x04, 0x0a, VARIABLE(1), 0x01, 0x00, 0x03, 'v', '_', 't', 0x02, 0x11, 0x00,
           0x04, 0x07, VARIABLE(2), 0x01, 0x00, 0x01, 'V', 0x01, VARIABLE(1),
219
           0x05, 0x02, VARIABLE(0), VARIABLE(2) });
220

221
  labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
222
  /* Register twice to make sure that only one registration gets encoded */
223
224
  labcomm2014_encoder_register_generated_encoding_B(encoder);
  labcomm2014_encoder_register_generated_encoding_B(encoder);
225
  EXPECT({ 0x02, 0x07, VARIABLE(3),0x01, 0x00,  0x01, 'B', 0x01, 0x21,
226
           0x05, 0x02, VARIABLE(3), LABCOMM_BIND_SELF });
227

228
  labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
229
  /* Register twice to make sure that only one registration gets encoded */
230
231
  labcomm2014_encoder_register_generated_encoding_R(encoder);
  labcomm2014_encoder_register_generated_encoding_R(encoder);
232
  EXPECT({ 0x02, 0x0a, VARIABLE(4), 0x01, 0x00, 0x01, 'R', 0x04,  0x10, 0x01, 0x04, 0x28,
233
           0x05, 0x02, VARIABLE(4), LABCOMM_BIND_SELF });
234

235
  labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
236
  /* Register twice to make sure that only one registration gets encoded */
237
238
239
240
  labcomm2014_encoder_sample_ref_register(encoder, 
                                      labcomm2014_signature_generated_encoding_V);
  labcomm2014_encoder_sample_ref_register(encoder, 
                                      labcomm2014_signature_generated_encoding_V);
241
  EXPECT({0x03, 0x08, VARIABLE(5), 0x01, 0x00, 0x01, 'V', 0x02, 0x11, 0x00});
242

243
  labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
244
  /* Register twice to make sure that only one registration gets encoded */
245
246
247
248
  labcomm2014_encoder_sample_ref_register(encoder, 
                                      labcomm2014_signature_generated_encoding_B);
  labcomm2014_encoder_sample_ref_register(encoder, 
                                      labcomm2014_signature_generated_encoding_B);
249
  EXPECT({0x03, 0x07, VARIABLE(6), 0x01, 0x00, 0x01, 'B', 0x01, 0x21});
250

251
  labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
252
  /* Register twice to make sure that only one registration gets encoded */
253
254
255
256
  labcomm2014_encoder_sample_ref_register(encoder, 
                                      labcomm2014_signature_generated_encoding_R);
  labcomm2014_encoder_sample_ref_register(encoder, 
                                      labcomm2014_signature_generated_encoding_R);
257
  EXPECT({0x03, 0x0a, VARIABLE(7), 0x01, 0x00, 0x01, 'R', 0x04, 0x10, 0x01, 0x04, 0x28});
258

259
260
261
  labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
  // was: labcomm2014_encode_generated_encoding_V(encoder, &V);
  labcomm2014_encode_generated_encoding_V(encoder);
262
  EXPECT({VARIABLE(10), 0x00 });
263

264
265
  labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
  labcomm2014_encode_generated_encoding_B(encoder, &B);
266
  EXPECT({VARIABLE(20), 0x01, 1});
267

268
  labcomm2014_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
269
270
271
272
273
274
275
276
  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_B);
  R.a[2] = labcomm2014_encoder_get_sample_ref(
    encoder, labcomm2014_signature_generated_encoding_UnusedE);
  R.a[3] = labcomm2014_encoder_get_sample_ref(
    encoder, labcomm2014_signature_generated_encoding_R);
277
  labcomm2014_encode_generated_encoding_R(encoder, &R);
278
279
  EXPECT({VARIABLE(4), 0x10, 0x00, 0x00, 0x00, VARIABLE(5),
                             0x00, 0x00, 0x00, VARIABLE(6),
280
                             0x00, 0x00, 0x00, 0x00,
281
                             0x00, 0x00, 0x00, VARIABLE(7)});
282
283
284
  return 0;
}