diff --git a/config.mk b/config.mk index dfcbbb2..da3e0b8 100644 --- a/config.mk +++ b/config.mk @@ -7,7 +7,7 @@ SRC_DIR = src TEST_DIR = test THIRDPARTY_DIR = thirdparty -INC_DIRS = include $(THIRDPARTY_DIR)/include trie/include lsm/include +INC_DIRS = include $(THIRDPARTY_DIR)/include lsm/include LIBS = m lsm LIB_DIRS = ./lsm/build diff --git a/trie/Makefile b/trie/Makefile deleted file mode 100644 index 5352623..0000000 --- a/trie/Makefile +++ /dev/null @@ -1,95 +0,0 @@ -# https://spin.atomicobject.com/2016/08/26/makefile-c-projects/ was a great -# base for this Makefile - --include config.mk - -LIB := $(BUILD_DIR)/$(LIB_FILENAME) - -SRCS != find '$(SRC_DIR)' -iname '*.c' -SRCS_H != find $(INC_DIRS) -iname '*.h' -SRCS_H_INTERNAL != find $(SRC_DIR) -iname '*.h' -SRCS_TEST != find '$(TEST_DIR)' -iname '*.c' - -OBJS := $(SRCS:%=$(BUILD_DIR)/%.o) -OBJS_TEST := $(SRCS_TEST:%=$(BUILD_DIR)/%.o) -DEPS := $(SRCS:%=$(BUILD_DIR)/%.d) $(SRCS_TEST:%=$(BUILD_DIR)/%.d) - -BINS_TEST := $(OBJS_TEST:%.c.o=%) -TARGETS_TEST := $(BINS_TEST:%=test-%) -TARGETS_MEM_TEST := $(BINS_TEST:%=test-mem-%) - -_CFLAGS := $(addprefix -I,$(INC_DIRS)) $(CFLAGS) -Wall -Wextra - -.PHONY: all -all: lib - - -# =====COMPILATION===== -# Utility used by the CI to lint -.PHONY: objs -objs: $(OBJS) - -.PHONY: lib -lib: $(LIB) -$(LIB): $(OBJS) - ar -rcs $@ $(OBJS) - -$(BUILD_DIR)/$(SRC_DIR)/%.c.o: $(SRC_DIR)/%.c - mkdir -p $(dir $@) - $(CC) -c $(_CFLAGS) $< -o $@ - - -# =====TESTING===== -.PHONY: test -test: $(TARGETS_TEST) - -.PHONY: test-mem -test-mem: $(TARGETS_MEM_TEST) - -.PHONY: $(TARGETS_TEST) -$(TARGETS_TEST): test-%: % - ./$^ - -.PHONY: $(TARGETS_MEM_TEST) -$(TARGETS_MEM_TEST): test-mem-%: % - valgrind --tool=memcheck --error-exitcode=1 --track-origins=yes --leak-check=full ./$^ - -.PHONY: build-test -build-test: $(BINS_TEST) - -$(BINS_TEST): %: %.c.o $(LIB) - $(CC) \ - $^ -o $@ - -# Along with the include directory, each test includes $(TEST_DIR) (which -# contains the acutest.h header file), and the src directory of the module it's -# testing. This allows tests to access internal methods, which aren't publicly -# exposed. -$(BUILD_DIR)/$(TEST_DIR)/%.c.o: $(TEST_DIR)/%.c - mkdir -p $(dir $@) - $(CC) $(_CFLAGS) -I$(TEST_DIR) \ - -I$(dir $(@:$(BUILD_DIR)/$(TEST_DIR)/%=$(SRC_DIR)/%)) \ - -c $< -o $@ - -# =====MAINTENANCE===== -.PHONY: lint -lint: - clang-format -n --Werror $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL) - -.PHONY: fmt -fmt: - clang-format -i $(SRCS) $(SRCS_H) $(SRCS_H_INTERNAL) - -.PHONY: clean -clean: - rm -rf $(BUILD_DIR) - - -.PHONY: bear -bear: clean - bear -- make - bear --append -- make build-test - - -# Make make aware of the .d files --include $(DEPS) diff --git a/trie/README.md b/trie/README.md deleted file mode 100644 index a9f0802..0000000 --- a/trie/README.md +++ /dev/null @@ -1,16 +0,0 @@ -# Trie design - -The underlying data structure is based on a combination of a ternary and a -Patricia trie. - -* Nodes are classic ternary trie nodes, meaning each node contains a binary - search tree -* Each node can define a skip, like a Patricia trie, of at most 8 characters. - These skipped characters are stored directly in the structs defining the - nodes. -* While the add function relies on the fact that the input is a NULL-terminated - C string, the trie itself does not store any NULL bytes. - -The goal of this datastructure is to be as optimized as possible for search -operations with short (usually < 8 characters) keys, as this is by far the most -common operation for a URL shortener/pastebin. diff --git a/trie/config.mk b/trie/config.mk deleted file mode 100644 index 69eb2f8..0000000 --- a/trie/config.mk +++ /dev/null @@ -1,13 +0,0 @@ -LIB_FILENAME = libtrie.a - -BUILD_DIR = build -SRC_DIR = src -TEST_DIR = test -INC_DIRS = include - -# -MMD: generate a .d file for every source file. This file can be imported by -# make and makes make aware that a header file has been changed, ensuring an -# object file is also recompiled if only a header is changed. -# -MP: generate a dummy target for every header file (according to the docs it -# prevents some errors when removing header files) -CFLAGS = -MMD -MP -g diff --git a/trie/include/trie.h b/trie/include/trie.h deleted file mode 100644 index 9662ed4..0000000 --- a/trie/include/trie.h +++ /dev/null @@ -1,148 +0,0 @@ -#ifndef AD3_TERNARYTRIE -#define AD3_TERNARYTRIE - -#define ALPHABET_SIZE 256 -#define DELIMITER '\0' -#define MAX(x, y) (((x) > (y)) ? (x) : (y)) - -// Should not be higher than 254 or stuff will break -#define TRIE_MAX_SKIP_SIZE 8 - -/** - * The implementation of a Ternary Trie. - * - * Each node should be represented by a binary tree in order to reduce the - * memory usage. - */ - -#include -#include -#include - -static const char charset[] = - "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; -static const size_t charset_len = sizeof(charset) - 1; - -// Length of randomly generated keys -#define RANDOM_KEY_LENGTH_SHORT 4 -#define RANDOM_KEY_LENGTH_LONG 16 - -/** - * Type definition for the struct representing the current Trie. - * - * You can (and should) redefine this in your c-file with the concrete fields. - */ -typedef struct ttrie Trie; - -typedef enum entry_type { Redirect, Paste, Unknown } EntryType; - -typedef struct entry { - EntryType type; - char *string; -} Entry; - -typedef enum trie_exit_code { - Ok = 0, - NotFound, - AlreadyPresent, - FileError -} TrieExitCode; - -Entry *entry_new(EntryType type, const char *string); - -/** - * Allocate & initialize a new trie, and populate it with the data from the - * given data file. - * - * @return 0 if everything was successful, non-zero otherwise - */ -TrieExitCode trie_init(Trie **trie_ptr, const char *file_path); - -/** - * De-allocate a trie by freeing the memory occupied by this trie. - * - * @param trie which should be freed - */ -void trie_free(Trie *trie); - -/** - * Search for an entry in the trie. - * - * @param trie - * @param entry_ptr pointer to Entry will be stored here, if found - * @param key key representing the entry - * @return 0 if the search was successful, 1 if not found - */ -TrieExitCode trie_search(Trie *trie, Entry **entry_ptr, const char *key); - -TrieExitCode trie_search_len(Trie *trie, Entry **entry_ptr, const char *key, - size_t key_len); - -/** - * Add a string to this trie. - * - * @param trie - * @param key key to represent entry with - * @param entry entry to add - * @return 0 if added, 1 if already in trie, something else if other errors - */ -TrieExitCode trie_add(Trie *trie, const char *key, Entry *entry); - -TrieExitCode trie_add_len(Trie *trie, const char *key, size_t key_len, - Entry *entry); - -/** - * Add an entry by generating a random string as the key. - * - * @param trie - * @param entry entry to add - * @param secure whether to generate a longer, more secure random key - * @return pointer to the generated key. This pointer is safe to use after - * unlocking the trie, and should be freed manually. - */ -TrieExitCode trie_add_random(Trie *trie, char **key_ptr, Entry *entry, - bool secure); - -/** - * Remove an entry from this trie given its key. - * - * @param trie - * @param key key representing entry - * @return true if the entry was present and has been removed, false if it was - * not present - */ -bool trie_remove(Trie *trie, const char *key); - -/** - * Returns the number of entries in this trie. - * - * @param trie - * @return the number of entries in this trie - */ -size_t trie_size(Trie *trie); - -/* - * Acquire a read lock on the trie. - * - * @return 0 if successful, non-zero otherwise (return value of - * pthread_rwlock_rdlock) - */ -int trie_rlock(Trie *trie); - -/* - * Acquire a write lock on the trie. - * - * @return 0 if successful, non-zero otherwise (return value of - * pthread_rwlock_wrlock) - */ -int trie_wlock(Trie *trie); - -/* - * Release the lock on a trie after having acquired it beforehand. - * - * @return 0 if successful, non-zero otherwise (return value of - * pthread_rwlock_unlock) - */ -int trie_unlock(Trie *trie); - -#endif // AD3_TERNARYTRIE diff --git a/trie/src/trie/trie.c b/trie/src/trie/trie.c deleted file mode 100644 index b1262a7..0000000 --- a/trie/src/trie/trie.c +++ /dev/null @@ -1,428 +0,0 @@ -#include -#include -#include -#include -#include - -#include "trie.h" -#include "trie_entry.h" -#include "trie_node.h" - -typedef struct ttrie { - TrieNode *root; - size_t size; - char *file_path; - pthread_rwlock_t lock; -} Trie; - -TrieExitCode trie_add_no_lock(Trie *trie, const char *key, Entry *entry); - -/** - * Allocate and initialize an empty Trie - * - * @return pointer to the empty Trie - */ -TrieExitCode trie_init(Trie **trie_ptr, const char *file_path) { - // Allocate & initialize trie - Trie *trie = calloc(1, sizeof(Trie)); - trie->root = tnode_init(); - pthread_rwlock_init(&trie->lock, NULL); - - if (file_path == NULL) { - trie->file_path = NULL; - *trie_ptr = trie; - return Ok; - } - - trie->file_path = strdup(file_path); - - // Populate trie with data from file - FILE *fp = fopen(file_path, "r"); - - if (fp == NULL) { - return FileError; - } - - // We read in lines of at most 8192 characters (sounds like enough) - char buffer[8192]; - EntryType type; - Entry *entry; - int i, j; - TrieExitCode status; - - while (fgets(buffer, 8192, fp)) { - i = 0; - - // Move index in buffer until we encounter first space character - while (buffer[i] != ' ') { - i++; - } - - // Split the buffer into two strings, the key and the payload - buffer[i] = '\0'; - - type = entry_type_from_char(buffer[i + 1]); - - // Skip type character & its surrounding spaces - j = i + 3; - - // Now remove the newline character - while (buffer[j] != '\n') { - j++; - } - - buffer[j] = '\0'; - - entry = entry_new(type, buffer + i + 3); - status = trie_add_no_lock(trie, buffer, entry); - - if (status != Ok) { - trie_free(trie); - return status; - } - } - - fclose(fp); - - *trie_ptr = trie; - - return Ok; -} - -/** - * De-allocate a TernaryTree by freeing its entire underlying structure. - * - * @param trie trie to free - */ -void trie_free(Trie *trie) { - tnode_free(trie->root); - free(trie); -} - -typedef struct searchresult { - TrieNode *parent; - TrieNode *child; -} SearchResult; - -SearchResult trie_search_node_len(Trie *trie, const char *key, size_t key_len) { - SearchResult out = {NULL, NULL}; - - size_t i = 0; - TrieNode **node_ptr = &(trie->root); - TrieNode **child_ptr; - - do { - child_ptr = tnode_search(*node_ptr, key[i], false); - - // We don't have to check whether *node_ptr is NULL, because if it was - // NULL, it wouldn't be in the binary tree. - if (child_ptr == NULL) { - return out; - } - - i++; - - if (memcmp((*child_ptr)->string, key + i, (*child_ptr)->string_len) != 0) { - return out; - } - - i += (*child_ptr)->string_len; - - if (i < key_len) { - node_ptr = child_ptr; - } - } while (i < key_len); - - // At this point, we've either arrived at an empty child, or traversed through - // the entire string. Therefore, all we have to do is check whether we're at - // the end of the string and if node represents a string. - if (i == key_len && (*child_ptr)->represents) { - out.parent = *node_ptr; - out.child = *child_ptr; - } - - return out; -} - -SearchResult trie_search_node(Trie *trie, const char *key) { - return trie_search_node_len(trie, key, strlen(key)); -} - -/** - * Returns whether the given string is present in the trie. - * - * @param trie trie to look in - * @param string string to look up - * @return true if the string is present in the trie, false otherwise - */ -TrieExitCode trie_search_len(Trie *trie, Entry **entry_ptr, const char *key, - size_t key_len) { - SearchResult res = trie_search_node_len(trie, key, key_len); - - if (res.child == NULL) { - return NotFound; - } - - *entry_ptr = res.child->entry; - - return Ok; -} - -TrieExitCode trie_search(Trie *trie, Entry **entry_ptr, const char *key) { - return trie_search_len(trie, entry_ptr, key, strlen(key)); -} - -/** - * Add the given string to the Trie. - * - * @param trie trie to add string to - * @param string string to add - * @return true if the string wasn't present in the trie and thus added, false - * otherwise - */ -TrieExitCode trie_add_len_no_lock(Trie *trie, const char *key, size_t key_len, - Entry *entry) { - size_t i = 0; - uint8_t offset; - TrieNode **node_ptr = &(trie->root); - TrieNode **child_node_ptr; - TrieNode *child_node; - - do { - offset = 0; - child_node_ptr = tnode_search(*node_ptr, key[i], true); - - i++; - - // We've reached a NULL child, so we add the remaining part of the string - // here - if (*child_node_ptr == NULL) { - child_node = tnode_init(); - - while (offset < TRIE_MAX_SKIP_SIZE && i + offset < key_len) { - offset++; - } - - memcpy(child_node->string, key + i, offset); - - child_node->string_len = offset; - *child_node_ptr = child_node; - - // If the remaining part of the string is still longer than the maximum - // allowed skip length, we continue through the loop. The next iteration - // will enter this if statement again, and perform the same loop, until - // the string is fully added to the trie. - if (i + offset < key_len) { - node_ptr = child_node_ptr; - i += offset; - - continue; - } - - child_node->represents = true; - child_node->entry = entry; - - trie->size++; - return Ok; - } - - while (offset < (*child_node_ptr)->string_len) { - // String no longer aligns with edge, so we have to split - if (key[i + offset] != (*child_node_ptr)->string[offset]) { - TrieNode *split_node = tnode_init(); - child_node = *child_node_ptr; - - // New string of the split node is the prefix that we were able - // to skip - if (offset > 0) { - memcpy(split_node->string, child_node->string, offset); - split_node->string_len = offset; - } - - // split_node replaces child_node as the child of node - *child_node_ptr = split_node; - TrieNode **new_node_ptr = - tnode_search(split_node, child_node->string[offset], true); - *new_node_ptr = child_node; - - // child_node has now become a child of split_node, so we update its - // string accordingely by removing the skipped prefix + the one - // character that's already stored by being a child of split_node - /* char *old_string = child_node->string.ptr; */ - uint8_t new_skip_len = child_node->string_len - (offset + 1); - - if (new_skip_len > 0) { - char old_string[TRIE_MAX_SKIP_SIZE]; - memcpy(old_string, child_node->string + offset + 1, new_skip_len); - memcpy(child_node->string, old_string, new_skip_len); - } - - child_node->string_len = new_skip_len; - - // The while loop will exit either way after this has happened, as - // child_node is now split_node and split_node's len is already set to - // offset. - break; - } - - offset++; - } - - node_ptr = child_node_ptr; - - i += offset; - } while (i < key_len); - - if ((*child_node_ptr)->represents) { - return AlreadyPresent; - } - - (*child_node_ptr)->represents = true; - (*child_node_ptr)->entry = entry; - trie->size++; - return Ok; -} - -TrieExitCode trie_add_no_lock(Trie *trie, const char *key, Entry *entry) { - return trie_add_len_no_lock(trie, key, strlen(key), entry); -} - -TrieExitCode trie_add_len(Trie *trie, const char *key, size_t key_len, - Entry *entry) { - if (trie->file_path != NULL) { - // Easiest way to make sure we don't add duplicate entries - // We use an internal function that doesn't require a read lock, as we're - // already inside a write lock - if (trie_search_node_len(trie, key, key_len).child != NULL) { - return AlreadyPresent; - } - - FILE *fp = fopen(trie->file_path, "a"); - - if (fp == NULL) { - return FileError; - } - - fputs(key, fp); - fputs(" ", fp); - fputc(entry_type_to_char(entry->type), fp); - fputs(" ", fp); - fputs(entry->string, fp); - fputs("\n", fp); - - fclose(fp); - } - - // This function *should* always return Ok. Otherwise, the function would've - // exited because the string was found in the trie. - return trie_add_len_no_lock(trie, key, key_len, entry); -} - -TrieExitCode trie_add(Trie *trie, const char *key, Entry *entry) { - return trie_add_len(trie, key, strlen(key), entry); -} - -TrieExitCode trie_add_random(Trie *trie, char **key_ptr, Entry *entry, - bool secure) { - // Generate random key - bool ok = false; - int key_length = secure ? RANDOM_KEY_LENGTH_LONG : RANDOM_KEY_LENGTH_SHORT; - char *key = malloc(key_length + 1); - key[key_length] = '\0'; - - // We naively generate new keys until we find a key that isn't in the trie - // yet. With charset_len ** RANDOM_KEY_LENGTH sufficiently large, this isn't a - // problem, because the chances of collisions are extremely small. - while (!ok) { - for (int i = 0; i < key_length; i++) { - key[i] = charset[rand() % charset_len]; - } - - ok = trie_search_node(trie, key).child == NULL; - } - - TrieExitCode return_value = trie_add(trie, key, entry); - - if (return_value == Ok) { - *key_ptr = key; - } else { - free(key); - } - - return return_value; -} - -/** - * Remove the given string from a Trie. - * - * @param trie trie to remove string from - * @param string string to remove - * @return true if the string was in the trie and thus removed, false otherwise - */ -/* bool trie_remove(Trie *trie, const char *string) { */ -/* pthread_rwlock_wrlock(&trie->lock); */ - -/* bool return_value = false; */ - -/* SearchResult res = trie_search_node(trie, string); */ - -/* if (res.child == NULL) { */ -/* goto end; */ -/* } */ - -/* trie->size--; */ -/* return_value = true; */ - -/* if (res.parent != NULL) { */ -/* // We're removing a full leaf, so we calculate the offset of the - * character */ -/* // to remove from the parent */ -/* if (res.child->type == 2) { */ -/* size_t str_len = strlen(string); */ -/* size_t suffix_len = strlen(res.child->ptr.string); */ - -/* tnode_remove(res.parent, string[str_len - suffix_len - 1]); */ -/* } */ -/* // In the other case, the character to remove from the parent is the last - */ -/* // character of the string */ -/* else if (res.child->size == 0) { */ -/* size_t i = 0; */ - -/* while (string[i + 1] != DELIMITER) { */ -/* i++; */ -/* } */ - -/* tnode_remove(res.parent, string[i]); */ -/* } else { */ -/* res.child->type = 0; */ - -/* goto end; */ -/* } */ - -/* tnode_free(res.child); */ -/* } */ -/* // We're in the root here */ -/* else { */ -/* res.child->type = 0; */ -/* } */ - -/* end: */ -/* pthread_rwlock_unlock(&trie->lock); */ - -/* return return_value; */ -/* } */ - -/** - * Return the current size of the given trie. - * - * @param trie trie to return size for - * @return size of the trie - */ -size_t trie_size(Trie *trie) { return trie->size; } - -int trie_rlock(Trie *trie) { return pthread_rwlock_rdlock(&trie->lock); } - -int trie_wlock(Trie *trie) { return pthread_rwlock_wrlock(&trie->lock); } - -int trie_unlock(Trie *trie) { return pthread_rwlock_unlock(&trie->lock); } diff --git a/trie/src/trie/trie_entry.c b/trie/src/trie/trie_entry.c deleted file mode 100644 index 2aa8cdf..0000000 --- a/trie/src/trie/trie_entry.c +++ /dev/null @@ -1,37 +0,0 @@ -#include "trie_entry.h" -#include - -EntryType entry_type_from_char(char c) { - switch (c) { - case '0': - return Redirect; - case '1': - return Paste; - default: - return Unknown; - } -} - -char entry_type_to_char(EntryType et) { - switch (et) { - case Redirect: - return '0'; - case Paste: - return '1'; - default: - return '\0'; - } -} - -Entry *entry_new(EntryType type, const char *string) { - Entry *entry = malloc(sizeof(Entry)); - entry->type = type; - - if (string != NULL) { - entry->string = strdup(string); - } else { - entry->string = NULL; - } - - return entry; -} diff --git a/trie/src/trie/trie_entry.h b/trie/src/trie/trie_entry.h deleted file mode 100644 index b3ca8cd..0000000 --- a/trie/src/trie/trie_entry.h +++ /dev/null @@ -1,7 +0,0 @@ -#include "trie.h" - -EntryType entry_type_from_char(char c); - -char entry_type_to_char(EntryType et); - -Entry *entry_new(EntryType type, const char *string); diff --git a/trie/src/trie/trie_node.c b/trie/src/trie/trie_node.c deleted file mode 100644 index 0da5163..0000000 --- a/trie/src/trie/trie_node.c +++ /dev/null @@ -1,265 +0,0 @@ -#include -#include -#include - -#include "trie_node.h" - -/** - * Allocate and initialize a new TrieInnerNode representing a given - * character. - * - * @param c character to represent - * @return pointer to newly allocated struct - */ -TrieInnerNode *tinode_init(char c) { - TrieInnerNode *node = calloc(1, sizeof(TrieInnerNode)); - node->key = c; - - return node; -} - -/** - * Allocate and initialize a new TrieNode. - * - * @return pointer to newly allocated struct - */ -TrieNode *tnode_init() { - TrieNode *node = malloc(sizeof(TrieNode)); - - node->tree_size = 0; - node->string_len = 0; - node->represents = false; - - return node; -} - -/** - * Free a TrieInnerNode and its underlying tree structure. This should - * usually only be called on the root of a binary tree to free the entire - * structure. - * - * @param node node whose tree to free - */ -void tinode_free_cascade(TrieInnerNode *node) { - if (node->left != NULL) { - tinode_free_cascade(node->left); - } - - if (node->right != NULL) { - tinode_free_cascade(node->right); - } - - if (node->next != NULL) { - tnode_free(node->next); - } - - free(node); -} - -/** - * Free a TrieNode and its underlying tree structure. - * - * @param node node to free - */ -void tnode_free(TrieNode *node) { - if (node->tree_size > 0) { - tinode_free_cascade(node->tree); - } - - // TODO properly free entry - /* if (node->payload != NULL) { */ - /* free(node->payload); */ - /* } */ - - free(node); -} - -/** - * This function performs a lookup in the underlying binary tree of the given - * TrieNode. If found, the return value is a pointer to the memory - * location where the TrieInnerNode representing the given character - * stores its `next` field. If not found, the return value is NULL, unless - * `create` is true. - * - * NOTE: a non-NULL return value does not mean that the dereferenced value is - * also not NULL. In particular, if `create` is set to true and the function had - * to create the new node, the dereferenced value will always be NULL. - * - * @param node node to perform lookup in. If node is a full leaf, the return - * value will always be NULL, regardless of the value of create. - * @param create whether to create the TrieInnerNode if it isn't present - * yet. If this is set to true, the function will never return NULL unless the - * node represents a leaf with a string, because the struct and therefore the - * address is created if it doesn't exist yet. - */ -TrieNode **tnode_search(TrieNode *node, const char c, bool create) { - // It can happen that the node has no initialized root yet - if (node->tree_size == 0) { - if (create) { - node->tree_size++; - node->tree = tinode_init(c); - - return &node->tree->next; - } - - return NULL; - } - - TrieInnerNode *parent = node->tree; - TrieInnerNode *child; - - // Iterate through the tree until we either find the character or realize it's - // not present in the tree - // FIXME don't use while (1) - while (1) { - if (parent->key == c) { - return &parent->next; - } else if (c < parent->key) { - child = parent->left; - } else { - child = parent->right; - } - - if (child == NULL) { - break; - } - - parent = child; - }; - - // child is NULL, meaning the character isn't in the binary tree yet. - - // If create is true, we create the new node so that we can still return a - // non-NULL pointer. - if (create) { - TrieInnerNode *new_node = tinode_init(c); - - if (c < parent->key) { - parent->left = new_node; - } else { - parent->right = new_node; - } - - node->tree_size++; - - return &new_node->next; - } - - return NULL; -} - -/** - * Split a remaining string leaf node in two. This function assumes it receives - * a full leaf as its input. - * - * @param node node to split - */ -/* void tnode_split(TrieNode *node) { */ -/* TrieNode *new_node = tnode_init(); */ -/* char key = node->ptr.string[0]; */ - -/* // There's a chance the remaining string was only 1 character, meaning the - * new */ -/* // node doesn't have to store a string */ -/* if (node->ptr.string[1] != DELIMITER) { */ -/* tnode_set_string(new_node, node->ptr.string + 1); */ -/* } else { */ -/* new_node->type = 1; */ -/* } */ - -/* new_node->entry = node->entry; */ - -/* node->type = 0; */ -/* node->size = 0; */ -/* node->entry = NULL; */ - -/* free(node->ptr.string); */ -/* node->ptr.string = NULL; */ - -/* // Initialize node's binary tree with the correct character */ -/* TrieNode **node_ptr = tnode_search(node, key, true); */ -/* *node_ptr = new_node; */ -/* } */ - -/* - * Remove the given character from a TrieInnerNode's subtree. The - * function assumes the character is indeed in the subtree. - */ -void tinode_remove(TrieInnerNode *node, const char c) { - TrieInnerNode **to_remove_ptr = &node; - - // We use pointers to pointers here so we can later free the removed node - // without having to know what its parent is - while ((*to_remove_ptr)->key != c) { - to_remove_ptr = (c < (*to_remove_ptr)->key) ? &(*to_remove_ptr)->left - : &(*to_remove_ptr)->right; - }; - - // If the node isn't a leaf, we have to replace it with another - if ((*to_remove_ptr)->left != NULL || (*to_remove_ptr)->right != NULL) { - TrieInnerNode *to_replace = *to_remove_ptr; - - // Replace with its only right child - if (to_replace->left == NULL) { - TrieInnerNode *to_remove = to_replace->right; - - to_replace->key = to_remove->key; - to_replace->next = to_remove->next; - to_replace->left = to_remove->left; - to_replace->right = to_remove->right; - - free(to_remove); - } - // Replace with its only left child - else if (to_replace->right == NULL) { - TrieInnerNode *to_remove = to_replace->left; - - to_replace->key = to_remove->key; - to_replace->next = to_remove->next; - to_replace->left = to_remove->left; - to_replace->right = to_remove->right; - - free(to_remove); - } - // Node has two children, so replace with successor - else { - TrieInnerNode *to_remove_parent = to_replace; - TrieInnerNode *to_remove = to_replace->right; - - while (to_remove->left != NULL) { - to_remove_parent = to_remove; - to_remove = to_remove->left; - } - - to_replace->key = to_remove->key; - to_replace->next = to_remove->next; - - if (to_remove_parent != to_replace) { - to_remove_parent->left = to_remove->right; - } else { - to_remove_parent->right = to_remove->right; - } - - free(to_remove); - } - } - // We're the leaf, so we free ourselves - else { - free(*to_remove_ptr); - *to_remove_ptr = NULL; - } -} - -/** - * Remove the given character from a TrieNode, respecting the rules - * of a binary search tree. This function assumes the character is in the search - * tree. - * - * @param node node to remove character from - * @param c character to remove - */ -void tnode_remove(TrieNode *node, const char c) { - tinode_remove(node->tree, c); - - node->tree_size--; -} diff --git a/trie/src/trie/trie_node.h b/trie/src/trie/trie_node.h deleted file mode 100644 index 229f5a6..0000000 --- a/trie/src/trie/trie_node.h +++ /dev/null @@ -1,53 +0,0 @@ -#include - -#include "trie.h" - -/** - * Represents a node of the binary tree contained within each non-leaf - * TrieNode. - */ -typedef struct tinode { - struct tinode *left; - struct tinode *right; - struct tnode *next; - char key; -} TrieInnerNode; - -/** - * Represents a node inside a Trie. A node can be in one of three states: - * - Internal node: a node that's part of a path to a leaf node. This node will - * always have a size greater than one, and an initialized root. - * - Leaf: a node solely used to represent a string ending there. Its size is 0, - * its ptr is unitialized and represents is true. - * - Full leaf: a leaf node that contains a string. This occurs when a string is - * added whose path is not fully in the tree yet, causing its remaining suffix - * to be stored as a single node. Its size will be zero, represents its true, - * and its string pointer is initialized. - */ -typedef struct tnode { - Entry *entry; - - TrieInnerNode *tree; - uint8_t tree_size; - - // Skips are at most TRIE_MAX_SKIP_SIZE characters, and are stored in the - // nodes - char string[TRIE_MAX_SKIP_SIZE]; - uint8_t string_len; - - bool represents; -} TrieNode; - -TrieInnerNode *tinode_init(char c); - -TrieNode *tnode_init(); - -void tinode_free_cascade(TrieInnerNode *node); - -void tnode_free(TrieNode *node); - -TrieNode **tnode_search(TrieNode *node, const char c, bool create); - -void tinode_remove(TrieInnerNode *node, const char c); - -void tnode_remove(TrieNode *node, const char c); diff --git a/trie/test/test.h b/trie/test/test.h deleted file mode 100644 index 9ab8f88..0000000 --- a/trie/test/test.h +++ /dev/null @@ -1,1839 +0,0 @@ -/* - * Acutest -- Another C/C++ Unit Test facility - * - * - * Copyright 2013-2020 Martin Mitas - * Copyright 2019 Garrett D'Amore - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. - */ - -#ifndef ACUTEST_H -#define ACUTEST_H - - -/************************ - *** Public interface *** - ************************/ - -/* By default, "acutest.h" provides the main program entry point (function - * main()). However, if the test suite is composed of multiple source files - * which include "acutest.h", then this causes a problem of multiple main() - * definitions. To avoid this problem, #define macro TEST_NO_MAIN in all - * compilation units but one. - */ - -/* Macro to specify list of unit tests in the suite. - * The unit test implementation MUST provide list of unit tests it implements - * with this macro: - * - * TEST_LIST = { - * { "test1_name", test1_func_ptr }, - * { "test2_name", test2_func_ptr }, - * ... - * { NULL, NULL } // zeroed record marking the end of the list - * }; - * - * The list specifies names of each test (must be unique) and pointer to - * a function implementing it. The function does not take any arguments - * and has no return values, i.e. every test function has to be compatible - * with this prototype: - * - * void test_func(void); - * - * Note the list has to be ended with a zeroed record. - */ -#define TEST_LIST const struct acutest_test_ acutest_list_[] - - -/* Macros for testing whether an unit test succeeds or fails. These macros - * can be used arbitrarily in functions implementing the unit tests. - * - * If any condition fails throughout execution of a test, the test fails. - * - * TEST_CHECK takes only one argument (the condition), TEST_CHECK_ allows - * also to specify an error message to print out if the condition fails. - * (It expects printf-like format string and its parameters). The macros - * return non-zero (condition passes) or 0 (condition fails). - * - * That can be useful when more conditions should be checked only if some - * preceding condition passes, as illustrated in this code snippet: - * - * SomeStruct* ptr = allocate_some_struct(); - * if(TEST_CHECK(ptr != NULL)) { - * TEST_CHECK(ptr->member1 < 100); - * TEST_CHECK(ptr->member2 > 200); - * } - */ -#define TEST_CHECK_(cond,...) acutest_check_((cond), __FILE__, __LINE__, __VA_ARGS__) -#define TEST_CHECK(cond) acutest_check_((cond), __FILE__, __LINE__, "%s", #cond) - - -/* These macros are the same as TEST_CHECK_ and TEST_CHECK except that if the - * condition fails, the currently executed unit test is immediately aborted. - * - * That is done either by calling abort() if the unit test is executed as a - * child process; or via longjmp() if the unit test is executed within the - * main Acutest process. - * - * As a side effect of such abortion, your unit tests may cause memory leaks, - * unflushed file descriptors, and other phenomena caused by the abortion. - * - * Therefore you should not use these as a general replacement for TEST_CHECK. - * Use it with some caution, especially if your test causes some other side - * effects to the outside world (e.g. communicating with some server, inserting - * into a database etc.). - */ -#define TEST_ASSERT_(cond,...) \ - do { \ - if(!acutest_check_((cond), __FILE__, __LINE__, __VA_ARGS__)) \ - acutest_abort_(); \ - } while(0) -#define TEST_ASSERT(cond) \ - do { \ - if(!acutest_check_((cond), __FILE__, __LINE__, "%s", #cond)) \ - acutest_abort_(); \ - } while(0) - - -#ifdef __cplusplus -/* Macros to verify that the code (the 1st argument) throws exception of given - * type (the 2nd argument). (Note these macros are only available in C++.) - * - * TEST_EXCEPTION_ is like TEST_EXCEPTION but accepts custom printf-like - * message. - * - * For example: - * - * TEST_EXCEPTION(function_that_throw(), ExpectedExceptionType); - * - * If the function_that_throw() throws ExpectedExceptionType, the check passes. - * If the function throws anything incompatible with ExpectedExceptionType - * (or if it does not thrown an exception at all), the check fails. - */ -#define TEST_EXCEPTION(code, exctype) \ - do { \ - bool exc_ok_ = false; \ - const char *msg_ = NULL; \ - try { \ - code; \ - msg_ = "No exception thrown."; \ - } catch(exctype const&) { \ - exc_ok_= true; \ - } catch(...) { \ - msg_ = "Unexpected exception thrown."; \ - } \ - acutest_check_(exc_ok_, __FILE__, __LINE__, #code " throws " #exctype);\ - if(msg_ != NULL) \ - acutest_message_("%s", msg_); \ - } while(0) -#define TEST_EXCEPTION_(code, exctype, ...) \ - do { \ - bool exc_ok_ = false; \ - const char *msg_ = NULL; \ - try { \ - code; \ - msg_ = "No exception thrown."; \ - } catch(exctype const&) { \ - exc_ok_= true; \ - } catch(...) { \ - msg_ = "Unexpected exception thrown."; \ - } \ - acutest_check_(exc_ok_, __FILE__, __LINE__, __VA_ARGS__); \ - if(msg_ != NULL) \ - acutest_message_("%s", msg_); \ - } while(0) -#endif /* #ifdef __cplusplus */ - - -/* Sometimes it is useful to split execution of more complex unit tests to some - * smaller parts and associate those parts with some names. - * - * This is especially handy if the given unit test is implemented as a loop - * over some vector of multiple testing inputs. Using these macros allow to use - * sort of subtitle for each iteration of the loop (e.g. outputting the input - * itself or a name associated to it), so that if any TEST_CHECK condition - * fails in the loop, it can be easily seen which iteration triggers the - * failure, without the need to manually output the iteration-specific data in - * every single TEST_CHECK inside the loop body. - * - * TEST_CASE allows to specify only single string as the name of the case, - * TEST_CASE_ provides all the power of printf-like string formatting. - * - * Note that the test cases cannot be nested. Starting a new test case ends - * implicitly the previous one. To end the test case explicitly (e.g. to end - * the last test case after exiting the loop), you may use TEST_CASE(NULL). - */ -#define TEST_CASE_(...) acutest_case_(__VA_ARGS__) -#define TEST_CASE(name) acutest_case_("%s", name) - - -/* Maximal output per TEST_CASE call. Longer messages are cut. - * You may define another limit prior including "acutest.h" - */ -#ifndef TEST_CASE_MAXSIZE -#define TEST_CASE_MAXSIZE 64 -#endif - - -/* printf-like macro for outputting an extra information about a failure. - * - * Intended use is to output some computed output versus the expected value, - * e.g. like this: - * - * if(!TEST_CHECK(produced == expected)) { - * TEST_MSG("Expected: %d", expected); - * TEST_MSG("Produced: %d", produced); - * } - * - * Note the message is only written down if the most recent use of any checking - * macro (like e.g. TEST_CHECK or TEST_EXCEPTION) in the current test failed. - * This means the above is equivalent to just this: - * - * TEST_CHECK(produced == expected); - * TEST_MSG("Expected: %d", expected); - * TEST_MSG("Produced: %d", produced); - * - * The macro can deal with multi-line output fairly well. It also automatically - * adds a final new-line if there is none present. - */ -#define TEST_MSG(...) acutest_message_(__VA_ARGS__) - - -/* Maximal output per TEST_MSG call. Longer messages are cut. - * You may define another limit prior including "acutest.h" - */ -#ifndef TEST_MSG_MAXSIZE -#define TEST_MSG_MAXSIZE 1024 -#endif - - -/* Macro for dumping a block of memory. - * - * Its intended use is very similar to what TEST_MSG is for, but instead of - * generating any printf-like message, this is for dumping raw block of a - * memory in a hexadecimal form: - * - * TEST_CHECK(size_produced == size_expected && - * memcmp(addr_produced, addr_expected, size_produced) == 0); - * TEST_DUMP("Expected:", addr_expected, size_expected); - * TEST_DUMP("Produced:", addr_produced, size_produced); - */ -#define TEST_DUMP(title, addr, size) acutest_dump_(title, addr, size) - -/* Maximal output per TEST_DUMP call (in bytes to dump). Longer blocks are cut. - * You may define another limit prior including "acutest.h" - */ -#ifndef TEST_DUMP_MAXSIZE -#define TEST_DUMP_MAXSIZE 1024 -#endif - - -/* Common test initialiation/clean-up - * - * In some test suites, it may be needed to perform some sort of the same - * initialization and/or clean-up in all the tests. - * - * Such test suites may use macros TEST_INIT and/or TEST_FINI prior including - * this header. The expansion of the macro is then used as a body of helper - * function called just before executing every single (TEST_INIT) or just after - * it ends (TEST_FINI). - * - * Examples of various ways how to use the macro TEST_INIT: - * - * #define TEST_INIT my_init_func(); - * #define TEST_INIT my_init_func() // Works even without the semicolon - * #define TEST_INIT setlocale(LC_ALL, NULL); - * #define TEST_INIT { setlocale(LC_ALL, NULL); my_init_func(); } - * - * TEST_FINI is to be used in the same way. - */ - - -/********************** - *** Implementation *** - **********************/ - -/* The unit test files should not rely on anything below. */ - -#include -#include -#include -#include -#include -#include - -#if defined(unix) || defined(__unix__) || defined(__unix) || defined(__APPLE__) -#define ACUTEST_UNIX_ 1 -#include -#include -#include -#include -#include -#include -#include - -#if defined CLOCK_PROCESS_CPUTIME_ID && defined CLOCK_MONOTONIC -#define ACUTEST_HAS_POSIX_TIMER_ 1 -#endif -#endif - -#if defined(_gnu_linux_) || defined(__linux__) -#define ACUTEST_LINUX_ 1 -#include -#include -#endif - -#if defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__) -#define ACUTEST_WIN_ 1 - #include - #include -#endif - -#if defined(__APPLE__) -#define ACUTEST_MACOS_ - #include - #include - #include - #include - #include -#endif - -#ifdef __cplusplus -#include -#endif - -#ifdef __has_include -#if __has_include() -#include -#endif -#endif - -/* Enable the use of the non-standard keyword __attribute__ to silence warnings under some compilers */ -#if defined(__GNUC__) || defined(__clang__) -#define ACUTEST_ATTRIBUTE_(attr) __attribute__((attr)) -#else -#define ACUTEST_ATTRIBUTE_(attr) -#endif - -/* Note our global private identifiers end with '_' to mitigate risk of clash - * with the unit tests implementation. */ - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef _MSC_VER -/* In the multi-platform code like ours, we cannot use the non-standard - * "safe" functions from Microsoft C lib like e.g. sprintf_s() instead of - * standard sprintf(). Hence, lets disable the warning C4996. */ - #pragma warning(push) - #pragma warning(disable: 4996) -#endif - - -struct acutest_test_ { - const char* name; - void (*func)(void); -}; - -struct acutest_test_data_ { - unsigned char flags; - double duration; -}; - -enum { - ACUTEST_FLAG_RUN_ = 1 << 0, - ACUTEST_FLAG_SUCCESS_ = 1 << 1, - ACUTEST_FLAG_FAILURE_ = 1 << 2, -}; - -extern const struct acutest_test_ acutest_list_[]; - -int acutest_check_(int cond, const char* file, int line, const char* fmt, ...); -void acutest_case_(const char* fmt, ...); -void acutest_message_(const char* fmt, ...); -void acutest_dump_(const char* title, const void* addr, size_t size); -void acutest_abort_(void) ACUTEST_ATTRIBUTE_(noreturn); - - -#ifndef TEST_NO_MAIN - -static char* acutest_argv0_ = NULL; -static size_t acutest_list_size_ = 0; -static struct acutest_test_data_* acutest_test_data_ = NULL; -static size_t acutest_count_ = 0; -static int acutest_no_exec_ = -1; -static int acutest_no_summary_ = 0; -static int acutest_tap_ = 0; -static int acutest_skip_mode_ = 0; -static int acutest_worker_ = 0; -static int acutest_worker_index_ = 0; -static int acutest_cond_failed_ = 0; -static int acutest_was_aborted_ = 0; -static FILE *acutest_xml_output_ = NULL; - -static int acutest_stat_failed_units_ = 0; -static int acutest_stat_run_units_ = 0; - -static const struct acutest_test_* acutest_current_test_ = NULL; -static int acutest_current_index_ = 0; -static char acutest_case_name_[TEST_CASE_MAXSIZE] = ""; -static int acutest_test_already_logged_ = 0; -static int acutest_case_already_logged_ = 0; -static int acutest_verbose_level_ = 2; -static int acutest_test_failures_ = 0; -static int acutest_colorize_ = 0; -static int acutest_timer_ = 0; - -static int acutest_abort_has_jmp_buf_ = 0; -static jmp_buf acutest_abort_jmp_buf_; - - -static void -acutest_cleanup_(void) -{ - free((void*) acutest_test_data_); -} - -static void ACUTEST_ATTRIBUTE_(noreturn) -acutest_exit_(int exit_code) -{ - acutest_cleanup_(); - exit(exit_code); -} - -#if defined ACUTEST_WIN_ -typedef LARGE_INTEGER acutest_timer_type_; - static LARGE_INTEGER acutest_timer_freq_; - static acutest_timer_type_ acutest_timer_start_; - static acutest_timer_type_ acutest_timer_end_; - - static void - acutest_timer_init_(void) - { - QueryPerformanceFrequency(´st_timer_freq_); - } - - static void - acutest_timer_get_time_(LARGE_INTEGER* ts) - { - QueryPerformanceCounter(ts); - } - - static double - acutest_timer_diff_(LARGE_INTEGER start, LARGE_INTEGER end) - { - double duration = (double)(end.QuadPart - start.QuadPart); - duration /= (double)acutest_timer_freq_.QuadPart; - return duration; - } - - static void - acutest_timer_print_diff_(void) - { - printf("%.6lf secs", acutest_timer_diff_(acutest_timer_start_, acutest_timer_end_)); - } -#elif defined ACUTEST_HAS_POSIX_TIMER_ -static clockid_t acutest_timer_id_; -typedef struct timespec acutest_timer_type_; -static acutest_timer_type_ acutest_timer_start_; -static acutest_timer_type_ acutest_timer_end_; - -static void -acutest_timer_init_(void) -{ - if(acutest_timer_ == 1) - acutest_timer_id_ = CLOCK_MONOTONIC; - else if(acutest_timer_ == 2) - acutest_timer_id_ = CLOCK_PROCESS_CPUTIME_ID; -} - -static void -acutest_timer_get_time_(struct timespec* ts) -{ - clock_gettime(acutest_timer_id_, ts); -} - -static double -acutest_timer_diff_(struct timespec start, struct timespec end) -{ - double endns; - double startns; - - endns = end.tv_sec; - endns *= 1e9; - endns += end.tv_nsec; - - startns = start.tv_sec; - startns *= 1e9; - startns += start.tv_nsec; - - return ((endns - startns)/ 1e9); -} - -static void -acutest_timer_print_diff_(void) -{ - printf("%.6lf secs", - acutest_timer_diff_(acutest_timer_start_, acutest_timer_end_)); -} -#else -typedef int acutest_timer_type_; - static acutest_timer_type_ acutest_timer_start_; - static acutest_timer_type_ acutest_timer_end_; - - void - acutest_timer_init_(void) - {} - - static void - acutest_timer_get_time_(int* ts) - { - (void) ts; - } - - static double - acutest_timer_diff_(int start, int end) - { - (void) start; - (void) end; - return 0.0; - } - - static void - acutest_timer_print_diff_(void) - {} -#endif - -#define ACUTEST_COLOR_DEFAULT_ 0 -#define ACUTEST_COLOR_GREEN_ 1 -#define ACUTEST_COLOR_RED_ 2 -#define ACUTEST_COLOR_DEFAULT_INTENSIVE_ 3 -#define ACUTEST_COLOR_GREEN_INTENSIVE_ 4 -#define ACUTEST_COLOR_RED_INTENSIVE_ 5 - -static int ACUTEST_ATTRIBUTE_(format (printf, 2, 3)) -acutest_colored_printf_(int color, const char* fmt, ...) -{ - va_list args; - char buffer[256]; - int n; - - va_start(args, fmt); - vsnprintf(buffer, sizeof(buffer), fmt, args); - va_end(args); - buffer[sizeof(buffer)-1] = '\0'; - - if(!acutest_colorize_) { - return printf("%s", buffer); - } - -#if defined ACUTEST_UNIX_ - { - const char* col_str; - switch(color) { - case ACUTEST_COLOR_GREEN_: col_str = "\033[0;32m"; break; - case ACUTEST_COLOR_RED_: col_str = "\033[0;31m"; break; - case ACUTEST_COLOR_GREEN_INTENSIVE_: col_str = "\033[1;32m"; break; - case ACUTEST_COLOR_RED_INTENSIVE_: col_str = "\033[1;31m"; break; - case ACUTEST_COLOR_DEFAULT_INTENSIVE_: col_str = "\033[1m"; break; - default: col_str = "\033[0m"; break; - } - printf("%s", col_str); - n = printf("%s", buffer); - printf("\033[0m"); - return n; - } -#elif defined ACUTEST_WIN_ - { - HANDLE h; - CONSOLE_SCREEN_BUFFER_INFO info; - WORD attr; - - h = GetStdHandle(STD_OUTPUT_HANDLE); - GetConsoleScreenBufferInfo(h, &info); - - switch(color) { - case ACUTEST_COLOR_GREEN_: attr = FOREGROUND_GREEN; break; - case ACUTEST_COLOR_RED_: attr = FOREGROUND_RED; break; - case ACUTEST_COLOR_GREEN_INTENSIVE_: attr = FOREGROUND_GREEN | FOREGROUND_INTENSITY; break; - case ACUTEST_COLOR_RED_INTENSIVE_: attr = FOREGROUND_RED | FOREGROUND_INTENSITY; break; - case ACUTEST_COLOR_DEFAULT_INTENSIVE_: attr = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY; break; - default: attr = 0; break; - } - if(attr != 0) - SetConsoleTextAttribute(h, attr); - n = printf("%s", buffer); - SetConsoleTextAttribute(h, info.wAttributes); - return n; - } -#else - n = printf("%s", buffer); - return n; -#endif -} - -static void -acutest_begin_test_line_(const struct acutest_test_* test) -{ - if(!acutest_tap_) { - if(acutest_verbose_level_ >= 3) { - acutest_colored_printf_(ACUTEST_COLOR_DEFAULT_INTENSIVE_, "Test %s:\n", test->name); - acutest_test_already_logged_++; - } else if(acutest_verbose_level_ >= 1) { - int n; - char spaces[48]; - - n = acutest_colored_printf_(ACUTEST_COLOR_DEFAULT_INTENSIVE_, "Test %s... ", test->name); - memset(spaces, ' ', sizeof(spaces)); - if(n < (int) sizeof(spaces)) - printf("%.*s", (int) sizeof(spaces) - n, spaces); - } else { - acutest_test_already_logged_ = 1; - } - } -} - -static void -acutest_finish_test_line_(int result) -{ - if(acutest_tap_) { - const char* str = (result == 0) ? "ok" : "not ok"; - - printf("%s %d - %s\n", str, acutest_current_index_ + 1, acutest_current_test_->name); - - if(result == 0 && acutest_timer_) { - printf("# Duration: "); - acutest_timer_print_diff_(); - printf("\n"); - } - } else { - int color = (result == 0) ? ACUTEST_COLOR_GREEN_INTENSIVE_ : ACUTEST_COLOR_RED_INTENSIVE_; - const char* str = (result == 0) ? "OK" : "FAILED"; - printf("[ "); - acutest_colored_printf_(color, "%s", str); - printf(" ]"); - - if(result == 0 && acutest_timer_) { - printf(" "); - acutest_timer_print_diff_(); - } - - printf("\n"); - } -} - -static void -acutest_line_indent_(int level) -{ - static const char spaces[] = " "; - int n = level * 2; - - if(acutest_tap_ && n > 0) { - n--; - printf("#"); - } - - while(n > 16) { - printf("%s", spaces); - n -= 16; - } - printf("%.*s", n, spaces); -} - -int ACUTEST_ATTRIBUTE_(format (printf, 4, 5)) -acutest_check_(int cond, const char* file, int line, const char* fmt, ...) -{ - const char *result_str; - int result_color; - int verbose_level; - - if(cond) { - result_str = "ok"; - result_color = ACUTEST_COLOR_GREEN_; - verbose_level = 3; - } else { - if(!acutest_test_already_logged_ && acutest_current_test_ != NULL) - acutest_finish_test_line_(-1); - - result_str = "failed"; - result_color = ACUTEST_COLOR_RED_; - verbose_level = 2; - acutest_test_failures_++; - acutest_test_already_logged_++; - } - - if(acutest_verbose_level_ >= verbose_level) { - va_list args; - - if(!acutest_case_already_logged_ && acutest_case_name_[0]) { - acutest_line_indent_(1); - acutest_colored_printf_(ACUTEST_COLOR_DEFAULT_INTENSIVE_, "Case %s:\n", acutest_case_name_); - acutest_test_already_logged_++; - acutest_case_already_logged_++; - } - - acutest_line_indent_(acutest_case_name_[0] ? 2 : 1); - if(file != NULL) { -#ifdef ACUTEST_WIN_ - const char* lastsep1 = strrchr(file, '\\'); - const char* lastsep2 = strrchr(file, '/'); - if(lastsep1 == NULL) - lastsep1 = file-1; - if(lastsep2 == NULL) - lastsep2 = file-1; - file = (lastsep1 > lastsep2 ? lastsep1 : lastsep2) + 1; -#else - const char* lastsep = strrchr(file, '/'); - if(lastsep != NULL) - file = lastsep+1; -#endif - printf("%s:%d: Check ", file, line); - } - - va_start(args, fmt); - vprintf(fmt, args); - va_end(args); - - printf("... "); - acutest_colored_printf_(result_color, "%s", result_str); - printf("\n"); - acutest_test_already_logged_++; - } - - acutest_cond_failed_ = (cond == 0); - return !acutest_cond_failed_; -} - -void ACUTEST_ATTRIBUTE_(format (printf, 1, 2)) -acutest_case_(const char* fmt, ...) -{ - va_list args; - - if(acutest_verbose_level_ < 2) - return; - - if(acutest_case_name_[0]) { - acutest_case_already_logged_ = 0; - acutest_case_name_[0] = '\0'; - } - - if(fmt == NULL) - return; - - va_start(args, fmt); - vsnprintf(acutest_case_name_, sizeof(acutest_case_name_) - 1, fmt, args); - va_end(args); - acutest_case_name_[sizeof(acutest_case_name_) - 1] = '\0'; - - if(acutest_verbose_level_ >= 3) { - acutest_line_indent_(1); - acutest_colored_printf_(ACUTEST_COLOR_DEFAULT_INTENSIVE_, "Case %s:\n", acutest_case_name_); - acutest_test_already_logged_++; - acutest_case_already_logged_++; - } -} - -void ACUTEST_ATTRIBUTE_(format (printf, 1, 2)) -acutest_message_(const char* fmt, ...) -{ - char buffer[TEST_MSG_MAXSIZE]; - char* line_beg; - char* line_end; - va_list args; - - if(acutest_verbose_level_ < 2) - return; - - /* We allow extra message only when something is already wrong in the - * current test. */ - if(acutest_current_test_ == NULL || !acutest_cond_failed_) - return; - - va_start(args, fmt); - vsnprintf(buffer, TEST_MSG_MAXSIZE, fmt, args); - va_end(args); - buffer[TEST_MSG_MAXSIZE-1] = '\0'; - - line_beg = buffer; - while(1) { - line_end = strchr(line_beg, '\n'); - if(line_end == NULL) - break; - acutest_line_indent_(acutest_case_name_[0] ? 3 : 2); - printf("%.*s\n", (int)(line_end - line_beg), line_beg); - line_beg = line_end + 1; - } - if(line_beg[0] != '\0') { - acutest_line_indent_(acutest_case_name_[0] ? 3 : 2); - printf("%s\n", line_beg); - } -} - -void -acutest_dump_(const char* title, const void* addr, size_t size) -{ - static const size_t BYTES_PER_LINE = 16; - size_t line_beg; - size_t truncate = 0; - - if(acutest_verbose_level_ < 2) - return; - - /* We allow extra message only when something is already wrong in the - * current test. */ - if(acutest_current_test_ == NULL || !acutest_cond_failed_) - return; - - if(size > TEST_DUMP_MAXSIZE) { - truncate = size - TEST_DUMP_MAXSIZE; - size = TEST_DUMP_MAXSIZE; - } - - acutest_line_indent_(acutest_case_name_[0] ? 3 : 2); - printf((title[strlen(title)-1] == ':') ? "%s\n" : "%s:\n", title); - - for(line_beg = 0; line_beg < size; line_beg += BYTES_PER_LINE) { - size_t line_end = line_beg + BYTES_PER_LINE; - size_t off; - - acutest_line_indent_(acutest_case_name_[0] ? 4 : 3); - printf("%08lx: ", (unsigned long)line_beg); - for(off = line_beg; off < line_end; off++) { - if(off < size) - printf(" %02x", ((const unsigned char*)addr)[off]); - else - printf(" "); - } - - printf(" "); - for(off = line_beg; off < line_end; off++) { - unsigned char byte = ((const unsigned char*)addr)[off]; - if(off < size) - printf("%c", (iscntrl(byte) ? '.' : byte)); - else - break; - } - - printf("\n"); - } - - if(truncate > 0) { - acutest_line_indent_(acutest_case_name_[0] ? 4 : 3); - printf(" ... (and more %u bytes)\n", (unsigned) truncate); - } -} - -/* This is called just before each test */ -static void -acutest_init_(const char *test_name) -{ -#ifdef TEST_INIT - TEST_INIT - ; /* Allow for a single unterminated function call */ -#endif - - /* Suppress any warnings about unused variable. */ - (void) test_name; -} - -/* This is called after each test */ -static void -acutest_fini_(const char *test_name) -{ -#ifdef TEST_FINI - TEST_FINI - ; /* Allow for a single unterminated function call */ -#endif - - /* Suppress any warnings about unused variable. */ - (void) test_name; -} - -void -acutest_abort_(void) -{ - if(acutest_abort_has_jmp_buf_) { - longjmp(acutest_abort_jmp_buf_, 1); - } else { - if(acutest_current_test_ != NULL) - acutest_fini_(acutest_current_test_->name); - abort(); - } -} - -static void -acutest_list_names_(void) -{ - const struct acutest_test_* test; - - printf("Unit tests:\n"); - for(test = ´st_list_[0]; test->func != NULL; test++) - printf(" %s\n", test->name); -} - -static void -acutest_remember_(int i) -{ - if(acutest_test_data_[i].flags & ACUTEST_FLAG_RUN_) - return; - - acutest_test_data_[i].flags |= ACUTEST_FLAG_RUN_; - acutest_count_++; -} - -static void -acutest_set_success_(int i, int success) -{ - acutest_test_data_[i].flags |= success ? ACUTEST_FLAG_SUCCESS_ : ACUTEST_FLAG_FAILURE_; -} - -static void -acutest_set_duration_(int i, double duration) -{ - acutest_test_data_[i].duration = duration; -} - -static int -acutest_name_contains_word_(const char* name, const char* pattern) -{ - static const char word_delim[] = " \t-_/.,:;"; - const char* substr; - size_t pattern_len; - - pattern_len = strlen(pattern); - - substr = strstr(name, pattern); - while(substr != NULL) { - int starts_on_word_boundary = (substr == name || strchr(word_delim, substr[-1]) != NULL); - int ends_on_word_boundary = (substr[pattern_len] == '\0' || strchr(word_delim, substr[pattern_len]) != NULL); - - if(starts_on_word_boundary && ends_on_word_boundary) - return 1; - - substr = strstr(substr+1, pattern); - } - - return 0; -} - -static int -acutest_lookup_(const char* pattern) -{ - int i; - int n = 0; - - /* Try exact match. */ - for(i = 0; i < (int) acutest_list_size_; i++) { - if(strcmp(acutest_list_[i].name, pattern) == 0) { - acutest_remember_(i); - n++; - break; - } - } - if(n > 0) - return n; - - /* Try word match. */ - for(i = 0; i < (int) acutest_list_size_; i++) { - if(acutest_name_contains_word_(acutest_list_[i].name, pattern)) { - acutest_remember_(i); - n++; - } - } - if(n > 0) - return n; - - /* Try relaxed match. */ - for(i = 0; i < (int) acutest_list_size_; i++) { - if(strstr(acutest_list_[i].name, pattern) != NULL) { - acutest_remember_(i); - n++; - } - } - - return n; -} - - -/* Called if anything goes bad in Acutest, or if the unit test ends in other - * way then by normal returning from its function (e.g. exception or some - * abnormal child process termination). */ -static void ACUTEST_ATTRIBUTE_(format (printf, 1, 2)) -acutest_error_(const char* fmt, ...) -{ - if(acutest_verbose_level_ == 0) - return; - - if(acutest_verbose_level_ >= 2) { - va_list args; - - acutest_line_indent_(1); - if(acutest_verbose_level_ >= 3) - acutest_colored_printf_(ACUTEST_COLOR_RED_INTENSIVE_, "ERROR: "); - va_start(args, fmt); - vprintf(fmt, args); - va_end(args); - printf("\n"); - } - - if(acutest_verbose_level_ >= 3) { - printf("\n"); - } -} - -/* Call directly the given test unit function. */ -static int -acutest_do_run_(const struct acutest_test_* test, int index) -{ - int status = -1; - - acutest_was_aborted_ = 0; - acutest_current_test_ = test; - acutest_current_index_ = index; - acutest_test_failures_ = 0; - acutest_test_already_logged_ = 0; - acutest_cond_failed_ = 0; - -#ifdef __cplusplus - try { -#endif - acutest_init_(test->name); - acutest_begin_test_line_(test); - - /* This is good to do in case the test unit crashes. */ - fflush(stdout); - fflush(stderr); - - if(!acutest_worker_) { - acutest_abort_has_jmp_buf_ = 1; - if(setjmp(acutest_abort_jmp_buf_) != 0) { - acutest_was_aborted_ = 1; - goto aborted; - } - } - - acutest_timer_get_time_(´st_timer_start_); - test->func(); - aborted: - acutest_abort_has_jmp_buf_ = 0; - acutest_timer_get_time_(´st_timer_end_); - - if(acutest_verbose_level_ >= 3) { - acutest_line_indent_(1); - if(acutest_test_failures_ == 0) { - acutest_colored_printf_(ACUTEST_COLOR_GREEN_INTENSIVE_, "SUCCESS: "); - printf("All conditions have passed.\n"); - - if(acutest_timer_) { - acutest_line_indent_(1); - printf("Duration: "); - acutest_timer_print_diff_(); - printf("\n"); - } - } else { - acutest_colored_printf_(ACUTEST_COLOR_RED_INTENSIVE_, "FAILED: "); - if(!acutest_was_aborted_) { - printf("%d condition%s %s failed.\n", - acutest_test_failures_, - (acutest_test_failures_ == 1) ? "" : "s", - (acutest_test_failures_ == 1) ? "has" : "have"); - } else { - printf("Aborted.\n"); - } - } - printf("\n"); - } else if(acutest_verbose_level_ >= 1 && acutest_test_failures_ == 0) { - acutest_finish_test_line_(0); - } - - status = (acutest_test_failures_ == 0) ? 0 : -1; - -#ifdef __cplusplus - } catch(std::exception& e) { - const char* what = e.what(); - acutest_check_(0, NULL, 0, "Threw std::exception"); - if(what != NULL) - acutest_message_("std::exception::what(): %s", what); - - if(acutest_verbose_level_ >= 3) { - acutest_line_indent_(1); - acutest_colored_printf_(ACUTEST_COLOR_RED_INTENSIVE_, "FAILED: "); - printf("C++ exception.\n\n"); - } - } catch(...) { - acutest_check_(0, NULL, 0, "Threw an exception"); - - if(acutest_verbose_level_ >= 3) { - acutest_line_indent_(1); - acutest_colored_printf_(ACUTEST_COLOR_RED_INTENSIVE_, "FAILED: "); - printf("C++ exception.\n\n"); - } - } -#endif - - acutest_fini_(test->name); - acutest_case_(NULL); - acutest_current_test_ = NULL; - - return status; -} - -/* Trigger the unit test. If possible (and not suppressed) it starts a child - * process who calls acutest_do_run_(), otherwise it calls acutest_do_run_() - * directly. */ -static void -acutest_run_(const struct acutest_test_* test, int index, int master_index) -{ - int failed = 1; - acutest_timer_type_ start, end; - - acutest_current_test_ = test; - acutest_test_already_logged_ = 0; - acutest_timer_get_time_(&start); - - if(!acutest_no_exec_) { - -#if defined(ACUTEST_UNIX_) - - pid_t pid; - int exit_code; - - /* Make sure the child starts with empty I/O buffers. */ - fflush(stdout); - fflush(stderr); - - pid = fork(); - if(pid == (pid_t)-1) { - acutest_error_("Cannot fork. %s [%d]", strerror(errno), errno); - failed = 1; - } else if(pid == 0) { - /* Child: Do the test. */ - acutest_worker_ = 1; - failed = (acutest_do_run_(test, index) != 0); - acutest_exit_(failed ? 1 : 0); - } else { - /* Parent: Wait until child terminates and analyze its exit code. */ - waitpid(pid, &exit_code, 0); - if(WIFEXITED(exit_code)) { - switch(WEXITSTATUS(exit_code)) { - case 0: failed = 0; break; /* test has passed. */ - case 1: /* noop */ break; /* "normal" failure. */ - default: acutest_error_("Unexpected exit code [%d]", WEXITSTATUS(exit_code)); - } - } else if(WIFSIGNALED(exit_code)) { - char tmp[32]; - const char* signame; - switch(WTERMSIG(exit_code)) { - case SIGINT: signame = "SIGINT"; break; - case SIGHUP: signame = "SIGHUP"; break; - case SIGQUIT: signame = "SIGQUIT"; break; - case SIGABRT: signame = "SIGABRT"; break; - case SIGKILL: signame = "SIGKILL"; break; - case SIGSEGV: signame = "SIGSEGV"; break; - case SIGILL: signame = "SIGILL"; break; - case SIGTERM: signame = "SIGTERM"; break; - default: sprintf(tmp, "signal %d", WTERMSIG(exit_code)); signame = tmp; break; - } - acutest_error_("Test interrupted by %s.", signame); - } else { - acutest_error_("Test ended in an unexpected way [%d].", exit_code); - } - } - -#elif defined(ACUTEST_WIN_) - - char buffer[512] = {0}; - STARTUPINFOA startupInfo; - PROCESS_INFORMATION processInfo; - DWORD exitCode; - - /* Windows has no fork(). So we propagate all info into the child - * through a command line arguments. */ - _snprintf(buffer, sizeof(buffer)-1, - "%s --worker=%d %s --no-exec --no-summary %s --verbose=%d --color=%s -- \"%s\"", - acutest_argv0_, index, acutest_timer_ ? "--time" : "", - acutest_tap_ ? "--tap" : "", acutest_verbose_level_, - acutest_colorize_ ? "always" : "never", - test->name); - memset(&startupInfo, 0, sizeof(startupInfo)); - startupInfo.cb = sizeof(STARTUPINFO); - if(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &startupInfo, &processInfo)) { - WaitForSingleObject(processInfo.hProcess, INFINITE); - GetExitCodeProcess(processInfo.hProcess, &exitCode); - CloseHandle(processInfo.hThread); - CloseHandle(processInfo.hProcess); - failed = (exitCode != 0); - if(exitCode > 1) { - switch(exitCode) { - case 3: acutest_error_("Aborted."); break; - case 0xC0000005: acutest_error_("Access violation."); break; - default: acutest_error_("Test ended in an unexpected way [%lu].", exitCode); break; - } - } - } else { - acutest_error_("Cannot create unit test subprocess [%ld].", GetLastError()); - failed = 1; - } - -#else - - /* A platform where we don't know how to run child process. */ - failed = (acutest_do_run_(test, index) != 0); - -#endif - - } else { - /* Child processes suppressed through --no-exec. */ - failed = (acutest_do_run_(test, index) != 0); - } - acutest_timer_get_time_(&end); - - acutest_current_test_ = NULL; - - acutest_stat_run_units_++; - if(failed) - acutest_stat_failed_units_++; - - acutest_set_success_(master_index, !failed); - acutest_set_duration_(master_index, acutest_timer_diff_(start, end)); -} - -#if defined(ACUTEST_WIN_) -/* Callback for SEH events. */ -static LONG CALLBACK -acutest_seh_exception_filter_(EXCEPTION_POINTERS *ptrs) -{ - acutest_check_(0, NULL, 0, "Unhandled SEH exception"); - acutest_message_("Exception code: 0x%08lx", ptrs->ExceptionRecord->ExceptionCode); - acutest_message_("Exception address: 0x%p", ptrs->ExceptionRecord->ExceptionAddress); - - fflush(stdout); - fflush(stderr); - - return EXCEPTION_EXECUTE_HANDLER; -} -#endif - - -#define ACUTEST_CMDLINE_OPTFLAG_OPTIONALARG_ 0x0001 -#define ACUTEST_CMDLINE_OPTFLAG_REQUIREDARG_ 0x0002 - -#define ACUTEST_CMDLINE_OPTID_NONE_ 0 -#define ACUTEST_CMDLINE_OPTID_UNKNOWN_ (-0x7fffffff + 0) -#define ACUTEST_CMDLINE_OPTID_MISSINGARG_ (-0x7fffffff + 1) -#define ACUTEST_CMDLINE_OPTID_BOGUSARG_ (-0x7fffffff + 2) - -typedef struct acutest_test_CMDLINE_OPTION_ { - char shortname; - const char* longname; - int id; - unsigned flags; -} ACUTEST_CMDLINE_OPTION_; - -static int -acutest_cmdline_handle_short_opt_group_(const ACUTEST_CMDLINE_OPTION_* options, - const char* arggroup, - int (*callback)(int /*optval*/, const char* /*arg*/)) -{ - const ACUTEST_CMDLINE_OPTION_* opt; - int i; - int ret = 0; - - for(i = 0; arggroup[i] != '\0'; i++) { - for(opt = options; opt->id != 0; opt++) { - if(arggroup[i] == opt->shortname) - break; - } - - if(opt->id != 0 && !(opt->flags & ACUTEST_CMDLINE_OPTFLAG_REQUIREDARG_)) { - ret = callback(opt->id, NULL); - } else { - /* Unknown option. */ - char badoptname[3]; - badoptname[0] = '-'; - badoptname[1] = arggroup[i]; - badoptname[2] = '\0'; - ret = callback((opt->id != 0 ? ACUTEST_CMDLINE_OPTID_MISSINGARG_ : ACUTEST_CMDLINE_OPTID_UNKNOWN_), - badoptname); - } - - if(ret != 0) - break; - } - - return ret; -} - -#define ACUTEST_CMDLINE_AUXBUF_SIZE_ 32 - -static int -acutest_cmdline_read_(const ACUTEST_CMDLINE_OPTION_* options, int argc, char** argv, - int (*callback)(int /*optval*/, const char* /*arg*/)) -{ - - const ACUTEST_CMDLINE_OPTION_* opt; - char auxbuf[ACUTEST_CMDLINE_AUXBUF_SIZE_+1]; - int after_doubledash = 0; - int i = 1; - int ret = 0; - - auxbuf[ACUTEST_CMDLINE_AUXBUF_SIZE_] = '\0'; - - while(i < argc) { - if(after_doubledash || strcmp(argv[i], "-") == 0) { - /* Non-option argument. */ - ret = callback(ACUTEST_CMDLINE_OPTID_NONE_, argv[i]); - } else if(strcmp(argv[i], "--") == 0) { - /* End of options. All the remaining members are non-option arguments. */ - after_doubledash = 1; - } else if(argv[i][0] != '-') { - /* Non-option argument. */ - ret = callback(ACUTEST_CMDLINE_OPTID_NONE_, argv[i]); - } else { - for(opt = options; opt->id != 0; opt++) { - if(opt->longname != NULL && strncmp(argv[i], "--", 2) == 0) { - size_t len = strlen(opt->longname); - if(strncmp(argv[i]+2, opt->longname, len) == 0) { - /* Regular long option. */ - if(argv[i][2+len] == '\0') { - /* with no argument provided. */ - if(!(opt->flags & ACUTEST_CMDLINE_OPTFLAG_REQUIREDARG_)) - ret = callback(opt->id, NULL); - else - ret = callback(ACUTEST_CMDLINE_OPTID_MISSINGARG_, argv[i]); - break; - } else if(argv[i][2+len] == '=') { - /* with an argument provided. */ - if(opt->flags & (ACUTEST_CMDLINE_OPTFLAG_OPTIONALARG_ | ACUTEST_CMDLINE_OPTFLAG_REQUIREDARG_)) { - ret = callback(opt->id, argv[i]+2+len+1); - } else { - sprintf(auxbuf, "--%s", opt->longname); - ret = callback(ACUTEST_CMDLINE_OPTID_BOGUSARG_, auxbuf); - } - break; - } else { - continue; - } - } - } else if(opt->shortname != '\0' && argv[i][0] == '-') { - if(argv[i][1] == opt->shortname) { - /* Regular short option. */ - if(opt->flags & ACUTEST_CMDLINE_OPTFLAG_REQUIREDARG_) { - if(argv[i][2] != '\0') - ret = callback(opt->id, argv[i]+2); - else if(i+1 < argc) - ret = callback(opt->id, argv[++i]); - else - ret = callback(ACUTEST_CMDLINE_OPTID_MISSINGARG_, argv[i]); - break; - } else { - ret = callback(opt->id, NULL); - - /* There might be more (argument-less) short options - * grouped together. */ - if(ret == 0 && argv[i][2] != '\0') - ret = acutest_cmdline_handle_short_opt_group_(options, argv[i]+2, callback); - break; - } - } - } - } - - if(opt->id == 0) { /* still not handled? */ - if(argv[i][0] != '-') { - /* Non-option argument. */ - ret = callback(ACUTEST_CMDLINE_OPTID_NONE_, argv[i]); - } else { - /* Unknown option. */ - char* badoptname = argv[i]; - - if(strncmp(badoptname, "--", 2) == 0) { - /* Strip any argument from the long option. */ - char* assignment = strchr(badoptname, '='); - if(assignment != NULL) { - size_t len = assignment - badoptname; - if(len > ACUTEST_CMDLINE_AUXBUF_SIZE_) - len = ACUTEST_CMDLINE_AUXBUF_SIZE_; - strncpy(auxbuf, badoptname, len); - auxbuf[len] = '\0'; - badoptname = auxbuf; - } - } - - ret = callback(ACUTEST_CMDLINE_OPTID_UNKNOWN_, badoptname); - } - } - } - - if(ret != 0) - return ret; - i++; - } - - return ret; -} - -static void -acutest_help_(void) -{ - printf("Usage: %s [options] [test...]\n", acutest_argv0_); - printf("\n"); - printf("Run the specified unit tests; or if the option '--skip' is used, run all\n"); - printf("tests in the suite but those listed. By default, if no tests are specified\n"); - printf("on the command line, all unit tests in the suite are run.\n"); - printf("\n"); - printf("Options:\n"); - printf(" -s, --skip Execute all unit tests but the listed ones\n"); - printf(" --exec[=WHEN] If supported, execute unit tests as child processes\n"); - printf(" (WHEN is one of 'auto', 'always', 'never')\n"); - printf(" -E, --no-exec Same as --exec=never\n"); -#if defined ACUTEST_WIN_ - printf(" -t, --time Measure test duration\n"); -#elif defined ACUTEST_HAS_POSIX_TIMER_ - printf(" -t, --time Measure test duration (real time)\n"); - printf(" --time=TIMER Measure test duration, using given timer\n"); - printf(" (TIMER is one of 'real', 'cpu')\n"); -#endif - printf(" --no-summary Suppress printing of test results summary\n"); - printf(" --tap Produce TAP-compliant output\n"); - printf(" (See https://testanything.org/)\n"); - printf(" -x, --xml-output=FILE Enable XUnit output to the given file\n"); - printf(" -l, --list List unit tests in the suite and exit\n"); - printf(" -v, --verbose Make output more verbose\n"); - printf(" --verbose=LEVEL Set verbose level to LEVEL:\n"); - printf(" 0 ... Be silent\n"); - printf(" 1 ... Output one line per test (and summary)\n"); - printf(" 2 ... As 1 and failed conditions (this is default)\n"); - printf(" 3 ... As 1 and all conditions (and extended summary)\n"); - printf(" -q, --quiet Same as --verbose=0\n"); - printf(" --color[=WHEN] Enable colorized output\n"); - printf(" (WHEN is one of 'auto', 'always', 'never')\n"); - printf(" --no-color Same as --color=never\n"); - printf(" -h, --help Display this help and exit\n"); - - if(acutest_list_size_ < 16) { - printf("\n"); - acutest_list_names_(); - } -} - -static const ACUTEST_CMDLINE_OPTION_ acutest_cmdline_options_[] = { - { 's', "skip", 's', 0 }, - { 0, "exec", 'e', ACUTEST_CMDLINE_OPTFLAG_OPTIONALARG_ }, - { 'E', "no-exec", 'E', 0 }, -#if defined ACUTEST_WIN_ - { 't', "time", 't', 0 }, - { 0, "timer", 't', 0 }, /* kept for compatibility */ -#elif defined ACUTEST_HAS_POSIX_TIMER_ - { 't', "time", 't', ACUTEST_CMDLINE_OPTFLAG_OPTIONALARG_ }, - { 0, "timer", 't', ACUTEST_CMDLINE_OPTFLAG_OPTIONALARG_ }, /* kept for compatibility */ -#endif - { 0, "no-summary", 'S', 0 }, - { 0, "tap", 'T', 0 }, - { 'l', "list", 'l', 0 }, - { 'v', "verbose", 'v', ACUTEST_CMDLINE_OPTFLAG_OPTIONALARG_ }, - { 'q', "quiet", 'q', 0 }, - { 0, "color", 'c', ACUTEST_CMDLINE_OPTFLAG_OPTIONALARG_ }, - { 0, "no-color", 'C', 0 }, - { 'h', "help", 'h', 0 }, - { 0, "worker", 'w', ACUTEST_CMDLINE_OPTFLAG_REQUIREDARG_ }, /* internal */ - { 'x', "xml-output", 'x', ACUTEST_CMDLINE_OPTFLAG_REQUIREDARG_ }, - { 0, NULL, 0, 0 } -}; - -static int -acutest_cmdline_callback_(int id, const char* arg) -{ - switch(id) { - case 's': - acutest_skip_mode_ = 1; - break; - - case 'e': - if(arg == NULL || strcmp(arg, "always") == 0) { - acutest_no_exec_ = 0; - } else if(strcmp(arg, "never") == 0) { - acutest_no_exec_ = 1; - } else if(strcmp(arg, "auto") == 0) { - /*noop*/ - } else { - fprintf(stderr, "%s: Unrecognized argument '%s' for option --exec.\n", acutest_argv0_, arg); - fprintf(stderr, "Try '%s --help' for more information.\n", acutest_argv0_); - acutest_exit_(2); - } - break; - - case 'E': - acutest_no_exec_ = 1; - break; - - case 't': -#if defined ACUTEST_WIN_ || defined ACUTEST_HAS_POSIX_TIMER_ - if(arg == NULL || strcmp(arg, "real") == 0) { - acutest_timer_ = 1; -#ifndef ACUTEST_WIN_ - } else if(strcmp(arg, "cpu") == 0) { - acutest_timer_ = 2; -#endif - } else { - fprintf(stderr, "%s: Unrecognized argument '%s' for option --time.\n", acutest_argv0_, arg); - fprintf(stderr, "Try '%s --help' for more information.\n", acutest_argv0_); - acutest_exit_(2); - } -#endif - break; - - case 'S': - acutest_no_summary_ = 1; - break; - - case 'T': - acutest_tap_ = 1; - break; - - case 'l': - acutest_list_names_(); - acutest_exit_(0); - break; - - case 'v': - acutest_verbose_level_ = (arg != NULL ? atoi(arg) : acutest_verbose_level_+1); - break; - - case 'q': - acutest_verbose_level_ = 0; - break; - - case 'c': - if(arg == NULL || strcmp(arg, "always") == 0) { - acutest_colorize_ = 1; - } else if(strcmp(arg, "never") == 0) { - acutest_colorize_ = 0; - } else if(strcmp(arg, "auto") == 0) { - /*noop*/ - } else { - fprintf(stderr, "%s: Unrecognized argument '%s' for option --color.\n", acutest_argv0_, arg); - fprintf(stderr, "Try '%s --help' for more information.\n", acutest_argv0_); - acutest_exit_(2); - } - break; - - case 'C': - acutest_colorize_ = 0; - break; - - case 'h': - acutest_help_(); - acutest_exit_(0); - break; - - case 'w': - acutest_worker_ = 1; - acutest_worker_index_ = atoi(arg); - break; - case 'x': - acutest_xml_output_ = fopen(arg, "w"); - if (!acutest_xml_output_) { - fprintf(stderr, "Unable to open '%s': %s\n", arg, strerror(errno)); - acutest_exit_(2); - } - break; - - case 0: - if(acutest_lookup_(arg) == 0) { - fprintf(stderr, "%s: Unrecognized unit test '%s'\n", acutest_argv0_, arg); - fprintf(stderr, "Try '%s --list' for list of unit tests.\n", acutest_argv0_); - acutest_exit_(2); - } - break; - - case ACUTEST_CMDLINE_OPTID_UNKNOWN_: - fprintf(stderr, "Unrecognized command line option '%s'.\n", arg); - fprintf(stderr, "Try '%s --help' for more information.\n", acutest_argv0_); - acutest_exit_(2); - break; - - case ACUTEST_CMDLINE_OPTID_MISSINGARG_: - fprintf(stderr, "The command line option '%s' requires an argument.\n", arg); - fprintf(stderr, "Try '%s --help' for more information.\n", acutest_argv0_); - acutest_exit_(2); - break; - - case ACUTEST_CMDLINE_OPTID_BOGUSARG_: - fprintf(stderr, "The command line option '%s' does not expect an argument.\n", arg); - fprintf(stderr, "Try '%s --help' for more information.\n", acutest_argv0_); - acutest_exit_(2); - break; - } - - return 0; -} - - -#ifdef ACUTEST_LINUX_ -static int -acutest_is_tracer_present_(void) -{ - /* Must be large enough so the line 'TracerPid: ${PID}' can fit in. */ - static const int OVERLAP = 32; - - char buf[512]; - int tracer_present = 0; - int fd; - size_t n_read = 0; - - fd = open("/proc/self/status", O_RDONLY); - if(fd == -1) - return 0; - - while(1) { - static const char pattern[] = "TracerPid:"; - const char* field; - - while(n_read < sizeof(buf) - 1) { - ssize_t n; - - n = read(fd, buf + n_read, sizeof(buf) - 1 - n_read); - if(n <= 0) - break; - n_read += n; - } - buf[n_read] = '\0'; - - field = strstr(buf, pattern); - if(field != NULL && field < buf + sizeof(buf) - OVERLAP) { - pid_t tracer_pid = (pid_t) atoi(field + sizeof(pattern) - 1); - tracer_present = (tracer_pid != 0); - break; - } - - if(n_read == sizeof(buf) - 1) { - /* Move the tail with the potentially incomplete line we're looking - * for to the beginning of the buffer. */ - memmove(buf, buf + sizeof(buf) - 1 - OVERLAP, OVERLAP); - n_read = OVERLAP; - } else { - break; - } - } - - close(fd); - return tracer_present; -} -#endif - -#ifdef ACUTEST_MACOS_ -static bool -acutest_AmIBeingDebugged(void) -{ - int junk; - int mib[4]; - struct kinfo_proc info; - size_t size; - - // Initialize the flags so that, if sysctl fails for some bizarre - // reason, we get a predictable result. - info.kp_proc.p_flag = 0; - - // Initialize mib, which tells sysctl the info we want, in this case - // we're looking for information about a specific process ID. - mib[0] = CTL_KERN; - mib[1] = KERN_PROC; - mib[2] = KERN_PROC_PID; - mib[3] = getpid(); - - // Call sysctl. - size = sizeof(info); - junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0); - assert(junk == 0); - - // We're being debugged if the P_TRACED flag is set. - return ( (info.kp_proc.p_flag & P_TRACED) != 0 ); -} -#endif - -int -main(int argc, char** argv) -{ - int i; - - acutest_argv0_ = argv[0]; - -#if defined ACUTEST_UNIX_ - acutest_colorize_ = isatty(STDOUT_FILENO); -#elif defined ACUTEST_WIN_ - #if defined _BORLANDC_ - acutest_colorize_ = isatty(_fileno(stdout)); - #else - acutest_colorize_ = _isatty(_fileno(stdout)); - #endif -#else - acutest_colorize_ = 0; -#endif - - /* Count all test units */ - acutest_list_size_ = 0; - for(i = 0; acutest_list_[i].func != NULL; i++) - acutest_list_size_++; - - acutest_test_data_ = (struct acutest_test_data_*)calloc(acutest_list_size_, sizeof(struct acutest_test_data_)); - if(acutest_test_data_ == NULL) { - fprintf(stderr, "Out of memory.\n"); - acutest_exit_(2); - } - - /* Parse options */ - acutest_cmdline_read_(acutest_cmdline_options_, argc, argv, acutest_cmdline_callback_); - - /* Initialize the proper timer. */ - acutest_timer_init_(); - -#if defined(ACUTEST_WIN_) - SetUnhandledExceptionFilter(acutest_seh_exception_filter_); -#ifdef _MSC_VER - _set_abort_behavior(0, _WRITE_ABORT_MSG); -#endif -#endif - - /* By default, we want to run all tests. */ - if(acutest_count_ == 0) { - for(i = 0; acutest_list_[i].func != NULL; i++) - acutest_remember_(i); - } - - /* Guess whether we want to run unit tests as child processes. */ - if(acutest_no_exec_ < 0) { - acutest_no_exec_ = 0; - - if(acutest_count_ <= 1) { - acutest_no_exec_ = 1; - } else { -#ifdef ACUTEST_WIN_ - if(IsDebuggerPresent()) - acutest_no_exec_ = 1; -#endif -#ifdef ACUTEST_LINUX_ - if(acutest_is_tracer_present_()) - acutest_no_exec_ = 1; -#endif -#ifdef ACUTEST_MACOS_ - if(acutest_AmIBeingDebugged()) - acutest_no_exec_ = 1; -#endif -#ifdef RUNNING_ON_VALGRIND - /* RUNNING_ON_VALGRIND is provided by optionally included */ - if(RUNNING_ON_VALGRIND) - acutest_no_exec_ = 1; -#endif - } - } - - if(acutest_tap_) { - /* TAP requires we know test result ("ok", "not ok") before we output - * anything about the test, and this gets problematic for larger verbose - * levels. */ - if(acutest_verbose_level_ > 2) - acutest_verbose_level_ = 2; - - /* TAP harness should provide some summary. */ - acutest_no_summary_ = 1; - - if(!acutest_worker_) - printf("1..%d\n", (int) acutest_count_); - } - - int index = acutest_worker_index_; - for(i = 0; acutest_list_[i].func != NULL; i++) { - int run = (acutest_test_data_[i].flags & ACUTEST_FLAG_RUN_); - if (acutest_skip_mode_) /* Run all tests except those listed. */ - run = !run; - if(run) - acutest_run_(´st_list_[i], index++, i); - } - - /* Write a summary */ - if(!acutest_no_summary_ && acutest_verbose_level_ >= 1) { - if(acutest_verbose_level_ >= 3) { - acutest_colored_printf_(ACUTEST_COLOR_DEFAULT_INTENSIVE_, "Summary:\n"); - - printf(" Count of all unit tests: %4d\n", (int) acutest_list_size_); - printf(" Count of run unit tests: %4d\n", acutest_stat_run_units_); - printf(" Count of failed unit tests: %4d\n", acutest_stat_failed_units_); - printf(" Count of skipped unit tests: %4d\n", (int) acutest_list_size_ - acutest_stat_run_units_); - } - - if(acutest_stat_failed_units_ == 0) { - acutest_colored_printf_(ACUTEST_COLOR_GREEN_INTENSIVE_, "SUCCESS:"); - printf(" All unit tests have passed.\n"); - } else { - acutest_colored_printf_(ACUTEST_COLOR_RED_INTENSIVE_, "FAILED:"); - printf(" %d of %d unit tests %s failed.\n", - acutest_stat_failed_units_, acutest_stat_run_units_, - (acutest_stat_failed_units_ == 1) ? "has" : "have"); - } - - if(acutest_verbose_level_ >= 3) - printf("\n"); - } - - if (acutest_xml_output_) { -#if defined ACUTEST_UNIX_ - char *suite_name = basename(argv[0]); -#elif defined ACUTEST_WIN_ - char suite_name[_MAX_FNAME]; - _splitpath(argv[0], NULL, NULL, suite_name, NULL); -#else - const char *suite_name = argv[0]; -#endif - fprintf(acutest_xml_output_, "\n"); - fprintf(acutest_xml_output_, "\n", - suite_name, (int)acutest_list_size_, acutest_stat_failed_units_, acutest_stat_failed_units_, - (int)acutest_list_size_ - acutest_stat_run_units_); - for(i = 0; acutest_list_[i].func != NULL; i++) { - struct acutest_test_data_ *details = ´st_test_data_[i]; - fprintf(acutest_xml_output_, " \n", acutest_list_[i].name, details->duration); - if (details->flags & ACUTEST_FLAG_FAILURE_) - fprintf(acutest_xml_output_, " \n"); - if (!(details->flags & ACUTEST_FLAG_FAILURE_) && !(details->flags & ACUTEST_FLAG_SUCCESS_)) - fprintf(acutest_xml_output_, " \n"); - fprintf(acutest_xml_output_, " \n"); - } - fprintf(acutest_xml_output_, "\n"); - fclose(acutest_xml_output_); - } - - acutest_cleanup_(); - - return (acutest_stat_failed_units_ == 0) ? 0 : 1; -} - - -#endif /* #ifndef TEST_NO_MAIN */ - -#ifdef _MSC_VER -#pragma warning(pop) -#endif - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif /* #ifndef ACUTEST_H */ diff --git a/trie/test/trie/fuzzy.h b/trie/test/trie/fuzzy.h deleted file mode 100644 index 00dc386..0000000 --- a/trie/test/trie/fuzzy.h +++ /dev/null @@ -1,205 +0,0 @@ -#ifndef AD3_FUZZYTEST -#define AD3_FUZZYTEST - -#include -#include -#include -#include -#include "trie.h" - -typedef struct fuzzyconfig { - int seed; - int word_length; - int word_count; -} FuzzyConfig; - -void random_clean_string(char* s, int len) { - char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,?"; - int charset_len = strlen(charset); - - // len - 1 ensures that we can still set the null byte for the final byte - int actual_len = rand() % (len - 1); - int key; - int i; - - for (i = 0; i < actual_len; i++) { - key = rand() % charset_len; - s[i] = charset[key]; - } - - s[i] = '\0'; -} - -void random_string(char* s, int len) { - int val = rand(); - - // String can't be an empty string as they aren't supported - s[0] = (char)(val % 255 + 1); - - for (int i = 1; i < len - 1; i++) { - val = rand(); - s[i] = (char)(val % 256); - } - - // Just in case no null characters were created - s[len - 1] = '\0'; -} - -void random_string_matrix(char** s, int count, int len) { - for (int i = 0; i < count; i++) { - random_string(s[i], len); - } -} - -char** init_string_matrix(int count, int len) { - char** matrix = malloc(count * sizeof(char*)); - - for (int i = 0; i < count; i++) { - matrix[i] = calloc(len, sizeof(char)); - } - - return matrix; -} - -/** - * Test a given trie implementation using randomly generated strings generated - * using a given seed. - * - * @param seed seed to use for generating random strings - * @param count how many strings to test with - * @param len maximum length of each string - * @param init_func function to creat a new trie of the wanted type - * @param free_func function to free the given trie - * @param add_func function to add a string to the given trie - * @param remove_func function to remove a string from the given trie - * @param size_func function to get the size of the given trie - * @return exit code describing failures, if any - */ -int fuzzy_test_trie_seed(FuzzyConfig conf) { - srand(conf.seed); - - char** matrix = init_string_matrix(conf.word_count, conf.word_length); - random_string_matrix(matrix, conf.word_count, conf.word_length); - bool* contains = calloc(conf.word_count, sizeof(bool)); - - // It's possible that the string matrix contains duplicate strings - bool** contains_dedupped = calloc(conf.word_count, sizeof(bool*)); - - for (int i = 0; i < conf.word_count; i++) { - if (contains_dedupped[i] == NULL) { - contains_dedupped[i] = contains + i; - - for (int j = i + 1; j < conf.word_count; j++) { - if (strcmp(matrix[i], matrix[j]) == 0) { - contains_dedupped[j] = contains + i; - } - } - } - } - - // We keep track of the size as well so that we can check whether this is - // also correct - size_t size = 0; - - Trie *ct; - trie_init(&ct, NULL); - - bool changed; - TrieExitCode status; - - // 0: success - // 1: invalid add - // 2: invalid remove - // 3: bad size after adds - // 4: bad size after removes - int exit_code = 0; - - // Add all strings to trie, checking for duplicates - for (int i = 0; i < conf.word_count; i++) { - status = trie_add(ct, matrix[i], NULL); - - // if changed is false, *contains_dedupped[i] should be true, as changed - // can only be false if the string is already contained in the trie. if - // changed is true, *contains_dedupped[i] should be false, as the string - // cannot be in the trie yet. - if (status == Ok && *contains_dedupped[i]) { - exit_code = 1; - goto END; - } - - if (!*contains_dedupped[i]) { - *contains_dedupped[i] = true; - size++; - } - } - - // Ensure size is correct - if (trie_size(ct) != size) { - printf("%i %i\n", trie_size(ct), size); - exit_code = 3; - goto END; - } - - // Remove all strings again, again taking duplicates into consideration - /* for (int i = 0; i < conf.word_count; i++) { */ - /* changed = remove_func(ct, matrix[i]); */ - - /* // The string shouldn't be in the trie, yet another add operation */ - /* // says it added it as well */ - /* if (changed != *contains_dedupped[i]) { */ - /* exit_code = 2; */ - /* goto END; */ - /* } */ - - /* if (*contains_dedupped[i]) { */ - /* *contains_dedupped[i] = false; */ - /* size--; */ - /* } */ - /* } */ - - // Finally, check that the trie is completely empty - /* if (size_func(ct) != 0) { */ - /* exit_code = 4; */ - /* } */ - -END: - trie_free(ct); - - // Even testing functions should properly free memory - free(contains); - free(contains_dedupped); - - for (int i = 0; i < conf.word_count; i++) { - free(matrix[i]); - } - - free(matrix); - - return exit_code; -} - -/** - * Same as fuzzy_test_trie_seed, except that the seed is randomly generated. - * - * @param count how many strings to test with - * @param len maximum length of each string - * @param init_func function to creat a new trie of the wanted type - * @param free_func function to free the given trie - * @param add_func function to add a string to the given trie - * @param remove_func function to remove a string from the given trie - * @param size_func function to get the size of the given trie - * @return the generated seed if the test wasn't successful, -1 otherwise. - */ -/* int fuzzy_test_trie(int count, int len, void* (*init_func) (), void (*free_func) (void*), bool (*add_func) (void*, char*), bool (*remove_func) (void*, char*), int (*size_func) (void*)) { */ -/* int seed = rand(); */ -/* bool succeeded = fuzzy_test_trie_seed(seed, count, len, init_func, free_func, add_func, remove_func, size_func); */ - -/* if (!succeeded) { */ -/* return seed; */ -/* } */ - -/* return -1; */ -/* } */ - - -#endif diff --git a/trie/test/trie/test_trie.c b/trie/test/trie/test_trie.c deleted file mode 100644 index 7a7e8d3..0000000 --- a/trie/test/trie/test_trie.c +++ /dev/null @@ -1,189 +0,0 @@ -#include "test.h" -#include "trie.h" -#include "fuzzy.h" - -#define TEST_SIZE(ct, size) \ - TEST_CHECK(trie_size(ct) == size); \ - TEST_MSG("Size: %zu", trie_size(ct)) - -# define TRIE_INIT() \ - Trie *ct; \ - trie_init(&ct, NULL); \ - TEST_CHECK(ct != NULL) - -void test_init() { - TRIE_INIT(); - TEST_SIZE(ct, 0); - trie_free(ct); -} - -void test_add_one() { - TRIE_INIT(); - - Entry *entry = entry_new(Redirect, ""); - const char* string = "this is a test"; - - TEST_CHECK(trie_add(ct, string, entry) == Ok); - Entry *entry2; - TEST_CHECK(trie_search(ct, &entry2, string) == Ok); - TEST_CHECK(entry == entry2); - TEST_SIZE(ct, 1); - trie_free(ct); -} - -void test_add_prefix() { - TRIE_INIT(); - - const char *s1 = "halloween-2022"; - const char *s2 = "halloween-202"; - - Entry *entry1 = entry_new(Redirect, ""); - Entry *entry2 = entry_new(Redirect, ""); - - TEST_CHECK(trie_add(ct, s1, entry1) == Ok); - TEST_CHECK(trie_add(ct, s2, entry2) == Ok); - - Entry *entry3; - - TEST_CHECK(trie_search(ct, &entry3, s1) == Ok); - TEST_CHECK(entry3 == entry1); - entry2 = NULL; - - TEST_CHECK(trie_search(ct, &entry3, s2) == Ok); - TEST_CHECK(entry3 == entry2); - - trie_free(ct); -} - -void test_search_not_present() { - TRIE_INIT(); - - TEST_CHECK(trie_add(ct, "this string exists", NULL) == Ok); - Entry *entry; - TEST_CHECK(trie_search(ct, &entry, "this string does not exist") == NotFound); - - trie_free(ct); -} - -void test_add_more() { - TRIE_INIT(); - - const char* one = "one"; - const char* two = "two"; - const char* twenty = "twenty"; - const char* twentytwo = "twentytwo"; - - Entry *entry = entry_new(Redirect, ""); - - TEST_CHECK(trie_add(ct, one, entry) == Ok); - TEST_CHECK(trie_add(ct, two, entry) == Ok); - TEST_CHECK(trie_add(ct, twenty, entry) == Ok); - TEST_CHECK(trie_add(ct, twentytwo, entry) == Ok); - - TEST_SIZE(ct, 4); - - Entry *entry2; - TEST_CHECK(trie_search(ct, &entry2, one) == Ok); - TEST_CHECK(entry2 == entry); - entry2 = NULL; - - TEST_CHECK(trie_search(ct, &entry2, two) == Ok); - TEST_CHECK(entry2 == entry); - entry2 = NULL; - - TEST_CHECK(trie_search(ct, &entry2, twenty) == Ok); - TEST_CHECK(entry2 == entry); - entry2 = NULL; - - TEST_CHECK(trie_search(ct, &entry2, twentytwo) == Ok); - TEST_CHECK(entry2 == entry); - entry2 = NULL; - - TEST_CHECK(trie_add(ct, one, NULL) == AlreadyPresent); - TEST_CHECK(trie_add(ct, two, NULL) == AlreadyPresent); - TEST_CHECK(trie_add(ct, twenty, NULL) == AlreadyPresent); - TEST_CHECK(trie_add(ct, twentytwo, NULL) == AlreadyPresent); - - trie_free(ct); -} - -/* void test_remove_one() { */ -/* Trie* ct = trie_init(); */ -/* TEST_CHECK(ct != NULL); */ - -/* const char* string = "this is a test"; */ -/* TEST_CHECK(trie_add(ct, string, NULL)); */ -/* TEST_SIZE(ct, 1); */ - -/* TEST_CHECK(trie_remove(ct, string)); */ -/* TEST_SIZE(ct, 0); */ - -/* trie_free(ct); */ -/* } */ - -/* void test_remove_more() { */ -/* Trie* ct = trie_init(); */ -/* TEST_CHECK(ct != NULL); */ - -/* const char* one = "one"; */ -/* const char* two = "two"; */ -/* const char* twenty = "twenty"; */ -/* const char* twentytwo = "twentytwo"; */ -/* TEST_CHECK(trie_add(ct, one, NULL)); */ -/* TEST_CHECK(trie_add(ct, two, NULL)); */ -/* TEST_CHECK(trie_add(ct, twenty, NULL)); */ -/* TEST_CHECK(trie_add(ct, twentytwo, NULL)); */ - -/* TEST_SIZE(ct, 4); */ - -/* TEST_CHECK(trie_remove(ct, one)); */ -/* TEST_CHECK(trie_remove(ct, two)); */ -/* TEST_CHECK(trie_remove(ct, twenty)); */ -/* TEST_CHECK(trie_remove(ct, twentytwo)); */ - -/* TEST_SIZE(ct, 0); */ - -/* trie_free(ct); */ -/* } */ - -/* void test_remove_not_present() { */ -/* Trie* ct = trie_init(); */ -/* TEST_CHECK(ct != NULL); */ - -/* TEST_CHECK(trie_add(ct, "this string exists", NULL)); */ -/* TEST_CHECK(!trie_remove(ct, "this string does not exist")); */ - -/* trie_free(ct); */ -/* } */ - -// Test seeds that are known to fail so we don't get regressions -void test_fuzzy_set() { - FuzzyConfig configs[] = { - { 403318210, 5, 500}, - { 588218406, 16, 460}, - { 297512224, 21, 500}, - { 403318210, 5, 500} - }; - - int count = sizeof(configs) / sizeof(FuzzyConfig); - int res; - - for (int i = 0; i < count; i++) { -res = fuzzy_test_trie_seed(configs[i]); - TEST_CHECK_(res == 0, - "Failed config, seed = %i, len = %i, count = %i, code=%i", configs[i].seed, configs[i].word_length, configs[i].word_count, res); - } -} - -TEST_LIST = { - {"trie init",test_init }, - { "trie add one",test_add_one }, - { "trie add more",test_add_more }, - { "trie search not present",test_search_not_present}, - - /* { "trie remove one",test_remove_one }, */ - /* { "trie remove more",test_remove_more }, */ - /* { "trie remove not present",test_remove_not_present}, */ - { "trie fuzzy set", test_fuzzy_set }, - { NULL, NULL} -}; diff --git a/trie/test/trie/test_trie_fuzzy.c b/trie/test/trie/test_trie_fuzzy.c deleted file mode 100644 index 33a2e24..0000000 --- a/trie/test/trie/test_trie_fuzzy.c +++ /dev/null @@ -1,34 +0,0 @@ -#include "test.h" -#include "trie.h" -#include "fuzzy.h" - -void test_fuzzy() { - // Randomize seed - srand(time(NULL)); - - FuzzyConfig config; - int counter = 0; - int res; - - for (int len = 1; len < 25; len += 5) { - for (int count = 10; count <= 500; count += 10) { - for (int i = 0; i < 50; i++) { - counter++; - - config.seed = rand(); - config.word_length = len; - config.word_count = count; - -res = fuzzy_test_trie_seed(config); - TEST_CHECK_(res == 0, - "Failed config, seed = %i, len = %i, count = %i, code = %i", config.seed, config.word_length, config.word_count, res); - } - } - } - TEST_MSG("fuzzy tests done = %i", counter); -} - -TEST_LIST = { - { "customtrie fuzzy", test_fuzzy }, - { NULL, NULL} -};