17ec681f3Smrg/*
27ec681f3Smrg * Copyright (C) 2020 Collabora, Ltd.
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
207ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
217ec681f3Smrg * SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#include "compiler.h"
257ec681f3Smrg#include "bi_test.h"
267ec681f3Smrg
277ec681f3Smrgunsigned nr_pass = 0;
287ec681f3Smrgunsigned nr_fail = 0;
297ec681f3Smrg
307ec681f3Smrgstatic void
317ec681f3Smrgbi_test_pack_format_1(struct util_dynarray *result)
327ec681f3Smrg{
337ec681f3Smrg   /* Test case from the blob */
347ec681f3Smrg   struct bi_packed_tuple tuples[] = {
357ec681f3Smrg      { 0x2380cb1c02200000, 0x10e0 },
367ec681f3Smrg   };
377ec681f3Smrg
387ec681f3Smrg   uint64_t header = 0x021000011800;
397ec681f3Smrg
407ec681f3Smrg   util_dynarray_clear(result);
417ec681f3Smrg   bi_pack_format(result, 1, tuples, 1, header, 0, 0, true);
427ec681f3Smrg   uint64_t *result_u64 = (uint64_t *) result->data;
437ec681f3Smrg
447ec681f3Smrg   BIT_ASSERT(result->size == 16);
457ec681f3Smrg   BIT_ASSERT(result_u64[0] == 0x80cb1c022000004a);
467ec681f3Smrg   BIT_ASSERT(result_u64[1] == 0x10800008c000e023);
477ec681f3Smrg}
487ec681f3Smrg
497ec681f3Smrgstatic void
507ec681f3Smrgbi_test_pack_format_2(struct util_dynarray *result)
517ec681f3Smrg{
527ec681f3Smrg   struct bi_packed_tuple tuples[] = {
537ec681f3Smrg      { 0x9380cb6044000044, 0xf65 },
547ec681f3Smrg      { 0xaf8721a05c000081, 0x1831 },
557ec681f3Smrg   };
567ec681f3Smrg
577ec681f3Smrg   util_dynarray_clear(result);
587ec681f3Smrg   bi_pack_format(result, 0, tuples, 2, 0x52800011800, 0, 0, false);
597ec681f3Smrg   bi_pack_format(result, 2, tuples, 2, 0x52800011800, 0, 0, false);
607ec681f3Smrg   uint64_t *result_u64 = (uint64_t *) result->data;
617ec681f3Smrg
627ec681f3Smrg   BIT_ASSERT(result->size == 32);
637ec681f3Smrg   BIT_ASSERT(result_u64[0] == 0x80cb604400004429);
647ec681f3Smrg   BIT_ASSERT(result_u64[1] == 0x29400008c0076593);
657ec681f3Smrg   BIT_ASSERT(result_u64[2] == 0x8721a05c00008103);
667ec681f3Smrg   BIT_ASSERT(result_u64[3] == 0x60000000000031af);
677ec681f3Smrg}
687ec681f3Smrg
697ec681f3Smrgstatic void
707ec681f3Smrgbi_test_pack_format_3(struct util_dynarray *result)
717ec681f3Smrg{
727ec681f3Smrg   struct bi_packed_tuple tuples[] = {
737ec681f3Smrg      { 0x93805b8040000000, 0xf65 },
747ec681f3Smrg      { 0x93886db05c000000, 0xf65 },
757ec681f3Smrg      { 0xb380cb180c000080, 0x18b1 },
767ec681f3Smrg   };
777ec681f3Smrg
787ec681f3Smrg   util_dynarray_clear(result);
797ec681f3Smrg   bi_pack_format(result, 0, tuples, 3, 0x3100000000, 0, 0, true);
807ec681f3Smrg   bi_pack_format(result, 3, tuples, 3, 0x3100000000, 0, 0, true);
817ec681f3Smrg   bi_pack_format(result, 4, tuples, 3, 0x3100000000, 0, 0, true);
827ec681f3Smrg   uint64_t *result_u64 = (uint64_t *) result->data;
837ec681f3Smrg
847ec681f3Smrg   BIT_ASSERT(result->size == 48);
857ec681f3Smrg   BIT_ASSERT(result_u64[0] == 0x805b804000000029);
867ec681f3Smrg   BIT_ASSERT(result_u64[1] == 0x188000000076593);
877ec681f3Smrg   BIT_ASSERT(result_u64[2] == 0x886db05c00000021);
887ec681f3Smrg   BIT_ASSERT(result_u64[3] == 0x58c0600004076593);
897ec681f3Smrg   BIT_ASSERT(result_u64[4] == 0x44);
907ec681f3Smrg   BIT_ASSERT(result_u64[5] == 0x60002c6ce0300000);
917ec681f3Smrg}
927ec681f3Smrg
937ec681f3Smrgstatic void
947ec681f3Smrgbi_test_pack_format_4(struct util_dynarray *result)
957ec681f3Smrg{
967ec681f3Smrg   struct bi_packed_tuple tuples[] = {
977ec681f3Smrg      { 0xad8c87004000005f, 0x2f18 },
987ec681f3Smrg      { 0xad8c87385c00004f, 0x2f18 },
997ec681f3Smrg      { 0xad8c87385c00006e, 0x2f18 },
1007ec681f3Smrg      { 0xb380cb182c000080, 0x18b1 },
1017ec681f3Smrg   };
1027ec681f3Smrg
1037ec681f3Smrg   uint64_t EC0 = (0x10000001ff000000) >> 4;
1047ec681f3Smrg
1057ec681f3Smrg   util_dynarray_clear(result);
1067ec681f3Smrg   bi_pack_format(result, 0, tuples, 4, 0x3100000000, EC0, 0, false);
1077ec681f3Smrg   bi_pack_format(result, 3, tuples, 4, 0x3100000000, EC0, 0, false);
1087ec681f3Smrg   bi_pack_format(result, 6, tuples, 4, 0x3100000000, EC0, 0, false);
1097ec681f3Smrg   uint64_t *result_u64 = (uint64_t *) result->data;
1107ec681f3Smrg
1117ec681f3Smrg   BIT_ASSERT(result->size == 48);
1127ec681f3Smrg   BIT_ASSERT(result_u64[0] == 0x8c87004000005f2d);
1137ec681f3Smrg   BIT_ASSERT(result_u64[1] == 0x1880000000718ad);
1147ec681f3Smrg   BIT_ASSERT(result_u64[2] == 0x8c87385c00004f25);
1157ec681f3Smrg   BIT_ASSERT(result_u64[3] == 0x39c2e000037718ad);
1167ec681f3Smrg   BIT_ASSERT(result_u64[4] == 0x80cb182c00008005);
1177ec681f3Smrg   BIT_ASSERT(result_u64[5] == 0xac01c62b6320b1b3);
1187ec681f3Smrg}
1197ec681f3Smrg
1207ec681f3Smrgstatic void
1217ec681f3Smrgbi_test_pack_format_5(struct util_dynarray *result)
1227ec681f3Smrg{
1237ec681f3Smrg   struct bi_packed_tuple tuples[] = {
1247ec681f3Smrg      { 0x9380688040000000, 0xf65 },
1257ec681f3Smrg      { 0xd4057300c000040, 0xf26 },
1267ec681f3Smrg      { 0x1f80cb1858000000, 0x19ab },
1277ec681f3Smrg      { 0x937401f85c000000, 0xf65 },
1287ec681f3Smrg      { 0xb380cb180c000080, 0x18a1 },
1297ec681f3Smrg   };
1307ec681f3Smrg
1317ec681f3Smrg   uint64_t EC0 = (0x183f800000) >> 4;
1327ec681f3Smrg
1337ec681f3Smrg   util_dynarray_clear(result);
1347ec681f3Smrg   bi_pack_format(result, 0, tuples, 5, 0x3100000000, EC0, 0, true);
1357ec681f3Smrg   bi_pack_format(result, 3, tuples, 5, 0x3100000000, EC0, 0, true);
1367ec681f3Smrg   bi_pack_format(result, 7, tuples, 5, 0x3100000000, EC0, 0, true);
1377ec681f3Smrg   bi_pack_format(result, 8, tuples, 5, 0x3100000000, EC0, 0, true);
1387ec681f3Smrg   uint64_t *result_u64 = (uint64_t *) result->data;
1397ec681f3Smrg
1407ec681f3Smrg   BIT_ASSERT(result->size == 64);
1417ec681f3Smrg   BIT_ASSERT(result_u64[0] == 0x8068804000000029);
1427ec681f3Smrg   BIT_ASSERT(result_u64[1] == 0x188000000076593);
1437ec681f3Smrg   BIT_ASSERT(result_u64[2] == 0x4057300c00004021);
1447ec681f3Smrg   BIT_ASSERT(result_u64[3] == 0x58c2c0000007260d);
1457ec681f3Smrg   BIT_ASSERT(result_u64[4] == 0x7401f85c0000008b);
1467ec681f3Smrg   BIT_ASSERT(result_u64[5] == 0x6ac7e0376593);
1477ec681f3Smrg   BIT_ASSERT(result_u64[6] == 0x80cb180c00008053);
1487ec681f3Smrg   BIT_ASSERT(result_u64[7] == 0x183f80a1b3);
1497ec681f3Smrg}
1507ec681f3Smrg
1517ec681f3Smrgstatic void
1527ec681f3Smrgbi_test_pack_format_6(struct util_dynarray *result)
1537ec681f3Smrg{
1547ec681f3Smrg   struct bi_packed_tuple tuples[] = {
1557ec681f3Smrg      { 0xad8c870068000048, 0x2f18 },
1567ec681f3Smrg      { 0xad8c87385c000050, 0x2f18 },
1577ec681f3Smrg      { 0xad8c87385c00006a, 0x2f18 },
1587ec681f3Smrg      { 0xad8c87385c000074, 0x2f18 },
1597ec681f3Smrg      { 0xad8c87385c000020, 0x2f18 },
1607ec681f3Smrg      { 0xad8c87385c000030, 0x2f18 },
1617ec681f3Smrg   };
1627ec681f3Smrg
1637ec681f3Smrg   uint64_t EC0 = (0x345678912345670) >> 4;
1647ec681f3Smrg
1657ec681f3Smrg   util_dynarray_clear(result);
1667ec681f3Smrg   bi_pack_format(result, 0, tuples, 6, 0x60000011800, EC0, 0, false);
1677ec681f3Smrg   bi_pack_format(result, 3, tuples, 6, 0x60000011800, EC0, 0, false);
1687ec681f3Smrg   bi_pack_format(result, 5, tuples, 6, 0x60000011800, EC0, 0, false);
1697ec681f3Smrg   bi_pack_format(result, 9, tuples, 6, 0x60000011800, EC0, 0, false);
1707ec681f3Smrg   bi_pack_format(result, 10, tuples, 6, 0x60000011800, EC0, 0, false);
1717ec681f3Smrg   uint64_t *result_u64 = (uint64_t *) result->data;
1727ec681f3Smrg
1737ec681f3Smrg   BIT_ASSERT(result->size == 80);
1747ec681f3Smrg   BIT_ASSERT(result_u64[0] == 0x8c8700680000482d);
1757ec681f3Smrg   BIT_ASSERT(result_u64[1] == 0x30000008c00718ad);
1767ec681f3Smrg   BIT_ASSERT(result_u64[2] == 0x8c87385c00005025);
1777ec681f3Smrg   BIT_ASSERT(result_u64[3] == 0x39c2e000035718ad);
1787ec681f3Smrg   BIT_ASSERT(result_u64[4] == 0x8c87385c00007401);
1797ec681f3Smrg   BIT_ASSERT(result_u64[5] == 0xb401c62b632718ad);
1807ec681f3Smrg   BIT_ASSERT(result_u64[6] == 0x8c87385c00002065);
1817ec681f3Smrg   BIT_ASSERT(result_u64[7] == 0x39c2e000018718ad);
1827ec681f3Smrg   BIT_ASSERT(result_u64[8] == 0x3456789123456706);
1837ec681f3Smrg   BIT_ASSERT(result_u64[9] == 0xa001c62b63200000);
1847ec681f3Smrg}
1857ec681f3Smrg
1867ec681f3Smrgstatic void
1877ec681f3Smrgbi_test_pack_format_7(struct util_dynarray *result)
1887ec681f3Smrg{
1897ec681f3Smrg   struct bi_packed_tuple tuples[] = {
1907ec681f3Smrg      { 0x9020074040000083, 0xf65 },
1917ec681f3Smrg      { 0x90000d4058100080, 0xf65 },
1927ec681f3Smrg      { 0x90000a3058700082, 0xf65 },
1937ec681f3Smrg      { 0x9020074008114581, 0xf65 },
1947ec681f3Smrg      { 0x90000d0058000080, 0xf65 },
1957ec681f3Smrg      { 0x9000083058700082, 0xf65 },
1967ec681f3Smrg      { 0x2380cb199ac38400, 0x327a },
1977ec681f3Smrg   };
1987ec681f3Smrg
1997ec681f3Smrg   util_dynarray_clear(result);
2007ec681f3Smrg   bi_pack_format(result, 0, tuples, 7, 0x3000100000, 0, 0, true);
2017ec681f3Smrg   bi_pack_format(result, 3, tuples, 7, 0x3000100000, 0, 0, true);
2027ec681f3Smrg   bi_pack_format(result, 5, tuples, 7, 0x3000100000, 0, 0, true);
2037ec681f3Smrg   bi_pack_format(result, 9, tuples, 7, 0x3000100000, 0, 0, true);
2047ec681f3Smrg   bi_pack_format(result, 11, tuples, 7, 0x3000100000, 0, 0, true);
2057ec681f3Smrg   uint64_t *result_u64 = (uint64_t *) result->data;
2067ec681f3Smrg
2077ec681f3Smrg   BIT_ASSERT(result->size == 80);
2087ec681f3Smrg   BIT_ASSERT(result_u64[0] == 0x2007404000008329);
2097ec681f3Smrg   BIT_ASSERT(result_u64[1] == 0x180008000076590);
2107ec681f3Smrg   BIT_ASSERT(result_u64[2] == 0xd405810008021);
2117ec681f3Smrg   BIT_ASSERT(result_u64[3] == 0x5182c38004176590);
2127ec681f3Smrg   BIT_ASSERT(result_u64[4] == 0x2007400811458101);
2137ec681f3Smrg   BIT_ASSERT(result_u64[5] == 0x2401d96400076590);
2147ec681f3Smrg   BIT_ASSERT(result_u64[6] == 0xd005800008061);
2157ec681f3Smrg   BIT_ASSERT(result_u64[7] == 0x4182c38004176590);
2167ec681f3Smrg   BIT_ASSERT(result_u64[8] == 0x80cb199ac3840047);
2177ec681f3Smrg   BIT_ASSERT(result_u64[9] == 0x3801d96400027a23);
2187ec681f3Smrg}
2197ec681f3Smrg
2207ec681f3Smrgstatic void
2217ec681f3Smrgbi_test_pack_format_8(struct util_dynarray *result)
2227ec681f3Smrg{
2237ec681f3Smrg   struct bi_packed_tuple tuples[] = {
2247ec681f3Smrg      { 0x442087037a2f8643, 0x3021 },
2257ec681f3Smrg      { 0x84008d0586100043, 0x200 },
2267ec681f3Smrg      { 0x7c008d0028014543, 0x0 },
2277ec681f3Smrg      { 0x1c00070058200081, 0x1980 },
2287ec681f3Smrg      { 0x1600dd878320400, 0x200 },
2297ec681f3Smrg      { 0x49709c1b08308900, 0x200 },
2307ec681f3Smrg      { 0x6c2007807881ca00, 0x40 },
2317ec681f3Smrg      { 0x8d70fc0d94900083, 0x800 },
2327ec681f3Smrg   };
2337ec681f3Smrg
2347ec681f3Smrg   uint64_t EC0 = (0x32e635d0) >> 4;
2357ec681f3Smrg
2367ec681f3Smrg   util_dynarray_clear(result);
2377ec681f3Smrg   bi_pack_format(result, 0, tuples, 8, 0x61001311800, EC0, 0, true);
2387ec681f3Smrg   bi_pack_format(result, 3, tuples, 8, 0x61001311800, EC0, 0, true);
2397ec681f3Smrg   bi_pack_format(result, 5, tuples, 8, 0x61001311800, EC0, 0, true);
2407ec681f3Smrg   bi_pack_format(result, 9, tuples, 8, 0x61001311800, EC0, 0, true);
2417ec681f3Smrg   bi_pack_format(result, 12, tuples, 8, 0x61001311800, EC0, 0, true);
2427ec681f3Smrg   bi_pack_format(result, 13, tuples, 8, 0x61001311800, EC0, 0, true);
2437ec681f3Smrg   uint64_t *result_u64 = (uint64_t *) result->data;
2447ec681f3Smrg
2457ec681f3Smrg   BIT_ASSERT(result->size == 96);
2467ec681f3Smrg   BIT_ASSERT(result_u64[0] == 0x2087037a2f86432e);
2477ec681f3Smrg   BIT_ASSERT(result_u64[1] == 0x30800988c0002144);
2487ec681f3Smrg   BIT_ASSERT(result_u64[2] == 0x8d058610004320);
2497ec681f3Smrg   BIT_ASSERT(result_u64[3] == 0x6801400a2a1a0084);
2507ec681f3Smrg   BIT_ASSERT(result_u64[4] == 0x7005820008101);
2517ec681f3Smrg   BIT_ASSERT(result_u64[5] == 0xc00001f0021801c);
2527ec681f3Smrg   BIT_ASSERT(result_u64[6] == 0x600dd87832040060);
2537ec681f3Smrg   BIT_ASSERT(result_u64[7] == 0xe0d8418448020001);
2547ec681f3Smrg   BIT_ASSERT(result_u64[8] == 0x2007807881ca00c0);
2557ec681f3Smrg   BIT_ASSERT(result_u64[9] == 0xc6ba80125c20406c);
2567ec681f3Smrg   BIT_ASSERT(result_u64[10] == 0x70fc0d9490008359);
2577ec681f3Smrg   BIT_ASSERT(result_u64[11] == 0x32e0008d);
2587ec681f3Smrg}
2597ec681f3Smrg
2607ec681f3Smrgint
2617ec681f3Smrgmain(int argc, const char **argv)
2627ec681f3Smrg{
2637ec681f3Smrg   struct util_dynarray result;
2647ec681f3Smrg   util_dynarray_init(&result, NULL);
2657ec681f3Smrg
2667ec681f3Smrg   bi_test_pack_format_1(&result);
2677ec681f3Smrg   bi_test_pack_format_2(&result);
2687ec681f3Smrg   bi_test_pack_format_3(&result);
2697ec681f3Smrg   bi_test_pack_format_4(&result);
2707ec681f3Smrg   bi_test_pack_format_5(&result);
2717ec681f3Smrg   bi_test_pack_format_6(&result);
2727ec681f3Smrg   bi_test_pack_format_7(&result);
2737ec681f3Smrg   bi_test_pack_format_8(&result);
2747ec681f3Smrg
2757ec681f3Smrg   util_dynarray_fini(&result);
2767ec681f3Smrg   TEST_END(nr_pass, nr_fail);
2777ec681f3Smrg}
278