]> sigrok.org Git - libsigrok.git/blob - tests/conv.c
ecbe56190f32359556ce4a751a5b1862058103ab
[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
38 START_TEST(test_endian_macro)
39 {
40         const uint8_t *p8;
41         const uint16_t *p16;
42         const uint32_t *p32;
43
44         p8 = (const void *)&buff1234[0];
45         fail_unless(R8(&p8[0]) == 0x11);
46         fail_unless(R8(&p8[1]) == 0x22);
47         fail_unless(R8(&p8[2]) == 0x33);
48         fail_unless(R8(&p8[3]) == 0x44);
49
50         p16 = (const void *)&buff1234[0];
51         fail_unless(RB16(&p16[0]) == 0x1122);
52         fail_unless(RB16(&p16[1]) == 0x3344);
53
54         p16 = (const void *)&buff1234[0];
55         fail_unless(RL16(&p16[0]) == 0x2211);
56         fail_unless(RL16(&p16[1]) == 0x4433);
57
58         p32 = (const void *)&buff1234[0];
59         fail_unless(RB32(&p32[0]) == 0x11223344);
60         fail_unless(RB32(&p32[1]) == 0x55667788);
61
62         p32 = (const void *)&buff1234[0];
63         fail_unless(RL32(&p32[0]) == 0x44332211);
64         fail_unless(RL32(&p32[1]) == 0x88776655);
65
66         p16 = (const void *)&buff1234[0];
67         fail_unless(RB16(p16++) == 0x1122);
68         fail_unless(RB16(p16++) == 0x3344);
69 }
70 END_TEST
71
72 START_TEST(test_endian_read)
73 {
74         fail_unless(read_u8(&buff1234[0]) == 0x11);
75         fail_unless(read_u8(&buff1234[3]) == 0x44);
76         fail_unless(read_u8(&buff1234[7]) == 0x88);
77
78         fail_unless(read_u16be(&buff1234[0]) == 0x1122);
79         fail_unless(read_u16be(&buff1234[6]) == 0x7788);
80
81         fail_unless(read_u16le(&buff1234[0]) == 0x2211);
82         fail_unless(read_u16le(&buff1234[6]) == 0x8877);
83
84         fail_unless(read_i16be(&buff1234[6]) == 0x7788);
85         fail_unless(read_i16le(&buff1234[6]) == (int16_t)0x8877);
86
87         fail_unless(read_u32be(&buff1234[0]) == 0x11223344);
88         fail_unless(read_u32be(&buff1234[4]) == 0x55667788);
89
90         fail_unless(read_u32le(&buff1234[0]) == 0x44332211);
91         fail_unless(read_u32le(&buff1234[4]) == 0x88776655);
92
93         fail_unless(read_i32be(&buff1234[0]) == 0x11223344);
94         fail_unless(read_i32be(&buff1234[4]) == 0x55667788);
95         fail_unless(read_i32le(&buff1234[4]) == (int32_t)0x88776655ull);
96
97         fail_unless(read_u64be(&buff1234[0]) == 0x1122334455667788ull);
98         fail_unless(read_u64le(&buff1234[0]) == 0x8877665544332211ull);
99         fail_unless(read_i64be(&buff1234[0]) == 0x1122334455667788ull);
100         fail_unless(read_i64le(&buff1234[0]) == (int64_t)0x8877665544332211ull);
101
102         fail_unless(read_fltbe(&buff8125fb[0]) == 8.125);
103         fail_unless(read_fltle(&buff8125fl[0]) == 8.125);
104 }
105 END_TEST
106
107 START_TEST(test_endian_read_inc)
108 {
109         const uint8_t *p;
110
111         p = &buff1234[0];
112         fail_unless(read_u8_inc(&p) == 0x11);
113         fail_unless(read_u8_inc(&p) == 0x22);
114         fail_unless(read_u8_inc(&p) == 0x33);
115         fail_unless(p == &buff1234[3 * sizeof(uint8_t)]);
116
117         p = &buff1234[0];
118         fail_unless(read_u16be_inc(&p) == 0x1122);
119         fail_unless(read_u16be_inc(&p) == 0x3344);
120         fail_unless(p == &buff1234[2 * sizeof(uint16_t)]);
121
122         p = &buff1234[0];
123         fail_unless(read_u16le_inc(&p) == 0x2211);
124         fail_unless(read_u16le_inc(&p) == 0x4433);
125         fail_unless(p == &buff1234[2 * sizeof(uint16_t)]);
126
127         p = &buff1234[0];
128         fail_unless(read_u24le_inc(&p) == 0x332211);
129         fail_unless(read_u24le_inc(&p) == 0x665544);
130         fail_unless(p == &buff1234[2 * 3 * sizeof(uint8_t)]);
131
132         p = &buff1234[0];
133         fail_unless(read_u32be_inc(&p) == 0x11223344ul);
134         fail_unless(read_u32be_inc(&p) == 0x55667788ul);
135         fail_unless(p == &buff1234[2 * sizeof(uint32_t)]);
136
137         p = &buff1234[0];
138         fail_unless(read_u32le_inc(&p) == 0x44332211ul);
139         fail_unless(read_u32le_inc(&p) == 0x88776655ul);
140         fail_unless(p == &buff1234[2 * sizeof(uint32_t)]);
141
142         p = &buff1234[0];
143         fail_unless(read_u64be_inc(&p) == 0x1122334455667788);
144         fail_unless(p == &buff1234[sizeof(uint64_t)]);
145
146         p = &buff1234[0];
147         fail_unless(read_u64le_inc(&p) == 0x8877665544332211ull);
148         fail_unless(p == &buff1234[sizeof(uint64_t)]);
149 }
150 END_TEST
151
152 START_TEST(test_endian_write)
153 {
154         uint8_t buff[2 * sizeof(uint64_t)];
155
156         memset(buff, 0, sizeof(buff));
157         write_u8(&buff[0], 0x11);
158         fail_unless(memcmp(&buff[0], &buff1234[0], sizeof(uint8_t)) == 0);
159
160         memset(buff, 0, sizeof(buff));
161         write_u8(&buff[0], 0x22);
162         write_u8(&buff[1], 0x33);
163         write_u8(&buff[2], 0x44);
164         write_u8(&buff[3], 0x55);
165         fail_unless(memcmp(&buff[0], &buff1234[1], 4 * sizeof(uint8_t)) == 0);
166
167         memset(buff, 0, sizeof(buff));
168         write_u16be(&buff[0 * sizeof(uint16_t)], 0x1122);
169         write_u16be(&buff[1 * sizeof(uint16_t)], 0x3344);
170         fail_unless(memcmp(&buff[0], &buff1234[0], 2 * sizeof(uint16_t)) == 0);
171
172         memset(buff, 0, sizeof(buff));
173         write_u16le(&buff[0 * sizeof(uint16_t)], 0x4433);
174         write_u16le(&buff[1 * sizeof(uint16_t)], 0x6655);
175         fail_unless(memcmp(&buff[0], &buff1234[2], 2 * sizeof(uint16_t)) == 0);
176
177         memset(buff, 0, sizeof(buff));
178         write_u32be(&buff[0 * sizeof(uint32_t)], 0x11223344);
179         write_u32be(&buff[1 * sizeof(uint32_t)], 0x55667788);
180         fail_unless(memcmp(&buff[0], &buff1234[0], 2 * sizeof(uint32_t)) == 0);
181
182         memset(buff, 0, sizeof(buff));
183         write_u32le(&buff[0 * sizeof(uint32_t)], 0x44332211);
184         write_u32le(&buff[1 * sizeof(uint32_t)], 0x88776655);
185         fail_unless(memcmp(&buff[0], &buff1234[0], 2 * sizeof(uint32_t)) == 0);
186
187         memset(buff, 0, sizeof(buff));
188         write_fltbe(&buff[0], 8.125);
189         fail_unless(memcmp(&buff[0], &buff8125fb[0], sizeof(float)) == 0);
190
191         memset(buff, 0, sizeof(buff));
192         write_fltle(&buff[0], 8.125);
193         fail_unless(memcmp(&buff[0], &buff8125fl[0], sizeof(float)) == 0);
194 }
195 END_TEST
196
197 START_TEST(test_endian_write_inc)
198 {
199         uint8_t buff[2 * sizeof(uint64_t)];
200         uint8_t *p;
201         size_t l;
202
203         memset(buff, 0, sizeof(buff));
204         p = &buff[0];
205         write_u8_inc(&p, 0x11);
206         write_u16be_inc(&p, 0x2233);
207         write_u32be_inc(&p, 0x44556677);
208         l = p - &buff[0];
209         fail_unless(l == sizeof(uint8_t) + sizeof(uint16_t) + sizeof(uint32_t));
210         fail_unless(memcmp(&buff[0], &buff1234[0], l) == 0);
211 }
212 END_TEST
213
214 Suite *suite_conv(void)
215 {
216         Suite *s;
217         TCase *tc;
218
219         s = suite_create("conv");
220
221         tc = tcase_create("endian");
222         tcase_add_test(tc, test_endian_macro);
223         tcase_add_test(tc, test_endian_read);
224         tcase_add_test(tc, test_endian_read_inc);
225         tcase_add_test(tc, test_endian_write);
226         tcase_add_test(tc, test_endian_write_inc);
227         suite_add_tcase(s, tc);
228
229         return s;
230 }