strlen.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include "test_helpers.h"
  4. int main(void) {
  5. char dest1[13] = "hello world!";
  6. int dest1_len = strlen(dest1);
  7. printf("%d\n", dest1_len);
  8. if(dest1_len != 12) {
  9. puts("strlen(\"hello world!\") failed");
  10. exit(EXIT_FAILURE);
  11. }
  12. char empty[1] = { 0 };
  13. int empty_len = strlen(empty);
  14. printf("%d\n", empty_len);
  15. if(empty_len != 0) {
  16. puts("strlen(\"\") failed");
  17. exit(EXIT_FAILURE);
  18. }
  19. dest1_len = strnlen(dest1, sizeof(dest1));
  20. printf("%d\n", dest1_len);
  21. if(dest1_len != 12) {
  22. puts("strnlen(\"hello world!\", 13) failed");
  23. exit(EXIT_FAILURE);
  24. }
  25. dest1_len = strnlen(dest1, sizeof(dest1) - 1);
  26. printf("%d\n", dest1_len);
  27. if(dest1_len != 12) {
  28. puts("strnlen(\"hello world!\", 12) failed");
  29. exit(EXIT_FAILURE);
  30. }
  31. dest1_len = strnlen(dest1, 0);
  32. printf("%d\n", dest1_len);
  33. if(dest1_len != 0) {
  34. puts("strnlen(\"hello world!\", 0) failed");
  35. exit(EXIT_FAILURE);
  36. }
  37. dest1_len = strnlen(dest1, 300);
  38. printf("%d\n", dest1_len);
  39. if(dest1_len != 12) {
  40. puts("strnlen(\"hello world!\", 300) failed");
  41. exit(EXIT_FAILURE);
  42. }
  43. dest1_len = strnlen_s(dest1, 6);
  44. printf("%d\n", dest1_len);
  45. if(dest1_len != 6) {
  46. puts("strnlen_s(\"hello world!\", 6) failed");
  47. exit(EXIT_FAILURE);
  48. }
  49. dest1_len = strnlen_s(dest1, 20);
  50. printf("%d\n", dest1_len);
  51. if(dest1_len != 12) {
  52. puts("strnlen_s(\"hello world!\", 20) failed");
  53. exit(EXIT_FAILURE);
  54. }
  55. int null_len = strnlen_s(NULL, 100);
  56. printf("%d\n", null_len);
  57. if(null_len != 0) {
  58. puts("strnlen_s(NULL, 100) failed");
  59. exit(EXIT_FAILURE);
  60. }
  61. return 0;
  62. }