]> sigrok.org Git - libsigrok.git/blame - tests/conv.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / tests / conv.c
CommitLineData
f2a9a7c2
GS
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2020 Gerhard Sittig <gerhard.sittig@gmx.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <config.h>
21#include <check.h>
22#include <libsigrok/libsigrok.h>
23#include <stdlib.h>
24#include <string.h>
25#include "lib.h"
26#include "libsigrok-internal.h"
27
28static const uint8_t buff1234[] = {
29 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
30};
d770bfbb
GS
31static const uint8_t buff8125fb[] = {
32 0x41, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
33};
34static const uint8_t buff8125fl[] = {
35 0x00, 0x00, 0x02, 0x41, 0x00, 0x00, 0x00, 0x00,
36};
8f87c528
GS
37static const uint8_t buff1234large[] = {
38 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
39 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
40 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
41 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
42 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
43 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
44 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
45 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40,
46};
f2a9a7c2
GS
47
48START_TEST(test_endian_macro)
49{
50 const uint8_t *p8;
51 const uint16_t *p16;
52 const uint32_t *p32;
53
54 p8 = (const void *)&buff1234[0];
55 fail_unless(R8(&p8[0]) == 0x11);
56 fail_unless(R8(&p8[1]) == 0x22);
57 fail_unless(R8(&p8[2]) == 0x33);
58 fail_unless(R8(&p8[3]) == 0x44);
59
60 p16 = (const void *)&buff1234[0];
61 fail_unless(RB16(&p16[0]) == 0x1122);
62 fail_unless(RB16(&p16[1]) == 0x3344);
63
64 p16 = (const void *)&buff1234[0];
65 fail_unless(RL16(&p16[0]) == 0x2211);
66 fail_unless(RL16(&p16[1]) == 0x4433);
67
68 p32 = (const void *)&buff1234[0];
69 fail_unless(RB32(&p32[0]) == 0x11223344);
70 fail_unless(RB32(&p32[1]) == 0x55667788);
71
72 p32 = (const void *)&buff1234[0];
73 fail_unless(RL32(&p32[0]) == 0x44332211);
74 fail_unless(RL32(&p32[1]) == 0x88776655);
75
76 p16 = (const void *)&buff1234[0];
77 fail_unless(RB16(p16++) == 0x1122);
78 fail_unless(RB16(p16++) == 0x3344);
79}
80END_TEST
81
d770bfbb
GS
82START_TEST(test_endian_read)
83{
84 fail_unless(read_u8(&buff1234[0]) == 0x11);
85 fail_unless(read_u8(&buff1234[3]) == 0x44);
86 fail_unless(read_u8(&buff1234[7]) == 0x88);
87
88 fail_unless(read_u16be(&buff1234[0]) == 0x1122);
89 fail_unless(read_u16be(&buff1234[6]) == 0x7788);
90
91 fail_unless(read_u16le(&buff1234[0]) == 0x2211);
92 fail_unless(read_u16le(&buff1234[6]) == 0x8877);
93
94 fail_unless(read_i16be(&buff1234[6]) == 0x7788);
95 fail_unless(read_i16le(&buff1234[6]) == (int16_t)0x8877);
96
97 fail_unless(read_u32be(&buff1234[0]) == 0x11223344);
98 fail_unless(read_u32be(&buff1234[4]) == 0x55667788);
99
100 fail_unless(read_u32le(&buff1234[0]) == 0x44332211);
101 fail_unless(read_u32le(&buff1234[4]) == 0x88776655);
102
103 fail_unless(read_i32be(&buff1234[0]) == 0x11223344);
104 fail_unless(read_i32be(&buff1234[4]) == 0x55667788);
105 fail_unless(read_i32le(&buff1234[4]) == (int32_t)0x88776655ull);
106
107 fail_unless(read_u64be(&buff1234[0]) == 0x1122334455667788ull);
108 fail_unless(read_u64le(&buff1234[0]) == 0x8877665544332211ull);
109 fail_unless(read_i64be(&buff1234[0]) == 0x1122334455667788ull);
110 fail_unless(read_i64le(&buff1234[0]) == (int64_t)0x8877665544332211ull);
111
112 fail_unless(read_fltbe(&buff8125fb[0]) == 8.125);
113 fail_unless(read_fltle(&buff8125fl[0]) == 8.125);
114}
115END_TEST
116
0c7c6a1d
GS
117START_TEST(test_endian_read_inc_len)
118{
119 const uint8_t *p;
120 size_t l;
121
122 /* Position to the start of the input stream. */
123 p = &buff1234[0];
124 l = sizeof(buff1234);
125
126 /* Read several fields of known type and values. */
127 fail_unless(l == 8);
128 fail_unless(read_u8_inc_len(&p, &l) == 0x11);
129 fail_unless(l == 7);
130 fail_unless(read_u8_inc_len(&p, &l) == 0x22);
131 fail_unless(l == 6);
132 fail_unless(read_u16le_inc_len(&p, &l) == 0x4433);
133 fail_unless(l == 4);
134 fail_unless(read_u16le_inc_len(&p, &l) == 0x6655);
135 fail_unless(l == 2);
136 fail_unless(read_u16le_inc_len(&p, &l) == 0x8877);
137 fail_unless(l == 0);
138
139 /* Read beyond the end of the input stream. */
140 fail_unless(read_u8_inc_len(&p, &l) == 0x0);
141 fail_unless(l == 0);
142 fail_unless(read_u16le_inc_len(&p, &l) == 0x0);
143 fail_unless(l == 0);
144}
145END_TEST
146
d770bfbb
GS
147START_TEST(test_endian_read_inc)
148{
149 const uint8_t *p;
150
151 p = &buff1234[0];
152 fail_unless(read_u8_inc(&p) == 0x11);
153 fail_unless(read_u8_inc(&p) == 0x22);
154 fail_unless(read_u8_inc(&p) == 0x33);
155 fail_unless(p == &buff1234[3 * sizeof(uint8_t)]);
156
157 p = &buff1234[0];
158 fail_unless(read_u16be_inc(&p) == 0x1122);
159 fail_unless(read_u16be_inc(&p) == 0x3344);
160 fail_unless(p == &buff1234[2 * sizeof(uint16_t)]);
161
162 p = &buff1234[0];
163 fail_unless(read_u16le_inc(&p) == 0x2211);
164 fail_unless(read_u16le_inc(&p) == 0x4433);
165 fail_unless(p == &buff1234[2 * sizeof(uint16_t)]);
166
167 p = &buff1234[0];
168 fail_unless(read_u24le_inc(&p) == 0x332211);
169 fail_unless(read_u24le_inc(&p) == 0x665544);
170 fail_unless(p == &buff1234[2 * 3 * sizeof(uint8_t)]);
171
172 p = &buff1234[0];
173 fail_unless(read_u32be_inc(&p) == 0x11223344ul);
174 fail_unless(read_u32be_inc(&p) == 0x55667788ul);
175 fail_unless(p == &buff1234[2 * sizeof(uint32_t)]);
176
177 p = &buff1234[0];
178 fail_unless(read_u32le_inc(&p) == 0x44332211ul);
179 fail_unless(read_u32le_inc(&p) == 0x88776655ul);
180 fail_unless(p == &buff1234[2 * sizeof(uint32_t)]);
181
182 p = &buff1234[0];
183 fail_unless(read_u64be_inc(&p) == 0x1122334455667788);
184 fail_unless(p == &buff1234[sizeof(uint64_t)]);
185
186 p = &buff1234[0];
187 fail_unless(read_u64le_inc(&p) == 0x8877665544332211ull);
188 fail_unless(p == &buff1234[sizeof(uint64_t)]);
189}
190END_TEST
191
192START_TEST(test_endian_write)
193{
194 uint8_t buff[2 * sizeof(uint64_t)];
195
196 memset(buff, 0, sizeof(buff));
197 write_u8(&buff[0], 0x11);
198 fail_unless(memcmp(&buff[0], &buff1234[0], sizeof(uint8_t)) == 0);
199
200 memset(buff, 0, sizeof(buff));
201 write_u8(&buff[0], 0x22);
202 write_u8(&buff[1], 0x33);
203 write_u8(&buff[2], 0x44);
204 write_u8(&buff[3], 0x55);
205 fail_unless(memcmp(&buff[0], &buff1234[1], 4 * sizeof(uint8_t)) == 0);
206
207 memset(buff, 0, sizeof(buff));
208 write_u16be(&buff[0 * sizeof(uint16_t)], 0x1122);
209 write_u16be(&buff[1 * sizeof(uint16_t)], 0x3344);
210 fail_unless(memcmp(&buff[0], &buff1234[0], 2 * sizeof(uint16_t)) == 0);
211
212 memset(buff, 0, sizeof(buff));
213 write_u16le(&buff[0 * sizeof(uint16_t)], 0x4433);
214 write_u16le(&buff[1 * sizeof(uint16_t)], 0x6655);
215 fail_unless(memcmp(&buff[0], &buff1234[2], 2 * sizeof(uint16_t)) == 0);
216
217 memset(buff, 0, sizeof(buff));
218 write_u32be(&buff[0 * sizeof(uint32_t)], 0x11223344);
219 write_u32be(&buff[1 * sizeof(uint32_t)], 0x55667788);
220 fail_unless(memcmp(&buff[0], &buff1234[0], 2 * sizeof(uint32_t)) == 0);
221
222 memset(buff, 0, sizeof(buff));
223 write_u32le(&buff[0 * sizeof(uint32_t)], 0x44332211);
224 write_u32le(&buff[1 * sizeof(uint32_t)], 0x88776655);
225 fail_unless(memcmp(&buff[0], &buff1234[0], 2 * sizeof(uint32_t)) == 0);
226
227 memset(buff, 0, sizeof(buff));
228 write_fltbe(&buff[0], 8.125);
229 fail_unless(memcmp(&buff[0], &buff8125fb[0], sizeof(float)) == 0);
230
231 memset(buff, 0, sizeof(buff));
232 write_fltle(&buff[0], 8.125);
233 fail_unless(memcmp(&buff[0], &buff8125fl[0], sizeof(float)) == 0);
234}
235END_TEST
236
237START_TEST(test_endian_write_inc)
238{
df98afc6 239 uint8_t buff[3 * sizeof(uint64_t)];
d770bfbb
GS
240 uint8_t *p;
241 size_t l;
242
243 memset(buff, 0, sizeof(buff));
8f87c528 244
d770bfbb
GS
245 p = &buff[0];
246 write_u8_inc(&p, 0x11);
247 write_u16be_inc(&p, 0x2233);
248 write_u32be_inc(&p, 0x44556677);
249 l = p - &buff[0];
250 fail_unless(l == sizeof(uint8_t) + sizeof(uint16_t) + sizeof(uint32_t));
251 fail_unless(memcmp(&buff[0], &buff1234[0], l) == 0);
8f87c528
GS
252
253 p = &buff[0];
254 write_u48le_inc(&p, 0x060504030201);
255 write_u48le_inc(&p, 0x0c0b0a090807);
256 write_u48le_inc(&p, 0x1211100f0e0d);
257 write_u48le_inc(&p, 0x181716151413);
258 l = p - &buff[0];
259 fail_unless(l == 4 * 48 / 8 * sizeof(uint8_t));
260 fail_unless(memcmp(&buff[0], &buff1234large[0], l) == 0);
1e389eb7
GS
261
262 p = &buff[0];
263 write_u24le_inc(&p, 0xfe030201);
264 write_u40le_inc(&p, 0xdcba0807060504ul);
265 l = p - &buff[0];
266 fail_unless(l == 24 / 8 + 40 / 8);
267 fail_unless(memcmp(&buff[0], &buff1234large[0], l) == 0);
d770bfbb
GS
268}
269END_TEST
270
f2a9a7c2
GS
271Suite *suite_conv(void)
272{
273 Suite *s;
274 TCase *tc;
275
276 s = suite_create("conv");
277
278 tc = tcase_create("endian");
279 tcase_add_test(tc, test_endian_macro);
d770bfbb
GS
280 tcase_add_test(tc, test_endian_read);
281 tcase_add_test(tc, test_endian_read_inc);
0c7c6a1d 282 tcase_add_test(tc, test_endian_read_inc_len);
d770bfbb
GS
283 tcase_add_test(tc, test_endian_write);
284 tcase_add_test(tc, test_endian_write_inc);
f2a9a7c2
GS
285 suite_add_tcase(s, tc);
286
287 return s;
288}