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.8KB

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