Add missing functions to string.h

This commit is contained in:
Darley Barreto
2024-12-28 22:46:45 +00:00
committed by Jeremy Soller
parent 841abb180a
commit ae5a5256b4
22 changed files with 245 additions and 23 deletions
+26
View File
@@ -0,0 +1,26 @@
#include <string.h>
#include <assert.h>
void test_stpcpy(const char *src, char *dest) {
char *end = stpcpy(dest, src);
assert(strcmp(dest, src) == 0);
assert(*end == '\0');
assert(end == dest + strlen(dest));
}
int main() {
char dest[20];
test_stpcpy("Hello, World!", dest);
// Test case 2: Empty string
test_stpcpy("", dest);
// Test case 3: String with special characters
test_stpcpy("Special chars: !@#$%^&*()", dest);
// Test case 4: String with spaces
test_stpcpy("A string with spaces", dest);
return 0;
}
+40
View File
@@ -0,0 +1,40 @@
#include <string.h>
#include <assert.h>
int main() {
// Define some test strings
const char *src = "Hello, World!";
char dest[50];
// Test 1: Copy exactly 5 characters from src to dest
char *result = stpncpy(dest, src, 5);
dest[5] = '\0'; // Ensure the destination string is null-terminated
assert(strcmp(dest, "Hello") == 0); // Verify the string in dest
assert(result == &dest[5]); // Ensure the return pointer points to the null terminator
// Test 2: Copy 15 characters from src to dest (more than the length of src)
result = stpncpy(dest, src, 15);
dest[13] = '\0'; // Ensure the destination string is null-terminated
assert(strcmp(dest, "Hello, World!") == 0); // Verify the string in dest
assert(result == &dest[13]); // Ensure the return pointer points to the null terminator
// Test 3: Copy 3 characters from src to dest
result = stpncpy(dest, src, 3);
dest[3] = '\0'; // Ensure the destination string is null-terminated
assert(strcmp(dest, "Hel") == 0); // Verify the string in dest
assert(result == &dest[3]); // Ensure the return pointer points to the null terminator
// Test 4: Copy 0 characters from src to dest
result = stpncpy(dest, src, 0);
dest[0] = '\0'; // Ensure the destination is explicitly null-terminated
assert(dest[0] == '\0'); // Ensure the destination is an empty string
assert(result == dest); // Ensure the return pointer points to the start of dest
// Test 5: Copy exactly the length of the source string
result = stpncpy(dest, src, strlen(src));
dest[strlen(src)] = '\0'; // Ensure the destination string is null-terminated
assert(strcmp(dest, "Hello, World!") == 0); // Verify the string in dest
assert(result == &dest[strlen(src)]); // Ensure the return pointer points to the null terminator
return 0;
}
+31
View File
@@ -0,0 +1,31 @@
#include <string.h>
#include <assert.h>
int main() {
// Test 1: Character is present in the string
const char *str1 = "Hello, World!";
const char *result1 = strchrnul(str1, 'o');
assert(result1 == &str1[4]); // 'o' is at position 4 in "Hello, World!"
// Test 2: Character is not in the string (should return the null terminator)
const char *str2 = "Hello, World!";
const char *result2 = strchrnul(str2, 'z');
assert(result2 == &str2[13]); // 'z' is not present, so it returns the null terminator
// Test 3: Character is the first character in the string
const char *str3 = "abcdef";
const char *result3 = strchrnul(str3, 'a');
assert(result3 == &str3[0]); // 'a' is at position 0
// Test 4: Character is the last character in the string
const char *str4 = "abcdef";
const char *result4 = strchrnul(str4, 'f');
assert(result4 == &str4[5]); // 'f' is at position 5, the last character
// Test 5: Searching for the null terminator itself
const char *str5 = "abcdef";
const char *result5 = strchrnul(str5, '\0');
assert(result5 == &str5[6]); // The null terminator is at position 6 (end of the string)
return 0;
}
+68
View File
@@ -0,0 +1,68 @@
#include <string.h>
#include <assert.h>
int main() {
// Test case 1: Basic case with multiple tokens
char str1[] = "apple,orange,banana";
char *delim = ",";
char *token = str1;
char *result = NULL;
// First token should be "apple"
result = strsep(&token, delim);
assert(result != NULL && strcmp(result, "apple") == 0);
// Second token should be "orange"
result = strsep(&token, delim);
assert(result != NULL && strcmp(result, "orange") == 0);
// Third token should be "banana"
result = strsep(&token, delim);
assert(result != NULL && strcmp(result, "banana") == 0);
// No more tokens
result = strsep(&token, delim);
assert(result == NULL);
// Test case 2: Empty string
char str2[] = "";
token = str2;
result = strsep(&token, delim);
// Test case 3: String with no delimiter
char str3[] = "apple";
token = str3;
result = strsep(&token, delim);
assert(result != NULL && strcmp(result, "apple") == 0);
assert(token == NULL); // No more tokens
// Test case 4: String starts with delimiter
char str4[] = ",apple,orange";
token = str4;
result = strsep(&token, delim);
assert(result != NULL && strlen(result) == 0); // First token should be an empty string ("")
result = strsep(&token, delim);
assert(result != NULL && strcmp(result, "apple") == 0);
// Test case 5: Multiple delimiters in a row
char str5[] = "apple,,orange";
token = str5;
result = strsep(&token, delim);
assert(result != NULL && strcmp(result, "apple") == 0);
result = strsep(&token, delim);
assert(result != NULL && strlen(result) == 0); // Empty token due to consecutive delimiters
result = strsep(&token, delim);
assert(result != NULL && strcmp(result, "orange") == 0);
// Test case 6: Delimiters at the end of the string
char str6[] = "apple,orange,";
token = str6;
result = strsep(&token, delim);
assert(result != NULL && strcmp(result, "apple") == 0);
result = strsep(&token, delim);
assert(result != NULL && strcmp(result, "orange") == 0);
result = strsep(&token, delim);
return 0;
}