267 lines
No EOL
10 KiB
C
267 lines
No EOL
10 KiB
C
/*
|
|
* 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 <stdio.h>
|
|
#include <Hallocy/Core/Allocator.h>
|
|
#include <Fledasty/Core/Queue.h>
|
|
#include <Fledasty/Core/Stack.h>
|
|
#include <Fledasty/Core/DynamicArray.h>
|
|
#include <Fledasty/Core/LinkedList.h>
|
|
#include <Fledasty/Core/DoublyLinkedList.h>
|
|
#include <Fledasty/Core/HashTable.h>
|
|
#include <Fledasty/Strings/UTF8String.h>
|
|
|
|
static inline size_t integer_hash_function(void *key) { return *(size_t*)key; }
|
|
|
|
int main() {
|
|
FledastyQueue test_queue;
|
|
fledasty_queue_initialize(&test_queue, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int));
|
|
|
|
for (int i = 0; i < 10; i += 1) {
|
|
fledasty_queue_push(&test_queue, &i);
|
|
}
|
|
|
|
int *peeked_queue_data = (int*)fledasty_queue_peek(&test_queue);
|
|
printf("Queue peeked: %d\n", *peeked_queue_data);
|
|
|
|
for (int i = test_queue.size; i > 0; i -= 1) {
|
|
int *popped_data = (int*)fledasty_queue_pop(&test_queue);
|
|
printf("Queue popped: %d\n", *popped_data);
|
|
}
|
|
|
|
fledasty_queue_clear(&test_queue);
|
|
if (fledasty_queue_is_empty(&test_queue)) {
|
|
printf("Queue is empty\n");
|
|
}
|
|
|
|
fledasty_queue_destroy(&test_queue);
|
|
|
|
FledastyStack test_stack;
|
|
fledasty_stack_initialize(&test_stack, (int[]){ 11, 12, 13, 14, 15 }, 5, sizeof(int));
|
|
|
|
for (int i = 0; i < 10; i += 1) {
|
|
fledasty_stack_push(&test_stack, &i);
|
|
}
|
|
|
|
int *peeked_stack_data = (int*)fledasty_stack_peek(&test_stack);
|
|
printf("Stack peeked: %d\n", *peeked_stack_data);
|
|
|
|
for (int i = test_stack.size; i > 0; i -= 1) {
|
|
int *popped_data = (int*)fledasty_stack_pop(&test_stack);
|
|
printf("Stack popped: %d\n", *popped_data);
|
|
}
|
|
|
|
fledasty_stack_clear(&test_stack);
|
|
if (fledasty_stack_is_empty(&test_stack)) {
|
|
printf("Stack is empty\n");
|
|
}
|
|
|
|
fledasty_stack_destroy(&test_stack);
|
|
|
|
FledastyDynamicArray test_dynamic_array;
|
|
fledasty_dynamic_array_initialize(&test_dynamic_array, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int));
|
|
for (int i = 0; i < 10; i += 1) {
|
|
fledasty_dynamic_array_append(&test_dynamic_array, &i);
|
|
}
|
|
|
|
int insert_value = 18;
|
|
fledasty_dynamic_array_insert_at_index(&test_dynamic_array, 1, &insert_value);
|
|
|
|
insert_value = 35;
|
|
int insert_at_value = 11;
|
|
fledasty_dynamic_array_insert_before_value(&test_dynamic_array, &insert_at_value, &insert_value);
|
|
insert_value = 90;
|
|
insert_at_value = 35;
|
|
fledasty_dynamic_array_insert_after_value(&test_dynamic_array, &insert_at_value, &insert_value);
|
|
|
|
int remove_value = 15;
|
|
fledasty_dynamic_array_remove_value(&test_dynamic_array, &remove_value);
|
|
fledasty_dynamic_array_remove_at_index(&test_dynamic_array, test_dynamic_array.size - 2);
|
|
|
|
for (int i = 0; i < test_dynamic_array.size; i += 1) {
|
|
int *dynamic_array_data = (int*)fledasty_dynamic_array_get(&test_dynamic_array, i);
|
|
printf("Dynamic array get: %d\n", *dynamic_array_data);
|
|
}
|
|
|
|
if (fledasty_dynamic_array_has_value(&test_dynamic_array, &insert_value)) {
|
|
printf("Dynamic array contains %d\n", insert_value);
|
|
}
|
|
|
|
fledasty_dynamic_array_clear(&test_dynamic_array);
|
|
if (fledasty_dynamic_array_is_empty(&test_dynamic_array)) {
|
|
printf("Dynamic array is empty\n");
|
|
}
|
|
|
|
fledasty_dynamic_array_destroy(&test_dynamic_array);
|
|
|
|
FledastyLinkedList test_linked_list;
|
|
fledasty_linked_list_initialize(&test_linked_list, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int));
|
|
|
|
for (int i = 0; i < 10; i += 1) {
|
|
fledasty_linked_list_append(&test_linked_list, &i);
|
|
}
|
|
|
|
insert_value = 35;
|
|
fledasty_linked_list_insert_at_index(&test_linked_list, 1, &insert_value);
|
|
insert_value = 28;
|
|
insert_at_value = 35;
|
|
fledasty_linked_list_insert_before_value(&test_linked_list, &insert_at_value, &insert_value);
|
|
insert_value = 90;
|
|
fledasty_linked_list_insert_after_value(&test_linked_list, &insert_at_value, &insert_value);
|
|
|
|
fledasty_linked_list_remove_at_index(&test_linked_list, 2);
|
|
remove_value = 0;
|
|
fledasty_linked_list_remove_value(&test_linked_list, &remove_value);
|
|
|
|
FledastyLinkedListNode *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", *(int*)test_linked_list_node->value);
|
|
test_linked_list_node = test_linked_list_node->next;
|
|
}
|
|
|
|
if (fledasty_linked_list_has_value(&test_linked_list, &insert_value)) {
|
|
printf("Linked list contains %d\n", insert_value);
|
|
}
|
|
|
|
fledasty_linked_list_clear(&test_linked_list);
|
|
if (fledasty_linked_list_is_empty(&test_linked_list)) {
|
|
printf("Linked list is empty\n");
|
|
}
|
|
|
|
fledasty_linked_list_destroy(&test_linked_list);
|
|
|
|
FledastyDoublyLinkedList test_doubly_linked_list;
|
|
fledasty_doubly_linked_list_initialize(&test_doubly_linked_list, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int));
|
|
for (int i = 0; i < 10; i += 1) {
|
|
fledasty_doubly_linked_list_append(&test_doubly_linked_list, &i);
|
|
}
|
|
|
|
insert_value = 35;
|
|
fledasty_doubly_linked_list_insert_at_index(&test_doubly_linked_list, test_doubly_linked_list.size - 1, &insert_value);
|
|
insert_value = 28;
|
|
insert_at_value = 35;
|
|
fledasty_doubly_linked_list_insert_before_value(&test_doubly_linked_list, &insert_at_value, &insert_value);
|
|
insert_value = 90;
|
|
fledasty_doubly_linked_list_insert_after_value(&test_doubly_linked_list, &insert_at_value, &insert_value);
|
|
|
|
fledasty_doubly_linked_list_remove_at_index(&test_doubly_linked_list, 2);
|
|
remove_value = 0;
|
|
fledasty_doubly_linked_list_remove_value(&test_doubly_linked_list, &remove_value);
|
|
|
|
FledastyDoublyLinkedListNode *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", *(int*)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", *(int*)test_doubly_linked_list_node->value);
|
|
test_doubly_linked_list_node = test_doubly_linked_list_node->previous;
|
|
}
|
|
|
|
if (fledasty_doubly_linked_list_has_value(&test_doubly_linked_list, &insert_value)) {
|
|
printf("Doubly linked list contains %d\n", insert_value);
|
|
}
|
|
|
|
fledasty_doubly_linked_list_clear(&test_doubly_linked_list);
|
|
if (fledasty_doubly_linked_list_is_empty(&test_doubly_linked_list)) {
|
|
printf("Doubly linked list is empty\n");
|
|
}
|
|
|
|
fledasty_doubly_list_destroy(&test_doubly_linked_list);
|
|
|
|
FledastyHashTable test_hash_table;
|
|
fledasty_hash_table_initialize(&test_hash_table, sizeof(int), sizeof(int), integer_hash_function);
|
|
|
|
for (int i = 0; i < 10; i += 1) {
|
|
int pow_value = i * i;
|
|
fledasty_hash_table_insert(&test_hash_table, &pow_value, &i);
|
|
}
|
|
|
|
int hash_table_value = 5;
|
|
int hash_table_remove = 2;
|
|
fledasty_hash_table_insert(&test_hash_table, &hash_table_remove, &hash_table_value);
|
|
fledasty_hash_table_remove(&test_hash_table, &hash_table_remove);
|
|
|
|
if (fledasty_hash_table_get(&test_hash_table, &hash_table_remove) != NULL) {
|
|
printf("Value not removed from hash table\n");
|
|
}
|
|
|
|
for (int i = 0; i < 10; i += 1) {
|
|
int pow_value = i * i;
|
|
int *hash_table_value = (int*)fledasty_hash_table_get(&test_hash_table, &pow_value);
|
|
printf("Hash table get: %d\n", *hash_table_value);
|
|
}
|
|
|
|
hash_table_value = 4;
|
|
if (fledasty_hash_table_has_key(&test_hash_table, &hash_table_value)) {
|
|
printf("Hash table contains key %d\n", hash_table_value);
|
|
}
|
|
|
|
fledasty_hash_table_clear(&test_hash_table);
|
|
if (fledasty_hash_table_is_empty(&test_hash_table)) {
|
|
printf("Hash table is empty\n");
|
|
}
|
|
|
|
fledasty_hash_table_destroy(&test_hash_table);
|
|
FledastyUtf8String test_utf8_string;
|
|
unsigned char *test_string = (unsigned char*)"😀€Testing";
|
|
fledasty_utf8_string_initialize(&test_utf8_string, test_string, 15);
|
|
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);
|
|
|
|
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");
|
|
}
|
|
|
|
hallocy_free(invalid_utf8);
|
|
hallocy_free(unicode);
|
|
fledasty_utf8_string_destroy(&encoded_string);
|
|
|
|
fledasty_utf8_string_destroy(&test_utf8_string);
|
|
printf("Done\n");
|
|
return 0;
|
|
} |