progress on url and new stuff
This commit is contained in:
48
strops.c
48
strops.c
@ -76,14 +76,25 @@ 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) {
|
||||
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));
|
||||
result[pos] = 0;
|
||||
size_t i;
|
||||
for (i = pos; i < strlen(string); i++) {
|
||||
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;
|
||||
}
|
||||
|
||||
@ -223,7 +234,7 @@ char* strops_remove_string(const char* string, const char* string_to_remove) {
|
||||
size_t strops_word_count(const char* string) {
|
||||
size_t word_count = 0;
|
||||
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 beginning_of_word = 0;
|
||||
@ -262,23 +273,48 @@ size_t strops_word_count(const char* string) {
|
||||
return word_count;
|
||||
}
|
||||
|
||||
/* TODO: verify that this is correct */
|
||||
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) {
|
||||
if (strops_is_url_encoded(string)) { return NULL; }
|
||||
/* 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));
|
||||
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));
|
||||
return result;
|
||||
}
|
||||
|
||||
char* strops_url_decode(const char* string) {
|
||||
if (!strops_is_url_encoded(string)) { return NULL; }
|
||||
char* result = malloc(strlen(string) + 1);
|
||||
memcpy(result, string, strlen(string));
|
||||
|
||||
result = realloc(result, strlen(result));
|
||||
return result;
|
||||
}
|
||||
|
16
strops.h
16
strops.h
@ -4,7 +4,8 @@
|
||||
#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.
|
||||
No function modifies the input string. The input string is copied at the beginning of a function.
|
||||
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_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_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);
|
||||
|
||||
/* https://www.w3schools.com/tags/ref_urlencode.asp */
|
||||
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_decode(const char* string);
|
||||
|
||||
#endif /* STROPS_H */
|
||||
|
||||
|
430
tests.c
430
tests.c
@ -1,6 +1,7 @@
|
||||
#include "strops.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <execinfo.h>
|
||||
|
||||
/* https://gist.githubusercontent.com/rexim/b5b0c38f53157037923e7cdd77ce685d/raw/86c3db57f485f3b6f7958f308ba7126fa81282d8/da_append.c */
|
||||
#define da_append(xs, x) \
|
||||
@ -22,238 +23,394 @@ typedef struct {
|
||||
size_t amount_successful;
|
||||
} Tests;
|
||||
|
||||
/* Test template (Maybe make a macro in the future?)
|
||||
/* Test templates (Maybe make a macro in the future?)
|
||||
int test_() {
|
||||
char* input = "";
|
||||
char* expected = "";
|
||||
char* result = strops_(input);
|
||||
int ret = 1;
|
||||
char* input;
|
||||
char* expected;
|
||||
char* result;
|
||||
input = "";
|
||||
expected = "";
|
||||
result = strops_(input);
|
||||
if (strcmp(result, expected) != 0) {
|
||||
printf("test_ failed\n");
|
||||
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() {
|
||||
char* input = "sTrInG";
|
||||
char* expected = "string";
|
||||
char* result = strops_to_lowercase(input);
|
||||
int ret = 1;
|
||||
char* input;
|
||||
char* expected;
|
||||
char* result;
|
||||
input = "sTrInG";
|
||||
expected = "string";
|
||||
result = strops_to_lowercase(input);
|
||||
if (strcmp(result, expected) != 0) {
|
||||
printf("test_to_lowercase failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
ret = 0;
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int test_to_uppercase() {
|
||||
char* input = "sTrInG";
|
||||
char* expected = "STRING";
|
||||
char* result = strops_to_uppercase(input);
|
||||
int ret = 1;
|
||||
char* input;
|
||||
char* expected;
|
||||
char* result;
|
||||
input = "sTrInG";
|
||||
expected = "STRING";
|
||||
result = strops_to_uppercase(input);
|
||||
if (strcmp(result, expected) != 0) {
|
||||
printf("test_to_uppercase failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
ret = 0;
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int test_is_lowercase() {
|
||||
char* input = "string";
|
||||
int expected = 1;
|
||||
int result = strops_is_lowercase(input);
|
||||
int ret = 1;
|
||||
char* input;
|
||||
int expected;
|
||||
int result;
|
||||
input = "string";
|
||||
expected = 1;
|
||||
result = strops_is_lowercase(input);
|
||||
if (result != expected) {
|
||||
printf("test_is_lowercase failed\n");
|
||||
printf("Got = %d\nExpected = %d\n", result, expected);
|
||||
return 1;
|
||||
ret = 0;
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int test_is_uppercase() {
|
||||
char* input = "sTrInG";
|
||||
int expected = 0;
|
||||
int result = strops_is_uppercase(input);
|
||||
int ret = 1;
|
||||
char* input;
|
||||
int expected;
|
||||
int result;
|
||||
input = "sTrInG";
|
||||
expected = 0;
|
||||
result = strops_is_uppercase(input);
|
||||
if (result != expected) {
|
||||
printf("test_is_uppercase failed\n");
|
||||
printf("Got = %d\nExpected = %d\n", result, expected);
|
||||
return 1;
|
||||
ret = 0;
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int test_insert_at_pos_string() {
|
||||
char* input = "Heo, World!";
|
||||
char* expected = "Hello, World!";
|
||||
char* result = strops_insert_at_pos_string(input, "ll", 2);
|
||||
int ret = 1;
|
||||
char* input;
|
||||
char* expected;
|
||||
char* result;
|
||||
input = "Heo, World!";
|
||||
expected = "Hello, World!";
|
||||
result = strops_insert_at_pos_string(input, "ll", 2);
|
||||
if (strcmp(result, expected) != 0) {
|
||||
printf("test_insert_at_pos_string failed\n");
|
||||
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() {
|
||||
char* input = "Hello";
|
||||
char* expected = "Hllo";
|
||||
char* result = strops_remove_at_pos_char(input, 1);
|
||||
int ret = 1;
|
||||
char* input;
|
||||
char* expected;
|
||||
char* result;
|
||||
input = "Hello";
|
||||
expected = "Hllo";
|
||||
result = strops_remove_at_pos_char(input, 1);
|
||||
if (strcmp(result, expected) != 0) {
|
||||
printf("test_remove_at_pos_char failed\n");
|
||||
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() {
|
||||
char* input = "String \t\f\v\r\n ";
|
||||
char* expected = "String";
|
||||
char* result = strops_trim_right_whitespace(input);
|
||||
int ret = 1;
|
||||
char* 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) {
|
||||
printf("\ntest_trim_right_whitespace failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
ret = 0;
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int test_trim_left_whitespace() {
|
||||
char* input = " \t\f\v\r\n String";
|
||||
char* expected = "String";
|
||||
char* result = strops_trim_left_whitespace(input);
|
||||
int ret = 1;
|
||||
char* 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) {
|
||||
printf("\ntest_trim_left_whitespace failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
ret = 0;
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int test_trim_both_whitespace() {
|
||||
char* input = " \t\f\v String \r\n ";
|
||||
char* expected = "String";
|
||||
char* result = strops_trim_both_whitespace(input);
|
||||
int ret = 1;
|
||||
char* 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) {
|
||||
printf("\ntest_trim_both_whitespace failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
ret = 0;
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int test_trim_right_chars() {
|
||||
char* input = "banana";
|
||||
char* expected = "b";
|
||||
char* result = strops_trim_right_chars(input, "na");
|
||||
int ret = 1;
|
||||
char* input;
|
||||
char* expected;
|
||||
char* result;
|
||||
input = "banana";
|
||||
expected = "b";
|
||||
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;
|
||||
ret = 0;
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int test_trim_left_chars() {
|
||||
char* input = "banana";
|
||||
char* expected = "nana";
|
||||
char* result = strops_trim_left_chars(input, "ba");
|
||||
int ret = 1;
|
||||
char* input;
|
||||
char* expected;
|
||||
char* result;
|
||||
input = "banana";
|
||||
expected = "nana";
|
||||
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;
|
||||
ret = 0;
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int test_trim_both_chars() {
|
||||
char* input = "anna";
|
||||
char* expected = "nn";
|
||||
char* result = strops_trim_both_chars(input, "a");
|
||||
int ret = 1;
|
||||
char* input;
|
||||
char* expected;
|
||||
char* result;
|
||||
input = "anna";
|
||||
expected = "nn";
|
||||
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;
|
||||
ret = 0;
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int test_trim_right_string() {
|
||||
char* input = "adad_artist";
|
||||
char* expected = "adad_";
|
||||
char* result = strops_trim_right_string(input, "artist");
|
||||
int ret = 1;
|
||||
char* input;
|
||||
char* expected;
|
||||
char* result;
|
||||
input = "adad_artist";
|
||||
expected = "adad_";
|
||||
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;
|
||||
ret = 0;
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int test_trim_left_string() {
|
||||
char* input = "adad_artist";
|
||||
char* expected = "_artist";
|
||||
char* result = strops_trim_left_string(input, "adad");
|
||||
int ret = 1;
|
||||
char* input;
|
||||
char* expected;
|
||||
char* result;
|
||||
input = "adad_artist";
|
||||
expected = "_artist";
|
||||
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;
|
||||
ret = 0;
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int test_trim_both_string() {
|
||||
char* input = "NULLNULL Bruh NULLNULL";
|
||||
char* expected = " Bruh ";
|
||||
char* result = strops_trim_both_string(input, "NULL");
|
||||
int ret = 1;
|
||||
char* input;
|
||||
char* expected;
|
||||
char* result;
|
||||
input = "NULLNULL Bruh NULLNULL";
|
||||
expected = " Bruh ";
|
||||
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;
|
||||
ret = 0;
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int test_remove_chars() {
|
||||
char* input = "banana";
|
||||
char* expected = "bnn";
|
||||
char* result = strops_remove_chars(input, "a");
|
||||
int ret = 1;
|
||||
char* input;
|
||||
char* expected;
|
||||
char* result;
|
||||
input = "banana";
|
||||
expected = "bnn";
|
||||
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;
|
||||
ret = 0;
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int test_remove_string() {
|
||||
char* input = "I hate you";
|
||||
char* expected = "I you";
|
||||
char* result = strops_remove_string(input, "hate");
|
||||
int ret = 1;
|
||||
char* input;
|
||||
char* expected;
|
||||
char* result;
|
||||
input = "I hate you";
|
||||
expected = "I you";
|
||||
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;
|
||||
ret = 0;
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int test_word_count() {
|
||||
char* input = "Hello, World!";
|
||||
size_t expected = 2;
|
||||
size_t result = strops_word_count(input);
|
||||
int ret = 1;
|
||||
char* input;
|
||||
size_t expected;
|
||||
size_t result;
|
||||
input = "Hello, World!";
|
||||
expected = 2;
|
||||
result = strops_word_count(input);
|
||||
if (result != expected) {
|
||||
printf("test_word_count failed\n");
|
||||
printf("Got = %ld\nExpected = %ld\n", result, expected);
|
||||
return 1;
|
||||
ret = 0;
|
||||
}
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int test_is_url_encoded() {
|
||||
int ret = 0;
|
||||
int ret = 1;
|
||||
char* input;
|
||||
int expected;
|
||||
int result;
|
||||
@ -264,7 +421,7 @@ int test_is_url_encoded() {
|
||||
if (result != expected) {
|
||||
printf("test_is_url_encoded failed\n");
|
||||
printf("Got = %d\nExpected = %d\n", result, expected);
|
||||
ret = 1;
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
input = "%28artist%29";
|
||||
@ -273,66 +430,117 @@ int test_is_url_encoded() {
|
||||
if (result != expected) {
|
||||
printf("test_is_url_encoded failed\n");
|
||||
printf("Got = %d\nExpected = %d\n", result, expected);
|
||||
ret = 1;
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int test_url_encode() {
|
||||
char* input = "(artist)";
|
||||
char* expected = "%28artist%29";
|
||||
char* result = strops_url_encode(input);
|
||||
int ret = 1;
|
||||
char* 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) {
|
||||
printf("test_url_encode failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
ret = 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() {
|
||||
char* input = "%28artist%29";
|
||||
char* expected = "(artist)";
|
||||
char* result = strops_url_decode(input);
|
||||
int ret = 1;
|
||||
char* 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) {
|
||||
printf("test_url_decode failed\n");
|
||||
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
||||
return 1;
|
||||
ret = 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() {
|
||||
Tests tests = { 0 };
|
||||
da_append(&tests, test_to_lowercase);
|
||||
|
||||
da_append(&tests, test_to_lowercase);
|
||||
da_append(&tests, test_to_uppercase);
|
||||
da_append(&tests, test_is_lowercase);
|
||||
da_append(&tests, test_is_uppercase);
|
||||
|
||||
da_append(&tests, test_insert_at_pos_string);
|
||||
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_left_whitespace);
|
||||
da_append(&tests, test_trim_both_whitespace);
|
||||
|
||||
da_append(&tests, test_trim_right_chars);
|
||||
da_append(&tests, test_trim_left_chars);
|
||||
da_append(&tests, test_trim_both_chars);
|
||||
|
||||
da_append(&tests, test_trim_right_string);
|
||||
da_append(&tests, test_trim_left_string);
|
||||
da_append(&tests, test_trim_both_string);
|
||||
|
||||
da_append(&tests, test_remove_chars);
|
||||
da_append(&tests, test_remove_string);
|
||||
|
||||
da_append(&tests, test_word_count);
|
||||
|
||||
da_append(&tests, test_is_url_encoded);
|
||||
da_append(&tests, test_url_encode);
|
||||
da_append(&tests, test_url_decode);
|
||||
|
||||
size_t i;
|
||||
for (i = 0; i < tests.count; i++) {
|
||||
if (tests.items[i]() == 0) {
|
||||
tests.amount_successful++;
|
||||
}
|
||||
tests.amount_successful += tests.items[i]();
|
||||
}
|
||||
|
||||
if (tests.amount_successful != tests.count) {
|
||||
|
Reference in New Issue
Block a user