void test_rb_tree_new (void) { RBTree* tree; tree = rb_tree_new ( (RBTreeCompareFunc) int_compare); assert (tree != NULL); assert (rb_tree_root_node (tree) == NULL); assert (rb_tree_num_entries (tree) == 0); rb_tree_free (tree); /* Test out of memory scenario */ alloc_test_set_limit (0); tree = rb_tree_new ( (RBTreeCompareFunc) int_compare); assert (tree == NULL); }
/* Initialize the playlist. */ void plist_init (struct plist *plist) { plist->num = 0; plist->allocated = INIT_SIZE; plist->not_deleted = 0; plist->items = (struct plist_item *)xmalloc (sizeof(struct plist_item) * INIT_SIZE); plist->serial = -1; plist->search_tree = rb_tree_new (rb_compare, rb_fname_compare, plist); plist->total_time = 0; plist->items_with_time = 0; }
void test_rb_tree_free (void) { RBTree* tree; /* Try freeing an empty tree */ tree = rb_tree_new ( (RBTreeCompareFunc) int_compare); rb_tree_free (tree); /* Create a big tree and free it */ tree = create_tree(); rb_tree_free (tree); }
RBTree* create_tree (void) { RBTree* tree; int i; /* Create a tree and fill with nodes */ tree = rb_tree_new ( (RBTreeCompareFunc) int_compare); for (i = 0; i < NUM_TEST_VALUES; ++i) { test_array[i] = i; rb_tree_insert (tree, &test_array[i], &test_array[i]); } return tree; }
void test_rb_tree_child (void) { RBTree* tree; RBTreeNode* root; RBTreeNode* left; RBTreeNode* right; int values[] = { 1, 2, 3 }; int* p; int i; /* Create a tree containing some values. Validate the * tree is consistent at all stages. */ tree = rb_tree_new ( (RBTreeCompareFunc) int_compare); for (i = 0; i < 3; ++i) { rb_tree_insert (tree, &values[i], &values[i]); } /* Check the tree */ root = rb_tree_root_node (tree); p = rb_tree_node_value (root); assert (*p == 2); left = rb_tree_node_child (root, RB_TREE_NODE_LEFT); p = rb_tree_node_value (left); assert (*p == 1); right = rb_tree_node_child (root, RB_TREE_NODE_RIGHT); p = rb_tree_node_value (right); assert (*p == 3); /* Check invalid values */ assert (rb_tree_node_child (root, -1) == NULL); assert (rb_tree_node_child (root, 10000) == NULL); assert (rb_tree_node_child (root, 2) == NULL); assert (rb_tree_node_child (root, -100000) == NULL); rb_tree_free (tree); }
void test_rb_tree_insert_lookup (void) { RBTree* tree; RBTreeNode* node; int i; int* value; /* Create a tree containing some values. Validate the * tree is consistent at all stages. */ tree = rb_tree_new ( (RBTreeCompareFunc) int_compare); for (i = 0; i < NUM_TEST_VALUES; ++i) { test_array[i] = i; rb_tree_insert (tree, &test_array[i], &test_array[i]); assert (rb_tree_num_entries (tree) == i + 1); validate_tree (tree); } assert (rb_tree_root_node (tree) != NULL); /* Check that all values can be read back again */ for (i = 0; i < NUM_TEST_VALUES; ++i) { node = rb_tree_lookup_node (tree, &i); assert (node != NULL); value = rb_tree_node_key (node); assert (*value == i); value = rb_tree_node_value (node); assert (*value == i); } /* Check that invalid nodes are not found */ i = -1; assert (rb_tree_lookup_node (tree, &i) == NULL); i = NUM_TEST_VALUES + 100; assert (rb_tree_lookup_node (tree, &i) == NULL); rb_tree_free (tree); }
void test_rb_tree_to_array (void) { RBTree* tree; int entries[] = { 89, 23, 42, 4, 16, 15, 8, 99, 50, 30 }; int sorted[] = { 4, 8, 15, 16, 23, 30, 42, 50, 89, 99 }; int num_entries = sizeof (entries) / sizeof (int); int i; int** array; /* Add all entries to the tree */ tree = rb_tree_new ( (RBTreeCompareFunc) int_compare); for (i = 0; i < num_entries; ++i) { rb_tree_insert (tree, &entries[i], NULL); } assert (rb_tree_num_entries (tree) == num_entries); /* Convert to an array and check the contents */ array = (int**) rb_tree_to_array (tree); for (i = 0; i < num_entries; ++i) { assert (*array[i] == sorted[i]); } free (array); /* Test out of memory scenario */ alloc_test_set_limit (0); array = (int**) rb_tree_to_array (tree); assert (array == NULL); validate_tree (tree); rb_tree_free (tree); }
int init_ntoa_cache() { return (rb_tree_new(&rb_ntoa_tree, rb_ntoa_tree_compare) == RB_OK); }
int init_access_cache() { return (rb_tree_new(&rb_access_tree, rb_access_tree_compare) == RB_OK); }
int main(int argc, char *argv[]) { if (argc != 2) { printf("Expected filename argument.\n"); exit(1); } FILE *fp = fopen(argv[1], "r"); if (!fp) { printf("Unable to open file '%s'.\n", argv[1]); exit(1); } rb_tree *tree = rb_tree_new(dict_str_cmp, NULL); char buf[512]; while (fgets(buf, sizeof(buf), fp)) { if (isupper(buf[0])) /* Disregard proper nouns. */ continue; strtok(buf, "\r\n"); int freq[256] = { 0 }; memset(freq, 0, sizeof(freq)); ASSERT(buf[0] != '\0'); for (char *p = buf; *p; p++) freq[tolower(*p)]++; char name[1024]; char *p = name; for (int i=1; i<256; i++) { if (freq[i]) { ASSERT(freq[i] < 10); *p++ = (char) i; *p++ = '0' + (char) freq[i]; } } *p = 0; WordList* word = xmalloc(sizeof(*word)); word->word = xstrdup(buf); WordList** wordp = (WordList**)rb_tree_insert(tree, xstrdup(name), NULL); word->next = *wordp; *wordp = word; } rb_itor *itor = rb_itor_new(tree); for (rb_itor_first(itor); rb_itor_valid(itor); rb_itor_next(itor)) { WordList *word = *rb_itor_data(itor); ASSERT(word != NULL); if (word->next) { int count = 1; while (word->next) count++, word = word->next; printf("%2d:[", count); word = *rb_itor_data(itor); while (word) { printf("%s%c", word->word, word->next ? ',' : ']'); word = word->next; } printf("\n"); } word = *rb_itor_data(itor); while (word) { WordList *next = word->next; free(word->word); free(word); word = next; } } while (rb_itor_next(itor)); rb_itor_free(itor); rb_tree_free(tree); return 0; }