You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

byteswap_test.c 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  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 as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. /** @file
  20. *
  21. * Byte-order swapping test functions
  22. *
  23. */
  24. /* Forcibly enable assertions */
  25. #undef NDEBUG
  26. #include <stdint.h>
  27. #include <assert.h>
  28. #include <byteswap.h>
  29. #include <ipxe/test.h>
  30. /* Provide global functions to allow inspection of generated assembly code */
  31. uint16_t test_bswap16 ( uint16_t x ) {
  32. return __bswap_16 ( x );
  33. }
  34. uint32_t test_bswap32 ( uint32_t x ) {
  35. return __bswap_32 ( x );
  36. }
  37. uint64_t test_bswap64 ( uint64_t x ) {
  38. return __bswap_64 ( x );
  39. }
  40. void test_bswap16s ( uint16_t *x ) {
  41. __bswap_16s ( x );
  42. }
  43. void test_bswap32s ( uint32_t *x ) {
  44. __bswap_32s ( x );
  45. }
  46. void test_bswap64s ( uint64_t *x ) {
  47. __bswap_64s ( x );
  48. }
  49. /**
  50. * Perform byte-order swapping
  51. *
  52. */
  53. static void byteswap_test_exec ( void ) {
  54. uint16_t test16;
  55. uint32_t test32;
  56. uint64_t test64;
  57. ok ( test_bswap16 ( 0x1234 ) == 0x3412 );
  58. ok ( test_bswap32 ( 0x12345678UL ) == 0x78563412UL );
  59. ok ( test_bswap64 ( 0x123456789abcdef0ULL ) == 0xf0debc9a78563412ULL );
  60. test16 = 0xabcd;
  61. test_bswap16s ( &test16 );
  62. ok ( test16 == 0xcdab );
  63. test32 = 0xabcdef01UL;
  64. test_bswap32s ( &test32 );
  65. ok ( test32 == 0x01efcdabUL );
  66. test64 = 0xabcdef0123456789ULL;
  67. test_bswap64s ( &test64 );
  68. ok ( test64 == 0x8967452301efcdabULL );
  69. }
  70. /** Byte-order swapping self-test */
  71. struct self_test byteswap_test __self_test = {
  72. .name = "byteswap",
  73. .exec = byteswap_test_exec,
  74. };