move files into root of repo

This commit is contained in:
2025-05-08 20:02:36 +02:00
parent 4b4ce3d36f
commit 6505add9f5
3 changed files with 148 additions and 118 deletions

346
tests.c Normal file
View File

@ -0,0 +1,346 @@
#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 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_is_lowercase() {
char* input = "string";
int expected = 1;
int result = strops_is_lowercase(input);
if (result != expected) {
printf("test_is_lowercase failed\n");
printf("Got = %d\nExpected = %d\n", result, expected);
return 1;
}
return 0;
}
int test_is_uppercase() {
char* input = "sTrInG";
int expected = 0;
int result = strops_is_uppercase(input);
if (result != expected) {
printf("test_is_uppercase failed\n");
printf("Got = %d\nExpected = %d\n", result, expected);
return 1;
}
return 0;
}
int test_insert_at_pos_string() {
char* input = "Heo, World!";
char* expected = "Hello, World!";
char* 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;
}
return 0;
}
int test_remove_at_pos_char() {
char* input = "Hello";
char* expected = "Hllo";
char* 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;
}
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_is_url_encoded() {
int ret = 0;
char* input;
int expected;
int result;
input = "(artist)";
expected = 0;
result = strops_is_url_encoded(input);
if (result != expected) {
printf("test_is_url_encoded failed\n");
printf("Got = %d\nExpected = %d\n", result, expected);
ret = 1;
}
input = "%28artist%29";
expected = 1;
result = strops_is_url_encoded(input);
if (result != expected) {
printf("test_is_url_encoded failed\n");
printf("Got = %d\nExpected = %d\n", result, expected);
ret = 1;
}
return ret;
}
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() {
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_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++;
}
}
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;
}
}