string.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <string.h>
  2. #include "test.h"
  3. size_t test_size;
  4. void test_string_h(void)
  5. {
  6. char dst[64] = { 0 };
  7. char less[] = "a";
  8. char more[] = "b";
  9. char haystack[] = "the five boxing wizards jump quickly";
  10. testing_header("string.h");
  11. test_true(NULL == 0);
  12. test_string(memcpy(dst + 1, "foo", 3), "foo");
  13. test_string(memmove(dst, dst + 1, 3), "fooo");
  14. test_string(strcpy(dst, "foo"), "foo");
  15. memset(dst, '\0', sizeof(dst));
  16. test_string(strncpy(dst, "foo", 1), "f");
  17. test_string(strcat(dst, "oo"), "foo");
  18. test_string(strncat(dst, "barf", 3), "foobar");
  19. test_int_equals(memcmp(less, less, 2), 0);
  20. test_true(memcmp(less, more, 2) < 0);
  21. test_true(memcmp(more, less, 2) > 0);
  22. test_int_equals(strcmp(less, less), 0);
  23. test_true(strcmp(less, more) < 0);
  24. test_true(strcmp(more, less) > 0);
  25. test_int_equals(strcoll(less, less), 0);
  26. test_true(strcoll(less, more) < 0);
  27. test_true(strcoll(more, less) > 0);
  28. test_int_equals(strncmp(less, less, 2), 0);
  29. test_true(strncmp(less, more, 2) < 0);
  30. test_true(strncmp(more, less, 2) > 0);
  31. /* TODO: strxfrm */
  32. test_true(memchr(haystack, '1', sizeof(haystack)) == NULL);
  33. test_true(memchr(haystack, 'f', sizeof(haystack)) == haystack + 4);
  34. test_true(strchr(haystack, '1') == NULL);
  35. test_true(strchr(haystack, 'f') == haystack + 4);
  36. test_true(strcspn(haystack, " abcd") == 3);
  37. test_true(strcspn(haystack, "eht") == 0);
  38. test_true(strcspn(haystack, "12345") == sizeof(haystack) - 1);
  39. test_true(strpbrk(haystack, "12345") == NULL);
  40. test_true(strpbrk(haystack, "abcd") == haystack + 9);
  41. test_true(strrchr(haystack, '1') == NULL);
  42. test_true(strrchr(haystack, 'f') == haystack + 4);
  43. test_true(strspn(haystack, "12345") == 0);
  44. test_true(strspn(haystack, "eht") == 3);
  45. test_true(strstr(haystack, "") == haystack);
  46. test_true(strstr(haystack, "wizard") == haystack + 16);
  47. test_true(strstr(haystack, "rogue") == NULL);
  48. /* TODO: strtok */
  49. /* TODO: memset */
  50. /* TODO: strerror */
  51. test_true(strlen(haystack) == sizeof(haystack) - 1);
  52. testing_end();
  53. }