1 /* vim: set ts=8 sts=4 sw=4 tw=80 noet: */ 2 /*====================================================================== 3 Copyright (C) 2004,2005,2009,2011 Walter Doekes <walter+tthsum@wjd.nu> 4 This file is part of tthsum. 5 6 tthsum is free software: you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 tthsum is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with tthsum. If not, see <http://www.gnu.org/licenses/>. 18 ======================================================================*/ 19 #include "endian.h" 20 21 #include "test.h" 22 #include "types.h" 23 24 test_defines()25static int test_defines() { 26 #ifndef BYTE_ORDER 27 FAIL("BYTE_ORDER is not defined"); 28 #endif 29 #ifndef BIG_ENDIAN 30 FAIL("BIG_ENDIAN is not defined"); 31 #endif 32 #ifndef LITTLE_ENDIAN 33 FAIL("LITTLE_ENDIAN is not defined"); 34 #endif 35 #if BIG_ENDIAN == LITTLE_ENDIAN 36 FAIL("BIG_ENDIAN == LITTLE_ENDIAN"); 37 #endif 38 #if BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN 39 FAIL("BYTE_ORDER not in (BIG_ENDIAN, LITTLE_ENDIAN)"); 40 #endif 41 return 0; 42 } 43 test_endian8()44static int test_endian8() { 45 uint8_t val8 = 0x1f; 46 char val = 0x7f; 47 if ((uint8_t)(val8 << 4) != 0xf0) 48 FAIL("The unsigned 8bit type has too many bits"); 49 if ((char)(val >> 4) != 0x07) 50 FAIL("The char type is broken"); 51 val = (char)0xf0; 52 if (((signed char)val >> 2) != -4) 53 FAIL("The char type is broken"); 54 return 0; 55 } 56 test_endian16()57static int test_endian16() { 58 uint16_t val16 = 0x4321; 59 uint8_t *p = (uint8_t*)&val16; 60 short val = 0x4321; 61 char *q = (char*)&val; 62 #if BYTE_ORDER == BIG_ENDIAN 63 if (p[0] != 0x43 || p[1] != 0x21) 64 FAIL("Big endian check fails on 16 bit numbers"); 65 if (q[0] != 0x43 || q[1] != 0x21) 66 FAIL("Big endian check fails on shorts"); 67 #else /* BYTE_ORDER != BIG_ENDIAN */ 68 if (p[0] != 0x21 || p[1] != 0x43) 69 FAIL("Little endian check fails on 16 bit numbers"); 70 if (q[0] != 0x21 || q[1] != 0x43) 71 FAIL("Little endian check fails on shorts"); 72 #endif /* BYTE_ORDER != BIG_ENDIAN */ 73 return 0; 74 } 75 test_endian32()76static int test_endian32() { 77 uint32_t val32 = 0x87654321; 78 uint8_t *p = (uint8_t*)&val32; 79 #if BYTE_ORDER == BIG_ENDIAN 80 if (p[0] != 0x87 || p[1] != 0x65 || p[2] != 0x43 || p[3] != 0x21) 81 FAIL("Big endian check fails on 32 bit numbers"); 82 #else /* BYTE_ORDER != BIG_ENDIAN */ 83 if (p[0] != 0x21 || p[1] != 0x43 || p[2] != 0x65 || p[3] != 0x87) 84 FAIL("Little endian check fails on 32 bit numbers"); 85 #endif /* BYTE_ORDER != BIG_ENDIAN */ 86 return 0; 87 } 88 test_endian64()89static int test_endian64() { 90 uint64_t val64 = _ULL(0x0fedcba987654321); 91 uint8_t *p = (uint8_t*)&val64; 92 #if BYTE_ORDER == BIG_ENDIAN 93 if (p[0] != 0x0f || p[1] != 0xed || p[2] != 0xcb || p[3] != 0xa9 94 || p[4] != 0x87 || p[5] != 0x65 || p[6] != 0x43 || p[7] != 0x21) 95 FAIL("Big endian check fails on 64 bit numbers"); 96 #else /* BYTE_ORDER != BIG_ENDIAN */ 97 if (p[0] != 0x21 || p[1] != 0x43 || p[2] != 0x65 || p[3] != 0x87 98 || p[4] != 0xa9 || p[5] != 0xcb || p[6] != 0xed || p[7] != 0x0f) 99 FAIL("Little endian check fails on 64 bit numbers"); 100 #endif /* BYTE_ORDER != BIG_ENDIAN */ 101 return 0; 102 } 103 104 105 TESTS(endian_test) 106 TEST(test_defines); 107 TEST(test_endian8); 108 TEST(test_endian16); 109 TEST(test_endian32); 110 TEST(test_endian64); 111 ENDTESTS 112