github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/tests/string.c (about) 1 #include "tests.h" 2 #include <string.h> 3 4 #define START_TEST(t) \ 5 diag(#t); \ 6 test_##t(); 7 8 void test_strtok() 9 { 10 char str[] = "- This, a sample string."; 11 char* pch; 12 printf("Splitting string \"%s\" into tokens:\n", str); 13 pch = strtok(str, " ,.-"); 14 printf("strtok: `%s`\n", pch); 15 // while (pch != NULL) 16 // { 17 // printf ("%s\n",pch); 18 // pch = strtok (NULL, " ,.-"); 19 // } 20 } 21 22 void test_strncmp() 23 { 24 char str[][5] = { "R2D2", "C3PO", "R2A6" }; 25 int n; 26 puts("Looking for R2 astromech droids..."); 27 for (n = 0; n < 3; n++) { 28 if (strncmp(str[n], "R2xx", 2) == 0) { 29 printf("found %s\n", str[n]); 30 } 31 } 32 } 33 34 void test_strcspn() 35 { 36 char str[] = "fcba73"; 37 char keys[] = "1234567890"; 38 int i; 39 i = strcspn(str, keys); 40 printf("The first number in str is at position %d.\n", i + 1); 41 } 42 43 void test_strpbrk() 44 { 45 char str[] = "This is a sample string"; 46 char key[] = "aeiou"; 47 char* pch; 48 printf("Vowels in '%s': ", str); 49 pch = strpbrk(str, key); 50 printf("strpbrk: `%s`\n", pch); 51 // while (pch != NULL) { 52 // printf ("%c " , *pch); 53 // pch = strpbrk (pch+1,key); 54 // } 55 printf("\n"); 56 } 57 58 void test_strspn() 59 { 60 int i; 61 char strtext[] = "129th"; 62 char cset[] = "1234567890"; 63 i = strspn(strtext, cset); 64 printf("The initial number has %d digits.\n", i); 65 } 66 67 int main() 68 { 69 plan(44); 70 71 diag("TODO: __builtin_object_size"); 72 // https://github.com/Konstantin8105/c4go/issues/359 73 74 { 75 diag("strcpy"); 76 char* src = "foo bar\0baz"; 77 char dest1[40]; 78 char* dest2; 79 dest2 = strcpy(dest1, src); 80 is_streq(dest1, "foo bar"); 81 is_streq(dest2, "foo bar"); 82 } 83 84 diag("strncpy"); 85 86 // If the end of the source C string (which is signaled by a null-character) 87 // is found before num characters have been copied, destination is padded 88 // with zeros until a total of num characters have been written to it. 89 { 90 char* src = "foo bar\0baz"; 91 char dest1[40]; 92 char* dest2; 93 94 dest1[7] = 'a'; 95 dest1[15] = 'b'; 96 dest1[25] = 'c'; 97 dest2 = strncpy(dest1, src, 20); 98 99 is_eq(dest1[0], 'f'); 100 is_eq(dest1[7], 0); 101 is_eq(dest1[15], 0); 102 is_eq(dest1[25], 'c'); 103 104 is_eq(dest2[0], 'f'); 105 is_eq(dest2[7], 0); 106 is_eq(dest2[15], 0); 107 is_eq(dest2[25], 'c'); 108 109 is_streq(dest1, "foo bar"); 110 is_streq(dest2, "foo bar"); 111 } 112 113 // No null-character is implicitly appended at the end of destination if 114 // source is longer than num. Thus, in this case, destination shall not be 115 // considered a null terminated C string (reading it as such would 116 // overflow). 117 { 118 char* src = "foo bar\0baz"; 119 char dest1[40]; 120 char* dest2; 121 122 dest1[7] = 'a'; 123 dest1[15] = 'b'; 124 dest1[25] = 'c'; 125 dest2 = strncpy(dest1, src, 5); 126 127 is_eq(dest1[0], 'f'); 128 is_eq(dest1[7], 'a'); 129 is_eq(dest1[15], 'b'); 130 is_eq(dest1[25], 'c'); 131 132 is_eq(dest2[0], 'f'); 133 is_eq(dest2[7], 'a'); 134 is_eq(dest2[15], 'b'); 135 is_eq(dest2[25], 'c'); 136 } 137 138 { 139 diag("strlen"); 140 is_eq(strlen(""), 0); 141 is_eq(strlen("a"), 1); 142 is_eq(strlen("foo"), 3); 143 // NULL causes a seg fault. 144 // is_eq(strlen(NULL), 0); 145 is_eq(strlen("fo\0o"), 2); 146 } 147 { 148 diag("strcat"); 149 char str[80]; 150 for (int i = 0; i < 80; i++) 151 str[i] = 0; 152 strcpy(str, "these "); 153 strcat(str, "strings "); 154 strcat(str, "are "); 155 strcat(str, "concatenated."); 156 is_streq(str, "these strings are concatenated."); 157 } 158 { 159 diag("strncat"); 160 char str[80]; 161 for (int i = 0; i < 80; i++) 162 str[i] = 0; 163 strncpy(str, "these", 3); 164 strncat(str, " strings", 7); 165 strncat(str, " is", 3); 166 strncat(str, " concatenated.", 14); 167 is_streq(str, "the string is concatenated."); 168 } 169 { 170 diag("strcmp"); 171 { 172 char* a = "ab"; 173 char* b = "ab"; 174 is_true(strcmp(a, b) == 0); 175 } 176 { 177 char* a = "bb"; 178 char* b = "ab"; 179 is_true(strcmp(a, b) > 0); 180 } 181 { 182 char* a = "ab"; 183 char* b = "bb"; 184 is_true(strcmp(a, b) < 0); 185 } 186 } 187 { 188 diag("strchr"); 189 char str[] = "This is a sample string"; 190 char* pch; 191 int amount = 0; 192 pch = strchr(str, 's'); 193 while (pch != NULL) { 194 pch = strchr(pch + 1, 's'); 195 amount++; 196 } 197 is_eq(amount, 4); 198 } 199 { 200 diag("memset"); 201 char str[] = "almost every programmer should know memset!"; 202 char* ptr = memset(str, '-', 6); 203 is_streq(str, "------ every programmer should know memset!"); 204 is_eq(ptr - str, 0); 205 } 206 { 207 diag("memmove"); 208 char str[] = "memmove can be very useful......"; 209 memmove(str + 20, str + 15, 11); 210 is_streq(str, "memmove can be very very useful."); 211 } 212 { 213 diag("memcmp"); 214 char a1[] = { 'a', 'b', 'c' }; 215 char a2[] = "abd"; 216 is_true(memcmp(a1, a1, 3) == 0); 217 is_true(memcmp(a1, a2, 3) < 0); 218 is_true(memcmp(a2, a1, 3) > 0); 219 } 220 { 221 diag("strstr"); 222 char str[] = "one two three"; 223 224 printf("%s\n", strstr(str, "one")); 225 is_streq(strstr(str, "one"), "one two three"); 226 227 printf("%s\n", strstr(str, "two")); 228 is_streq(strstr(str, "two"), "two three"); 229 230 printf("%s\n", strstr(str, "three")); 231 is_streq(strstr(str, "three"), "three"); 232 is_null(strstr(str, "fo")); 233 } 234 { 235 diag("memcpy"); 236 char myname[] = "Pierre de Fermat"; 237 char name[40]; 238 memcpy(name, myname, strlen(myname) + 1); 239 is_streq(name, myname); 240 printf("name = `%s`\n", name); 241 } 242 { 243 diag("strrchr"); 244 char str[] = "This is a sample string"; 245 char failData[] = "faildata"; 246 char* pch = &failData; 247 pch = strrchr(str, 's'); 248 is_eq(pch - str + 1, 18); 249 } 250 { 251 diag("strdup"); 252 const char* s1 = "String"; 253 char* s2 = strdup(s1); 254 is_streq(s1, s2); 255 is_true(s1 != s2); 256 } 257 START_TEST(strtok); 258 START_TEST(strncmp); 259 START_TEST(strcspn); 260 START_TEST(strpbrk); 261 START_TEST(strspn); 262 { 263 diag("strerror"); 264 strerror(0); 265 } 266 267 done_testing(); 268 }