test_labcomm_generated_encoding.c 3.72 KB
Newer Older
1
2
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include "labcomm_private.h"
#include "labcomm_private.h"
#include "test/gen/generated_encoding.h"

int test_write(struct labcomm_writer *w, labcomm_writer_action_t a, ...)
{
  fprintf(stderr, "test_write should not be called\n");
  exit(1);
}

int test_read(struct labcomm_reader *r, labcomm_reader_action_t a, ...)
{
  fprintf(stderr, "test_read should not be called\n");
  exit(1);
}

#define IOCTL_WRITER_ASSERT_BYTES 4096
#define IOCTL_WRITER_RESET 4097

static unsigned char buffer[128];

static int buffer_writer_ioctl(
  struct labcomm_writer *w, int action, va_list arg)
{
  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;
}

static int buffer_writer(
  labcomm_writer_t *w, 
  labcomm_writer_action_t action,
  ...)
{
  switch (action) {
    case labcomm_writer_alloc: {
      w->data_size = sizeof(buffer);
      w->count = w->data_size;
      w->data = buffer;
      w->pos = 0;
      w->ioctl = buffer_writer_ioctl;
    } break;
    case labcomm_writer_start: 
    case labcomm_writer_start_signature: {
    } break;
    case labcomm_writer_continue: 
    case labcomm_writer_continue_signature: {
      fprintf(stderr, "Should not come here %s:%d\n", __FILE__, __LINE__);
      exit(1);
    } break;
    case labcomm_writer_end: 
    case labcomm_writer_end_signature: {
    } break;
    case labcomm_writer_free: {
      w->data = 0;
      w->data_size = 0;
      w->count = 0;
      w->pos = 0;
    } break;
  }
  return w->error;
}

void dump_encoder(labcomm_encoder_t *encoder)
{
  int i;
  
  for (i = 0 ; i < encoder->writer.pos ; i++) {
    printf("%2.2x ", encoder->writer.data[i]);
  }
  printf("\n");
}

int main(void)
{
  generated_encoding_V V;
  generated_encoding_B B = 1;

  labcomm_encoder_t *encoder = labcomm_encoder_new(buffer_writer, buffer);

  labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
  labcomm_encoder_register_generated_encoding_V(encoder);
  {
    int expected[] = { 0x02, -1, 0x01, 'V', 0x11, 0x00 };
    labcomm_encoder_ioctl(encoder, IOCTL_WRITER_ASSERT_BYTES, 
			  __LINE__, 
			  6, expected);
  }

  labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
  labcomm_encoder_register_generated_encoding_B(encoder);
  {
    int expected[] = { 0x02, -1, 0x01, 'B', 0x21 };
    labcomm_encoder_ioctl(encoder, IOCTL_WRITER_ASSERT_BYTES, 
			  __LINE__, 
			  5, expected);
  }

  labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
  labcomm_encode_generated_encoding_V(encoder, &V);
  {
    int expected[] = { -1 };
    labcomm_encoder_ioctl(encoder, IOCTL_WRITER_ASSERT_BYTES, 
			  __LINE__, 
			  1, expected);
  }

  labcomm_encoder_ioctl(encoder, IOCTL_WRITER_RESET);
  labcomm_encode_generated_encoding_B(encoder, &B);
  {
    int expected[] = { -1, 1 };
    labcomm_encoder_ioctl(encoder, IOCTL_WRITER_ASSERT_BYTES, 
			  __LINE__, 
			  2, expected);
  }

  return 0;
}