test_byte_reverse.cxx 2.11 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "util/ByteReverse.hxx"
21
#include "util/Compiler.h"
22

23
#include <gtest/gtest.h>
24

25
#include <string.h>
26
#include <stdlib.h>
27

28
TEST(ByteReverse, A)
29
{
30
	alignas(uint16_t) static const char src[] = "123456";
31
	static const char result[] = "214365";
32
	alignas(uint16_t)static uint8_t dest[std::size(src)];
33 34

	reverse_bytes(dest, (const uint8_t *)src,
35
		      (const uint8_t *)(src + std::size(src) - 1), 2);
36
	EXPECT_STREQ(result, (const char *)dest);
37 38
}

39
TEST(ByteReverse, B)
40 41 42
{
	static const char src[] = "123456";
	static const char result[] = "321654";
43
	static uint8_t dest[std::size(src)];
44 45

	reverse_bytes(dest, (const uint8_t *)src,
46
		      (const uint8_t *)(src + std::size(src) - 1), 3);
47
	EXPECT_STREQ(result, (const char *)dest);
48 49
}

50
TEST(ByteReverse, C)
51
{
52
	alignas(uint32_t) static const char src[] = "12345678";
53
	static const char result[] = "43218765";
54
	alignas(uint32_t) static uint8_t dest[std::size(src)];
55 56

	reverse_bytes(dest, (const uint8_t *)src,
57
		      (const uint8_t *)(src + std::size(src) - 1), 4);
58
	EXPECT_STREQ(result, (const char *)dest);
59 60
}

61
TEST(ByteReverse, D)
62 63 64
{
	static const char src[] = "1234567890";
	static const char result[] = "5432109876";
65
	static uint8_t dest[std::size(src)];
66 67

	reverse_bytes(dest, (const uint8_t *)src,
68
		      (const uint8_t *)(src + std::size(src) - 1), 5);
69
	EXPECT_STREQ(result, (const char *)dest);
70
}