progress on implementation
This commit is contained in:
98
src/strops.c
98
src/strops.c
@ -3,8 +3,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/* Function template
|
||||
char* strops_(const char* string) {
|
||||
char *result = malloc(strlen(string));
|
||||
@ -76,3 +74,99 @@ char* strops_trim_both_whitespace(const char* string) {
|
||||
result = strops_trim_left_whitespace(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
char* strops_trim_right_chars(const char* string, const char* chars_to_remove) {
|
||||
char *result = malloc(strlen(string));
|
||||
memcpy(result, string, strlen(string));
|
||||
while(strchr(chars_to_remove, result[strlen(result) - 1]) != NULL) {
|
||||
result = strops_remove_at_pos(result, strlen(result) - 1);
|
||||
}
|
||||
result = realloc(result, strlen(result));
|
||||
return result;
|
||||
}
|
||||
|
||||
char* strops_trim_left_chars(const char* string, const char* chars_to_remove) {
|
||||
char *result = malloc(strlen(string));
|
||||
memcpy(result, string, strlen(string));
|
||||
while(strchr(chars_to_remove, result[0]) != NULL) {
|
||||
result = strops_remove_at_pos(result, 0);
|
||||
}
|
||||
result = realloc(result, strlen(result));
|
||||
return result;
|
||||
}
|
||||
|
||||
char* strops_trim_both_chars(const char* string, const char* chars_to_remove) {
|
||||
char *result = malloc(strlen(string));
|
||||
memcpy(result, string, strlen(string));
|
||||
result = strops_trim_right_chars(result, chars_to_remove);
|
||||
result = strops_trim_left_chars(result, chars_to_remove);
|
||||
return result;
|
||||
}
|
||||
|
||||
char* strops_trim_right_string(const char* string, const char* string_to_remove) {
|
||||
char *result = malloc(strlen(string));
|
||||
memcpy(result, string, strlen(string));
|
||||
return result;
|
||||
}
|
||||
|
||||
char* strops_trim_left_string(const char* string, const char* string_to_remove) {
|
||||
char *result = malloc(strlen(string));
|
||||
memcpy(result, string, strlen(string));
|
||||
return result;
|
||||
}
|
||||
|
||||
char* strops_trim_both_string(const char* string, const char* string_to_remove) {
|
||||
char *result = malloc(strlen(string));
|
||||
memcpy(result, string, strlen(string));
|
||||
result = strops_trim_right_string(result, string_to_remove);
|
||||
result = strops_trim_left_string(result, string_to_remove);
|
||||
return result;
|
||||
}
|
||||
|
||||
char* strops_remove_chars(const char* string, const char* chars_to_remove) {
|
||||
char *result = malloc(strlen(string));
|
||||
memcpy(result, string, strlen(string));
|
||||
|
||||
size_t i;
|
||||
for (i = 0; i < strlen(chars_to_remove); i++) {
|
||||
while(strchr(result, chars_to_remove[i]) != NULL) {
|
||||
result = strops_remove_at_pos(result, strlen(result) - strlen(strchr(result, chars_to_remove[i])));
|
||||
}
|
||||
}
|
||||
result = realloc(result, strlen(result));
|
||||
return result;
|
||||
}
|
||||
|
||||
char* strops_remove_string(const char* string, const char* string_to_remove) {
|
||||
char *result = malloc(strlen(string));
|
||||
memcpy(result, string, strlen(string));
|
||||
|
||||
while(strstr(result, string_to_remove) != NULL) {
|
||||
size_t offset = strlen(result) - strlen(strstr(result, string_to_remove));
|
||||
size_t i;
|
||||
for (i = 0; i < strlen(string_to_remove); i++) {
|
||||
result = strops_remove_at_pos(result, offset);
|
||||
}
|
||||
}
|
||||
result = realloc(result, strlen(result));
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t strops_word_count(const char* string) {
|
||||
char *result = malloc(strlen(string));
|
||||
memcpy(result, string, strlen(string));
|
||||
return 0;
|
||||
}
|
||||
|
||||
char* strops_url_encode(const char* string) {
|
||||
char *result = malloc(strlen(string));
|
||||
memcpy(result, string, strlen(string));
|
||||
return result;
|
||||
}
|
||||
|
||||
char* strops_url_decode(const char* string) {
|
||||
char *result = malloc(strlen(string));
|
||||
memcpy(result, string, strlen(string));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -30,7 +30,7 @@ char* strops_trim_both_string(const char* string, const char* string_to_remove);
|
||||
char* strops_remove_chars(const char* string, const char* chars_to_remove);
|
||||
char* strops_remove_string(const char* string, const char* string_to_remove);
|
||||
|
||||
size_t word_count(const char* string);
|
||||
size_t strops_word_count(const char* string);
|
||||
|
||||
char* strops_url_encode(const char* string);
|
||||
char* strops_url_decode(const char* string);
|
||||
|
224
src/tests.c
224
src/tests.c
@ -39,36 +39,180 @@ int test_to_uppercase() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_trim_right() {
|
||||
int test_remove_at_pos() {
|
||||
char* input = "Hello";
|
||||
char* expected = "Hllo";
|
||||
char* result = strops_remove_at_pos(input, 1);
|
||||
if (strcmp(result, expected) != 0) {
|
||||
printf("test_remove_at_pos failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_trim_right_whitespace() {
|
||||
char* input = "String \t\f\v\r\n ";
|
||||
char* expected = "String";
|
||||
char* result = strops_trim_right_whitespace(input);
|
||||
if (strcmp(result, expected) != 0) {
|
||||
printf("test_trim_right failed\n");
|
||||
printf("\ntest_trim_right_whitespace failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_trim_left() {
|
||||
int test_trim_left_whitespace() {
|
||||
char* input = " \t\f\v\r\n String";
|
||||
char* expected = "String";
|
||||
char* result = strops_trim_left_whitespace(input);
|
||||
if (strcmp(result, expected) != 0) {
|
||||
printf("test_trim_left failed\n");
|
||||
printf("\ntest_trim_left_whitespace failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_trim_both() {
|
||||
int test_trim_both_whitespace() {
|
||||
char* input = " \t\f\v String \r\n ";
|
||||
char* expected = "String";
|
||||
char* result = strops_trim_both_whitespace(input);
|
||||
if (strcmp(result, expected) != 0) {
|
||||
printf("test_trim_both failed\n");
|
||||
printf("\ntest_trim_both_whitespace failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_trim_right_chars() {
|
||||
char* input = "banana";
|
||||
char* expected = "b";
|
||||
char* result = strops_trim_right_chars(input, "na");
|
||||
if (strcmp(result, expected) != 0) {
|
||||
printf("test_right_chars failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_trim_left_chars() {
|
||||
char* input = "banana";
|
||||
char* expected = "nana";
|
||||
char* result = strops_trim_left_chars(input, "ba");
|
||||
if (strcmp(result, expected) != 0) {
|
||||
printf("test_left_chars failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_trim_both_chars() {
|
||||
char* input = "anna";
|
||||
char* expected = "nn";
|
||||
char* result = strops_trim_both_chars(input, "a");
|
||||
if (strcmp(result, expected) != 0) {
|
||||
printf("test_trim_both_chars failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_trim_right_string() {
|
||||
char* input = "adad_artist";
|
||||
char* expected = "adad_";
|
||||
char* result = strops_trim_right_string(input, "artist");
|
||||
if (strcmp(result, expected) != 0) {
|
||||
printf("test_trim_right_string failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_trim_left_string() {
|
||||
char* input = "adad_artist";
|
||||
char* expected = "_artist";
|
||||
char* result = strops_trim_left_string(input, "adad");
|
||||
if (strcmp(result, expected) != 0) {
|
||||
printf("test_trim_left_string failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_trim_both_string() {
|
||||
char* input = "NULL Bruh NULL";
|
||||
char* expected = " Bruh ";
|
||||
char* result = strops_trim_both_string(input, "NULL");
|
||||
if (strcmp(result, expected) != 0) {
|
||||
printf("test_trim_both_string failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_remove_chars() {
|
||||
char* input = "banana";
|
||||
char* expected = "bnn";
|
||||
char* result = strops_remove_chars(input, "a");
|
||||
if (strcmp(result, expected) != 0) {
|
||||
printf("test_remove_chars failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_remove_string() {
|
||||
char* input = "I hate you";
|
||||
char* expected = "I you";
|
||||
char* result = strops_remove_string(input, "hate");
|
||||
if (strcmp(result, expected) != 0) {
|
||||
printf("test_remove_string failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_word_count() {
|
||||
char* input = "Hello, World!";
|
||||
size_t expected = 2;
|
||||
size_t result = strops_word_count(input);
|
||||
if (result != expected) {
|
||||
printf("test_word_count failed\n");
|
||||
printf("Got = %ld\nExpected = %ld\n", result, expected);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_url_encode() {
|
||||
char* input = "(artist)";
|
||||
char* expected = "%28artist%29";
|
||||
char* result = strops_url_encode(input);
|
||||
if (strcmp(result, expected) != 0) {
|
||||
printf("test_url_encode failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_url_decode() {
|
||||
char* input = "%28artist%29";
|
||||
char* expected = "(artist)";
|
||||
char* result = strops_url_decode(input);
|
||||
if (strcmp(result, expected) != 0) {
|
||||
printf("test_url_decode failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
}
|
||||
@ -76,7 +220,7 @@ int test_trim_both() {
|
||||
}
|
||||
|
||||
int main() {
|
||||
int amount_of_tests = 5;
|
||||
int amount_of_tests = 17;
|
||||
int amount_of_successful_tests = 0;
|
||||
int ret;
|
||||
|
||||
@ -97,17 +241,77 @@ int main() {
|
||||
amount_of_successful_tests++;
|
||||
}
|
||||
|
||||
ret = test_trim_right();
|
||||
ret = test_remove_at_pos();
|
||||
if (ret == 0) {
|
||||
amount_of_successful_tests++;
|
||||
}
|
||||
|
||||
ret = test_trim_left();
|
||||
ret = test_trim_right_whitespace();
|
||||
if (ret == 0) {
|
||||
amount_of_successful_tests++;
|
||||
}
|
||||
|
||||
ret = test_trim_both();
|
||||
ret = test_trim_left_whitespace();
|
||||
if (ret == 0) {
|
||||
amount_of_successful_tests++;
|
||||
}
|
||||
|
||||
ret = test_trim_both_whitespace();
|
||||
if (ret == 0) {
|
||||
amount_of_successful_tests++;
|
||||
}
|
||||
|
||||
ret = test_trim_right_chars();
|
||||
if (ret == 0) {
|
||||
amount_of_successful_tests++;
|
||||
}
|
||||
|
||||
ret = test_trim_left_chars();
|
||||
if (ret == 0) {
|
||||
amount_of_successful_tests++;
|
||||
}
|
||||
|
||||
ret = test_trim_both_chars();
|
||||
if (ret == 0) {
|
||||
amount_of_successful_tests++;
|
||||
}
|
||||
|
||||
ret = test_trim_right_string();
|
||||
if (ret == 0) {
|
||||
amount_of_successful_tests++;
|
||||
}
|
||||
|
||||
ret = test_trim_left_string();
|
||||
if (ret == 0) {
|
||||
amount_of_successful_tests++;
|
||||
}
|
||||
|
||||
ret = test_trim_both_string();
|
||||
if (ret == 0) {
|
||||
amount_of_successful_tests++;
|
||||
}
|
||||
|
||||
ret = test_remove_chars();
|
||||
if (ret == 0) {
|
||||
amount_of_successful_tests++;
|
||||
}
|
||||
|
||||
ret = test_remove_string();
|
||||
if (ret == 0) {
|
||||
amount_of_successful_tests++;
|
||||
}
|
||||
|
||||
ret = test_word_count();
|
||||
if (ret == 0) {
|
||||
amount_of_successful_tests++;
|
||||
}
|
||||
|
||||
ret = test_url_encode();
|
||||
if (ret == 0) {
|
||||
amount_of_successful_tests++;
|
||||
}
|
||||
|
||||
ret = test_url_decode();
|
||||
if (ret == 0) {
|
||||
amount_of_successful_tests++;
|
||||
}
|
||||
|
Reference in New Issue
Block a user