327 lines
8.1 KiB
C
327 lines
8.1 KiB
C
#include "strops.h"
|
|
#include <stdio.h>
|
|
|
|
/* Test template (Maybe make a macro in the future?)
|
|
int test_() {
|
|
char* input = "";
|
|
char* expected = "";
|
|
char* result = strops_(input);
|
|
if (strcmp(result, expected) != 0) {
|
|
printf("test_ failed\n");
|
|
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
*/
|
|
|
|
int test_to_lowercase() {
|
|
char* input = "sTrInG";
|
|
char* expected = "string";
|
|
char* 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;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int test_to_uppercase() {
|
|
char* input = "sTrInG";
|
|
char* expected = "STRING";
|
|
char* 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;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
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("\ntest_trim_right_whitespace failed\n");
|
|
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
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("\ntest_trim_left_whitespace failed\n");
|
|
printf("Got = '%s'\nExpected = '%s'\n", result, expected);
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
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("\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 = "NULLNULL Bruh NULLNULL";
|
|
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;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int main() {
|
|
int amount_of_tests = 17;
|
|
int amount_of_successful_tests = 0;
|
|
int ret;
|
|
|
|
/* Test template
|
|
ret = test_();
|
|
if (ret == 0) {
|
|
amount_of_successful_tests++;
|
|
}
|
|
*/
|
|
|
|
ret = test_to_lowercase();
|
|
if (ret == 0) {
|
|
amount_of_successful_tests++;
|
|
}
|
|
|
|
ret = test_to_uppercase();
|
|
if (ret == 0) {
|
|
amount_of_successful_tests++;
|
|
}
|
|
|
|
ret = test_remove_at_pos();
|
|
if (ret == 0) {
|
|
amount_of_successful_tests++;
|
|
}
|
|
|
|
ret = test_trim_right_whitespace();
|
|
if (ret == 0) {
|
|
amount_of_successful_tests++;
|
|
}
|
|
|
|
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++;
|
|
}
|
|
|
|
if (amount_of_successful_tests != amount_of_tests) {
|
|
printf("%d/%d tests passed successfully\n", amount_of_successful_tests, amount_of_tests);
|
|
return 1;
|
|
} else {
|
|
printf("All tests passed successfully\n");
|
|
return 0;
|
|
}
|
|
}
|