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.

string_test.c 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 <stdio.h>
  31. #include <string.h>
  32. #include <strings.h>
  33. #include <ipxe/string.h>
  34. #include <ipxe/test.h>
  35. /**
  36. * Perform string self-tests
  37. *
  38. */
  39. static void string_test_exec ( void ) {
  40. /* Test strlen() */
  41. ok ( strlen ( "" ) == 0 );
  42. ok ( strlen ( "Hello" ) == 5 );
  43. ok ( strlen ( "Hello world!" ) == 12 );
  44. ok ( strlen ( "Hello\0world!" ) == 5 );
  45. /* Test strnlen() */
  46. ok ( strnlen ( "", 0 ) == 0 );
  47. ok ( strnlen ( "", 10 ) == 0 );
  48. ok ( strnlen ( "Hello", 0 ) == 0 );
  49. ok ( strnlen ( "Hello", 3 ) == 3 );
  50. ok ( strnlen ( "Hello", 5 ) == 5 );
  51. ok ( strnlen ( "Hello", 16 ) == 5 );
  52. ok ( strnlen ( "Hello world!", 5 ) == 5 );
  53. ok ( strnlen ( "Hello world!", 11 ) == 11 );
  54. ok ( strnlen ( "Hello world!", 16 ) == 12 );
  55. /* Test strchr() */
  56. ok ( strchr ( "", 'a' ) == NULL );
  57. ok ( *(strchr ( "Testing", 'e' )) == 'e' );
  58. ok ( *(strchr ( "Testing", 'g' )) == 'g' );
  59. ok ( strchr ( "Testing", 'x' ) == NULL );
  60. /* Test strrchr() */
  61. ok ( strrchr ( "", 'a' ) == NULL );
  62. ok ( *(strrchr ( "Haystack", 'a' )) == 'a' );
  63. ok ( *(strrchr ( "Haystack", 'k' )) == 'k' );
  64. ok ( strrchr ( "Haystack", 'x' ) == NULL );
  65. /* Test memchr() */
  66. ok ( memchr ( "", '\0', 0 ) == NULL );
  67. ok ( *((uint8_t *)memchr ( "post\0null", 'l', 9 )) == 'l' );
  68. ok ( *((uint8_t *)memchr ( "post\0null", '\0', 9 )) == '\0' );
  69. ok ( memchr ( "thingy", 'z', 6 ) == NULL );
  70. /* Test strcmp() */
  71. ok ( strcmp ( "", "" ) == 0 );
  72. ok ( strcmp ( "Hello", "Hello" ) == 0 );
  73. ok ( strcmp ( "Hello", "hello" ) != 0 );
  74. ok ( strcmp ( "Hello", "Hello world!" ) != 0 );
  75. ok ( strcmp ( "Hello world!", "Hello" ) != 0 );
  76. /* Test strncmp() */
  77. ok ( strncmp ( "", "", 0 ) == 0 );
  78. ok ( strncmp ( "", "", 15 ) == 0 );
  79. ok ( strncmp ( "Goodbye", "Goodbye", 16 ) == 0 );
  80. ok ( strncmp ( "Goodbye", "Hello", 16 ) != 0 );
  81. ok ( strncmp ( "Goodbye", "Goodbye world", 32 ) != 0 );
  82. ok ( strncmp ( "Goodbye", "Goodbye world", 7 ) == 0 );
  83. /* Test strcasecmp() */
  84. ok ( strcasecmp ( "", "" ) == 0 );
  85. ok ( strcasecmp ( "Uncle Jack", "Uncle jack" ) == 0 );
  86. ok ( strcasecmp ( "Uncle Jack", "Uncle" ) != 0 );
  87. ok ( strcasecmp ( "Uncle", "Uncle Jack" ) != 0 );
  88. ok ( strcasecmp ( "not", "equal" ) != 0 );
  89. /* Test memcmp() */
  90. ok ( memcmp ( "", "", 0 ) == 0 );
  91. ok ( memcmp ( "Foo", "Foo", 3 ) == 0 );
  92. ok ( memcmp ( "Foo", "Bar", 3 ) != 0 );
  93. /* Test strstr() */
  94. {
  95. const char haystack[] = "find me!";
  96. char *found;
  97. found = strstr ( haystack, "find" );
  98. ok ( found == &haystack[0] );
  99. found = strstr ( haystack, "me" );
  100. ok ( found == &haystack[5] );
  101. found = strstr ( haystack, "me." );
  102. ok ( found == NULL );
  103. }
  104. /* Test memset() */
  105. {
  106. static uint8_t test[7] = { '>', 1, 1, 1, 1, 1, '<' };
  107. static const uint8_t expected[7] = { '>', 0, 0, 0, 0, 0, '<' };
  108. memset ( ( test + 1 ), 0, ( sizeof ( test ) - 2 ) );
  109. ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
  110. }
  111. {
  112. static uint8_t test[4] = { '>', 0, 0, '<' };
  113. static const uint8_t expected[4] = { '>', 0xeb, 0xeb, '<' };
  114. memset ( ( test + 1 ), 0xeb, ( sizeof ( test ) - 2 ) );
  115. ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
  116. }
  117. /* Test memmove() */
  118. {
  119. static uint8_t test[11] =
  120. { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, '<' };
  121. static const uint8_t expected[11] =
  122. { '>', 3, 4, 5, 6, 7, 8, 7, 8, 9, '<' };
  123. memmove ( ( test + 1 ), ( test + 3 ), 6 );
  124. ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
  125. }
  126. {
  127. static uint8_t test[12] =
  128. { '>', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '<' };
  129. static const uint8_t expected[12] =
  130. { '>', 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, '<' };
  131. memmove ( ( test + 6 ), ( test + 1 ), 5 );
  132. ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
  133. }
  134. /* Test memswap() */
  135. {
  136. static uint8_t test[8] =
  137. { '>', 1, 2, 3, 7, 8, 9, '<' };
  138. static const uint8_t expected[8] =
  139. { '>', 7, 8, 9, 1, 2, 3, '<' };
  140. memswap ( ( test + 1 ), ( test + 4 ), 3 );
  141. ok ( memcmp ( test, expected, sizeof ( test ) ) == 0 );
  142. }
  143. /* Test strdup() */
  144. {
  145. const char *orig = "testing testing";
  146. char *dup = strdup ( orig );
  147. ok ( dup != NULL );
  148. ok ( dup != orig );
  149. ok ( strcmp ( dup, orig ) == 0 );
  150. free ( dup );
  151. }
  152. /* Test strndup() */
  153. {
  154. const char *normal = "testing testing";
  155. const char unterminated[6] = { 'h', 'e', 'l', 'l', 'o', '!' };
  156. char *dup;
  157. dup = strndup ( normal, 32 );
  158. ok ( dup != NULL );
  159. ok ( dup != normal );
  160. ok ( strcmp ( dup, normal ) == 0 );
  161. free ( dup );
  162. dup = strndup ( normal, 4 );
  163. ok ( dup != NULL );
  164. ok ( strcmp ( dup, "test" ) == 0 );
  165. free ( dup );
  166. dup = strndup ( unterminated, 5 );
  167. ok ( dup != NULL );
  168. ok ( strcmp ( dup, "hello" ) == 0 );
  169. free ( dup );
  170. }
  171. /* Test strcpy() */
  172. {
  173. const char longer[7] = "copyme";
  174. const char shorter[3] = "hi";
  175. char dest[7];
  176. char *copy;
  177. copy = strcpy ( dest, longer );
  178. ok ( copy == dest );
  179. ok ( memcmp ( dest, longer, 7 ) == 0 );
  180. copy = strcpy ( dest, shorter );
  181. ok ( copy == dest );
  182. ok ( memcmp ( dest, shorter, 3 ) == 0 );
  183. ok ( memcmp ( ( dest + 3 ), ( longer + 3 ), 4 ) == 0 );
  184. }
  185. /* Test strncpy() */
  186. {
  187. const char src[5] = "copy";
  188. const char orig[8] = { 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x' };
  189. const char zero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  190. char dest[8];
  191. char *copy;
  192. memcpy ( dest, orig, sizeof ( dest ) );
  193. copy = strncpy ( dest, src, 5 );
  194. ok ( copy == dest );
  195. ok ( memcmp ( dest, src, 5 ) == 0 );
  196. ok ( memcmp ( dest + 5, orig + 5, 3 ) == 0 );
  197. memcpy ( dest, orig, sizeof ( dest ) );
  198. copy = strncpy ( dest, src, 4 );
  199. ok ( copy == dest );
  200. ok ( memcmp ( dest, src, 4 ) == 0 );
  201. ok ( memcmp ( dest + 4, orig + 4, 4 ) == 0 );
  202. memcpy ( dest, orig, sizeof ( dest ) );
  203. copy = strncpy ( dest, src, 8 );
  204. ok ( copy == dest );
  205. ok ( memcmp ( dest, src, 5 ) == 0 );
  206. ok ( memcmp ( dest + 5, zero + 5, 3 ) == 0 );
  207. memcpy ( dest, orig, sizeof ( dest ) );
  208. copy = strncpy ( dest, "", 8 );
  209. ok ( copy == dest );
  210. ok ( memcmp ( dest, zero, 8 ) == 0 );
  211. }
  212. /* Test strcat() */
  213. {
  214. char buf[16] = "append";
  215. char *dest;
  216. dest = strcat ( buf, " this" );
  217. ok ( dest == buf );
  218. ok ( strcmp ( buf, "append this" ) == 0 );
  219. }
  220. /* Test digit_value() */
  221. {
  222. unsigned int i;
  223. char buf[2];
  224. for ( i = 0 ; i < 16 ; i++ ) {
  225. snprintf ( buf, sizeof ( buf ), "%x", i );
  226. ok ( digit_value ( buf[0] ) == i );
  227. snprintf ( buf, sizeof ( buf ), "%X", i );
  228. ok ( digit_value ( buf[0] ) == i );
  229. }
  230. ok ( digit_value ( 0 ) >= 16 );
  231. ok ( digit_value ( 9 ) >= 16 );
  232. ok ( digit_value ( '0' - 1 ) >= 16 );
  233. ok ( digit_value ( '9' + 1 ) >= 16 );
  234. ok ( digit_value ( 'A' - 1 ) >= 16 );
  235. ok ( digit_value ( 'F' + 1 ) >= 16 );
  236. ok ( digit_value ( 'a' - 1 ) >= 16 );
  237. ok ( digit_value ( 'f' + 1 ) >= 16 );
  238. }
  239. /* Test strtoul() */
  240. ok ( strtoul ( "12345", NULL, 0 ) == 12345UL );
  241. ok ( strtoul ( " 741", NULL, 10 ) == 741UL );
  242. ok ( strtoul ( " 555a", NULL, 0 ) == 555UL );
  243. ok ( strtoul ( " 555a", NULL, 16 ) == 0x555aUL );
  244. ok ( strtoul ( "-12", NULL, 0 ) == -12UL );
  245. ok ( strtoul ( "+3", NULL, 0 ) == 3UL );
  246. ok ( strtoul ( "721", NULL, 0 ) == 721UL );
  247. ok ( strtoul ( "721", NULL, 8 ) == 0721UL );
  248. ok ( strtoul ( "0721", NULL, 0 ) == 0721UL );
  249. ok ( strtoul ( "", NULL, 0 ) == 0UL );
  250. ok ( strtoul ( "\t0xcAfe", NULL, 0 ) == 0xcafeUL );
  251. ok ( strtoul ( "0xffffffff", NULL, 0 ) == 0xffffffffUL );
  252. {
  253. static const char string[] = "123aHa.world";
  254. char *endp;
  255. ok ( strtoul ( string, &endp, 0 ) == 123UL );
  256. ok ( endp == &string[3] );
  257. ok ( strtoul ( string, &endp, 16 ) == 0x123aUL );
  258. ok ( endp == &string[4] );
  259. ok ( strtoul ( string, &endp, 26 ) ==
  260. ( ( ( ( ( 1 * 26 + 2 ) * 26 + 3 ) * 26 + 10 ) * 26
  261. + 17 ) * 26 + 10 ) );
  262. ok ( endp == &string[6] );
  263. }
  264. }
  265. /** String self-test */
  266. struct self_test string_test __self_test = {
  267. .name = "string",
  268. .exec = string_test_exec,
  269. };