456 lines
12 KiB
C
456 lines
12 KiB
C
#include "strops.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
/* https://gist.githubusercontent.com/rexim/b5b0c38f53157037923e7cdd77ce685d/raw/86c3db57f485f3b6f7958f308ba7126fa81282d8/da_append.c */
|
|
#define da_append(xs, x) \
|
|
do { \
|
|
if ((xs)->count >= (xs)->capacity) { \
|
|
if ((xs)->capacity == 0) (xs)->capacity = 256; \
|
|
else (xs)->capacity *= 2; \
|
|
(xs)->items = realloc((xs)->items, (xs)->capacity*sizeof(*(xs)->items)); \
|
|
} \
|
|
\
|
|
(xs)->items[(xs)->count++] = (x); \
|
|
} while (0)
|
|
|
|
typedef int(*fnptr)();
|
|
typedef struct {
|
|
fnptr* items;
|
|
size_t capacity;
|
|
size_t count;
|
|
size_t amount_successful;
|
|
} Tests;
|
|
|
|
/* Test templates (Maybe make a macro in the future?)
|
|
int test_() {
|
|
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);
|
|
ret = 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_contains_char() {
|
|
int ret = 1;
|
|
char* input;
|
|
char expected;
|
|
char result;
|
|
input = "string";
|
|
expected = 1;
|
|
result = strops_contains_char(input, 'i');
|
|
if (result != expected) {
|
|
printf("test_ failed\n");
|
|
printf("Got = %d\nExpected = %d\n", result, expected);
|
|
ret = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int test_contains_string() {
|
|
int ret = 1;
|
|
char* input;
|
|
char expected;
|
|
char result;
|
|
input = "I suck at C";
|
|
expected = 1;
|
|
result = strops_contains_string(input, "suck");
|
|
if (result != expected) {
|
|
printf("test_ failed\n");
|
|
printf("Got = %d\nExpected = %d\n", result, expected);
|
|
ret = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int test_to_lowercase() {
|
|
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);
|
|
ret = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int test_to_uppercase() {
|
|
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);
|
|
ret = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int test_is_lowercase() {
|
|
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);
|
|
ret = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int test_is_uppercase() {
|
|
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);
|
|
ret = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int test_insert_at_pos_string() {
|
|
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);
|
|
ret = 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 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);
|
|
ret = 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_trim_right_whitespace() {
|
|
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);
|
|
ret = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int test_trim_left_whitespace() {
|
|
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);
|
|
ret = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int test_trim_both_whitespace() {
|
|
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);
|
|
ret = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int test_trim_right_chars() {
|
|
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);
|
|
ret = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int test_trim_left_chars() {
|
|
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);
|
|
ret = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int test_trim_both_chars() {
|
|
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);
|
|
ret = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int test_trim_right_string() {
|
|
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);
|
|
ret = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int test_trim_left_string() {
|
|
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);
|
|
ret = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int test_trim_both_string() {
|
|
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);
|
|
ret = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int test_remove_chars() {
|
|
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);
|
|
ret = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int test_remove_string() {
|
|
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);
|
|
ret = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int test_word_count() {
|
|
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);
|
|
ret = 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int main() {
|
|
Tests tests = { 0 };
|
|
|
|
da_append(&tests, test_contains_char);
|
|
da_append(&tests, test_contains_string);
|
|
|
|
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_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);
|
|
|
|
size_t i;
|
|
for (i = 0; i < tests.count; i++) {
|
|
tests.amount_successful += tests.items[i]();
|
|
}
|
|
|
|
if (tests.amount_successful != tests.count) {
|
|
printf("%d/%d tests passed successfully\n", tests.amount_successful, tests.count);
|
|
return 1;
|
|
} else {
|
|
printf("All tests passed successfully\n");
|
|
return 0;
|
|
}
|
|
}
|
|
|