c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek/*
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * Copyright (c) 2016-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek *
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * Permission is hereby granted, free of charge, to any person obtaining a copy
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * of this software and associated documentation files (the "Software"), to deal
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * in the Software without restriction, including without limitation the rights
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * copies of the Software, and to permit persons to whom the Software is
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * furnished to do so, subject to the following conditions:
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek *
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * The above copyright notice and this permission notice shall be included in
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * all copies or substantial portions of the Software.
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek *
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * SOFTWARE.
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek */
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek#include "test-lib.h"
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek#include "byteorder.h"
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstruct bswap_run {
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek uint64_t in;
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek uint8_t out8;
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek uint16_t out16;
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek uint32_t out32;
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek uint64_t out64;
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek};
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic const struct bswap_run runs[] = {
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek {
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .in = 0,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .out8 = 0,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .out16 = 0,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .out32 = 0,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .out64 = 0,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek },
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek {
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .in = 0xffffffffffffffff,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .out8 = 0xff,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .out16 = 0xffff,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .out32 = 0xffffffff,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .out64 = 0xffffffffffffffff,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek },
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek {
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .in = 0x123456789abcdef0,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .out8 = 0xf0,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .out16 = 0xf0de,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .out32 = 0xf0debc9a,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .out64 = 0xf0debc9a78563412,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek },
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek {
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .in = 0x8080808080808080,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .out8 = 0x80,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .out16 = 0x8080,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .out32 = 0x80808080,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .out64 = 0x8080808080808080,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek },
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek};
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek#define CHECK(iter, size, in, exp) \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek do { \
08259c1f206026ca9b9f4b4e97603943c6093defJosef 'Jeff' Sipek uint##size##_t got = i_bswap_##size(in); \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek test_begin(t_strdup_printf("byteorder - bswap " \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek "(size:%-2u iter:%u)", \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek size, iter)); \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek test_assert(got == exp); \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek test_end(); \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek } while (0)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic void __test(int iter, const struct bswap_run *run)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek{
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek CHECK(iter, 8, run->in & 0xff, run->out8);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek CHECK(iter, 16, run->in & 0xffff, run->out16);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek CHECK(iter, 32, run->in & 0xffffffff, run->out32);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek CHECK(iter, 64, run->in, run->out64);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek}
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic void test_bswap(void)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek{
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek unsigned int i;
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek for (i = 0; i < N_ELEMENTS(runs) ; i++)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek __test(i, &runs[i]);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek}
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstruct unaligned_run {
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek uint8_t in[8];
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek /* outputs */
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek uint8_t be8;
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek uint16_t be16;
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek uint32_t be32;
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek uint64_t be64;
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek uint8_t le8;
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek uint16_t le16;
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek uint32_t le32;
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek uint64_t le64;
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
debc93e622751c6c09e8105e504c5833f1ca0d6dMartti Rannanjärvi#ifdef WORDS_BIGENDIAN
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek#define cpu8 be8
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek#define cpu16 be16
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek#define cpu32 be32
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek#define cpu64 be64
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek#else
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek#define cpu8 le8
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek#define cpu16 le16
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek#define cpu32 le32
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek#define cpu64 le64
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek#endif
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek};
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic const struct unaligned_run uruns[] = {
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek {
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .in = {
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek 0x00, 0x00, 0x00, 0x00,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek 0x00, 0x00, 0x00, 0x00,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek },
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .be8 = 0,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .be16 = 0,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .be32 = 0,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .be64 = 0,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .le8 = 0,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .le16 = 0,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .le32 = 0,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .le64 = 0,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek },
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek {
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .in = {
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek 0xff, 0xff, 0xff, 0xff,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek 0xff, 0xff, 0xff, 0xff,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek },
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .be8 = 0xff,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .be16 = 0xffff,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .be32 = 0xffffffff,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .be64 = 0xffffffffffffffff,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .le8 = 0xff,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .le16 = 0xffff,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .le32 = 0xffffffff,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .le64 = 0xffffffffffffffff,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek },
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek {
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .in = {
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek 0x12, 0x34, 0x56, 0x78,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek 0x9a, 0xbc, 0xde, 0xf0,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek },
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .be8 = 0x12,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .be16 = 0x1234,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .be32 = 0x12345678,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .be64 = 0x123456789abcdef0,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .le8 = 0x12,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .le16 = 0x3412,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .le32 = 0x78563412,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .le64 = 0xf0debc9a78563412,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek },
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek {
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .in = {
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek 0x80, 0x80, 0x80, 0x80,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek 0x80, 0x80, 0x80, 0x80,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek },
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .be8 = 0x80,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .be16 = 0x8080,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .be32 = 0x80808080,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .be64 = 0x8080808080808080,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .le8 = 0x80,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .le16 = 0x8080,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .le32 = 0x80808080,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek .le64 = 0x8080808080808080,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek },
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek};
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek#define __CHECK_READ(iter, size, pfx, in, fxn, exp) \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek do { \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek uint##size##_t got = fxn(in); \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek test_begin(t_strdup_printf("byteorder - unaligned read "\
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek "(%-3s size:%-2u iter:%u)", \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek pfx, size, iter)); \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek test_assert(got == exp); \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek test_end(); \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek } while (0)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek#define CHECK_READ(iter, size, in, be_exp, le_exp, cpu_exp) \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek do { \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek __CHECK_READ(iter, size, "BE", in, \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek be##size##_to_cpu_unaligned, be_exp); \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek __CHECK_READ(iter, size, "LE", in, \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek le##size##_to_cpu_unaligned, le_exp); \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek __CHECK_READ(iter, size, "CPU", in, \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek cpu##size##_to_cpu_unaligned, cpu_exp); \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek } while (0)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic void __test_read(int iter, const struct unaligned_run *run)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek{
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek CHECK_READ(iter, 8, run->in, run->be8, run->le8, run->cpu8);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek CHECK_READ(iter, 16, run->in, run->be16, run->le16, run->cpu16);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek CHECK_READ(iter, 32, run->in, run->be32, run->le32, run->cpu32);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek CHECK_READ(iter, 64, run->in, run->be64, run->le64, run->cpu64);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek}
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek#define __CHECK_WRITE(iter, size, pfx, in, fxn, exp) \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek do { \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek uint8_t got[size / 8]; \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek fxn(in, got); \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek test_begin(t_strdup_printf("byteorder - unaligned write "\
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek "(%-3s size:%-2u iter:%u)", \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek pfx, size, iter)); \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek test_assert(memcmp(got, exp, sizeof(got)) == 0); \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek test_end(); \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek } while (0)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek#define CHECK_WRITE(iter, size, out, be_in, le_in) \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek do { \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek __CHECK_WRITE(iter, size, "BE", be_in, \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek cpu##size##_to_be_unaligned, out); \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek __CHECK_WRITE(iter, size, "LE", le_in, \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek cpu##size##_to_le_unaligned, out); \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek } while (0)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic void __test_write(int iter, const struct unaligned_run *run)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek{
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek CHECK_WRITE(iter, 8, run->in, run->be8, run->le8);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek CHECK_WRITE(iter, 16, run->in, run->be16, run->le16);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek CHECK_WRITE(iter, 32, run->in, run->be32, run->le32);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek CHECK_WRITE(iter, 64, run->in, run->be64, run->le64);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek}
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic void test_unaligned(void)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek{
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek unsigned int i;
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek for (i = 0; i < N_ELEMENTS(uruns) ; i++)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek __test_read(i, &uruns[i]);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek for (i = 0; i < N_ELEMENTS(uruns) ; i++)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek __test_write(i, &uruns[i]);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek}
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekvoid test_byteorder(void)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek{
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek test_bswap();
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek test_unaligned();
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek}