1 /* test_libFLAC - Unit tester for libFLAC
2 * Copyright (C) 2014 Xiph.Org Foundation
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "share/compat.h"
27 #include "FLAC/assert.h"
28 #include "share/endswap.h"
29 #include "private/md5.h"
33 FLAC__bool
test_endswap(void)
36 uint16_t u16
= 0xabcd;
37 int32_t i32
= 0x12345678;
38 uint32_t u32
= 0xabcdef01;
41 unsigned char bytes
[4];
46 printf("\n+++ libFLAC unit test: endswap (%s endian host)\n\n", CPU_IS_LITTLE_ENDIAN
? "little" : "big");
48 printf("testing ENDSWAP_16 on int16_t ... ");
49 if (((int16_t) ENDSWAP_16(i16
)) == i16
) {
50 printf("\nFAILED, ENDSWAP_16(0x%04x) -> 0x%04x == 0x%04x\n", i16
, ENDSWAP_16(i16
), i16
);
53 if (((int16_t) ENDSWAP_16(ENDSWAP_16(i16
))) != i16
) {
54 printf("\nFAILED, ENDSWAP_16(ENDSWAP_16(0x%04x)) -> 0x%04x != 0x%04x\n", i16
, ENDSWAP_16(ENDSWAP_16(i16
)), i16
);
59 printf("testing ENDSWAP_16 on uint16_t ... ");
60 if (((uint16_t) ENDSWAP_16(u16
)) == u16
) {
61 printf("\nFAILED, ENDSWAP_16(0x%04x) -> 0x%04x == 0x%04x\n", u16
, ENDSWAP_16(u16
), u16
);
64 if (((uint16_t) ENDSWAP_16(ENDSWAP_16(u16
))) != u16
) {
65 printf("\nFAILED, ENDSWAP_16(ENDSWAP_16(0x%04x)) -> 0x%04x != 0x%04x\n", u16
, ENDSWAP_16(ENDSWAP_16(u16
)), u16
);
70 printf("testing ENDSWAP_32 on int32_t ... ");
71 if (((int32_t) ENDSWAP_32 (i32
)) == i32
) {
72 printf("\nFAILED, ENDSWAP_32(0x%08x) -> 0x%08x == 0x%08x\n", i32
, (unsigned) ENDSWAP_32 (i32
), i32
);
75 if (((int32_t) ENDSWAP_32 (ENDSWAP_32 (i32
))) != i32
) {
76 printf("\nFAILED, ENDSWAP_32(ENDSWAP_32(0x%08x)) -> 0x%08x != 0x%08x\n", i32
, (unsigned) ENDSWAP_32(ENDSWAP_32 (i32
)), i32
);
81 printf("testing ENDSWAP_32 on uint32_t ... ");
82 if (((uint32_t) ENDSWAP_32(u32
)) == u32
) {
83 printf("\nFAILED, ENDSWAP_32(0x%08x) -> 0x%08x == 0x%08x\n", u32
, (unsigned) ENDSWAP_32(u32
), u32
);
86 if (((uint32_t) ENDSWAP_32 (ENDSWAP_32(u32
))) != u32
) {
87 printf("\nFAILED, ENDSWAP_32(ENDSWAP_32(0x%08x)) -> 0x%08x != 0%08x\n", u32
, (unsigned) ENDSWAP_32(ENDSWAP_32(u32
)), u32
);
92 printf("testing H2LE_16 on uint16_t ... ");
93 data
.u16
= H2LE_16(0x1234);
94 if (data
.bytes
[0] != 0x34 || data
.bytes
[1] != 0x12) {
95 printf("\nFAILED, H2LE_16(0x%04x) -> { 0x%02x, 0x%02x }\n", data
.u16
, data
.bytes
[0] & 0xff, data
.bytes
[1] & 0xff);
100 printf("testing H2LE_32 on uint32_t ... ");
101 data
.u32
= H2LE_32(0x12345678);
102 if (data
.bytes
[0] != 0x78 || data
.bytes
[1] != 0x56 || data
.bytes
[2] != 0x34 || data
.bytes
[3] != 0x12) {
103 printf("\nFAILED, H2LE_32(0x%08x) -> { 0x%02x, 0x%02x, 0x%02x, 0x%02x }\n",
104 data
.u32
, data
.bytes
[0] & 0xff, data
.bytes
[1] & 0xff, data
.bytes
[2] & 0xff, data
.bytes
[3] & 0xff);
109 printf("\nPASSED!\n");