summaryrefslogtreecommitdiffstats
path: root/src/test_libFLAC/endswap.c
blob: 808f81f3a6d3bf9f323cbfc477090255b92c0f92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* test_libFLAC - Unit tester for libFLAC
 * Copyright (C) 2014-2023  Xiph.Org Foundation
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdio.h>
#include <string.h>

#include "share/compat.h"
#include "FLAC/assert.h"
#include "share/endswap.h"
#include "private/md5.h"
#include "endswap.h"


FLAC__bool test_endswap(void)
{
	int16_t i16 = 0x1234;
	uint16_t u16 = 0xabcd;
	int32_t i32 = 0x12345678;
	uint32_t u32 = 0xabcdef01;

	union {
		uint8_t bytes[4];
		uint16_t u16;
		uint32_t u32;
	} data;

	printf("\n+++ libFLAC unit test: endswap (%s endian host)\n\n", CPU_IS_BIG_ENDIAN ? "big" : "little");

	printf("testing ENDSWAP_16 on int16_t ... ");
	if (((int16_t) ENDSWAP_16(i16)) == i16) {
		printf("\nFAILED, ENDSWAP_16(0x%04x) -> 0x%04x == 0x%04x\n", i16, ENDSWAP_16(i16), i16);
		return false;
	}
	if (((int16_t) ENDSWAP_16(ENDSWAP_16(i16))) != i16) {
		printf("\nFAILED, ENDSWAP_16(ENDSWAP_16(0x%04x)) -> 0x%04x != 0x%04x\n", i16, ENDSWAP_16(ENDSWAP_16(i16)), i16);
		return false;
	}
	puts("OK");

	printf("testing ENDSWAP_16 on uint16_t ... ");
	if (((uint16_t) ENDSWAP_16(u16)) == u16) {
		printf("\nFAILED, ENDSWAP_16(0x%04x) -> 0x%04x == 0x%04x\n", u16, ENDSWAP_16(u16), u16);
		return false;
	}
	if (((uint16_t) ENDSWAP_16(ENDSWAP_16(u16))) != u16) {
		printf("\nFAILED, ENDSWAP_16(ENDSWAP_16(0x%04x)) -> 0x%04x != 0x%04x\n", u16, ENDSWAP_16(ENDSWAP_16(u16)), u16);
		return false;
	}
	puts("OK");

	printf("testing ENDSWAP_32 on int32_t ... ");
	if (((int32_t) ENDSWAP_32 (i32)) == i32) {
		printf("\nFAILED, ENDSWAP_32(0x%08x) -> 0x%08x == 0x%08x\n", i32, (uint32_t) ENDSWAP_32 (i32), i32);
		return false;
	}
	if (((int32_t) ENDSWAP_32 (ENDSWAP_32 (i32))) != i32) {
		printf("\nFAILED, ENDSWAP_32(ENDSWAP_32(0x%08x)) -> 0x%08x != 0x%08x\n", i32, (uint32_t) ENDSWAP_32(ENDSWAP_32 (i32)), i32);
		return false;
	}
	puts("OK");

	printf("testing ENDSWAP_32 on uint32_t ... ");
	if (((uint32_t) ENDSWAP_32(u32)) == u32) {
		printf("\nFAILED, ENDSWAP_32(0x%08x) -> 0x%08x == 0x%08x\n", u32, (uint32_t) ENDSWAP_32(u32), u32);
		return false;
	}
	if (((uint32_t) ENDSWAP_32 (ENDSWAP_32(u32))) != u32) {
		printf("\nFAILED, ENDSWAP_32(ENDSWAP_32(0x%08x)) -> 0x%08x != 0%08x\n", u32, (uint32_t) ENDSWAP_32(ENDSWAP_32(u32)), u32);
		return false;
	}
	puts("OK");

	printf("testing H2LE_16 on uint16_t ... ");
	data.u16 = H2LE_16(0x1234);
	if (data.bytes [0] != 0x34 || data.bytes [1] != 0x12) {
		printf("\nFAILED, H2LE_16(0x%04x) -> { 0x%02x, 0x%02x }\n", data.u16, data.bytes [0] & 0xff, data.bytes [1] & 0xff);
		return false;
	}
	puts("OK");

	printf("testing H2LE_32 on uint32_t ... ");
	data.u32 = H2LE_32(0x12345678);
	if (data.bytes [0] != 0x78 || data.bytes [1] != 0x56 || data.bytes [2] != 0x34 || data.bytes [3] != 0x12) {
		printf("\nFAILED,  H2LE_32(0x%08x) -> { 0x%02x, 0x%02x, 0x%02x, 0x%02x }\n",
			data.u32, data.bytes [0] & 0xff, data.bytes [1] & 0xff, data.bytes [2] & 0xff, data.bytes [3] & 0xff);
		return false;
	}
	puts("OK");

	printf("\nPASSED!\n");
	return true;
}