]> sigrok.org Git - libsigrok.git/blob - tests/conv.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / tests / conv.c
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
28 static const uint8_t buff1234[] = {
29         0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
30 };
31 static const uint8_t buff8125fb[] = {
32         0x41, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
33 };
34 static const uint8_t buff8125fl[] = {
35         0x00, 0x00, 0x02, 0x41, 0x00, 0x00, 0x00, 0x00,
36 };
37 static 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 };
47
48 START_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 }
80 END_TEST
81
82 START_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 }
115 END_TEST
116
117 START_TEST(test_endian_read_inc)
118 {
119         const uint8_t *p;
120
121         p = &buff1234[0];
122         fail_unless(read_u8_inc(&p) == 0x11);
123         fail_unless(read_u8_inc(&p) == 0x22);
124         fail_unless(read_u8_inc(&p) == 0x33);
125         fail_unless(p == &buff1234[3 * sizeof(uint8_t)]);
126
127         p = &buff1234[0];
128         fail_unless(read_u16be_inc(&p) == 0x1122);
129         fail_unless(read_u16be_inc(&p) == 0x3344);
130         fail_unless(p == &buff1234[2 * sizeof(uint16_t)]);
131
132         p = &buff1234[0];
133         fail_unless(read_u16le_inc(&p) == 0x2211);
134         fail_unless(read_u16le_inc(&p) == 0x4433);
135         fail_unless(p == &buff1234[2 * sizeof(uint16_t)]);
136
137         p = &buff1234[0];
138         fail_unless(read_u24le_inc(&p) == 0x332211);
139         fail_unless(read_u24le_inc(&p) == 0x665544);
140         fail_unless(p == &buff1234[2 * 3 * sizeof(uint8_t)]);
141
142         p = &buff1234[0];
143         fail_unless(read_u32be_inc(&p) == 0x11223344ul);
144         fail_unless(read_u32be_inc(&p) == 0x55667788ul);
145         fail_unless(p == &buff1234[2 * sizeof(uint32_t)]);
146
147         p = &buff1234[0];
148         fail_unless(read_u32le_inc(&p) == 0x44332211ul);
149         fail_unless(read_u32le_inc(&p) == 0x88776655ul);
150         fail_unless(p == &buff1234[2 * sizeof(uint32_t)]);
151
152         p = &buff1234[0];
153         fail_unless(read_u64be_inc(&p) == 0x1122334455667788);
154         fail_unless(p == &buff1234[sizeof(uint64_t)]);
155
156         p = &buff1234[0];
157         fail_unless(read_u64le_inc(&p) == 0x8877665544332211ull);
158         fail_unless(p == &buff1234[sizeof(uint64_t)]);
159 }
160 END_TEST
161
162 START_TEST(test_endian_write)
163 {
164         uint8_t buff[2 * sizeof(uint64_t)];
165
166         memset(buff, 0, sizeof(buff));
167         write_u8(&buff[0], 0x11);
168         fail_unless(memcmp(&buff[0], &buff1234[0], sizeof(uint8_t)) == 0);
169
170         memset(buff, 0, sizeof(buff));
171         write_u8(&buff[0], 0x22);
172         write_u8(&buff[1], 0x33);
173         write_u8(&buff[2], 0x44);
174         write_u8(&buff[3], 0x55);
175         fail_unless(memcmp(&buff[0], &buff1234[1], 4 * sizeof(uint8_t)) == 0);
176
177         memset(buff, 0, sizeof(buff));
178         write_u16be(&buff[0 * sizeof(uint16_t)], 0x1122);
179         write_u16be(&buff[1 * sizeof(uint16_t)], 0x3344);
180         fail_unless(memcmp(&buff[0], &buff1234[0], 2 * sizeof(uint16_t)) == 0);
181
182         memset(buff, 0, sizeof(buff));
183         write_u16le(&buff[0 * sizeof(uint16_t)], 0x4433);
184         write_u16le(&buff[1 * sizeof(uint16_t)], 0x6655);
185         fail_unless(memcmp(&buff[0], &buff1234[2], 2 * sizeof(uint16_t)) == 0);
186
187         memset(buff, 0, sizeof(buff));
188         write_u32be(&buff[0 * sizeof(uint32_t)], 0x11223344);
189         write_u32be(&buff[1 * sizeof(uint32_t)], 0x55667788);
190         fail_unless(memcmp(&buff[0], &buff1234[0], 2 * sizeof(uint32_t)) == 0);
191
192         memset(buff, 0, sizeof(buff));
193         write_u32le(&buff[0 * sizeof(uint32_t)], 0x44332211);
194         write_u32le(&buff[1 * sizeof(uint32_t)], 0x88776655);
195         fail_unless(memcmp(&buff[0], &buff1234[0], 2 * sizeof(uint32_t)) == 0);
196
197         memset(buff, 0, sizeof(buff));
198         write_fltbe(&buff[0], 8.125);
199         fail_unless(memcmp(&buff[0], &buff8125fb[0], sizeof(float)) == 0);
200
201         memset(buff, 0, sizeof(buff));
202         write_fltle(&buff[0], 8.125);
203         fail_unless(memcmp(&buff[0], &buff8125fl[0], sizeof(float)) == 0);
204 }
205 END_TEST
206
207 START_TEST(test_endian_write_inc)
208 {
209         uint8_t buff[3 * sizeof(uint64_t)];
210         uint8_t *p;
211         size_t l;
212
213         memset(buff, 0, sizeof(buff));
214
215         p = &buff[0];
216         write_u8_inc(&p, 0x11);
217         write_u16be_inc(&p, 0x2233);
218         write_u32be_inc(&p, 0x44556677);
219         l = p - &buff[0];
220         fail_unless(l == sizeof(uint8_t) + sizeof(uint16_t) + sizeof(uint32_t));
221         fail_unless(memcmp(&buff[0], &buff1234[0], l) == 0);
222
223         p = &buff[0];
224         write_u48le_inc(&p, 0x060504030201);
225         write_u48le_inc(&p, 0x0c0b0a090807);
226         write_u48le_inc(&p, 0x1211100f0e0d);
227         write_u48le_inc(&p, 0x181716151413);
228         l = p - &buff[0];
229         fail_unless(l == 4 * 48 / 8 * sizeof(uint8_t));
230         fail_unless(memcmp(&buff[0], &buff1234large[0], l) == 0);
231
232         p = &buff[0];
233         write_u24le_inc(&p, 0xfe030201);
234         write_u40le_inc(&p, 0xdcba0807060504ul);
235         l = p - &buff[0];
236         fail_unless(l == 24 / 8 + 40 / 8);
237         fail_unless(memcmp(&buff[0], &buff1234large[0], l) == 0);
238 }
239 END_TEST
240
241 Suite *suite_conv(void)
242 {
243         Suite *s;
244         TCase *tc;
245
246         s = suite_create("conv");
247
248         tc = tcase_create("endian");
249         tcase_add_test(tc, test_endian_macro);
250         tcase_add_test(tc, test_endian_read);
251         tcase_add_test(tc, test_endian_read_inc);
252         tcase_add_test(tc, test_endian_write);
253         tcase_add_test(tc, test_endian_write_inc);
254         suite_add_tcase(s, tc);
255
256         return s;
257 }