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.

bitops_test.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2016 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 (at your option) 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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * Bit operations self-tests
  27. *
  28. */
  29. /* Forcibly enable assertions */
  30. #undef NDEBUG
  31. #include <stdint.h>
  32. #include <string.h>
  33. #include <assert.h>
  34. #include <ipxe/bitops.h>
  35. #include <ipxe/test.h>
  36. /**
  37. * Perform bit operations self-tests
  38. *
  39. */
  40. static void bitops_test_exec ( void ) {
  41. uint8_t bits[32];
  42. /* Initialise bits */
  43. memset ( bits, 0, sizeof ( bits ) );
  44. /* Test set_bit() */
  45. set_bit ( 0, bits );
  46. ok ( bits[0] == 0x01 );
  47. set_bit ( 17, bits );
  48. ok ( bits[2] == 0x02 );
  49. set_bit ( 22, bits );
  50. ok ( bits[2] == 0x42 );
  51. set_bit ( 22, bits );
  52. ok ( bits[2] == 0x42 );
  53. /* Test clear_bit() */
  54. clear_bit ( 0, bits );
  55. ok ( bits[0] == 0x00 );
  56. bits[5] = 0xff;
  57. clear_bit ( 42, bits );
  58. ok ( bits[5] == 0xfb );
  59. clear_bit ( 42, bits );
  60. ok ( bits[5] == 0xfb );
  61. clear_bit ( 44, bits );
  62. ok ( bits[5] == 0xeb );
  63. /* Test test_and_set_bit() */
  64. ok ( test_and_set_bit ( 0, bits ) == 0 );
  65. ok ( bits[0] == 0x01 );
  66. ok ( test_and_set_bit ( 0, bits ) != 0 );
  67. ok ( bits[0] == 0x01 );
  68. ok ( test_and_set_bit ( 69, bits ) == 0 );
  69. ok ( bits[8] == 0x20 );
  70. ok ( test_and_set_bit ( 69, bits ) != 0 );
  71. ok ( bits[8] == 0x20 );
  72. ok ( test_and_set_bit ( 69, bits ) != 0 );
  73. ok ( bits[8] == 0x20 );
  74. /* Test test_and_clear_bit() */
  75. ok ( test_and_clear_bit ( 0, bits ) != 0 );
  76. ok ( bits[0] == 0x00 );
  77. ok ( test_and_clear_bit ( 0, bits ) == 0 );
  78. ok ( bits[0] == 0x00 );
  79. bits[31] = 0xeb;
  80. ok ( test_and_clear_bit ( 255, bits ) != 0 );
  81. ok ( bits[31] == 0x6b );
  82. ok ( test_and_clear_bit ( 255, bits ) == 0 );
  83. ok ( bits[31] == 0x6b );
  84. ok ( test_and_clear_bit ( 255, bits ) == 0 );
  85. ok ( bits[31] == 0x6b );
  86. }
  87. /** Bit operations self-test */
  88. struct self_test bitops_test __self_test = {
  89. .name = "bitops",
  90. .exec = bitops_test_exec,
  91. };