mpd/test/util/test_byte_reverse.cxx

54 lines
1.4 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project
2013-10-16 21:04:52 +02:00
#include "util/ByteReverse.hxx"
2018-10-16 19:01:13 +02:00
#include <gtest/gtest.h>
#include <string.h>
#include <stdlib.h>
2018-10-16 19:01:13 +02:00
TEST(ByteReverse, A)
{
alignas(uint16_t) static const char src[] = "123456";
static const char result[] = "214365";
alignas(uint16_t)static uint8_t dest[std::size(src)];
reverse_bytes(dest, (const uint8_t *)src,
(const uint8_t *)(src + std::size(src) - 1), 2);
2018-10-16 19:01:13 +02:00
EXPECT_STREQ(result, (const char *)dest);
}
2018-10-16 19:01:13 +02:00
TEST(ByteReverse, B)
{
static const char src[] = "123456";
static const char result[] = "321654";
static uint8_t dest[std::size(src)];
reverse_bytes(dest, (const uint8_t *)src,
(const uint8_t *)(src + std::size(src) - 1), 3);
2018-10-16 19:01:13 +02:00
EXPECT_STREQ(result, (const char *)dest);
}
2018-10-16 19:01:13 +02:00
TEST(ByteReverse, C)
{
alignas(uint32_t) static const char src[] = "12345678";
static const char result[] = "43218765";
alignas(uint32_t) static uint8_t dest[std::size(src)];
reverse_bytes(dest, (const uint8_t *)src,
(const uint8_t *)(src + std::size(src) - 1), 4);
2018-10-16 19:01:13 +02:00
EXPECT_STREQ(result, (const char *)dest);
}
2018-10-16 19:01:13 +02:00
TEST(ByteReverse, D)
{
static const char src[] = "1234567890";
static const char result[] = "5432109876";
static uint8_t dest[std::size(src)];
reverse_bytes(dest, (const uint8_t *)src,
(const uint8_t *)(src + std::size(src) - 1), 5);
2018-10-16 19:01:13 +02:00
EXPECT_STREQ(result, (const char *)dest);
}