test_labcomm_private.c 4.76 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
#include <stdint.h>
#include <string.h>
#include "labcomm_private.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);
}

static unsigned char buffer[128];
static labcomm_encoder_t encoder = {
  .context = NULL,
  .writer =  {
    .context = NULL,
    .data = buffer,
    .data_size = sizeof(buffer),
    .count = sizeof(buffer),
    .pos = 0,
    .error = 0,
    .write = test_write,
    .ioctl = NULL,
    .on_error = NULL,
  },
  .do_register = NULL,
  .do_encode = NULL,
  .on_error = NULL,
};

static labcomm_decoder_t decoder = {
  .context = NULL,
  .reader =  {
    .context = NULL,
    .data = buffer,
    .data_size = sizeof(buffer),
    .count = sizeof(buffer),
    .pos = 0,
//    .error = 0,
    .read = test_read,
    .ioctl = NULL,
    .on_error = NULL,
  },
  .do_register = NULL,
//  .do_decode = NULL,
  .on_error = NULL,
};

typedef unsigned int packed32;
typedef unsigned char boolean;

#define TEST_WRITE_READ(type, format, value, expect_count, expect_bytes) \
  {									\
    type decoded;							\
    encoder.writer.pos = 0;						\
    labcomm_encode_##type(&encoder, value);				\
    writer_assert(#type, __LINE__, expect_count, (uint8_t*)expect_bytes); \
    decoder.reader.pos = 0;					\
    decoded = labcomm_decode_##type(&decoder);				\
    if (decoded != value) {						\
      fprintf(stderr, "Decode error" format " != " format " @%s:%d \n", value, decoded, \
	      __FILE__, __LINE__);					\
      exit(1);								\
    }									\
  }

static void writer_assert(char *type,
			  int line,
			  int count,
			  uint8_t *bytes)
{
  if (encoder.writer.pos != count) {
    fprintf(stderr, 
	    "Wrong number of bytes written for '%s' (%d != %d) @%s:%d\n",
	   type, encoder.writer.pos, count, __FILE__, line);
    exit(1);
  }
  if (memcmp(encoder.writer.data, bytes, count) != 0) {
    int i;

    fprintf(stderr, "Wrong bytes written for '%s' ( ", type);
    for (i = 0 ; i < count ; i++) {
      fprintf(stderr, "%2.2x ", encoder.writer.data[i]);
    }
    fprintf(stderr, "!= ");
    for (i = 0 ; i < count ; i++) {
      fprintf(stderr, "%2.2x ", bytes[i]);
    }
    fprintf(stderr, ") @%s:%d\n", __FILE__, line);
    exit(1);
  }
}

int main(void)
{
  TEST_WRITE_READ(packed32, "%d", 0x0, 1, "\x00");
  TEST_WRITE_READ(packed32, "%d", 0x7f, 1, "\x7f");
  TEST_WRITE_READ(packed32, "%d", 0x80, 2, "\x80\x80");
  TEST_WRITE_READ(packed32, "%d", 0x3fff, 2, "\xbf\xff");
  TEST_WRITE_READ(packed32, "%d", 0x4000, 3, "\xc0\x40\x00");
  TEST_WRITE_READ(packed32, "%d", 0x1fffff, 3, "\xdf\xff\xff");
  TEST_WRITE_READ(packed32, "%d", 0x200000, 4, "\xe0\x20\x00\x00");
  TEST_WRITE_READ(packed32, "%d", 0xfffffff, 4, "\xef\xff\xff\xff");
  TEST_WRITE_READ(packed32, "%d", 0x10000000, 5, "\xf0\x10\x00\x00\x00");
  TEST_WRITE_READ(packed32, "%d", 0xffffffff, 5, "\xf0\xff\xff\xff\xff");
  TEST_WRITE_READ(boolean, "%d", 0, 1, "\00");
  TEST_WRITE_READ(short, "%d", 0, 2, "\x00\x00");
  TEST_WRITE_READ(short, "%d", 0x7fff, 2, "\x7f\xff");
  TEST_WRITE_READ(short, "%d", -1, 2, "\xff\xff");
  TEST_WRITE_READ(int, "%d", 0, 4, "\x00\x00\x00\x00");
  TEST_WRITE_READ(int, "%d", 0x7fffffff, 4, "\x7f\xff\xff\xff");
  TEST_WRITE_READ(int, "%d", -1, 4, "\xff\xff\xff\xff");
  TEST_WRITE_READ(float, "%f", 0.0, 4, "\x00\x00\x00\x00");
  TEST_WRITE_READ(float, "%f", 1.0, 4, "\x3f\x80\x00\x00");
  TEST_WRITE_READ(float, "%f", 2.0, 4, "\x40\x00\x00\x00");
  TEST_WRITE_READ(float, "%f", 0.5, 4, "\x3f\x00\x00\x00");
  TEST_WRITE_READ(float, "%f", 0.25, 4, "\x3e\x80\x00\x00");
  TEST_WRITE_READ(float, "%f", -0.0, 4, "\x80\x00\x00\x00");
  TEST_WRITE_READ(float, "%f", -1.0, 4, "\xbf\x80\x00\x00");
  TEST_WRITE_READ(float, "%f", -2.0, 4, "\xc0\x00\x00\x00");
  TEST_WRITE_READ(float, "%f", -0.5, 4, "\xbf\x00\x00\x00");
  TEST_WRITE_READ(float, "%f", -0.25, 4, "\xbe\x80\x00\x00");
  TEST_WRITE_READ(double, "%f", 0.0, 8, "\x00\x00\x00\x00\x00\x00\x00\x00");
  TEST_WRITE_READ(double, "%f", 1.0, 8, "\x3f\xf0\x00\x00\x00\x00\x00\x00");
  TEST_WRITE_READ(double, "%f", 2.0, 8, "\x40\x00\x00\x00\x00\x00\x00\x00");
  TEST_WRITE_READ(double, "%f", 0.5, 8, "\x3f\xe0\x00\x00\x00\x00\x00\x00");
  TEST_WRITE_READ(double, "%f", 0.25, 8, "\x3f\xd0\x00\x00\x00\x00\x00\x00");
  TEST_WRITE_READ(double, "%f", -0.0, 8, "\x80\x00\x00\x00\x00\x00\x00\x00");
  TEST_WRITE_READ(double, "%f", -1.0, 8, "\xbf\xf0\x00\x00\x00\x00\x00\x00");
  TEST_WRITE_READ(double, "%f", -2.0, 8, "\xc0\x00\x00\x00\x00\x00\x00\x00");
  TEST_WRITE_READ(double, "%f", -0.5, 8, "\xbf\xe0\x00\x00\x00\x00\x00\x00");
  TEST_WRITE_READ(double, "%f", -0.25, 8, "\xbf\xd0\x00\x00\x00\x00\x00\x00");
  fprintf(stderr, "%s succeded\n", __FILE__);
  return 0;
}