tst_byteswap.c revision 9ec53cf4f32cc65e91a9c71651f05a6d872a7088
1/* 2 * This testing program makes sure the byteswap functions work 3 * 4 * Copyright (C) 2000 by Theodore Ts'o. 5 * 6 * %Begin-Header% 7 * This file may be redistributed under the terms of the GNU Public 8 * License. 9 * %End-Header% 10 */ 11 12#include <stdio.h> 13#include <string.h> 14#if HAVE_UNISTD_H 15#include <unistd.h> 16#endif 17#include <fcntl.h> 18#include <time.h> 19#include <sys/stat.h> 20#include <sys/types.h> 21#if HAVE_ERRNO_H 22#include <errno.h> 23#endif 24 25#if EXT2_FLAT_INCLUDES 26#include "ext2_fs.h" 27#else 28#include <linux/ext2_fs.h> 29#endif 30 31#include "ext2fs.h" 32 33__u16 test1[] = { 34 0x0001, 0x0100, 35 0x1234, 0x3412, 36 0xff00, 0x00ff, 37 0x4000, 0x0040, 38 0xfeff, 0xfffe, 39 0x0000, 0x0000 40 }; 41 42__u32 test2[] = { 43 0x00000001, 0x01000000, 44 0x80000000, 0x00000080, 45 0x12345678, 0x78563412, 46 0xffff0000, 0x0000ffff, 47 0x00ff0000, 0x0000ff00, 48 0xff000000, 0x000000ff, 49 0x00000000, 0x00000000 50 }; 51 52int main(int argc, char *argv) 53{ 54 int i; 55 int errors = 0; 56 57 printf("Testing ext2fs_swab16\n"); 58 i=0; 59 do { 60 printf("swab16(0x%04x) = 0x%04x\n", test1[i], 61 ext2fs_swab16(test1[i])); 62 if (ext2fs_swab16(test1[i]) != test1[i+1]) { 63 printf("Error!!! %04x != %04x\n", 64 ext2fs_swab16(test1[i]), test1[i+1]); 65 errors++; 66 } 67 if (ext2fs_swab16(test1[i+1]) != test1[i]) { 68 printf("Error!!! %04x != %04x\n", 69 ext2fs_swab16(test1[i+1]), test1[i]); 70 errors++; 71 } 72 i += 2; 73 } while (test1[i] != 0); 74 75 printf("Testing ext2fs_swab32\n"); 76 i = 0; 77 do { 78 printf("swab32(0x%08x) = 0x%08x\n", test2[i], 79 ext2fs_swab32(test2[i])); 80 if (ext2fs_swab32(test2[i]) != test2[i+1]) { 81 printf("Error!!! %04x != %04x\n", 82 ext2fs_swab32(test2[i]), test2[i+1]); 83 errors++; 84 } 85 if (ext2fs_swab32(test2[i+1]) != test2[i]) { 86 printf("Error!!! %04x != %04x\n", 87 ext2fs_swab32(test2[i+1]), test2[i]); 88 errors++; 89 } 90 i += 2; 91 } while (test2[i] != 0); 92 93 if (!errors) 94 printf("No errors found in the byteswap implementation!\n"); 95 96 return errors; 97} 98