1 /* test_libFLAC - Unit tester for libFLAC
2  * Copyright (C) 2014-2016  Xiph.Org Foundation
3  *
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.
8  *
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.
13  *
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.
17  */
18 
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22 
23 #include <stdio.h>
24 #include <string.h>
25 
26 #include "share/compat.h"
27 #include "FLAC/assert.h"
28 #include "share/endswap.h"
29 #include "private/md5.h"
30 #include "endswap.h"
31 
32 
test_endswap(void)33 FLAC__bool test_endswap(void)
34 {
35 	int16_t i16 = 0x1234;
36 	uint16_t u16 = 0xabcd;
37 	int32_t i32 = 0x12345678;
38 	uint32_t u32 = 0xabcdef01;
39 
40 	union {
41 		uint8_t bytes[4];
42 		uint16_t u16;
43 		uint32_t u32;
44 	} data;
45 
46 	printf("\n+++ libFLAC unit test: endswap (%s endian host)\n\n", CPU_IS_LITTLE_ENDIAN ? "little" : "big");
47 
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;
52 	}
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;
56 	}
57 	puts("OK");
58 
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;
63 	}
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;
67 	}
68 	puts("OK");
69 
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, (uint32_t) ENDSWAP_32 (i32), i32);
73 		return false;
74 	}
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, (uint32_t) ENDSWAP_32(ENDSWAP_32 (i32)), i32);
77 		return false;
78 	}
79 	puts("OK");
80 
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, (uint32_t) ENDSWAP_32(u32), u32);
84 		return false;
85 	}
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, (uint32_t) ENDSWAP_32(ENDSWAP_32(u32)), u32);
88 		return false;
89 	}
90 	puts("OK");
91 
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;
97 	}
98 	puts("OK");
99 
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;
106 	}
107 	puts("OK");
108 
109 	printf("\nPASSED!\n");
110 	return true;
111 }
112