Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

string_test.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 (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. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * String self-tests
  23. *
  24. * memcpy() tests are handled separately
  25. */
  26. /* Forcibly enable assertions */
  27. #undef NDEBUG
  28. #include <stdint.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <ipxe/test.h>
  32. /**
  33. * Perform string self-tests
  34. *
  35. */
  36. static void string_test_exec ( void ) {
  37. /* Test strlen() */
  38. ok ( strlen ( "" ) == 0 );
  39. ok ( strlen ( "Hello" ) == 5 );
  40. ok ( strlen ( "Hello world!" ) == 12 );
  41. ok ( strlen ( "Hello\0world!" ) == 5 );
  42. /* Test strnlen() */
  43. ok ( strnlen ( "", 0 ) == 0 );
  44. ok ( strnlen ( "", 10 ) == 0 );
  45. ok ( strnlen ( "Hello", 0 ) == 0 );
  46. ok ( strnlen ( "Hello", 3 ) == 3 );
  47. ok ( strnlen ( "Hello", 5 ) == 5 );
  48. ok ( strnlen ( "Hello", 16 ) == 5 );
  49. ok ( strnlen ( "Hello world!", 5 ) == 5 );
  50. ok ( strnlen ( "Hello world!", 11 ) == 11 );
  51. ok ( strnlen ( "Hello world!", 16 ) == 12 );
  52. /* Test strchr() */
  53. ok ( strchr ( "", 'a' ) == NULL );
  54. ok ( *(strchr ( "Testing", 'e' )) == 'e' );
  55. ok ( *(strchr ( "Testing", 'g' )) == 'g' );
  56. ok ( strchr ( "Testing", 'x' ) == NULL );
  57. /* Test strcmp() */
  58. ok ( strcmp ( "", "" ) == 0 );
  59. ok ( strcmp ( "Hello", "Hello" ) == 0 );
  60. ok ( strcmp ( "Hello", "hello" ) != 0 );
  61. ok ( strcmp ( "Hello", "Hello world!" ) != 0 );
  62. ok ( strcmp ( "Hello world!", "Hello" ) != 0 );
  63. /* Test strncmp() */
  64. ok ( strncmp ( "", "", 0 ) == 0 );
  65. ok ( strncmp ( "", "", 15 ) == 0 );
  66. ok ( strncmp ( "Goodbye", "Goodbye", 16 ) == 0 );
  67. ok ( strncmp ( "Goodbye", "Hello", 16 ) != 0 );
  68. ok ( strncmp ( "Goodbye", "Goodbye world", 32 ) != 0 );
  69. ok ( strncmp ( "Goodbye", "Goodbye world", 7 ) == 0 );
  70. /* Test memcmp() */
  71. ok ( memcmp ( "", "", 0 ) == 0 );
  72. ok ( memcmp ( "Foo", "Foo", 3 ) == 0 );
  73. ok ( memcmp ( "Foo", "Bar", 3 ) != 0 );
  74. /* Test memset() */
  75. {
  76. static uint8_t test[7] = { '>', 1, 1, 1, 1, 1, '<' };
  77. static const uint8_t expected[7] = { '>', 0, 0, 0, 0, 0, '<' };
  78. memset ( ( test + 1 ), 0, ( sizeof ( test ) - 2 ) );
  79. ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
  80. }
  81. {
  82. static uint8_t test[4] = { '>', 0, 0, '<' };
  83. static const uint8_t expected[4] = { '>', 0xeb, 0xeb, '<' };
  84. memset ( ( test + 1 ), 0xeb, ( sizeof ( test ) - 2 ) );
  85. ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
  86. }
  87. /* Test memmove() */
  88. {
  89. static uint8_t test[11] =
  90. { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, '<' };
  91. static const uint8_t expected[11] =
  92. { '>', 3, 4, 5, 6, 7, 8, 7, 8, 9, '<' };
  93. memmove ( ( test + 1 ), ( test + 3 ), 6 );
  94. ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
  95. }
  96. {
  97. static uint8_t test[12] =
  98. { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '<' };
  99. static const uint8_t expected[12] =
  100. { '>', 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, '<' };
  101. memmove ( ( test + 6 ), ( test + 1 ), 5 );
  102. ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
  103. }
  104. /* Test memswap() */
  105. {
  106. static uint8_t test[8] =
  107. { '>', 1, 2, 3, 7, 8, 9, '<' };
  108. static const uint8_t expected[8] =
  109. { '>', 7, 8, 9, 1, 2, 3, '<' };
  110. memswap ( ( test + 1 ), ( test + 4 ), 3 );
  111. ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
  112. }
  113. }
  114. /** String self-test */
  115. struct self_test string_test __self_test = {
  116. .name = "string",
  117. .exec = string_test_exec,
  118. };