/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ----------------------------------------------------------------------------- * File: Main.c * Description: * Executes all tests. * * Author: Mineplay * ----------------------------------------------------------------------------- */ #include #include #include #include #include #include #include #include #include #include #include #include static inline bool compare_integers(const int first_value, const int second_value) { return first_value == second_value; } static inline size_t integer_hash_function(const int key) { return key; } static inline int integer_less_than_integer(const int first_key, const int second_key) { return first_key < second_key; } FLEDASTY_STACK_DEFINE(int, int) FLEDASTY_STACK_IMPLEMENT(int, int) FLEDASTY_QUEUE_DEFINE(int, int) FLEDASTY_QUEUE_IMPLEMENT(int, int) FLEDASTY_DYNAMIC_ARRAY_DEFINE(int, int) FLEDASTY_DYNAMIC_ARRAY_IMPLEMENT(int, int, compare_integers) FLEDASTY_HASH_TABLE_DEFINE(int, int, int_int, compare_integers) FLEDASTY_HASH_TABLE_IMPLEMENT(int, int, int_int, compare_integers) FLEDASTY_LINKED_LIST_DEFINE(int, int) FLEDASTY_LINKED_LIST_IMPLEMENT(int, int, compare_integers) FLEDASTY_DOUBLY_LINKED_LIST_DEFINE(int, int) FLEDASTY_DOUBLY_LINKED_LIST_IMPLEMENT(int, int, compare_integers) FLEDASTY_BINARY_SEARCH_TREE_DEFINE(int, int, int_int) FLEDASTY_BINARY_SEARCH_TREE_IMPLEMENT(int, int, int_int, compare_integers, integer_less_than_integer) int main() { FledastyQueue_int test_queue = { 0, 0, 0, 0, NULL }; for (int i = 0; i < 10; i += 1) { fledasty_queue_int_push(&test_queue, i); } fledasty_queue_int_shrink_to_fit(&test_queue); printf("Queue schrinked to fit %s\n", (test_queue.capacity == test_queue.size) ? "succeeded" : "failed"); fledasty_queue_int_push(&test_queue, 10); printf("Queue peeked: %d\n", fledasty_queue_int_peek(&test_queue)); for (int i = test_queue.size; i > 0; i -= 1) { printf("Queue popped: %d\n", fledasty_queue_int_pop(&test_queue)); } fledasty_queue_int_clear(&test_queue); if (fledasty_queue_int_is_empty(&test_queue)) { printf("Queue is empty\n"); } fledasty_queue_int_free(&test_queue); FledastyStack_int test_stack = { 0, 0, NULL }; for (int i = 0; i < 10; i += 1) { fledasty_stack_int_push(&test_stack, i); } fledasty_stack_int_shrink_to_fit(&test_stack); printf("Stack schrinked to fit %s\n", (test_stack.capacity == test_stack.size) ? "succeeded" : "failed"); printf("Stack peeked: %d\n", fledasty_stack_int_peek(&test_stack)); for (int i = test_stack.size; i > 0; i -= 1) { printf("Stack popped: %d\n", fledasty_stack_int_pop(&test_stack)); } fledasty_stack_int_clear(&test_stack); if (fledasty_stack_int_is_empty(&test_stack)) { printf("Stack is empty\n"); } fledasty_stack_int_free(&test_stack); FledastyDynamicArray_int test_dynamic_array = { 0, 0, NULL }; for (int i = 0; i < 10; i += 1) { fledasty_dynamic_array_int_append(&test_dynamic_array, i); } fledasty_dynamic_array_int_shrink_to_fit(&test_dynamic_array); printf("Dynamic array schrinked to fit %s\n", (test_dynamic_array.capacity == test_dynamic_array.size) ? "succeeded" : "failed"); fledasty_dynamic_array_int_insert_at_index(&test_dynamic_array, 1, 18); fledasty_dynamic_array_int_insert_before_value(&test_dynamic_array, 1, 35); fledasty_dynamic_array_int_insert_after_value(&test_dynamic_array, 35, 90); fledasty_dynamic_array_int_remove_value(&test_dynamic_array, 35); fledasty_dynamic_array_int_remove_at_index(&test_dynamic_array, test_dynamic_array.size - 2); for (int i = 0; i < test_dynamic_array.size; i += 1) { printf("Dynamic array get: %d\n", test_dynamic_array.buffer[i]); } if (fledasty_dynamic_array_int_has_value(&test_dynamic_array, 90)) { printf("Dynamic array contains 90\n"); } fledasty_dynamic_array_int_clear(&test_dynamic_array); if (fledasty_dynamic_array_int_is_empty(&test_dynamic_array)) { printf("Dynamic array is empty\n"); } fledasty_dynamic_array_int_free(&test_dynamic_array); FledastyLinkedList_int test_linked_list = { 0, NULL, NULL }; for (int i = 0; i < 10; i += 1) { fledasty_linked_list_int_append(&test_linked_list, i); } fledasty_linked_list_int_insert_at_index(&test_linked_list, 1, 35); fledasty_linked_list_int_insert_before_value(&test_linked_list, 35, 28); fledasty_linked_list_int_insert_after_value(&test_linked_list, 35, 90); fledasty_linked_list_int_remove_at_index(&test_linked_list, 2); fledasty_linked_list_int_remove_value(&test_linked_list, 0); FledastyLinkedListNode_int *test_linked_list_node = test_linked_list.start; for (int i = 0; i < test_linked_list.size; i += 1) { printf("Linked list get: %d\n", test_linked_list_node->value); test_linked_list_node = test_linked_list_node->next; } if (fledasty_linked_list_int_has_value(&test_linked_list, 90)) { printf("Linked list contains 90\n"); } fledasty_linked_list_int_clear(&test_linked_list); if (fledasty_linked_list_int_is_empty(&test_linked_list)) { printf("Linked list is empty\n"); } fledasty_linked_list_int_free(&test_linked_list); FledastyDoublyLinkedList_int test_doubly_linked_list = { 0, NULL , NULL }; for (int i = 0; i < 10; i += 1) { fledasty_doubly_linked_list_int_append(&test_doubly_linked_list, i); } fledasty_doubly_linked_list_int_insert_at_index(&test_doubly_linked_list, test_doubly_linked_list.size - 1, 35); fledasty_doubly_linked_list_int_insert_before_value(&test_doubly_linked_list, 35, 28); fledasty_doubly_linked_list_int_insert_after_value(&test_doubly_linked_list, 35, 90); fledasty_doubly_linked_list_int_remove_at_index(&test_doubly_linked_list, 2); fledasty_doubly_linked_list_int_remove_value(&test_doubly_linked_list, 0); FledastyDoublyLinkedListNode_int *test_doubly_linked_list_node = test_doubly_linked_list.start; for (int i = 0; i < test_doubly_linked_list.size; i += 1) { printf("Doubly linked list get: %d\n", test_doubly_linked_list_node->value); test_doubly_linked_list_node = test_doubly_linked_list_node->next; } test_doubly_linked_list_node = test_doubly_linked_list.end; for (int i = 0; i < test_doubly_linked_list.size; i += 1) { printf("Doubly linked list get backwards: %d\n", test_doubly_linked_list_node->value); test_doubly_linked_list_node = test_doubly_linked_list_node->previous; } if (fledasty_doubly_linked_list_int_has_value(&test_doubly_linked_list, 35)) { printf("Doubly linked list contains 35\n"); } fledasty_doubly_linked_list_int_clear(&test_doubly_linked_list); if (fledasty_doubly_linked_list_int_is_empty(&test_doubly_linked_list)) { printf("Doubly linked list is empty\n"); } fledasty_doubly_list_int_free(&test_doubly_linked_list); FledastyHashTable_int_int test_hash_table = { .size = 0, .capacity = 0, .Table = NULL, .hash_function = (size_t(*)(const int))integer_hash_function }; for (int i = 0; i < 10; i += 1) { fledasty_hash_table_int_int_insert(&test_hash_table, i * i, i); } fledasty_hash_table_int_int_shrink_to_fit(&test_hash_table); fledasty_hash_table_int_int_insert(&test_hash_table, 2, 5); fledasty_hash_table_int_int_remove(&test_hash_table, 2); if (fledasty_hash_table_int_int_get(&test_hash_table, 2) != 0) { printf("Value not removed from hash table\n"); } for (int i = 0; i < 10; i += 1) { printf("Hash table get: %d\n", fledasty_hash_table_int_int_get(&test_hash_table, i * i)); } if (fledasty_hash_table_int_int_has_key(&test_hash_table, 4)) { printf("Hash table contains key 4\n"); } fledasty_hash_table_int_int_clear(&test_hash_table); if (fledasty_hash_table_int_int_is_empty(&test_hash_table)) { printf("Hash table is empty\n"); } fledasty_hash_table_int_int_free(&test_hash_table); FledastyUtf8String test_utf8_string = { 0, 0, NULL }; unsigned char *test_string = (unsigned char*)"πŸ˜€β‚¬Testing"; fledasty_utf8_string_append(&test_utf8_string, test_string, 14); printf("%s\n", test_string); printf("%s\n", test_utf8_string.character_string); fledasty_utf8_string_append(&test_utf8_string, (unsigned char*)"πŸ˜€", 4); printf("Append: %s\n", test_utf8_string.character_string); fledasty_utf8_string_insert_before_string(&test_utf8_string, (unsigned char*)"πŸ˜€", 4, (unsigned char*)"Hello", 5); printf("Insert Before: %s\n", test_utf8_string.character_string); fledasty_utf8_string_insert_after_string(&test_utf8_string, (unsigned char*)"πŸ˜€", 4, (unsigned char*)"Bye", 3); printf("Insert After: %s\n", test_utf8_string.character_string); fledasty_utf8_string_insert_at_index(&test_utf8_string, test_utf8_string.size, (unsigned char*)"index", 5); printf("Insert at Index: %s\n", test_utf8_string.character_string); fledasty_utf8_string_replace_string(&test_utf8_string, (unsigned char*)"πŸ˜€", 4, (unsigned char*)"𓃢 ", 5); printf("Replace: %s\n", test_utf8_string.character_string); fledasty_utf8_string_pop(&test_utf8_string); printf("Pop: %s\n", test_utf8_string.character_string); fledasty_utf8_string_remove(&test_utf8_string, (unsigned char*)"𓃢 ", 5); printf("Remove: %s\n", test_utf8_string.character_string); fledasty_utf8_string_remove_range(&test_utf8_string, 0, 6); printf("Remove range: %s\n", test_utf8_string.character_string); fledasty_utf8_string_shrink_to_fit(&test_utf8_string); if (fledasty_utf8_string_has_string(&test_utf8_string, (unsigned char*)"πŸ˜€", 4)) { printf("String contains πŸ˜€!\n"); } uint32_t hash_x32 = fledasty_mur_mur_3_hash_x32(test_utf8_string.character_string, test_utf8_string.size, 0); printf("UTF-8 String hash using murmur 32 is: %u\n", hash_x32); FledastyHash128 hash_x64 = fledasty_mur_mur_3_hash_x64_128(test_utf8_string.character_string, test_utf8_string.size, 0); printf("UTF-8 String hash using murmur 64 is: low = %lu, high = %lu\n", hash_x64.low, hash_x64.high); size_t unicode_length = 0; uint32_t *unicode = fledasty_utf8_string_decode(&test_utf8_string, &unicode_length); FledastyUtf8String encoded_string = fledasty_utf8_string_encode(unicode, unicode_length); printf("%s\n", encoded_string.character_string); if (fledasty_utf8_string_validate(test_utf8_string.character_string, encoded_string.size)) { printf("UTF-8 test string is valid!\n"); } if (fledasty_utf8_string_validate(encoded_string.character_string, encoded_string.size)) { printf("UTF-8 encoded string is valid!\n"); } unsigned char *invalid_utf8 = (unsigned char*)hallocy_malloc(2 * sizeof(unsigned char)); invalid_utf8[0] = 0xDF; invalid_utf8[1] = 0xFF; if (!fledasty_utf8_string_validate(invalid_utf8, 2)) { printf("UTF-8 invalid string is invalid!\n"); } fledasty_utf8_string_clear(&test_utf8_string); if (fledasty_utf8_string_is_empty(&test_utf8_string)) { printf("UTF-8 string is empty!\n"); } hallocy_free(invalid_utf8); hallocy_free(unicode); fledasty_utf8_string_free(&encoded_string); fledasty_utf8_string_free(&test_utf8_string); FledastyString normal_test_string = { 0, 0, NULL }; fledasty_string_append(&normal_test_string, "Testing", 7); printf("Append: %s\n", normal_test_string.character_string); fledasty_string_insert_at_index(&normal_test_string, normal_test_string.size, "index", 5); printf("Insert at index: %s\n", normal_test_string.character_string); fledasty_string_insert_before_string(&normal_test_string, "nd", 2, "Hello", 5); printf("Insert before: %s\n", normal_test_string.character_string); fledasty_string_insert_after_string(&normal_test_string, "nd", 2, "Bye", 3); printf("Insert after: %s\n", normal_test_string.character_string); fledasty_string_pop(&normal_test_string); printf("Pop: %s\n", normal_test_string.character_string); fledasty_string_remove(&normal_test_string, "Bye", 3); printf("Removed string: %s\n", normal_test_string.character_string); fledasty_string_remove_range(&normal_test_string, 0, 6); printf("Remove range: %s\n", normal_test_string.character_string); fledasty_string_replace_string(&normal_test_string, "Hello", 5, "Goodbye", 7); printf("Replace: %s\n", normal_test_string.character_string); fledasty_string_shrink_to_fit(&normal_test_string); if (fledasty_string_has_string(&normal_test_string, "bye", 3)) { printf("String contains bye!\n"); } fledasty_string_clear(&normal_test_string); if (fledasty_string_is_empty(&normal_test_string)) { printf("String is empty!\n"); } fledasty_string_free(&normal_test_string); FledastyBinarySearchTree_int_int test_search_tree = { 0, 0, NULL }; fledasty_binary_search_tree_int_int_insert(&test_search_tree, 5, 10); fledasty_binary_search_tree_int_int_insert(&test_search_tree, 3, 6); fledasty_binary_search_tree_int_int_insert(&test_search_tree, 4, 8); fledasty_binary_search_tree_int_int_insert(&test_search_tree, 1, 2); fledasty_binary_search_tree_int_int_insert(&test_search_tree, 8, 16); fledasty_binary_search_tree_int_int_insert(&test_search_tree, 10, 20); fledasty_binary_search_tree_int_int_insert(&test_search_tree, 7, 14); FledastyBinarySearchTreeNode_int_int **in_order_result = fledasty_binary_search_tree_int_int_in_order_traversal(&test_search_tree); for (int i = 0; i < test_search_tree.size; i += 1) { printf("In order (After Insert): %d\n", in_order_result[i]->key); } hallocy_free(in_order_result); printf("Error code: %d\n", fledasty_binary_search_tree_int_int_remove(&test_search_tree, 3)); in_order_result = fledasty_binary_search_tree_int_int_in_order_traversal(&test_search_tree); for (int i = 0; i < test_search_tree.size; i += 1) { printf("In order (After Remove): %d\n", in_order_result[i]->key); } hallocy_free(in_order_result); FledastyBinarySearchTreeNode_int_int *node = fledasty_binary_search_tree_int_int_get(&test_search_tree, 7); printf("Search tree get result: %d\n", node->value); fledasty_binary_search_tree_int_int_free(&test_search_tree); printf("Done\n"); return 0; }