Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5.  
  6. void test_strcmp(char *str, char *str2)
  7. {
  8. printf("strcmp(%s, %s) = %i\n", str, str2, strcmp(str, str2));
  9. }
  10.  
  11. void test_strncmp(char *str, char *str2, int size)
  12. {
  13. printf("strncmp(%s, %s, %i) = %i\n", str, str2, size, strncmp(str, str2, size));
  14. }
  15.  
  16. void test_strcasecmp(char *str, char *str2)
  17. {
  18. printf("strcasecmp(%s, %s) = %i\n", str, str2, strcasecmp(str, str2));
  19. }
  20.  
  21. void test_strchr(char *str, int c)
  22. {
  23. printf("strchr(%s, %c) = %s\n", str, c, strchr(str, c));
  24. }
  25.  
  26. void test_rindex(char *str, int c)
  27. {
  28. printf("rindex(%s, %c) = %s\n", str, c, rindex(str, c));
  29. }
  30.  
  31. void test_strstr(char *str, char *str2)
  32. {
  33. strstr(str, str2);
  34. printf("strstr(%s, %s) = %s\n", str, str2, strstr(str, str2));
  35. }
  36.  
  37. int main(int ac, char **av) {
  38. char *str = calloc(1024, 1);
  39. void *ptr = str;
  40. strcpy(str, "sahel est vraiment trop fort");
  41. size_t len = strlen(str);
  42. printf("len: %lu\n", len);
  43.  
  44. for (size_t i = 0;i<10000;i++)
  45. test_strchr(str, i);
  46. test_strchr("", 0);
  47. test_strchr("", 'z');
  48.  
  49. for (size_t i = 0;i<10000;i++)
  50. test_rindex(str, i);
  51. test_rindex("", 0);
  52. test_rindex("", 'z');
  53.  
  54. char *save = memset(NULL, 0, 0);
  55. printf("memset null: %p\n", save);
  56. printf("memset (%%i): ");
  57. fflush(stdout);
  58. len = strlen(str);
  59. str = memset(str, '0', len);
  60. write(1, str, len);
  61. printf("\n");
  62.  
  63. save = memcpy(NULL, NULL, 0);
  64. printf("memcpy null: %p\n", save);
  65. str = memcpy(str, "Sahel est vraiment trop fort", 28);
  66. printf("strcpy: %s\n", str);
  67.  
  68. test_strcmp("abcdef", "abcdef");
  69. test_strcmp("abcdef", "000bcdef");
  70. test_strcmp("abcdef", "");
  71. test_strcmp("", "abcdef");
  72. test_strcmp("", "");
  73. test_strcmp("abcdef", "a");
  74. //memset(str, 0, 1024);
  75. fflush(stdout);
  76. str = memmove(str, str + 4, strlen(str + 4));
  77. printf("1memmove: %s\n", str);
  78. str = memmove(str + 4, str, strlen(str));
  79. printf("2memmove: %s\n", str);
  80. fflush(stdout);
  81.  
  82. test_strncmp("abcdef", "abcdf", 4);
  83. test_strncmp("abcdef", "abcdf", 5);
  84. test_strncmp("abcdef", "abcdf", 0);
  85. test_strncmp(NULL, "", 0);
  86. test_strncmp("", NULL, 0);
  87. test_strncmp(NULL, NULL, 0);
  88.  
  89. test_strcasecmp("ABCDEF", "ABCDEF");fflush(stdout);
  90. test_strcasecmp("ABCDEF", "abcdef");fflush(stdout);
  91. test_strcasecmp("", "ABCDEF");fflush(stdout);
  92. test_strcasecmp("", "");fflush(stdout);
  93.  
  94. test_strstr("", "");
  95. test_strstr(str, "");
  96. test_strstr("", str);
  97. test_strstr(str, "V");
  98. test_strstr(str, "m");
  99. test_strstr(str, "cent");
  100. test_strstr(str, "est");
  101. test_strstr(str, "estz");
  102. test_strstr(str, "fort\0z");
  103. test_strstr("Un test incoryable!", "Un test incoryable!");
  104. fflush(stdout);
  105.  
  106. printf("1strpbrk: %s\n", strpbrk(str, "a"));
  107. printf("2strpbrk: %s\n", strpbrk(str, "ze"));
  108. printf("3strpbrk: %s\n", strpbrk(str, "zzz"));
  109. printf("4strpbrk: %s\n", strpbrk(str, "cent"));
  110. printf("5strpbrk: %s\n", strpbrk(str, "e"));
  111. printf("6strpbrk: %s\n", strpbrk(str, ""));
  112. printf("7strpbrk: %s\n", strpbrk("", ""));
  113. fflush(stdout);
  114. printf("8strpbrk: %s\n", strpbrk(NULL, ""));
  115. //printf("8strpbrk: %s\n", strpbrk(NULL, NULL));
  116. fflush(stdout);
  117. printf("1strcspn %lu\n", strcspn(str, "Vincent"));
  118. printf("2strcspn %lu\n", strcspn(str, "bonjour je "));
  119. printf("3strcspn %lu\n", strcspn(str, "abcdefghijklmnoqrstuvwxyz "));
  120. printf("4strcspn %lu\n", strcspn(str, "a"));
  121. printf("5strcspn %lu\n", strcspn(str, "lolqsd"));
  122. printf("6strcspn %lu\n", strcspn(str, ""));
  123. printf("7strcspn %lu\n", strcspn("", "as"));
  124. printf("7strcspn %lu\n", strcspn("Je suis une chaine", "!*$"));
  125. printf("8strcspn %lu\n", strcspn("", ""));
  126. printf("9strcspn %lu\n", strcspn("", NULL));
  127. fflush(stdout);
  128. free(ptr);
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement