1/*
2 * Copyright (C) 2020 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24#include "compiler.h"
25#include "bi_test.h"
26
27unsigned nr_pass = 0;
28unsigned nr_fail = 0;
29
30static void
31bi_test_pack_format_1(struct util_dynarray *result)
32{
33   /* Test case from the blob */
34   struct bi_packed_tuple tuples[] = {
35      { 0x2380cb1c02200000, 0x10e0 },
36   };
37
38   uint64_t header = 0x021000011800;
39
40   util_dynarray_clear(result);
41   bi_pack_format(result, 1, tuples, 1, header, 0, 0, true);
42   uint64_t *result_u64 = (uint64_t *) result->data;
43
44   BIT_ASSERT(result->size == 16);
45   BIT_ASSERT(result_u64[0] == 0x80cb1c022000004a);
46   BIT_ASSERT(result_u64[1] == 0x10800008c000e023);
47}
48
49static void
50bi_test_pack_format_2(struct util_dynarray *result)
51{
52   struct bi_packed_tuple tuples[] = {
53      { 0x9380cb6044000044, 0xf65 },
54      { 0xaf8721a05c000081, 0x1831 },
55   };
56
57   util_dynarray_clear(result);
58   bi_pack_format(result, 0, tuples, 2, 0x52800011800, 0, 0, false);
59   bi_pack_format(result, 2, tuples, 2, 0x52800011800, 0, 0, false);
60   uint64_t *result_u64 = (uint64_t *) result->data;
61
62   BIT_ASSERT(result->size == 32);
63   BIT_ASSERT(result_u64[0] == 0x80cb604400004429);
64   BIT_ASSERT(result_u64[1] == 0x29400008c0076593);
65   BIT_ASSERT(result_u64[2] == 0x8721a05c00008103);
66   BIT_ASSERT(result_u64[3] == 0x60000000000031af);
67}
68
69static void
70bi_test_pack_format_3(struct util_dynarray *result)
71{
72   struct bi_packed_tuple tuples[] = {
73      { 0x93805b8040000000, 0xf65 },
74      { 0x93886db05c000000, 0xf65 },
75      { 0xb380cb180c000080, 0x18b1 },
76   };
77
78   util_dynarray_clear(result);
79   bi_pack_format(result, 0, tuples, 3, 0x3100000000, 0, 0, true);
80   bi_pack_format(result, 3, tuples, 3, 0x3100000000, 0, 0, true);
81   bi_pack_format(result, 4, tuples, 3, 0x3100000000, 0, 0, true);
82   uint64_t *result_u64 = (uint64_t *) result->data;
83
84   BIT_ASSERT(result->size == 48);
85   BIT_ASSERT(result_u64[0] == 0x805b804000000029);
86   BIT_ASSERT(result_u64[1] == 0x188000000076593);
87   BIT_ASSERT(result_u64[2] == 0x886db05c00000021);
88   BIT_ASSERT(result_u64[3] == 0x58c0600004076593);
89   BIT_ASSERT(result_u64[4] == 0x44);
90   BIT_ASSERT(result_u64[5] == 0x60002c6ce0300000);
91}
92
93static void
94bi_test_pack_format_4(struct util_dynarray *result)
95{
96   struct bi_packed_tuple tuples[] = {
97      { 0xad8c87004000005f, 0x2f18 },
98      { 0xad8c87385c00004f, 0x2f18 },
99      { 0xad8c87385c00006e, 0x2f18 },
100      { 0xb380cb182c000080, 0x18b1 },
101   };
102
103   uint64_t EC0 = (0x10000001ff000000) >> 4;
104
105   util_dynarray_clear(result);
106   bi_pack_format(result, 0, tuples, 4, 0x3100000000, EC0, 0, false);
107   bi_pack_format(result, 3, tuples, 4, 0x3100000000, EC0, 0, false);
108   bi_pack_format(result, 6, tuples, 4, 0x3100000000, EC0, 0, false);
109   uint64_t *result_u64 = (uint64_t *) result->data;
110
111   BIT_ASSERT(result->size == 48);
112   BIT_ASSERT(result_u64[0] == 0x8c87004000005f2d);
113   BIT_ASSERT(result_u64[1] == 0x1880000000718ad);
114   BIT_ASSERT(result_u64[2] == 0x8c87385c00004f25);
115   BIT_ASSERT(result_u64[3] == 0x39c2e000037718ad);
116   BIT_ASSERT(result_u64[4] == 0x80cb182c00008005);
117   BIT_ASSERT(result_u64[5] == 0xac01c62b6320b1b3);
118}
119
120static void
121bi_test_pack_format_5(struct util_dynarray *result)
122{
123   struct bi_packed_tuple tuples[] = {
124      { 0x9380688040000000, 0xf65 },
125      { 0xd4057300c000040, 0xf26 },
126      { 0x1f80cb1858000000, 0x19ab },
127      { 0x937401f85c000000, 0xf65 },
128      { 0xb380cb180c000080, 0x18a1 },
129   };
130
131   uint64_t EC0 = (0x183f800000) >> 4;
132
133   util_dynarray_clear(result);
134   bi_pack_format(result, 0, tuples, 5, 0x3100000000, EC0, 0, true);
135   bi_pack_format(result, 3, tuples, 5, 0x3100000000, EC0, 0, true);
136   bi_pack_format(result, 7, tuples, 5, 0x3100000000, EC0, 0, true);
137   bi_pack_format(result, 8, tuples, 5, 0x3100000000, EC0, 0, true);
138   uint64_t *result_u64 = (uint64_t *) result->data;
139
140   BIT_ASSERT(result->size == 64);
141   BIT_ASSERT(result_u64[0] == 0x8068804000000029);
142   BIT_ASSERT(result_u64[1] == 0x188000000076593);
143   BIT_ASSERT(result_u64[2] == 0x4057300c00004021);
144   BIT_ASSERT(result_u64[3] == 0x58c2c0000007260d);
145   BIT_ASSERT(result_u64[4] == 0x7401f85c0000008b);
146   BIT_ASSERT(result_u64[5] == 0x6ac7e0376593);
147   BIT_ASSERT(result_u64[6] == 0x80cb180c00008053);
148   BIT_ASSERT(result_u64[7] == 0x183f80a1b3);
149}
150
151static void
152bi_test_pack_format_6(struct util_dynarray *result)
153{
154   struct bi_packed_tuple tuples[] = {
155      { 0xad8c870068000048, 0x2f18 },
156      { 0xad8c87385c000050, 0x2f18 },
157      { 0xad8c87385c00006a, 0x2f18 },
158      { 0xad8c87385c000074, 0x2f18 },
159      { 0xad8c87385c000020, 0x2f18 },
160      { 0xad8c87385c000030, 0x2f18 },
161   };
162
163   uint64_t EC0 = (0x345678912345670) >> 4;
164
165   util_dynarray_clear(result);
166   bi_pack_format(result, 0, tuples, 6, 0x60000011800, EC0, 0, false);
167   bi_pack_format(result, 3, tuples, 6, 0x60000011800, EC0, 0, false);
168   bi_pack_format(result, 5, tuples, 6, 0x60000011800, EC0, 0, false);
169   bi_pack_format(result, 9, tuples, 6, 0x60000011800, EC0, 0, false);
170   bi_pack_format(result, 10, tuples, 6, 0x60000011800, EC0, 0, false);
171   uint64_t *result_u64 = (uint64_t *) result->data;
172
173   BIT_ASSERT(result->size == 80);
174   BIT_ASSERT(result_u64[0] == 0x8c8700680000482d);
175   BIT_ASSERT(result_u64[1] == 0x30000008c00718ad);
176   BIT_ASSERT(result_u64[2] == 0x8c87385c00005025);
177   BIT_ASSERT(result_u64[3] == 0x39c2e000035718ad);
178   BIT_ASSERT(result_u64[4] == 0x8c87385c00007401);
179   BIT_ASSERT(result_u64[5] == 0xb401c62b632718ad);
180   BIT_ASSERT(result_u64[6] == 0x8c87385c00002065);
181   BIT_ASSERT(result_u64[7] == 0x39c2e000018718ad);
182   BIT_ASSERT(result_u64[8] == 0x3456789123456706);
183   BIT_ASSERT(result_u64[9] == 0xa001c62b63200000);
184}
185
186static void
187bi_test_pack_format_7(struct util_dynarray *result)
188{
189   struct bi_packed_tuple tuples[] = {
190      { 0x9020074040000083, 0xf65 },
191      { 0x90000d4058100080, 0xf65 },
192      { 0x90000a3058700082, 0xf65 },
193      { 0x9020074008114581, 0xf65 },
194      { 0x90000d0058000080, 0xf65 },
195      { 0x9000083058700082, 0xf65 },
196      { 0x2380cb199ac38400, 0x327a },
197   };
198
199   util_dynarray_clear(result);
200   bi_pack_format(result, 0, tuples, 7, 0x3000100000, 0, 0, true);
201   bi_pack_format(result, 3, tuples, 7, 0x3000100000, 0, 0, true);
202   bi_pack_format(result, 5, tuples, 7, 0x3000100000, 0, 0, true);
203   bi_pack_format(result, 9, tuples, 7, 0x3000100000, 0, 0, true);
204   bi_pack_format(result, 11, tuples, 7, 0x3000100000, 0, 0, true);
205   uint64_t *result_u64 = (uint64_t *) result->data;
206
207   BIT_ASSERT(result->size == 80);
208   BIT_ASSERT(result_u64[0] == 0x2007404000008329);
209   BIT_ASSERT(result_u64[1] == 0x180008000076590);
210   BIT_ASSERT(result_u64[2] == 0xd405810008021);
211   BIT_ASSERT(result_u64[3] == 0x5182c38004176590);
212   BIT_ASSERT(result_u64[4] == 0x2007400811458101);
213   BIT_ASSERT(result_u64[5] == 0x2401d96400076590);
214   BIT_ASSERT(result_u64[6] == 0xd005800008061);
215   BIT_ASSERT(result_u64[7] == 0x4182c38004176590);
216   BIT_ASSERT(result_u64[8] == 0x80cb199ac3840047);
217   BIT_ASSERT(result_u64[9] == 0x3801d96400027a23);
218}
219
220static void
221bi_test_pack_format_8(struct util_dynarray *result)
222{
223   struct bi_packed_tuple tuples[] = {
224      { 0x442087037a2f8643, 0x3021 },
225      { 0x84008d0586100043, 0x200 },
226      { 0x7c008d0028014543, 0x0 },
227      { 0x1c00070058200081, 0x1980 },
228      { 0x1600dd878320400, 0x200 },
229      { 0x49709c1b08308900, 0x200 },
230      { 0x6c2007807881ca00, 0x40 },
231      { 0x8d70fc0d94900083, 0x800 },
232   };
233
234   uint64_t EC0 = (0x32e635d0) >> 4;
235
236   util_dynarray_clear(result);
237   bi_pack_format(result, 0, tuples, 8, 0x61001311800, EC0, 0, true);
238   bi_pack_format(result, 3, tuples, 8, 0x61001311800, EC0, 0, true);
239   bi_pack_format(result, 5, tuples, 8, 0x61001311800, EC0, 0, true);
240   bi_pack_format(result, 9, tuples, 8, 0x61001311800, EC0, 0, true);
241   bi_pack_format(result, 12, tuples, 8, 0x61001311800, EC0, 0, true);
242   bi_pack_format(result, 13, tuples, 8, 0x61001311800, EC0, 0, true);
243   uint64_t *result_u64 = (uint64_t *) result->data;
244
245   BIT_ASSERT(result->size == 96);
246   BIT_ASSERT(result_u64[0] == 0x2087037a2f86432e);
247   BIT_ASSERT(result_u64[1] == 0x30800988c0002144);
248   BIT_ASSERT(result_u64[2] == 0x8d058610004320);
249   BIT_ASSERT(result_u64[3] == 0x6801400a2a1a0084);
250   BIT_ASSERT(result_u64[4] == 0x7005820008101);
251   BIT_ASSERT(result_u64[5] == 0xc00001f0021801c);
252   BIT_ASSERT(result_u64[6] == 0x600dd87832040060);
253   BIT_ASSERT(result_u64[7] == 0xe0d8418448020001);
254   BIT_ASSERT(result_u64[8] == 0x2007807881ca00c0);
255   BIT_ASSERT(result_u64[9] == 0xc6ba80125c20406c);
256   BIT_ASSERT(result_u64[10] == 0x70fc0d9490008359);
257   BIT_ASSERT(result_u64[11] == 0x32e0008d);
258}
259
260int
261main(int argc, const char **argv)
262{
263   struct util_dynarray result;
264   util_dynarray_init(&result, NULL);
265
266   bi_test_pack_format_1(&result);
267   bi_test_pack_format_2(&result);
268   bi_test_pack_format_3(&result);
269   bi_test_pack_format_4(&result);
270   bi_test_pack_format_5(&result);
271   bi_test_pack_format_6(&result);
272   bi_test_pack_format_7(&result);
273   bi_test_pack_format_8(&result);
274
275   util_dynarray_fini(&result);
276   TEST_END(nr_pass, nr_fail);
277}
278