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

  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
26
#include <string.h>
#include <errno.h>
#include "labcomm_private.h"
Anders Blomdell's avatar
Anders Blomdell committed
27
28
29
#include "labcomm_default_error_handler.h"
#include "labcomm_default_memory.h"
#include "labcomm_pthread_scheduler.h"
30
31
32
33
34
35
#include "test/gen/generated_encoding.h"

#define IOCTL_WRITER_ASSERT_BYTES 4096
#define IOCTL_WRITER_RESET 4097

static unsigned char buffer[128];
36
struct labcomm_writer *writer;
37

38
39
static int buf_writer_alloc(
  struct labcomm_writer *w, 
40
  struct labcomm_writer_action_context *action_context)
41
{
42
  writer = w; /* Hack */
43
44
45
46
47
48
49
50
  w->data_size = sizeof(buffer);
  w->count = w->data_size;
  w->data = buffer;
  w->pos = 0;
  
  return 0;
}

51
52
53
static int buf_writer_free(
  struct labcomm_writer *w, 
  struct labcomm_writer_action_context *action_context)
54
55
56
57
{
  return 0;
}

58
59
60
61
static int buf_writer_start(
  struct labcomm_writer *w,
  struct labcomm_writer_action_context *action_context,
  int index,
62
  const struct labcomm_signature *signature,
63
  void *value)
64
65
66
67
{
  return 0;
}

68
69
70
static int buf_writer_end(
  struct labcomm_writer *w, 
  struct labcomm_writer_action_context *action_context)
71
72
73
74
{
  return 0;
}

75
76
77
static int buf_writer_flush(
  struct labcomm_writer *w, 
  struct labcomm_writer_action_context *action_context)
78
79
80
81
82
83
84
85
{
  fprintf(stderr, "Should not come here %s:%d\n", __FILE__, __LINE__);
  exit(1);
  
  return 0;
}

static int buf_writer_ioctl(
86
87
  struct labcomm_writer *w, 
  struct labcomm_writer_action_context *action_context,
88
  int signature_index, const struct labcomm_signature *signature,
89
  uint32_t action, va_list arg)
90
91
92
93
94
95
96
97
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
131
132
133
134
135
136
{
  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;

      if (w->pos != count) {
	fprintf(stderr, "Invalid length encoded %d != %d (%s:%d)\n", 
		w->pos, count, __FILE__, line);
	mismatch = 1;
      } 
      for (mismatch = 0, i = 0 ; i < count ; i++) {
	if (expected[i] >= 0 && expected[i] != buffer[i]) {
	  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) {
	    printf(".. ");
	  } else {
	    printf("%2.2x ", expected[i] );
	  }
	}
	printf("\n");
	exit(1);
      }
      result = 0;
    } break;
    case IOCTL_WRITER_RESET: {
      w->pos = 0;
      result = 0;
    }
  }
  return result;
}

137
const struct labcomm_writer_action writer_action = {
138
139
140
141
142
143
144
  .alloc = buf_writer_alloc,
  .free = buf_writer_free,
  .start = buf_writer_start,
  .end = buf_writer_end,
  .flush = buf_writer_flush,
  .ioctl = buf_writer_ioctl
};
145

146
147
148
149
150
static struct labcomm_writer_action_context action_context = {
  .next = NULL,
  .action = &writer_action,
  .context = NULL
}; 
151
static struct labcomm_writer buffer_writer = {
152
  .action_context = &action_context,
153
154
155
156
157
158
159
  .data = buffer,
  .data_size = sizeof(buffer),
  .count = sizeof(buffer),
  .pos = 0,
  .error = 0,
};

160
void dump_encoder(struct labcomm_encoder *encoder)
161
162
163
{
  int i;
  
164
165
  for (i = 0 ; i < writer->pos ; i++) {
    printf("%2.2x ", writer->data[i]);
166
167
168
169
  }
  printf("\n");
}

170
171
172
173
174
175
176
177
178
#define EXPECT(...)							\
  {									\
    int expected[] = __VA_ARGS__;					\
    labcomm_encoder_ioctl(encoder, IOCTL_WRITER_ASSERT_BYTES,		\
			  __LINE__,					\
			  sizeof(expected)/sizeof(expected[0]),		\
			  expected);					\
  }

179
180
181
int main(void)
{
  generated_encoding_B B = 1;
182
  generated_encoding_R R;
183

Anders Blomdell's avatar
Anders Blomdell committed
184
185
186
187
188
  struct labcomm_encoder *encoder = labcomm_encoder_new(
    &buffer_writer, 
    labcomm_default_error_handler,
    labcomm_default_memory,
    labcomm_pthread_scheduler_new(labcomm_default_memory));
189
190

  labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
191
  /* Register twice to make sure that only one registration gets encoded */
192
  labcomm_encoder_register_generated_encoding_V(encoder);
193
  labcomm_encoder_register_generated_encoding_V(encoder);
194
  EXPECT({ 0x02, 0x06, -1, 0x01, 'V', 0x02, 0x11, 0x00 });
195
196

  labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
197
  /* Register twice to make sure that only one registration gets encoded */
198
  labcomm_encoder_register_generated_encoding_B(encoder);
199
  labcomm_encoder_register_generated_encoding_B(encoder);
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  EXPECT({0x02, 0x05, -1, 0x01, 'B', 0x01, 0x21});

  labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
  /* Register twice to make sure that only one registration gets encoded */
  labcomm_encoder_sample_ref_register(encoder, 
                                      labcomm_signature_generated_encoding_V);
  labcomm_encoder_sample_ref_register(encoder, 
                                      labcomm_signature_generated_encoding_V);
  EXPECT({0x03, 0x06, -1, 0x01, 'V', 0x02, 0x11, 0x00});

  labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
  /* Register twice to make sure that only one registration gets encoded */
  labcomm_encoder_sample_ref_register(encoder, 
                                      labcomm_signature_generated_encoding_B);
  labcomm_encoder_sample_ref_register(encoder, 
                                      labcomm_signature_generated_encoding_B);
  EXPECT({0x03, 0x05, -1, 0x01, 'B', 0x01, 0x21});

  labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
  /* Register twice to make sure that only one registration gets encoded */
  labcomm_encoder_sample_ref_register(encoder, 
                                      labcomm_signature_generated_encoding_R);
  labcomm_encoder_sample_ref_register(encoder, 
                                      labcomm_signature_generated_encoding_R);
  EXPECT({0x03, 0x08, -1, 0x01, 'R', 0x04, 0x10, 0x01, 0x04, 0x03});
225
226

  labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
227
228
  // was: labcomm_encode_generated_encoding_V(encoder, &V);
  labcomm_encode_generated_encoding_V(encoder);
229
  EXPECT({-1, 0x00 });
230
231
232

  labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
  labcomm_encode_generated_encoding_B(encoder, &B);
233
  EXPECT({-1, 0x01, 1});
234

235
236
237
238
239
240
241
242
243
244
  labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
  R.a[0] = labcomm_signature_generated_encoding_V;
  R.a[1] = labcomm_signature_generated_encoding_B;
  R.a[2] = labcomm_signature_generated_encoding_UnusedE;
  R.a[3] = labcomm_signature_generated_encoding_R;
  labcomm_encode_generated_encoding_R(encoder, &R);
  EXPECT({-1, 0x10, 0x00, 0x00, 0x00, -1,
                    0x00, 0x00, 0x00, -1,
                    0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, -1});
245
246
247
  return 0;
}