progress on url and new stuff

This commit is contained in:
2025-05-09 00:10:40 +02:00
parent fd30b35d10
commit 28a235b680
3 changed files with 397 additions and 141 deletions

View File

@ -7,14 +7,14 @@
/* Function template /* Function template
char* strops_(const char* string) { char* strops_(const char* string) {
char *result = malloc(strlen(string)); char* result = malloc(strlen(string));
memcpy(result, string, strlen(string)); memcpy(result, string, strlen(string));
return result; return result;
} }
*/ */
char* strops_to_lowercase(const char* string) { char* strops_to_lowercase(const char* string) {
char *result = malloc(strlen(string)); char* result = malloc(strlen(string));
memcpy(result, string, strlen(string)); memcpy(result, string, strlen(string));
size_t i; size_t i;
for (i = 0; i < strlen(string); i++) { for (i = 0; i < strlen(string); i++) {
@ -26,7 +26,7 @@ char* strops_to_lowercase(const char* string) {
} }
char* strops_to_uppercase(const char* string) { char* strops_to_uppercase(const char* string) {
char *result = malloc(strlen(string)); char* result = malloc(strlen(string));
memcpy(result, string, strlen(string)); memcpy(result, string, strlen(string));
size_t i; size_t i;
for (i = 0; i < strlen(string); i++) { for (i = 0; i < strlen(string); i++) {
@ -60,7 +60,7 @@ int strops_is_uppercase(const char* string) {
char* strops_insert_at_pos_string(const char* string, const char* string_to_insert, size_t pos) { char* strops_insert_at_pos_string(const char* string, const char* string_to_insert, size_t pos) {
assert(pos <= strlen(string) && "pos needs to be inside string"); assert(pos <= strlen(string) && "pos needs to be inside string");
size_t string_length = strlen(string) + strlen(string_to_insert) + 1; size_t string_length = strlen(string) + strlen(string_to_insert) + 1;
char *result = malloc(string_length); char* result = malloc(string_length);
memcpy(result, string, strlen(string)); memcpy(result, string, strlen(string));
size_t i, j; size_t i, j;
/* Make space for string_to_insert */ /* Make space for string_to_insert */
@ -76,19 +76,30 @@ char* strops_insert_at_pos_string(const char* string, const char* string_to_inse
char* strops_remove_at_pos_char(const char* string, size_t pos) { char* strops_remove_at_pos_char(const char* string, size_t pos) {
assert(pos <= strlen(string) && "pos needs to be inside string"); assert(pos <= strlen(string) && "pos needs to be inside string");
char *result = malloc(strlen(string)); char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string)); memcpy(result, string, strlen(string));
result[pos] = 0; result[pos] = 0;
size_t i; size_t i;
for (i = pos; i < strlen(string); i++) { for (i = pos; i < strlen(string); i++) {
result[i] = result[i + 1]; result[i] = result[i + 1];
} }
result = realloc(result, strlen(result)); return result;
}
char* strops_remove_at_pos_string(const char* string, const char* string_to_remove, size_t pos) {
char* result = malloc(strlen(string));
memcpy(result, string, strlen(string));
return result;
}
char* strops_replace_at_pos_string(const char* string, const char* string_to_remove, const char* string_to_insert, size_t pos) {
char* result = malloc(strlen(string));
memcpy(result, string, strlen(string));
return result; return result;
} }
char* strops_trim_right_whitespace(const char* string) { char* strops_trim_right_whitespace(const char* string) {
char *result = malloc(strlen(string) + 1); char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string)); memcpy(result, string, strlen(string));
while (strchr("\t\n\v\f\r ", result[strlen(result) - 1]) != NULL) { while (strchr("\t\n\v\f\r ", result[strlen(result) - 1]) != NULL) {
result = strops_remove_at_pos_char(result, strlen(result) - 1); result = strops_remove_at_pos_char(result, strlen(result) - 1);
@ -98,7 +109,7 @@ char* strops_trim_right_whitespace(const char* string) {
} }
char* strops_trim_left_whitespace(const char* string) { char* strops_trim_left_whitespace(const char* string) {
char *result = malloc(strlen(string) + 1); char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string)); memcpy(result, string, strlen(string));
while (strchr("\t\n\v\f\r ", result[0]) != NULL) { while (strchr("\t\n\v\f\r ", result[0]) != NULL) {
result = strops_remove_at_pos_char(result, 0); result = strops_remove_at_pos_char(result, 0);
@ -108,7 +119,7 @@ char* strops_trim_left_whitespace(const char* string) {
} }
char* strops_trim_both_whitespace(const char* string) { char* strops_trim_both_whitespace(const char* string) {
char *result = malloc(strlen(string) + 1); char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string)); memcpy(result, string, strlen(string));
result = strops_trim_right_whitespace(result); result = strops_trim_right_whitespace(result);
result = strops_trim_left_whitespace(result); result = strops_trim_left_whitespace(result);
@ -116,7 +127,7 @@ char* strops_trim_both_whitespace(const char* string) {
} }
char* strops_trim_right_chars(const char* string, const char* chars_to_remove) { char* strops_trim_right_chars(const char* string, const char* chars_to_remove) {
char *result = malloc(strlen(string) + 1); char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string)); memcpy(result, string, strlen(string));
while (strchr(chars_to_remove, result[strlen(result) - 1]) != NULL) { while (strchr(chars_to_remove, result[strlen(result) - 1]) != NULL) {
result = strops_remove_at_pos_char(result, strlen(result) - 1); result = strops_remove_at_pos_char(result, strlen(result) - 1);
@ -126,7 +137,7 @@ char* strops_trim_right_chars(const char* string, const char* chars_to_remove) {
} }
char* strops_trim_left_chars(const char* string, const char* chars_to_remove) { char* strops_trim_left_chars(const char* string, const char* chars_to_remove) {
char *result = malloc(strlen(string) + 1); char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string)); memcpy(result, string, strlen(string));
while (strchr(chars_to_remove, result[0]) != NULL) { while (strchr(chars_to_remove, result[0]) != NULL) {
result = strops_remove_at_pos_char(result, 0); result = strops_remove_at_pos_char(result, 0);
@ -136,7 +147,7 @@ char* strops_trim_left_chars(const char* string, const char* chars_to_remove) {
} }
char* strops_trim_both_chars(const char* string, const char* chars_to_remove) { char* strops_trim_both_chars(const char* string, const char* chars_to_remove) {
char *result = malloc(strlen(string) + 1); char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string)); memcpy(result, string, strlen(string));
result = strops_trim_right_chars(result, chars_to_remove); result = strops_trim_right_chars(result, chars_to_remove);
result = strops_trim_left_chars(result, chars_to_remove); result = strops_trim_left_chars(result, chars_to_remove);
@ -145,7 +156,7 @@ char* strops_trim_both_chars(const char* string, const char* chars_to_remove) {
char* strops_trim_right_string(const char* string, const char* string_to_remove) { char* strops_trim_right_string(const char* string, const char* string_to_remove) {
assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string"); assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string");
char *result = malloc(strlen(string) + 1); char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string)); memcpy(result, string, strlen(string));
size_t offset = strlen(result) - strlen(string_to_remove); size_t offset = strlen(result) - strlen(string_to_remove);
@ -164,10 +175,10 @@ char* strops_trim_right_string(const char* string, const char* string_to_remove)
char* strops_trim_left_string(const char* string, const char* string_to_remove) { char* strops_trim_left_string(const char* string, const char* string_to_remove) {
assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string"); assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string");
char *result = malloc(strlen(string) + 1); char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string)); memcpy(result, string, strlen(string));
char *tmp = malloc(strlen(string_to_remove) + 1); char* tmp = malloc(strlen(string_to_remove) + 1);
memcpy(tmp, result, strlen(string_to_remove)); memcpy(tmp, result, strlen(string_to_remove));
while (strcmp(tmp, string_to_remove) == 0) { while (strcmp(tmp, string_to_remove) == 0) {
size_t i; size_t i;
@ -183,7 +194,7 @@ char* strops_trim_left_string(const char* string, const char* string_to_remove)
char* strops_trim_both_string(const char* string, const char* string_to_remove) { char* strops_trim_both_string(const char* string, const char* string_to_remove) {
assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string"); assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string");
char *result = malloc(strlen(string) + 1); char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string)); memcpy(result, string, strlen(string));
result = strops_trim_right_string(result, string_to_remove); result = strops_trim_right_string(result, string_to_remove);
result = strops_trim_left_string(result, string_to_remove); result = strops_trim_left_string(result, string_to_remove);
@ -191,7 +202,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_chars(const char* string, const char* chars_to_remove) {
char *result = malloc(strlen(string) + 1); char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string)); memcpy(result, string, strlen(string));
size_t i; size_t i;
@ -206,7 +217,7 @@ char* strops_remove_chars(const char* string, const char* chars_to_remove) {
char* strops_remove_string(const char* string, const char* string_to_remove) { char* strops_remove_string(const char* string, const char* string_to_remove) {
assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string"); assert(strlen(string) >= strlen(string_to_remove) && "string_to_remove cannot be bigger than string");
char *result = malloc(strlen(string) + 1); char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string)); memcpy(result, string, strlen(string));
while (strstr(result, string_to_remove) != NULL) { while (strstr(result, string_to_remove) != NULL) {
@ -222,8 +233,8 @@ char* strops_remove_string(const char* string, const char* string_to_remove) {
size_t strops_word_count(const char* string) { size_t strops_word_count(const char* string) {
size_t word_count = 0; size_t word_count = 0;
char *tmp = malloc(strlen(string) + 1); char* tmp = malloc(strlen(string) + 1);
memcpy(tmp, string, strlen(string)); memcpy(tmp, string, strlen(string));char* strpos_remove_at_pos_string(const char* string, const char* string_to_remove, size_t pos);
size_t i = 0; size_t i = 0;
size_t beginning_of_word = 0; size_t beginning_of_word = 0;
@ -262,23 +273,48 @@ size_t strops_word_count(const char* string) {
return word_count; return word_count;
} }
/* TODO: verify that this is correct */
int strops_is_url_encoded(const char* string) { int strops_is_url_encoded(const char* string) {
return -1; if (strchr(string, '%') != NULL) {
return 1;
}
return 0;
} }
char* strops_url_encode(const char* string) { char* strops_url_encode(const char* string) {
if (strops_is_url_encoded(string)) { return NULL; }
/* multiplied by 3, because example: ( => %28. Three times the chars */ /* multiplied by 3, because example: ( => %28. Three times the chars */
char *result = malloc(strlen(string) * 3 + 1); char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string)); memcpy(result, string, strlen(string));
char *unsafe_chars = ""; char* unsafe_chars = "()";
char replacement[4];
replacement[0] = '%';
replacement[3] = '\0';
size_t i, j;
for (i = 0; i < strlen(unsafe_chars); i++) {
j = 0;
while (strchr(result, unsafe_chars[i]) != NULL) {
if (result[j] == unsafe_chars[i]) {
char bad = result[j];
sprintf(replacement + 1, "%02X", bad);
result = strops_remove_at_pos_char(result, j);
result = strops_insert_at_pos_string(result, replacement, j);
j += 2;
} else {
j += 1;
}
}
}
result = realloc(result, strlen(result)); result = realloc(result, strlen(result));
return result; return result;
} }
char* strops_url_decode(const char* string) { char* strops_url_decode(const char* string) {
char *result = malloc(strlen(string) + 1); if (!strops_is_url_encoded(string)) { return NULL; }
char* result = malloc(strlen(string) + 1);
memcpy(result, string, strlen(string)); memcpy(result, string, strlen(string));
result = realloc(result, strlen(result)); result = realloc(result, strlen(result));
return result; return result;
} }

View File

@ -4,7 +4,8 @@
#include <stddef.h> #include <stddef.h>
/* /*
All functions return a new heap-allocated string to which the requested operation was applied to. ATTENTION! THIS LIBRARY CURRENTLY LEAKS MEMORY!
Functions that return char*, return a new heap-allocated string.
The user of this library is responsible for freeing the memory of the result. The user of this library is responsible for freeing the memory of the result.
No function modifies the input string. The input string is copied at the beginning of a function. No function modifies the input string. The input string is copied at the beginning of a function.
Only 7-Bit Ascii is supported. Only 7-Bit Ascii is supported.
@ -17,6 +18,8 @@ int strops_is_uppercase(const char* string);
char* strops_insert_at_pos_string(const char* string, const char* string_to_insert, size_t pos); char* strops_insert_at_pos_string(const char* string, const char* string_to_insert, size_t pos);
char* strops_remove_at_pos_char(const char* string, size_t pos); char* strops_remove_at_pos_char(const char* string, size_t pos);
char* strops_remove_at_pos_string(const char* string, const char* string_to_remove, size_t pos);
char* strops_replace_at_pos_string(const char* string, const char* string_to_remove, const char* string_to_insert, size_t pos);
char* strops_trim_right_whitespace(const char* string); char* strops_trim_right_whitespace(const char* string);
char* strops_trim_left_whitespace(const char* string); char* strops_trim_left_whitespace(const char* string);
@ -35,9 +38,18 @@ char* strops_remove_string(const char* string, const char* string_to_remove);
size_t strops_word_count(const char* string); size_t strops_word_count(const char* string);
/* https://www.w3schools.com/tags/ref_urlencode.asp */
int strops_is_url_encoded(const char* string); int strops_is_url_encoded(const char* string);
/*
https://www.w3schools.com/tags/ref_urlencode.asp
PLEASE NEVER USE THEM WITHOUT UNDERSTANDING THEIR ISSUES!
They will fully remove the following '%20 %21 %22 %23 %24 %26 %27 %28 %29 %2A %2C %2E %2F \
%3B %3C %3E %3F %5B %5C %5D %5E %60 %7B %7C %7D %7E'
These will get turned into their ascii counterpart '%25 %2B %2D %2E %2F %3A %3D %40 %5F'
They will return NULL on failure
*/
char* strops_url_encode(const char* string); char* strops_url_encode(const char* string);
char* strops_url_decode(const char* string); char* strops_url_decode(const char* string);
#endif /* STROPS_H */ #endif /* STROPS_H */

436
tests.c
View File

@ -1,6 +1,7 @@
#include "strops.h" #include "strops.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <execinfo.h>
/* https://gist.githubusercontent.com/rexim/b5b0c38f53157037923e7cdd77ce685d/raw/86c3db57f485f3b6f7958f308ba7126fa81282d8/da_append.c */ /* https://gist.githubusercontent.com/rexim/b5b0c38f53157037923e7cdd77ce685d/raw/86c3db57f485f3b6f7958f308ba7126fa81282d8/da_append.c */
#define da_append(xs, x) \ #define da_append(xs, x) \
@ -22,238 +23,394 @@ typedef struct {
size_t amount_successful; size_t amount_successful;
} Tests; } Tests;
/* Test template (Maybe make a macro in the future?) /* Test templates (Maybe make a macro in the future?)
int test_() { int test_() {
char* input = ""; int ret = 1;
char* expected = ""; char* input;
char* result = strops_(input); char* expected;
char* result;
input = "";
expected = "";
result = strops_(input);
if (strcmp(result, expected) != 0) { if (strcmp(result, expected) != 0) {
printf("test_ failed\n"); printf("test_ failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected); printf("Got = '%s'\nExpected = '%s'\n", result, expected);
return 1; ret = 0;
} }
return 0; return ret;
}
int test_() {
int ret = 1;
char* input;
int expected;
int result;
input = "";
expected = -1;
result = strops_(input);
if (result != expected) {
printf("test_ failed\n");
printf("Got = %d\nExpected = %d\n", result, expected);
ret = 0;
}
return ret;
} }
*/ */
int test_to_lowercase() { int test_to_lowercase() {
char* input = "sTrInG"; int ret = 1;
char* expected = "string"; char* input;
char* result = strops_to_lowercase(input); char* expected;
char* result;
input = "sTrInG";
expected = "string";
result = strops_to_lowercase(input);
if (strcmp(result, expected) != 0) { if (strcmp(result, expected) != 0) {
printf("test_to_lowercase failed\n"); printf("test_to_lowercase failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected); printf("Got = '%s'\nExpected = '%s'\n", result, expected);
return 1; ret = 0;
} }
return 0; return ret;
} }
int test_to_uppercase() { int test_to_uppercase() {
char* input = "sTrInG"; int ret = 1;
char* expected = "STRING"; char* input;
char* result = strops_to_uppercase(input); char* expected;
char* result;
input = "sTrInG";
expected = "STRING";
result = strops_to_uppercase(input);
if (strcmp(result, expected) != 0) { if (strcmp(result, expected) != 0) {
printf("test_to_uppercase failed\n"); printf("test_to_uppercase failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected); printf("Got = '%s'\nExpected = '%s'\n", result, expected);
return 1; ret = 0;
} }
return 0; return ret;
} }
int test_is_lowercase() { int test_is_lowercase() {
char* input = "string"; int ret = 1;
int expected = 1; char* input;
int result = strops_is_lowercase(input); int expected;
int result;
input = "string";
expected = 1;
result = strops_is_lowercase(input);
if (result != expected) { if (result != expected) {
printf("test_is_lowercase failed\n"); printf("test_is_lowercase failed\n");
printf("Got = %d\nExpected = %d\n", result, expected); printf("Got = %d\nExpected = %d\n", result, expected);
return 1; ret = 0;
} }
return 0; return ret;
} }
int test_is_uppercase() { int test_is_uppercase() {
char* input = "sTrInG"; int ret = 1;
int expected = 0; char* input;
int result = strops_is_uppercase(input); int expected;
int result;
input = "sTrInG";
expected = 0;
result = strops_is_uppercase(input);
if (result != expected) { if (result != expected) {
printf("test_is_uppercase failed\n"); printf("test_is_uppercase failed\n");
printf("Got = %d\nExpected = %d\n", result, expected); printf("Got = %d\nExpected = %d\n", result, expected);
return 1; ret = 0;
} }
return 0; return ret;
} }
int test_insert_at_pos_string() { int test_insert_at_pos_string() {
char* input = "Heo, World!"; int ret = 1;
char* expected = "Hello, World!"; char* input;
char* result = strops_insert_at_pos_string(input, "ll", 2); char* expected;
char* result;
input = "Heo, World!";
expected = "Hello, World!";
result = strops_insert_at_pos_string(input, "ll", 2);
if (strcmp(result, expected) != 0) { if (strcmp(result, expected) != 0) {
printf("test_insert_at_pos_string failed\n"); printf("test_insert_at_pos_string failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected); printf("Got = '%s'\nExpected = '%s'\n", result, expected);
return 1; ret = 0;
} }
return 0; input = "llo";
expected = "Hello";
result = strops_insert_at_pos_string(input, "He", 0);
if (strcmp(result, expected) != 0) {
printf("test_insert_at_pos_string failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
ret = 0;
}
input = "He";
expected = "Hello";
result = strops_insert_at_pos_string(input, "llo", 2);
if (strcmp(result, expected) != 0) {
printf("test_insert_at_pos_string failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
ret = 0;
}
return ret;
} }
int test_remove_at_pos_char() { int test_remove_at_pos_char() {
char* input = "Hello"; int ret = 1;
char* expected = "Hllo"; char* input;
char* result = strops_remove_at_pos_char(input, 1); char* expected;
char* result;
input = "Hello";
expected = "Hllo";
result = strops_remove_at_pos_char(input, 1);
if (strcmp(result, expected) != 0) { if (strcmp(result, expected) != 0) {
printf("test_remove_at_pos_char failed\n"); printf("test_remove_at_pos_char failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected); printf("Got = '%s'\nExpected = '%s'\n", result, expected);
return 1; ret = 0;
} }
return 0; input = "_adad";
expected = "adad";
result = strops_remove_at_pos_char(input, 0);
if (strcmp(result, expected) != 0) {
printf("test_remove_at_pos_char failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
ret = 0;
}
input = "adad_";
expected = "adad";
result = strops_remove_at_pos_char(input, 4);
if (strcmp(result, expected) != 0) {
printf("test_remove_at_pos_char failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
ret = 0;
}
return ret;
}
int test_remove_at_pos_string() {
int ret = 1;
char* input;
char* expected;
char* result;
input = "NULL BRUH NULL";
expected = "NULL NULL";
result = strops_remove_at_pos_string(input, " BRUH", 4);
if (strcmp(result, expected) != 0) {
printf("test_remove_at_pos_string failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
ret = 0;
}
return ret;
}
int test_replace_at_pos_string() {
int ret = 1;
char* input;
char* expected;
char* result;
input = "What happened in Tiananmen Square in 1989";
expected = "Nothing happened in Tiananmen Square in 1989";
result = strops_replace_at_pos_string(input, "What", "Nothing", 0);
if (strcmp(result, expected) != 0) {
printf("test_replace_at_pos_string failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
ret = 0;
}
return ret;
} }
int test_trim_right_whitespace() { int test_trim_right_whitespace() {
char* input = "String \t\f\v\r\n "; int ret = 1;
char* expected = "String"; char* input;
char* result = strops_trim_right_whitespace(input); char* expected;
char* result;
input = "String \t\f\v\r\n ";
expected = "String";
result = strops_trim_right_whitespace(input);
if (strcmp(result, expected) != 0) { if (strcmp(result, expected) != 0) {
printf("\ntest_trim_right_whitespace failed\n"); printf("\ntest_trim_right_whitespace failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected); printf("Got = '%s'\nExpected = '%s'\n", result, expected);
return 1; ret = 0;
} }
return 0; return ret;
} }
int test_trim_left_whitespace() { int test_trim_left_whitespace() {
char* input = " \t\f\v\r\n String"; int ret = 1;
char* expected = "String"; char* input;
char* result = strops_trim_left_whitespace(input); char* expected;
char* result;
input = " \t\f\v\r\n String";
expected = "String";
result = strops_trim_left_whitespace(input);
if (strcmp(result, expected) != 0) { if (strcmp(result, expected) != 0) {
printf("\ntest_trim_left_whitespace failed\n"); printf("\ntest_trim_left_whitespace failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected); printf("Got = '%s'\nExpected = '%s'\n", result, expected);
return 1; ret = 0;
} }
return 0; return ret;
} }
int test_trim_both_whitespace() { int test_trim_both_whitespace() {
char* input = " \t\f\v String \r\n "; int ret = 1;
char* expected = "String"; char* input;
char* result = strops_trim_both_whitespace(input); char* expected;
char* result;
input = " \t\f\v String \r\n ";
expected = "String";
result = strops_trim_both_whitespace(input);
if (strcmp(result, expected) != 0) { if (strcmp(result, expected) != 0) {
printf("\ntest_trim_both_whitespace failed\n"); printf("\ntest_trim_both_whitespace failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected); printf("Got = '%s'\nExpected = '%s'\n", result, expected);
return 1; ret = 0;
} }
return 0; return ret;
} }
int test_trim_right_chars() { int test_trim_right_chars() {
char* input = "banana"; int ret = 1;
char* expected = "b"; char* input;
char* result = strops_trim_right_chars(input, "na"); char* expected;
char* result;
input = "banana";
expected = "b";
result = strops_trim_right_chars(input, "na");
if (strcmp(result, expected) != 0) { if (strcmp(result, expected) != 0) {
printf("test_right_chars failed\n"); printf("test_right_chars failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected); printf("Got = '%s'\nExpected = '%s'\n", result, expected);
return 1; ret = 0;
} }
return 0; return ret;
} }
int test_trim_left_chars() { int test_trim_left_chars() {
char* input = "banana"; int ret = 1;
char* expected = "nana"; char* input;
char* result = strops_trim_left_chars(input, "ba"); char* expected;
char* result;
input = "banana";
expected = "nana";
result = strops_trim_left_chars(input, "ba");
if (strcmp(result, expected) != 0) { if (strcmp(result, expected) != 0) {
printf("test_left_chars failed\n"); printf("test_left_chars failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected); printf("Got = '%s'\nExpected = '%s'\n", result, expected);
return 1; ret = 0;
} }
return 0; return ret;
} }
int test_trim_both_chars() { int test_trim_both_chars() {
char* input = "anna"; int ret = 1;
char* expected = "nn"; char* input;
char* result = strops_trim_both_chars(input, "a"); char* expected;
char* result;
input = "anna";
expected = "nn";
result = strops_trim_both_chars(input, "a");
if (strcmp(result, expected) != 0) { if (strcmp(result, expected) != 0) {
printf("test_trim_both_chars failed\n"); printf("test_trim_both_chars failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected); printf("Got = '%s'\nExpected = '%s'\n", result, expected);
return 1; ret = 0;
} }
return 0; return ret;
} }
int test_trim_right_string() { int test_trim_right_string() {
char* input = "adad_artist"; int ret = 1;
char* expected = "adad_"; char* input;
char* result = strops_trim_right_string(input, "artist"); char* expected;
char* result;
input = "adad_artist";
expected = "adad_";
result = strops_trim_right_string(input, "artist");
if (strcmp(result, expected) != 0) { if (strcmp(result, expected) != 0) {
printf("test_trim_right_string failed\n"); printf("test_trim_right_string failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected); printf("Got = '%s'\nExpected = '%s'\n", result, expected);
return 1; ret = 0;
} }
return 0; return ret;
} }
int test_trim_left_string() { int test_trim_left_string() {
char* input = "adad_artist"; int ret = 1;
char* expected = "_artist"; char* input;
char* result = strops_trim_left_string(input, "adad"); char* expected;
char* result;
input = "adad_artist";
expected = "_artist";
result = strops_trim_left_string(input, "adad");
if (strcmp(result, expected) != 0) { if (strcmp(result, expected) != 0) {
printf("test_trim_left_string failed\n"); printf("test_trim_left_string failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected); printf("Got = '%s'\nExpected = '%s'\n", result, expected);
return 1; ret = 0;
} }
return 0; return ret;
} }
int test_trim_both_string() { int test_trim_both_string() {
char* input = "NULLNULL Bruh NULLNULL"; int ret = 1;
char* expected = " Bruh "; char* input;
char* result = strops_trim_both_string(input, "NULL"); char* expected;
char* result;
input = "NULLNULL Bruh NULLNULL";
expected = " Bruh ";
result = strops_trim_both_string(input, "NULL");
if (strcmp(result, expected) != 0) { if (strcmp(result, expected) != 0) {
printf("test_trim_both_string failed\n"); printf("test_trim_both_string failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected); printf("Got = '%s'\nExpected = '%s'\n", result, expected);
return 1; ret = 0;
} }
return 0; return ret;
} }
int test_remove_chars() { int test_remove_chars() {
char* input = "banana"; int ret = 1;
char* expected = "bnn"; char* input;
char* result = strops_remove_chars(input, "a"); char* expected;
char* result;
input = "banana";
expected = "bnn";
result = strops_remove_chars(input, "a");
if (strcmp(result, expected) != 0) { if (strcmp(result, expected) != 0) {
printf("test_remove_chars failed\n"); printf("test_remove_chars failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected); printf("Got = '%s'\nExpected = '%s'\n", result, expected);
return 1; ret = 0;
} }
return 0; return ret;
} }
int test_remove_string() { int test_remove_string() {
char* input = "I hate you"; int ret = 1;
char* expected = "I you"; char* input;
char* result = strops_remove_string(input, "hate"); char* expected;
char* result;
input = "I hate you";
expected = "I you";
result = strops_remove_string(input, "hate");
if (strcmp(result, expected) != 0) { if (strcmp(result, expected) != 0) {
printf("test_remove_string failed\n"); printf("test_remove_string failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected); printf("Got = '%s'\nExpected = '%s'\n", result, expected);
return 1; ret = 0;
} }
return 0; return ret;
} }
int test_word_count() { int test_word_count() {
char* input = "Hello, World!"; int ret = 1;
size_t expected = 2; char* input;
size_t result = strops_word_count(input); size_t expected;
size_t result;
input = "Hello, World!";
expected = 2;
result = strops_word_count(input);
if (result != expected) { if (result != expected) {
printf("test_word_count failed\n"); printf("test_word_count failed\n");
printf("Got = %ld\nExpected = %ld\n", result, expected); printf("Got = %ld\nExpected = %ld\n", result, expected);
return 1; ret = 0;
} }
return 0; return ret;
} }
int test_is_url_encoded() { int test_is_url_encoded() {
int ret = 0; int ret = 1;
char* input; char* input;
int expected; int expected;
int result; int result;
@ -264,7 +421,7 @@ int test_is_url_encoded() {
if (result != expected) { if (result != expected) {
printf("test_is_url_encoded failed\n"); printf("test_is_url_encoded failed\n");
printf("Got = %d\nExpected = %d\n", result, expected); printf("Got = %d\nExpected = %d\n", result, expected);
ret = 1; ret = 0;
} }
input = "%28artist%29"; input = "%28artist%29";
@ -273,69 +430,120 @@ int test_is_url_encoded() {
if (result != expected) { if (result != expected) {
printf("test_is_url_encoded failed\n"); printf("test_is_url_encoded failed\n");
printf("Got = %d\nExpected = %d\n", result, expected); printf("Got = %d\nExpected = %d\n", result, expected);
ret = 1; ret = 0;
} }
return ret; return ret;
} }
int test_url_encode() { int test_url_encode() {
char* input = "(artist)"; int ret = 1;
char* expected = "%28artist%29"; char* input;
char* result = strops_url_encode(input); char* expected;
char* result;
input = "(artist)";
expected = "%28artist%29";
result = strops_url_encode(input);
if (result == NULL) {
printf("test_url_encode failed\n");
printf("Got = NULL\nExpected = '%s'\n", expected);
return 0;
}
if (strcmp(result, expected) != 0) { if (strcmp(result, expected) != 0) {
printf("test_url_encode failed\n"); printf("test_url_encode failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected); printf("Got = '%s'\nExpected = '%s'\n", result, expected);
return 1; ret = 0;
} }
return 0; input = "()";
expected = "%28%29";
result = strops_url_encode(input);
if (result == NULL) {
printf("test_url_encode failed\n");
printf("Got = NULL\nExpected = '%s'\n", expected);
return 0;
}
if (strcmp(result, expected) != 0) {
printf("test_url_encode failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
ret = 0;
}
return ret;
} }
int test_url_decode() { int test_url_decode() {
char* input = "%28artist%29"; int ret = 1;
char* expected = "(artist)"; char* input;
char* result = strops_url_decode(input); char* expected;
char* result;
input = "%28artist%29";
expected = "(artist)";
result = strops_url_decode(input);
if (result == NULL) {
printf("test_url_decode failed\n");
printf("Got = NULL\nExpected = '%s'\n", expected);
return 0;
}
if (strcmp(result, expected) != 0) { if (strcmp(result, expected) != 0) {
printf("test_url_decode failed\n"); printf("test_url_decode failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected); printf("Got = '%s'\nExpected = '%s'\n", result, expected);
return 1; ret = 0;
} }
return 0; input = "%28%29";
expected = "()";
result = strops_url_decode(input);
if (result == NULL) {
printf("test_url_decode failed\n");
printf("Got = NULL\nExpected = '%s'\n", expected);
return 0;
}
if (strcmp(result, expected) != 0) {
printf("test_url_decode failed\n");
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
ret = 0;
}
return ret;
} }
int main() { int main() {
Tests tests = { 0 }; Tests tests = { 0 };
da_append(&tests, test_to_lowercase);
da_append(&tests, test_to_lowercase); da_append(&tests, test_to_lowercase);
da_append(&tests, test_to_uppercase); da_append(&tests, test_to_uppercase);
da_append(&tests, test_is_lowercase); da_append(&tests, test_is_lowercase);
da_append(&tests, test_is_uppercase); da_append(&tests, test_is_uppercase);
da_append(&tests, test_insert_at_pos_string); da_append(&tests, test_insert_at_pos_string);
da_append(&tests, test_remove_at_pos_char); da_append(&tests, test_remove_at_pos_char);
da_append(&tests, test_remove_at_pos_string);
da_append(&tests, test_replace_at_pos_string);
da_append(&tests, test_trim_right_whitespace); da_append(&tests, test_trim_right_whitespace);
da_append(&tests, test_trim_left_whitespace); da_append(&tests, test_trim_left_whitespace);
da_append(&tests, test_trim_both_whitespace); da_append(&tests, test_trim_both_whitespace);
da_append(&tests, test_trim_right_chars); da_append(&tests, test_trim_right_chars);
da_append(&tests, test_trim_left_chars); da_append(&tests, test_trim_left_chars);
da_append(&tests, test_trim_both_chars); da_append(&tests, test_trim_both_chars);
da_append(&tests, test_trim_right_string); da_append(&tests, test_trim_right_string);
da_append(&tests, test_trim_left_string); da_append(&tests, test_trim_left_string);
da_append(&tests, test_trim_both_string); da_append(&tests, test_trim_both_string);
da_append(&tests, test_remove_chars); da_append(&tests, test_remove_chars);
da_append(&tests, test_remove_string); da_append(&tests, test_remove_string);
da_append(&tests, test_word_count); da_append(&tests, test_word_count);
da_append(&tests, test_is_url_encoded); da_append(&tests, test_is_url_encoded);
da_append(&tests, test_url_encode); da_append(&tests, test_url_encode);
da_append(&tests, test_url_decode); da_append(&tests, test_url_decode);
size_t i; size_t i;
for (i = 0; i < tests.count; i++) { for (i = 0; i < tests.count; i++) {
if (tests.items[i]() == 0) { tests.amount_successful += tests.items[i]();
tests.amount_successful++;
}
} }
if (tests.amount_successful != tests.count) { if (tests.amount_successful != tests.count) {
printf("%d/%d tests passed successfully\n", tests.amount_successful, tests.count); printf("%d/%d tests passed successfully\n", tests.amount_successful, tests.count);
return 1; return 1;
} else { } else {