mirror of
https://github.com/theoludwig/libcproject.git
synced 2025-05-21 23:21:15 +02:00
chore: initial commit
This commit is contained in:
73
test/character_test.c
Normal file
73
test/character_test.c
Normal file
@ -0,0 +1,73 @@
|
||||
#include "character_test.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "libcproject.h"
|
||||
#include "test.h"
|
||||
|
||||
void character_test() {
|
||||
character_append_test();
|
||||
character_append_at_test();
|
||||
character_to_upper_test();
|
||||
character_to_lower_test();
|
||||
character_get_is_digit_test();
|
||||
character_get_alphabet_position_test();
|
||||
}
|
||||
|
||||
void character_append_test() {
|
||||
char string[5] = "abc";
|
||||
character_append(string, 'd');
|
||||
assert(assert_string_equal(string, "abcd"));
|
||||
}
|
||||
|
||||
void character_append_at_test() {
|
||||
char string[5] = "abc";
|
||||
character_append_at(string, 'd', 1);
|
||||
assert(assert_string_equal(string, "adbc"));
|
||||
|
||||
char string2[5] = "abcd";
|
||||
character_append_at(string2, 'e', 4);
|
||||
assert(assert_string_equal(string2, "abcde"));
|
||||
}
|
||||
|
||||
void character_to_upper_test() {
|
||||
assert(character_to_upper('a') == 'A');
|
||||
assert(character_to_upper('e') == 'E');
|
||||
assert(character_to_upper('A') == 'A');
|
||||
}
|
||||
|
||||
void character_to_lower_test() {
|
||||
assert(character_to_lower('A') == 'a');
|
||||
assert(character_to_lower('E') == 'e');
|
||||
assert(character_to_lower('a') == 'a');
|
||||
}
|
||||
|
||||
void character_get_is_digit_test() {
|
||||
assert(character_get_is_digit('0'));
|
||||
assert(character_get_is_digit('1'));
|
||||
assert(character_get_is_digit('2'));
|
||||
assert(character_get_is_digit('3'));
|
||||
assert(character_get_is_digit('4'));
|
||||
assert(character_get_is_digit('5'));
|
||||
assert(character_get_is_digit('6'));
|
||||
assert(character_get_is_digit('7'));
|
||||
assert(character_get_is_digit('8'));
|
||||
assert(character_get_is_digit('9'));
|
||||
assert(!character_get_is_digit('a'));
|
||||
assert(!character_get_is_digit('/'));
|
||||
assert(!character_get_is_digit(':'));
|
||||
}
|
||||
|
||||
void character_get_alphabet_position_test() {
|
||||
assert(character_get_alphabet_position('a') == 1);
|
||||
assert(character_get_alphabet_position('A') == 1);
|
||||
assert(character_get_alphabet_position('B') == 2);
|
||||
assert(character_get_alphabet_position('D') == 4);
|
||||
assert(character_get_alphabet_position('m') == 13);
|
||||
assert(character_get_alphabet_position('n') == 14);
|
||||
assert(character_get_alphabet_position('z') == 26);
|
||||
assert(character_get_alphabet_position('@') == 0);
|
||||
assert(character_get_alphabet_position('-') == 0);
|
||||
}
|
18
test/character_test.h
Normal file
18
test/character_test.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef __CHARACTER_TEST__
|
||||
#define __CHARACTER_TEST__
|
||||
|
||||
void character_test();
|
||||
|
||||
void character_append_test();
|
||||
|
||||
void character_append_at_test();
|
||||
|
||||
void character_to_upper_test();
|
||||
|
||||
void character_to_lower_test();
|
||||
|
||||
void character_get_is_digit_test();
|
||||
|
||||
void character_get_alphabet_position_test();
|
||||
|
||||
#endif
|
102
test/convert_test.c
Normal file
102
test/convert_test.c
Normal file
@ -0,0 +1,102 @@
|
||||
#include "convert_test.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "libcproject.h"
|
||||
#include "test.h"
|
||||
|
||||
void convert_test() {
|
||||
convert_character_to_string_test();
|
||||
convert_character_to_digit_test();
|
||||
convert_digit_to_character_test();
|
||||
convert_string_to_number_test();
|
||||
convert_number_to_string_test();
|
||||
convert_number_from_base_to_another_test();
|
||||
}
|
||||
|
||||
void convert_character_to_string_test() {
|
||||
assert(assert_string_equal(convert_character_to_string('a'), "a"));
|
||||
assert(assert_string_equal(convert_character_to_string('A'), "A"));
|
||||
assert(assert_string_equal(convert_character_to_string('0'), "0"));
|
||||
assert(assert_string_equal(convert_character_to_string(' '), " "));
|
||||
assert(assert_string_equal(convert_character_to_string('\0'), ""));
|
||||
}
|
||||
|
||||
void convert_character_to_digit_test() {
|
||||
assert(convert_character_to_digit('0') == 0);
|
||||
assert(convert_character_to_digit('1') == 1);
|
||||
assert(convert_character_to_digit('2') == 2);
|
||||
assert(convert_character_to_digit('3') == 3);
|
||||
assert(convert_character_to_digit('4') == 4);
|
||||
assert(convert_character_to_digit('5') == 5);
|
||||
assert(convert_character_to_digit('6') == 6);
|
||||
assert(convert_character_to_digit('7') == 7);
|
||||
assert(convert_character_to_digit('8') == 8);
|
||||
assert(convert_character_to_digit('9') == 9);
|
||||
}
|
||||
|
||||
void convert_digit_to_character_test() {
|
||||
assert(convert_digit_to_character(0) == '0');
|
||||
assert(convert_digit_to_character(1) == '1');
|
||||
assert(convert_digit_to_character(2) == '2');
|
||||
assert(convert_digit_to_character(3) == '3');
|
||||
assert(convert_digit_to_character(4) == '4');
|
||||
assert(convert_digit_to_character(5) == '5');
|
||||
assert(convert_digit_to_character(6) == '6');
|
||||
assert(convert_digit_to_character(7) == '7');
|
||||
assert(convert_digit_to_character(8) == '8');
|
||||
assert(convert_digit_to_character(9) == '9');
|
||||
}
|
||||
|
||||
void convert_string_to_number_test() {
|
||||
assert(convert_string_to_number("0") == 0);
|
||||
assert(convert_string_to_number("1") == 1);
|
||||
assert(convert_string_to_number("2") == 2);
|
||||
assert(convert_string_to_number("3") == 3);
|
||||
assert(convert_string_to_number("4") == 4);
|
||||
assert(convert_string_to_number("5") == 5);
|
||||
assert(convert_string_to_number("6") == 6);
|
||||
assert(convert_string_to_number("7") == 7);
|
||||
assert(convert_string_to_number("8") == 8);
|
||||
assert(convert_string_to_number("9") == 9);
|
||||
assert(convert_string_to_number("10") == 10);
|
||||
assert(convert_string_to_number("11") == 11);
|
||||
assert(convert_string_to_number("20") == 20);
|
||||
assert(convert_string_to_number("-0") == 0);
|
||||
assert(convert_string_to_number("-1") == -1);
|
||||
assert(convert_string_to_number("-20") == -20);
|
||||
}
|
||||
|
||||
void convert_number_to_string_test() {
|
||||
assert(assert_string_equal(convert_number_to_string(0), "0"));
|
||||
assert(assert_string_equal(convert_number_to_string(1), "1"));
|
||||
assert(assert_string_equal(convert_number_to_string(2), "2"));
|
||||
assert(assert_string_equal(convert_number_to_string(3), "3"));
|
||||
assert(assert_string_equal(convert_number_to_string(4), "4"));
|
||||
assert(assert_string_equal(convert_number_to_string(5), "5"));
|
||||
assert(assert_string_equal(convert_number_to_string(6), "6"));
|
||||
assert(assert_string_equal(convert_number_to_string(7), "7"));
|
||||
assert(assert_string_equal(convert_number_to_string(8), "8"));
|
||||
assert(assert_string_equal(convert_number_to_string(9), "9"));
|
||||
assert(assert_string_equal(convert_number_to_string(10), "10"));
|
||||
assert(assert_string_equal(convert_number_to_string(11), "11"));
|
||||
assert(assert_string_equal(convert_number_to_string(20), "20"));
|
||||
assert(assert_string_equal(convert_number_to_string(-0), "0"));
|
||||
assert(assert_string_equal(convert_number_to_string(-1), "-1"));
|
||||
assert(assert_string_equal(convert_number_to_string(-20), "-20"));
|
||||
}
|
||||
|
||||
void convert_number_from_base_to_another_test() {
|
||||
assert(assert_string_equal(convert_number_from_base_to_another("15", 10, 16), "F"));
|
||||
assert(assert_string_equal(convert_number_from_base_to_another("100000000", 2, 16), "100"));
|
||||
assert(assert_string_equal(convert_number_from_base_to_another("FFFFFF", 16, 10), "16777215"));
|
||||
assert(assert_string_equal(convert_number_from_base_to_another("1D57", 17, 35), "75C"));
|
||||
assert(assert_string_equal(convert_number_from_base_to_another("80E", 20, 5), "100324"));
|
||||
assert(assert_string_equal(convert_number_from_base_to_another("99", 10, 10), "99"));
|
||||
assert(assert_string_equal(convert_number_from_base_to_another("3433024", 6, 28), "8008"));
|
||||
assert(assert_string_equal(convert_number_from_base_to_another("30288G3A", 17, 36), "KF12OI"));
|
||||
assert(assert_string_equal(convert_number_from_base_to_another("10", 9, 9), "10"));
|
||||
assert(assert_string_equal(convert_number_from_base_to_another("10E", 23, 8), "1037"));
|
||||
}
|
18
test/convert_test.h
Normal file
18
test/convert_test.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef __CONVERT_TEST__
|
||||
#define __CONVERT_TEST__
|
||||
|
||||
void convert_test();
|
||||
|
||||
void convert_character_to_string_test();
|
||||
|
||||
void convert_character_to_digit_test();
|
||||
|
||||
void convert_digit_to_character_test();
|
||||
|
||||
void convert_string_to_number_test();
|
||||
|
||||
void convert_number_to_string_test();
|
||||
|
||||
void convert_number_from_base_to_another_test();
|
||||
|
||||
#endif
|
33
test/dictionary_test.c
Normal file
33
test/dictionary_test.c
Normal file
@ -0,0 +1,33 @@
|
||||
#include "dictionary_test.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "libcproject.h"
|
||||
|
||||
void dictionary_test() {
|
||||
struct dictionary *dictionary = dictionary_initialization();
|
||||
assert(dictionary->length == 0);
|
||||
dictionary_add(dictionary, "key", (void *)'a');
|
||||
dictionary_add(dictionary, "key1", (void *)'b');
|
||||
dictionary_add(dictionary, "key2", (void *)'c');
|
||||
dictionary_add(dictionary, "key3", (void *)'d');
|
||||
dictionary_add(dictionary, "key4", (void *)'e');
|
||||
dictionary_add(dictionary, "key5", (void *)'f');
|
||||
assert(dictionary->length == 6);
|
||||
assert(dictionary_get(dictionary, "key")->data == (void *)'a');
|
||||
assert(dictionary_get(dictionary, "key1")->data == (void *)'b');
|
||||
assert(dictionary_get(dictionary, "key2")->data == (void *)'c');
|
||||
assert(dictionary_get(dictionary, "key3")->data == (void *)'d');
|
||||
assert(dictionary_get(dictionary, "key4")->data == (void *)'e');
|
||||
assert(dictionary_get(dictionary, "key5")->data == (void *)'f');
|
||||
dictionary_add(dictionary, "key5", (void *)'a');
|
||||
assert(dictionary_get(dictionary, "key5")->data == (void *)'a');
|
||||
assert(dictionary_contains_key(dictionary, "key5"));
|
||||
assert(!dictionary_contains_key(dictionary, "invalid key"));
|
||||
assert(dictionary_contains_key(dictionary, "key5"));
|
||||
dictionary_remove(dictionary, "key5");
|
||||
assert(dictionary->length == 5);
|
||||
assert(!dictionary_contains_key(dictionary, "key5"));
|
||||
}
|
6
test/dictionary_test.h
Normal file
6
test/dictionary_test.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef __DICTIONARY_TEST__
|
||||
#define __DICTIONARY_TEST__
|
||||
|
||||
void dictionary_test();
|
||||
|
||||
#endif
|
99
test/linked_list_test.c
Normal file
99
test/linked_list_test.c
Normal file
@ -0,0 +1,99 @@
|
||||
#include "linked_list_test.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "libcproject.h"
|
||||
|
||||
void linked_list_test() {
|
||||
linked_list_initialization_test();
|
||||
linked_list_add_in_head_test();
|
||||
linked_list_delete_in_head_test();
|
||||
linked_list_add_after_last_test();
|
||||
linked_list_reverse_test();
|
||||
linked_list_reverse_mutate_test();
|
||||
}
|
||||
|
||||
void linked_list_initialization_test() {
|
||||
struct linked_list *list = linked_list_initialization();
|
||||
assert(list->length == 0);
|
||||
assert(list->head == NULL);
|
||||
}
|
||||
|
||||
void linked_list_add_in_head_test() {
|
||||
struct linked_list *list = linked_list_initialization();
|
||||
linked_list_add_in_head(list, (void *)4);
|
||||
linked_list_add_in_head(list, (void *)8);
|
||||
linked_list_add_in_head(list, (void *)15);
|
||||
assert(list->length == 3);
|
||||
assert(((uintptr_t)list->head->data) == 15);
|
||||
assert(((uintptr_t)list->head->next->data) == 8);
|
||||
assert(((uintptr_t)list->head->next->next->data) == 4);
|
||||
assert(list->head->next->next->next == NULL);
|
||||
}
|
||||
|
||||
void linked_list_delete_in_head_test() {
|
||||
struct linked_list *list = linked_list_initialization();
|
||||
linked_list_add_in_head(list, (void *)4);
|
||||
linked_list_add_in_head(list, (void *)8);
|
||||
linked_list_add_in_head(list, (void *)15);
|
||||
linked_list_delete_in_head(list);
|
||||
assert(list->length == 2);
|
||||
assert(((uintptr_t)list->head->data) == 8);
|
||||
assert(((uintptr_t)list->head->next->data) == 4);
|
||||
assert(list->head->next->next == NULL);
|
||||
}
|
||||
|
||||
void linked_list_add_after_last_test() {
|
||||
struct linked_list *list = linked_list_initialization();
|
||||
linked_list_add_in_head(list, (void *)4);
|
||||
linked_list_add_in_head(list, (void *)8);
|
||||
linked_list_add_in_head(list, (void *)15);
|
||||
assert(list->length == 3);
|
||||
assert(((uintptr_t)list->head->data) == 15);
|
||||
assert(((uintptr_t)list->head->next->data) == 8);
|
||||
assert(((uintptr_t)list->head->next->next->data) == 4);
|
||||
linked_list_add_after_last(list, (void *)18);
|
||||
assert(list->length == 4);
|
||||
assert(((uintptr_t)list->head->data) == 15);
|
||||
assert(((uintptr_t)list->head->next->data) == 8);
|
||||
assert(((uintptr_t)list->head->next->next->data) == 4);
|
||||
assert(((uintptr_t)list->head->next->next->next->data) == 18);
|
||||
}
|
||||
|
||||
void linked_list_reverse_test() {
|
||||
struct linked_list *list = linked_list_initialization();
|
||||
linked_list_add_after_last(list, (void *)'A');
|
||||
linked_list_add_after_last(list, (void *)'B');
|
||||
linked_list_add_after_last(list, (void *)'C');
|
||||
linked_list_add_after_last(list, (void *)'D');
|
||||
assert(list->length == 4);
|
||||
struct linked_list *list_reversed = linked_list_reverse(list);
|
||||
assert(list->length == 4);
|
||||
assert((list->head->data) == (void *)'A');
|
||||
assert((list->head->next->data) == (void *)'B');
|
||||
assert((list->head->next->next->data) == (void *)'C');
|
||||
assert((list->head->next->next->next->data) == (void *)'D');
|
||||
assert(list_reversed->length == 4);
|
||||
assert((list_reversed->head->data) == (void *)'D');
|
||||
assert((list_reversed->head->next->data) == (void *)'C');
|
||||
assert((list_reversed->head->next->next->data) == (void *)'B');
|
||||
assert((list_reversed->head->next->next->next->data) == (void *)'A');
|
||||
}
|
||||
|
||||
void linked_list_reverse_mutate_test() {
|
||||
struct linked_list *list = linked_list_initialization();
|
||||
linked_list_add_after_last(list, (void *)'A');
|
||||
linked_list_add_after_last(list, (void *)'B');
|
||||
linked_list_add_after_last(list, (void *)'C');
|
||||
linked_list_add_after_last(list, (void *)'D');
|
||||
assert(list->length == 4);
|
||||
linked_list_reverse_mutate(list);
|
||||
assert(list->length == 4);
|
||||
assert((list->head->data) == (void *)'D');
|
||||
assert((list->head->next->data) == (void *)'C');
|
||||
assert((list->head->next->next->data) == (void *)'B');
|
||||
assert((list->head->next->next->next->data) == (void *)'A');
|
||||
}
|
18
test/linked_list_test.h
Normal file
18
test/linked_list_test.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef __LINKED_LIST_TEST__
|
||||
#define __LINKED_LIST_TEST__
|
||||
|
||||
void linked_list_test();
|
||||
|
||||
void linked_list_initialization_test();
|
||||
|
||||
void linked_list_add_in_head_test();
|
||||
|
||||
void linked_list_delete_in_head_test();
|
||||
|
||||
void linked_list_add_after_last_test();
|
||||
|
||||
void linked_list_reverse_test();
|
||||
|
||||
void linked_list_reverse_mutate_test();
|
||||
|
||||
#endif
|
24
test/main.c
Normal file
24
test/main.c
Normal file
@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "character_test.h"
|
||||
#include "convert_test.h"
|
||||
#include "dictionary_test.h"
|
||||
#include "linked_list_test.h"
|
||||
#include "mathematics_test.h"
|
||||
#include "queue_test.h"
|
||||
#include "stack_test.h"
|
||||
#include "string_test.h"
|
||||
|
||||
int main() {
|
||||
character_test();
|
||||
convert_test();
|
||||
dictionary_test();
|
||||
linked_list_test();
|
||||
mathematics_test();
|
||||
queue_test();
|
||||
stack_test();
|
||||
string_test();
|
||||
printf("Success: Tests passed!\n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
62
test/mathematics_test.c
Normal file
62
test/mathematics_test.c
Normal file
@ -0,0 +1,62 @@
|
||||
#include "mathematics_test.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "libcproject.h"
|
||||
|
||||
void mathematics_test() {
|
||||
mathematics_absolute_value_test();
|
||||
mathematics_pow_test();
|
||||
mathematics_root_test();
|
||||
mathematics_square_root_test();
|
||||
mathematics_factorial_test();
|
||||
}
|
||||
|
||||
void mathematics_absolute_value_test() {
|
||||
assert(mathematics_absolute_value(0) == 0);
|
||||
assert(mathematics_absolute_value(-0) == 0);
|
||||
assert(mathematics_absolute_value(1) == 1);
|
||||
assert(mathematics_absolute_value(-1) == 1);
|
||||
assert(mathematics_absolute_value(2) == 2);
|
||||
assert(mathematics_absolute_value(-2) == 2);
|
||||
}
|
||||
|
||||
void mathematics_pow_test() {
|
||||
assert(mathematics_pow(0, 0) == 1);
|
||||
assert(mathematics_pow(0, 1) == 0);
|
||||
assert(mathematics_pow(2, 2) == 4);
|
||||
assert(mathematics_pow(5, 2) == 25);
|
||||
assert(mathematics_pow(3, 3) == 27);
|
||||
assert(mathematics_pow(7, 5) == 16807);
|
||||
}
|
||||
|
||||
void mathematics_root_test() {
|
||||
assert(mathematics_get_is_equal(mathematics_root(0, 0), 0));
|
||||
assert(mathematics_get_is_equal(mathematics_root(0, 1), 0));
|
||||
assert(mathematics_get_is_equal(mathematics_root(2, 2), 1));
|
||||
assert(mathematics_get_is_equal(mathematics_root(27, 3), 3));
|
||||
assert(mathematics_get_is_equal(mathematics_root(16807, 5), 7));
|
||||
}
|
||||
|
||||
void mathematics_square_root_test() {
|
||||
assert(mathematics_get_is_equal(mathematics_square_root(0), 0));
|
||||
assert(mathematics_get_is_equal(mathematics_square_root(1), 1));
|
||||
assert(mathematics_get_is_equal(mathematics_square_root(4), 2));
|
||||
assert(mathematics_get_is_equal(mathematics_square_root(9), 3));
|
||||
assert(mathematics_get_is_equal(mathematics_square_root(25), 5));
|
||||
assert(mathematics_get_is_equal(mathematics_square_root(100), 10));
|
||||
}
|
||||
|
||||
void mathematics_factorial_test() {
|
||||
assert(mathematics_factorial(0) == 1);
|
||||
assert(mathematics_factorial(1) == 1);
|
||||
assert(mathematics_factorial(2) == 2);
|
||||
assert(mathematics_factorial(3) == 6);
|
||||
assert(mathematics_factorial(4) == 24);
|
||||
assert(mathematics_factorial(5) == 120);
|
||||
assert(mathematics_factorial(6) == 720);
|
||||
assert(mathematics_factorial(7) == 5040);
|
||||
assert(mathematics_factorial(8) == 40320);
|
||||
assert(mathematics_factorial(9) == 362880);
|
||||
assert(mathematics_factorial(10) == 3628800);
|
||||
}
|
16
test/mathematics_test.h
Normal file
16
test/mathematics_test.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef __MATHEMATICS_TEST__
|
||||
#define __MATHEMATICS_TEST__
|
||||
|
||||
void mathematics_test();
|
||||
|
||||
void mathematics_absolute_value_test();
|
||||
|
||||
void mathematics_pow_test();
|
||||
|
||||
void mathematics_root_test();
|
||||
|
||||
void mathematics_square_root_test();
|
||||
|
||||
void mathematics_factorial_test();
|
||||
|
||||
#endif
|
44
test/queue_test.c
Normal file
44
test/queue_test.c
Normal file
@ -0,0 +1,44 @@
|
||||
#include "queue_test.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "libcproject.h"
|
||||
|
||||
void queue_test() {
|
||||
queue_initialization_test();
|
||||
queue_push_test();
|
||||
queue_pop_test();
|
||||
}
|
||||
|
||||
void queue_initialization_test() {
|
||||
struct queue *queue = queue_initialization();
|
||||
assert(queue->length == 0);
|
||||
assert(queue->first == NULL);
|
||||
}
|
||||
|
||||
void queue_push_test() {
|
||||
struct queue *queue = queue_initialization();
|
||||
queue_push(queue, (void *)4);
|
||||
queue_push(queue, (void *)8);
|
||||
queue_push(queue, (void *)15);
|
||||
assert(queue->length == 3);
|
||||
assert(((uintptr_t)queue->first->data) == 4);
|
||||
assert(((uintptr_t)queue->first->next->data) == 8);
|
||||
assert(((uintptr_t)queue->first->next->next->data) == 15);
|
||||
assert(queue->first->next->next->next == NULL);
|
||||
}
|
||||
|
||||
void queue_pop_test() {
|
||||
struct queue *queue = queue_initialization();
|
||||
queue_push(queue, (void *)4);
|
||||
queue_push(queue, (void *)8);
|
||||
queue_push(queue, (void *)15);
|
||||
void *node = queue_pop(queue);
|
||||
assert(queue->length == 2);
|
||||
assert(((uintptr_t)node) == 4);
|
||||
assert(((uintptr_t)queue->first->data) == 8);
|
||||
assert(((uintptr_t)queue->first->next->data) == 15);
|
||||
assert(queue->first->next->next == NULL);
|
||||
}
|
12
test/queue_test.h
Normal file
12
test/queue_test.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef __QUEUE_TEST__
|
||||
#define __QUEUE_TEST__
|
||||
|
||||
void queue_test();
|
||||
|
||||
void queue_initialization_test();
|
||||
|
||||
void queue_push_test();
|
||||
|
||||
void queue_pop_test();
|
||||
|
||||
#endif
|
44
test/stack_test.c
Normal file
44
test/stack_test.c
Normal file
@ -0,0 +1,44 @@
|
||||
#include "stack_test.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "libcproject.h"
|
||||
|
||||
void stack_test() {
|
||||
stack_initialization_test();
|
||||
stack_push_test();
|
||||
stack_pop_test();
|
||||
}
|
||||
|
||||
void stack_initialization_test() {
|
||||
struct stack *stack = stack_initialization();
|
||||
assert(stack->length == 0);
|
||||
assert(stack->first == NULL);
|
||||
}
|
||||
|
||||
void stack_push_test() {
|
||||
struct stack *stack = stack_initialization();
|
||||
stack_push(stack, (void *)4);
|
||||
stack_push(stack, (void *)8);
|
||||
stack_push(stack, (void *)15);
|
||||
assert(stack->length == 3);
|
||||
assert(((uintptr_t)stack->first->data) == 15);
|
||||
assert(((uintptr_t)stack->first->next->data) == 8);
|
||||
assert(((uintptr_t)stack->first->next->next->data) == 4);
|
||||
assert(stack->first->next->next->next == NULL);
|
||||
}
|
||||
|
||||
void stack_pop_test() {
|
||||
struct stack *stack = stack_initialization();
|
||||
stack_push(stack, (void *)4);
|
||||
stack_push(stack, (void *)8);
|
||||
stack_push(stack, (void *)15);
|
||||
void *node = stack_pop(stack);
|
||||
assert(stack->length == 2);
|
||||
assert(((uintptr_t)node) == 15);
|
||||
assert(((uintptr_t)stack->first->data) == 8);
|
||||
assert(((uintptr_t)stack->first->next->data) == 4);
|
||||
assert(stack->first->next->next == NULL);
|
||||
}
|
12
test/stack_test.h
Normal file
12
test/stack_test.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef __STACK_TEST__
|
||||
#define __STACK_TEST__
|
||||
|
||||
void stack_test();
|
||||
|
||||
void stack_initialization_test();
|
||||
|
||||
void stack_push_test();
|
||||
|
||||
void stack_pop_test();
|
||||
|
||||
#endif
|
222
test/string_test.c
Normal file
222
test/string_test.c
Normal file
@ -0,0 +1,222 @@
|
||||
#include "string_test.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "libcproject.h"
|
||||
#include "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();
|
||||
string_get_is_equal_test();
|
||||
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() {
|
||||
char *string = "Hello World!";
|
||||
size_t string_length = string_get_length(string);
|
||||
assert(string_length == 12);
|
||||
}
|
||||
|
||||
void string_to_uppercase_test() {
|
||||
char *string = "heLlO world";
|
||||
string = string_to_uppercase(string);
|
||||
assert(assert_string_equal(string, "HELLO WORLD"));
|
||||
}
|
||||
|
||||
void string_to_lowercase_test() {
|
||||
char *string = "HellO WoRLd";
|
||||
string = string_to_lowercase(string);
|
||||
assert(assert_string_equal(string, "hello world"));
|
||||
}
|
||||
|
||||
void string_replace_test() {
|
||||
char *string = "hello world";
|
||||
string = string_replace(string, 'l', 'z');
|
||||
assert(assert_string_equal(string, "hezzo worzd"));
|
||||
}
|
||||
|
||||
void string_trim_start_test() {
|
||||
char *string = " hello world ";
|
||||
string = string_trim_start(string);
|
||||
assert(assert_string_equal(string, "hello world "));
|
||||
}
|
||||
|
||||
void string_trim_end_test() {
|
||||
char *string = " hello world ";
|
||||
string = string_trim_end(string);
|
||||
assert(assert_string_equal(string, " hello world"));
|
||||
}
|
||||
|
||||
void string_trim_test() {
|
||||
char *string = " hello world ";
|
||||
string = string_trim(string);
|
||||
assert(assert_string_equal(string, "hello world"));
|
||||
}
|
||||
|
||||
void string_copy_test() {
|
||||
char *string = "hello world";
|
||||
char *string2 = string_copy(string);
|
||||
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() {
|
||||
char *string = "hello world";
|
||||
string = string_capitalize(string);
|
||||
assert(assert_string_equal(string, "Hello world"));
|
||||
}
|
||||
|
||||
void string_total_occurrences_of_character_test() {
|
||||
char *string = "hello world";
|
||||
assert(string_total_occurrences_of_character(string, 'l') == 3);
|
||||
}
|
||||
|
||||
void string_reverse_test() {
|
||||
char *string = "hello world";
|
||||
string = string_reverse(string);
|
||||
assert(assert_string_equal(string, "dlrow olleh"));
|
||||
}
|
||||
|
||||
void string_get_is_equal_test() {
|
||||
char *string1 = "hello world";
|
||||
char *string2 = "dlrow olleh";
|
||||
char *string3 = "dlrow olleh";
|
||||
assert(!string_get_is_equal(string1, string2));
|
||||
assert(string_get_is_equal(string1, string1));
|
||||
assert(string_get_is_equal(string2, string3));
|
||||
}
|
||||
|
||||
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() {
|
||||
char *string = "abc def ghij kl";
|
||||
size_t result_length = 0;
|
||||
char **result = string_split(string, ' ', &result_length);
|
||||
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() {
|
||||
char *string = "abc def ghij kl";
|
||||
size_t result_length = 0;
|
||||
char **result = string_split(string, ' ', &result_length);
|
||||
char *new_string = string_join(result, ' ', result_length);
|
||||
char *new_string2 = string_join(result, '+', result_length);
|
||||
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() {
|
||||
char *string = "abcdef";
|
||||
char *substring = string_substring(string, 1, 3);
|
||||
assert(assert_string_equal(substring, "bcd"));
|
||||
}
|
||||
|
||||
void string_get_is_substring_test() {
|
||||
char *string = "abcdef";
|
||||
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() {
|
||||
char *string = "abcdef";
|
||||
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"));
|
||||
}
|
52
test/string_test.h
Normal file
52
test/string_test.h
Normal file
@ -0,0 +1,52 @@
|
||||
#ifndef __STRING_TEST__
|
||||
#define __STRING_TEST__
|
||||
|
||||
void string_test();
|
||||
|
||||
void string_get_length_test();
|
||||
|
||||
void string_to_uppercase_test();
|
||||
|
||||
void string_to_lowercase_test();
|
||||
|
||||
void string_replace_test();
|
||||
|
||||
void string_trim_start_test();
|
||||
|
||||
void string_trim_end_test();
|
||||
|
||||
void string_trim_test();
|
||||
|
||||
void string_copy_test();
|
||||
|
||||
void string_capitalize_test();
|
||||
|
||||
void string_total_occurrences_of_character_test();
|
||||
|
||||
void string_reverse_test();
|
||||
|
||||
void string_get_is_equal_test();
|
||||
|
||||
void string_get_is_integer_test();
|
||||
|
||||
void string_split_test();
|
||||
|
||||
void string_join_test();
|
||||
|
||||
void string_concatenate_test();
|
||||
|
||||
void string_get_has_unique_characters_test();
|
||||
|
||||
void string_substring_test();
|
||||
|
||||
void string_get_is_substring_test();
|
||||
|
||||
void string_get_formatted_number_test();
|
||||
|
||||
void string_get_last_occurence_of_character_test();
|
||||
|
||||
void string_starts_with_test();
|
||||
|
||||
void string_ends_with_test();
|
||||
|
||||
#endif
|
19
test/test.c
Normal file
19
test/test.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
bool assert_string_equal(const char *actual, const char *expected) {
|
||||
if (strcmp(expected, actual) != 0) {
|
||||
printf("FAIL: expected = \"%s\" ; actual = \"%s\"\n", expected, actual);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool assert_string_not_equal(const char *actual, const char *expected) {
|
||||
if (strcmp(expected, actual) == 0) {
|
||||
printf("FAIL: expected = \"%s\" ; actual = \"%s\"\n", expected, actual);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
10
test/test.h
Normal file
10
test/test.h
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef __TEST__
|
||||
#define __TEST__
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
bool assert_string_equal(const char *actual, const char *expected);
|
||||
|
||||
bool assert_string_not_equal(const char *actual, const char *expected);
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user