Set version to 1.3.1pre1.
[flac.git] / src / test_libFLAC / endswap.c
blob2fd6d5213011ae3b2f2714c5e6b434c4d54d3929
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.
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <stdio.h>
24 #include <string.h>
26 #include "share/compat.h"
27 #include "FLAC/assert.h"
28 #include "share/endswap.h"
29 #include "private/md5.h"
30 #include "endswap.h"
33 FLAC__bool test_endswap(void)
35 int16_t i16 = 0x1234;
36 uint16_t u16 = 0xabcd;
37 int32_t i32 = 0x12345678;
38 uint32_t u32 = 0xabcdef01;
40 union {
41 unsigned char bytes[4];
42 uint16_t u16;
43 uint32_t u32;
44 } data;
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);
51 return false;
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);
55 return false;
57 puts("OK");
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);
62 return false;
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);
66 return false;
68 puts("OK");
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);
73 return false;
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);
77 return false;
79 puts("OK");
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);
84 return false;
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);
88 return false;
90 puts("OK");
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);
96 return false;
98 puts("OK");
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);
105 return false;
107 puts("OK");
109 printf("\nPASSED!\n");
110 return true;