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.

memset_test.c 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (C) 2015 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. * memset() self-tests
  27. *
  28. */
  29. /* Forcibly enable assertions */
  30. #undef NDEBUG
  31. #include <string.h>
  32. #include <ipxe/test.h>
  33. /* Provide global functions to allow inspection of generated code */
  34. void memset_zero_0 ( void *dest ) { memset ( dest, 0, 0 ); }
  35. void memset_zero_1 ( void *dest ) { memset ( dest, 0, 1 ); }
  36. void memset_zero_2 ( void *dest ) { memset ( dest, 0, 2 ); }
  37. void memset_zero_3 ( void *dest ) { memset ( dest, 0, 3 ); }
  38. void memset_zero_4 ( void *dest ) { memset ( dest, 0, 4 ); }
  39. void memset_zero_5 ( void *dest ) { memset ( dest, 0, 5 ); }
  40. void memset_zero_6 ( void *dest ) { memset ( dest, 0, 6 ); }
  41. void memset_zero_7 ( void *dest ) { memset ( dest, 0, 7 ); }
  42. void memset_zero_8 ( void *dest ) { memset ( dest, 0, 8 ); }
  43. void memset_zero_9 ( void *dest ) { memset ( dest, 0, 9 ); }
  44. void memset_zero_10 ( void *dest ) { memset ( dest, 0, 10 ); }
  45. void memset_zero_11 ( void *dest ) { memset ( dest, 0, 11 ); }
  46. void memset_zero_12 ( void *dest ) { memset ( dest, 0, 12 ); }
  47. void memset_zero_13 ( void *dest ) { memset ( dest, 0, 13 ); }
  48. void memset_zero_14 ( void *dest ) { memset ( dest, 0, 14 ); }
  49. void memset_zero_15 ( void *dest ) { memset ( dest, 0, 15 ); }
  50. void memset_zero_16 ( void *dest ) { memset ( dest, 0, 16 ); }
  51. void memset_zero_17 ( void *dest ) { memset ( dest, 0, 17 ); }
  52. void memset_zero_18 ( void *dest ) { memset ( dest, 0, 18 ); }
  53. void memset_zero_19 ( void *dest ) { memset ( dest, 0, 19 ); }
  54. void memset_zero_20 ( void *dest ) { memset ( dest, 0, 20 ); }
  55. void memset_zero_21 ( void *dest ) { memset ( dest, 0, 21 ); }
  56. void memset_zero_22 ( void *dest ) { memset ( dest, 0, 22 ); }
  57. void memset_zero_23 ( void *dest ) { memset ( dest, 0, 23 ); }
  58. void memset_zero_24 ( void *dest ) { memset ( dest, 0, 24 ); }
  59. void memset_zero_25 ( void *dest ) { memset ( dest, 0, 25 ); }
  60. void memset_zero_26 ( void *dest ) { memset ( dest, 0, 26 ); }
  61. void memset_zero_27 ( void *dest ) { memset ( dest, 0, 27 ); }
  62. void memset_zero_28 ( void *dest ) { memset ( dest, 0, 28 ); }
  63. void memset_zero_29 ( void *dest ) { memset ( dest, 0, 29 ); }
  64. void memset_zero_30 ( void *dest ) { memset ( dest, 0, 30 ); }
  65. void memset_zero_31 ( void *dest ) { memset ( dest, 0, 31 ); }
  66. /**
  67. * Force a call to the variable-length implementation of memset()
  68. *
  69. * @v dest Destination address
  70. * @v fill Fill pattern
  71. * @v len Length of data
  72. * @ret dest Destination address
  73. */
  74. __attribute__ (( noinline )) void * memset_var ( void *dest, unsigned int fill,
  75. size_t len ) {
  76. return memset ( dest, fill, len );
  77. }
  78. /**
  79. * Perform a constant-length memset() test
  80. *
  81. * @v len Length of data
  82. */
  83. #define MEMSET_TEST_CONSTANT( len ) do { \
  84. uint8_t dest_const[ 1 + len + 1 ]; \
  85. uint8_t dest_var[ 1 + len + 1 ]; \
  86. static uint8_t zero[len]; \
  87. unsigned int i; \
  88. \
  89. for ( i = 0 ; i < sizeof ( dest_const ) ; i++ ) \
  90. dest_const[i] = 0xaa; \
  91. memset ( ( dest_const + 1 ), 0, len ); \
  92. ok ( dest_const[0] == 0xaa ); \
  93. ok ( dest_const[ sizeof ( dest_const ) - 1 ] == 0xaa ); \
  94. ok ( memcmp ( ( dest_const + 1 ), zero, len ) == 0 ); \
  95. \
  96. for ( i = 0 ; i < sizeof ( dest_var ) ; i++ ) \
  97. dest_var[i] = 0xbb; \
  98. memset_var ( ( dest_var + 1 ), 0, len ); \
  99. ok ( dest_var[0] == 0xbb ); \
  100. ok ( dest_var[ sizeof ( dest_var ) - 1 ] == 0xbb ); \
  101. ok ( memcmp ( ( dest_var + 1 ), zero, len ) == 0 ); \
  102. } while ( 0 )
  103. /**
  104. * Perform memset() self-tests
  105. *
  106. */
  107. static void memset_test_exec ( void ) {
  108. /* Constant-length tests */
  109. MEMSET_TEST_CONSTANT ( 0 );
  110. MEMSET_TEST_CONSTANT ( 1 );
  111. MEMSET_TEST_CONSTANT ( 2 );
  112. MEMSET_TEST_CONSTANT ( 3 );
  113. MEMSET_TEST_CONSTANT ( 4 );
  114. MEMSET_TEST_CONSTANT ( 5 );
  115. MEMSET_TEST_CONSTANT ( 6 );
  116. MEMSET_TEST_CONSTANT ( 7 );
  117. MEMSET_TEST_CONSTANT ( 8 );
  118. MEMSET_TEST_CONSTANT ( 9 );
  119. MEMSET_TEST_CONSTANT ( 10 );
  120. MEMSET_TEST_CONSTANT ( 11 );
  121. MEMSET_TEST_CONSTANT ( 12 );
  122. MEMSET_TEST_CONSTANT ( 13 );
  123. MEMSET_TEST_CONSTANT ( 14 );
  124. MEMSET_TEST_CONSTANT ( 15 );
  125. MEMSET_TEST_CONSTANT ( 16 );
  126. MEMSET_TEST_CONSTANT ( 17 );
  127. MEMSET_TEST_CONSTANT ( 18 );
  128. MEMSET_TEST_CONSTANT ( 19 );
  129. MEMSET_TEST_CONSTANT ( 20 );
  130. MEMSET_TEST_CONSTANT ( 21 );
  131. MEMSET_TEST_CONSTANT ( 22 );
  132. MEMSET_TEST_CONSTANT ( 23 );
  133. MEMSET_TEST_CONSTANT ( 24 );
  134. MEMSET_TEST_CONSTANT ( 25 );
  135. MEMSET_TEST_CONSTANT ( 26 );
  136. MEMSET_TEST_CONSTANT ( 27 );
  137. MEMSET_TEST_CONSTANT ( 28 );
  138. MEMSET_TEST_CONSTANT ( 29 );
  139. MEMSET_TEST_CONSTANT ( 30 );
  140. MEMSET_TEST_CONSTANT ( 31 );
  141. }
  142. /** memset() self-test */
  143. struct self_test memset_test __self_test = {
  144. .name = "memset",
  145. .exec = memset_test_exec,
  146. };