(root)/
util-linux-2.39/
tests/
helpers/
test_byteswap.c
       1  /*
       2   * SPDX-License-Identifier: GPL-2.0-or-later
       3   *
       4   * This testing program makes sure the byteswap functions work
       5   *
       6   * Copyright (C) 2000 by Theodore Ts'o.
       7   * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
       8   */
       9  #include <stdio.h>
      10  #include <string.h>
      11  #include <unistd.h>
      12  #include <fcntl.h>
      13  #include <time.h>
      14  #include <sys/stat.h>
      15  #include <sys/types.h>
      16  #include <errno.h>
      17  #include <inttypes.h>
      18  
      19  #include "bitops.h"
      20  
      21  static uint16_t ary16[] = {
      22  	0x0001, 0x0100,
      23  	0x1234, 0x3412,
      24  	0xff00, 0x00ff,
      25  	0x4000, 0x0040,
      26  	0xfeff, 0xfffe,
      27  	0x0000, 0x0000
      28  	};
      29  
      30  static uint32_t ary32[] = {
      31  	0x00000001, 0x01000000,
      32  	0x80000000, 0x00000080,
      33  	0x12345678, 0x78563412,
      34  	0xffff0000, 0x0000ffff,
      35  	0x00ff0000, 0x0000ff00,
      36  	0xff000000, 0x000000ff,
      37  	0x00000000, 0x00000000
      38  	};
      39  
      40  static uint64_t ary64[] = {
      41  	0x0000000000000001, 0x0100000000000000,
      42  	0x8000000000000000, 0x0000000000000080,
      43  	0x1234567812345678, 0x7856341278563412,
      44  	0xffffffff00000000, 0x00000000ffffffff,
      45  	0x00ff000000000000, 0x000000000000ff00,
      46  	0xff00000000000000, 0x00000000000000ff,
      47  	0x0000000000000000, 0x0000000000000000
      48  	};
      49  
      50  int main(void)
      51  {
      52  	int	i;
      53  	int	errors = 0;
      54  
      55  	printf("Testing swab16\n");
      56  	i=0;
      57  	do {
      58  		printf("swab16(0x%04"PRIx16") = 0x%04"PRIx16"\n",
      59  				ary16[i], swab16(ary16[i]));
      60  		if (swab16(ary16[i]) != ary16[i+1]) {
      61  			printf("Error!!!   %04"PRIx16" != %04"PRIx16"\n",
      62  			       swab16(ary16[i]), ary16[i+1]);
      63  			errors++;
      64  		}
      65  		if (swab16(ary16[i+1]) != ary16[i]) {
      66  			printf("Error!!!   %04"PRIx16" != %04"PRIx16"\n",
      67  			       swab16(ary16[i+1]), ary16[i]);
      68  			errors++;
      69  		}
      70  		i += 2;
      71  	} while (ary16[i] != 0);
      72  
      73  	printf("Testing swab32\n");
      74  	i = 0;
      75  	do {
      76  		printf("swab32(0x%08"PRIx32") = 0x%08"PRIx32"\n",
      77  				ary32[i], swab32(ary32[i]));
      78  		if (swab32(ary32[i]) != ary32[i+1]) {
      79  			printf("Error!!!   %04"PRIx32" != %04"PRIx32"\n",
      80  				swab32(ary32[i]), ary32[i+1]);
      81  			errors++;
      82  		}
      83  		if (swab32(ary32[i+1]) != ary32[i]) {
      84  			printf("Error!!!   %04"PRIx32" != %04"PRIx32"\n",
      85  			       swab32(ary32[i+1]), ary32[i]);
      86  			errors++;
      87  		}
      88  		i += 2;
      89  	} while (ary32[i] != 0);
      90  
      91  	printf("Testing swab64\n");
      92  	i = 0;
      93  	do {
      94  		printf("swab64(0x%016"PRIx64") = 0x%016"PRIx64"\n",
      95  				ary64[i], swab64(ary64[i]));
      96  		if (swab64(ary64[i]) != ary64[i+1]) {
      97  			printf("Error!!!   %016"PRIx64" != %016"PRIx64"\n",
      98  				swab64(ary64[i]), ary64[i+1]);
      99  			errors++;
     100  		}
     101  		if (swab64(ary64[i+1]) != ary64[i]) {
     102  			printf("Error!!!   %016"PRIx64" != %016"PRIx64"\n",
     103  			       swab64(ary64[i+1]), ary64[i]);
     104  			errors++;
     105  		}
     106  		i += 2;
     107  	} while (ary64[i] != 0);
     108  
     109  	if (!errors)
     110  		printf("No errors found in the byteswap implementation\n");
     111  
     112  	return errors;
     113  }