1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-11-14 16:03:13 +01:00
libcproject/test/string_test.c

216 lines
7.8 KiB
C
Raw Normal View History

2023-01-05 19:28:05 +01:00
#include "string_test.h"
void string_test() {
string_get_length_test();
string_to_uppercase_test();
string_to_lowercase_test();
string_replace_test();
string_trim_start_test();
string_trim_end_test();
string_trim_test();
string_copy_test();
string_capitalize_test();
string_total_occurrences_of_character_test();
string_reverse_test();
2023-01-05 21:13:10 +01:00
string_equals_test();
2023-01-05 19:28:05 +01:00
string_get_is_integer_test();
string_split_test();
string_join_test();
string_concatenate_test();
string_get_has_unique_characters_test();
string_substring_test();
string_get_is_substring_test();
string_get_formatted_number_test();
string_get_last_occurence_of_character_test();
string_starts_with_test();
string_ends_with_test();
}
void string_get_length_test() {
2023-06-25 15:17:46 +02:00
string_t string = "Hello World!";
2023-01-05 19:28:05 +01:00
size_t string_length = string_get_length(string);
assert(string_length == 12);
}
void string_to_uppercase_test() {
2023-06-25 15:17:46 +02:00
string_t string = "heLlO world";
2023-01-05 19:28:05 +01:00
string = string_to_uppercase(string);
assert(assert_string_equal(string, "HELLO WORLD"));
}
void string_to_lowercase_test() {
2023-06-25 15:17:46 +02:00
string_t string = "HellO WoRLd";
2023-01-05 19:28:05 +01:00
string = string_to_lowercase(string);
assert(assert_string_equal(string, "hello world"));
}
void string_replace_test() {
2023-06-25 15:17:46 +02:00
string_t string = "hello world";
2023-01-05 19:28:05 +01:00
string = string_replace(string, 'l', 'z');
assert(assert_string_equal(string, "hezzo worzd"));
}
void string_trim_start_test() {
2023-06-25 15:17:46 +02:00
string_t string = " hello world ";
2023-01-05 19:28:05 +01:00
string = string_trim_start(string);
assert(assert_string_equal(string, "hello world "));
}
void string_trim_end_test() {
2023-06-25 15:17:46 +02:00
string_t string = " hello world ";
2023-01-05 19:28:05 +01:00
string = string_trim_end(string);
assert(assert_string_equal(string, " hello world"));
}
void string_trim_test() {
2023-06-25 15:17:46 +02:00
string_t string = " hello world ";
2023-01-05 19:28:05 +01:00
string = string_trim(string);
assert(assert_string_equal(string, "hello world"));
}
void string_copy_test() {
2023-06-25 15:17:46 +02:00
string_t string = "hello world";
string_t string2 = string_copy(string);
2023-01-05 19:28:05 +01:00
assert(assert_string_equal(string, string2));
string2[0] = 'a';
assert(assert_string_not_equal(string, string2));
assert(assert_string_equal(string, "hello world"));
assert(assert_string_equal(string2, "aello world"));
}
void string_capitalize_test() {
2023-06-25 15:17:46 +02:00
string_t string = "hello world";
2023-01-05 19:28:05 +01:00
string = string_capitalize(string);
assert(assert_string_equal(string, "Hello world"));
}
void string_total_occurrences_of_character_test() {
2023-06-25 15:17:46 +02:00
string_t string = "hello world";
2023-01-05 19:28:05 +01:00
assert(string_total_occurrences_of_character(string, 'l') == 3);
}
void string_reverse_test() {
2023-06-25 15:17:46 +02:00
string_t string = "hello world";
2023-01-05 19:28:05 +01:00
string = string_reverse(string);
assert(assert_string_equal(string, "dlrow olleh"));
}
2023-01-05 21:13:10 +01:00
void string_equals_test() {
2023-06-25 15:17:46 +02:00
string_t string1 = "hello world";
string_t string2 = "dlrow olleh";
string_t string3 = "dlrow olleh";
2023-01-05 21:13:10 +01:00
assert(!string_equals(string1, string2));
assert(string_equals(string1, string1));
assert(string_equals(string2, string3));
2023-01-05 19:28:05 +01:00
}
void string_get_is_integer_test() {
assert(string_get_is_integer("1"));
assert(string_get_is_integer("12"));
assert(string_get_is_integer("-12"));
assert(!string_get_is_integer("1 000"));
assert(!string_get_is_integer("abc"));
assert(!string_get_is_integer("- 12"));
assert(!string_get_is_integer(" -12"));
assert(!string_get_is_integer("-12 "));
assert(!string_get_is_integer("-"));
}
void string_split_test() {
2023-06-25 15:17:46 +02:00
string_t string = "abc def ghij kl";
2023-01-05 19:28:05 +01:00
size_t result_length = 0;
2023-06-25 15:17:46 +02:00
string_t* result = string_split(string, ' ', &result_length);
2023-01-05 19:28:05 +01:00
assert(result_length == 4);
assert(assert_string_equal(result[0], "abc"));
assert(assert_string_equal(result[1], "def"));
assert(assert_string_equal(result[2], "ghij"));
assert(assert_string_equal(result[3], "kl"));
}
void string_join_test() {
2023-06-25 15:17:46 +02:00
string_t string = "abc def ghij kl";
2023-01-05 19:28:05 +01:00
size_t result_length = 0;
2023-06-25 15:17:46 +02:00
string_t* result = string_split(string, ' ', &result_length);
string_t new_string = string_join(result, ' ', result_length);
string_t new_string2 = string_join(result, '+', result_length);
2023-01-05 19:28:05 +01:00
assert(assert_string_equal(new_string, string));
assert(assert_string_equal(new_string2, "abc+def+ghij+kl"));
}
void string_concatenate_test() {
assert(assert_string_equal(string_concatenate("abc", "def"), "abcdef"));
assert(assert_string_equal(string_concatenate("abc ", " defghi"), "abc defghi"));
}
void string_get_has_unique_characters_test() {
assert(string_get_has_unique_characters("ABCDEF"));
assert(!string_get_has_unique_characters("AAABCDEF"));
assert(!string_get_has_unique_characters("AaaBCDEF"));
}
void string_substring_test() {
2023-06-25 15:17:46 +02:00
string_t string = "abcdef";
string_t substring = string_substring(string, 1, 3);
2023-01-05 19:28:05 +01:00
assert(assert_string_equal(substring, "bcd"));
}
void string_get_is_substring_test() {
2023-06-25 15:17:46 +02:00
string_t string = "abcdef";
2023-01-05 19:28:05 +01:00
assert(string_get_is_substring(string, "abc"));
assert(string_get_is_substring(string, "bcd"));
assert(string_get_is_substring(string, "de"));
assert(string_get_is_substring(string, "f"));
assert(!string_get_is_substring(string, "af"));
assert(string_get_is_substring("3662277", "2277"));
assert(string_get_is_substring("3662277", "62"));
assert(!string_get_is_substring("3662277", "322"));
}
void string_get_formatted_number_test() {
assert(assert_string_equal(string_get_formatted_number(1000, " "), "1 000"));
assert(assert_string_equal(string_get_formatted_number(123, ","), "123"));
assert(assert_string_equal(string_get_formatted_number(1234, ","), "1,234"));
assert(assert_string_equal(string_get_formatted_number(12345, ","), "12,345"));
assert(assert_string_equal(string_get_formatted_number(123456, ","), "123,456"));
assert(assert_string_equal(string_get_formatted_number(1234567, ","), "1,234,567"));
assert(assert_string_equal(string_get_formatted_number(12345678, ","), "12,345,678"));
assert(assert_string_equal(string_get_formatted_number(123456789, ","), "123,456,789"));
assert(assert_string_equal(string_get_formatted_number(1234567890, ","), "1,234,567,890"));
assert(assert_string_equal(string_get_formatted_number(-123, ","), "-123"));
assert(assert_string_equal(string_get_formatted_number(-1234, ","), "-1,234"));
assert(assert_string_equal(string_get_formatted_number(-12345, ","), "-12,345"));
assert(assert_string_equal(string_get_formatted_number(-123456, ","), "-123,456"));
assert(assert_string_equal(string_get_formatted_number(-1234567, ","), "-1,234,567"));
assert(assert_string_equal(string_get_formatted_number(-12345678, ","), "-12,345,678"));
assert(assert_string_equal(string_get_formatted_number(-123456789, ","), "-123,456,789"));
assert(assert_string_equal(string_get_formatted_number(-1234567890, ","), "-1,234,567,890"));
}
void string_get_last_occurence_of_character_test() {
2023-06-25 15:17:46 +02:00
string_t string = "abcdef";
2023-01-05 19:28:05 +01:00
assert(assert_string_equal(string_get_last_occurence_of_character(string, 'a'), "abcdef"));
assert(assert_string_equal(string_get_last_occurence_of_character(string, 'b'), "bcdef"));
assert(assert_string_equal(string_get_last_occurence_of_character(string, 'c'), "cdef"));
assert(assert_string_equal(string_get_last_occurence_of_character(string, 'd'), "def"));
assert(assert_string_equal(string_get_last_occurence_of_character(string, 'e'), "ef"));
assert(assert_string_equal(string_get_last_occurence_of_character(string, 'f'), "f"));
}
void string_starts_with_test() {
assert(string_starts_with("abcdef", "abc"));
assert(!string_starts_with("abcdef", "bcd"));
assert(!string_starts_with("abcdef", "def"));
assert(!string_starts_with("abcdef", "ef"));
assert(!string_starts_with("abcdef", "f"));
assert(!string_starts_with("abcdef", "abcdefg"));
}
void string_ends_with_test() {
assert(string_ends_with("abcdef", "def"));
assert(string_ends_with("abcdef", "ef"));
assert(string_ends_with("abcdef", "f"));
assert(!string_ends_with("abcdef", "abc"));
assert(!string_ends_with("abcdef", "bcd"));
assert(!string_ends_with("abcdef", "abcdefg"));
}