test_byte_reverse.cxx 2.14 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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/Macros.hxx"
22
#include "util/Compiler.h"
23

24
#include <gtest/gtest.h>
25

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

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

	reverse_bytes(dest, (const uint8_t *)src,
36
		      (const uint8_t *)(src + ARRAY_SIZE(src) - 1), 2);
37
	EXPECT_STREQ(result, (const char *)dest);
38 39
}

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

	reverse_bytes(dest, (const uint8_t *)src,
47
		      (const uint8_t *)(src + ARRAY_SIZE(src) - 1), 3);
48
	EXPECT_STREQ(result, (const char *)dest);
49 50
}

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

	reverse_bytes(dest, (const uint8_t *)src,
58
		      (const uint8_t *)(src + ARRAY_SIZE(src) - 1), 4);
59
	EXPECT_STREQ(result, (const char *)dest);
60 61
}

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

	reverse_bytes(dest, (const uint8_t *)src,
69
		      (const uint8_t *)(src + ARRAY_SIZE(src) - 1), 5);
70
	EXPECT_STREQ(result, (const char *)dest);
71
}