progress on url and new stuff
This commit is contained in:
436
tests.c
436
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,69 +430,120 @@ 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;
|
||||
}
|
||||
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() {
|
||||
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;
|
||||
}
|
||||
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() {
|
||||
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) {
|
||||
if (tests.amount_successful != tests.count) {
|
||||
printf("%d/%d tests passed successfully\n", tests.amount_successful, tests.count);
|
||||
return 1;
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user