void test_trie_remove(void) { Trie *trie; char buf[10]; int i; unsigned int entries; trie = generate_trie(); /* Test remove on non-existent values. */ assert(trie_remove(trie, "000000000000000") == 0); assert(trie_remove(trie, "") == 0); entries = trie_num_entries(trie); assert(entries == NUM_TEST_VALUES); /* Remove all values */ for (i=0; i<NUM_TEST_VALUES; ++i) { sprintf(buf, "%i", i); /* Remove value and check counter */ assert(trie_remove(trie, buf) != 0); --entries; assert(trie_num_entries(trie) == entries); } trie_free(trie); }
Trie * trie_remove(Trie * node, char *tema) { int filhos = 0; assert(tema != NULL && *tema != '\0' && node != NULL && POS(tema) >= 0); if(*(tema) != '\0' && node->table[POS(tema)] != NULL) { node->table[POS(tema)] = trie_remove(node->table[POS(tema)], tema+1); } //verificar se tem filhos for(int i = 0; i < TRIE_TABLE_SIZE; i++) { if(node->table[i] != NULL) filhos++; } //nao remover se ouver filhos if(filhos > 0) { return node; } //remover apenas se coicidir com o fim da palavra e so remover letras //dessa palavra se nao pertencerem a outra palavra e nao tiverem filhos else if( (filhos == 0 && node->word == 0) || (*tema == '\0' && node->word == 1)) { free(node); return NULL; } //retornar o no intocavel. return node; }
void add_deligatured_word( unsigned char *word, Trie *deligatured_words ) { int *ligature_positions; int *ligature_ids; int ligature_count = word_ligatures( word, &ligature_positions, &ligature_ids ); // printf( "%s (%d)\n", word, ligature_count ); if ( ligature_count > 0 ) { unsigned char *deligatured_word; deligatured_word = mark_ligatures( word, ligature_positions, ligature_ids, ligature_count ); /* printf( "%s ==> %s\n", word, deligatured_word ); */ // warn user when 2 "real" words map to the same deligatured word Trie *node = trie_contains( deligatured_words, deligatured_word ); unsigned char *buf = 0; if ( node ) { unsigned char* existing_word = ( (LigatureData*) node->data )->word; // TODO: after a 2nd "real" source word is added to // node->data->word, reoccurring source words will be repeated // in our concatenated list; not a big deal, since this entry // will have to be edited by the user anyway, but we could do // better... if ( strcmp( word, existing_word ) ) { // if real words are different... // we will remove the existing node and replace it with one // whose "real" word is a concatenation of both words; the // user will be required to edit the result in the generated // file fprintf( stderr, "warning: deligatured word '%s' has multiple source words: %s, %s; EDIT FILE!\n", deligatured_word, word, existing_word ); buf = (unsigned char*) malloc( 2 + strlen( existing_word ) + 1 + strlen( word ) + 1 ); buf[0] = 0; strcat( buf, "{" ); strcat( buf, existing_word ); strcat( buf, "|" ); strcat( buf, word ); strcat( buf, "}" ); word = buf; trie_remove( deligatured_words, deligatured_word, _free_ligature_data_callback2 ); } } LigatureData *lig_data = (LigatureData*) malloc( sizeof( LigatureData ) ); lig_data->word = (unsigned char*) strdup( word ); lig_data->ligature_positions = ligature_positions; lig_data->ligature_ids = ligature_ids; lig_data->ligature_count = ligature_count; trie_add( deligatured_words, deligatured_word, lig_data ); free( deligatured_word ); if ( buf ) { free( buf ); } } }
static void test_trie_negative_keys(void) { char my_key[] = { 'a', 'b', 'c', -50, -20, '\0' }; Trie *trie; void *value; trie = trie_new(); assert(trie_insert(trie, my_key, "hello world") != 0); value = trie_lookup(trie, my_key); assert(!strcmp(value, "hello world")); assert(trie_remove(trie, my_key) != 0); assert(trie_remove(trie, my_key) == 0); assert(trie_lookup(trie, my_key) == NULL); trie_free(trie); }
void test_trie_insert_empty(void) { Trie *trie; char buf[10]; trie = trie_new(); /* Test insert on empty string */ assert(trie_insert(trie, "", buf) != 0); assert(trie_num_entries(trie) != 0); assert(trie_lookup(trie, "") == buf); assert(trie_remove(trie, "") != 0); assert(trie_num_entries(trie) == 0); trie_free(trie); }
void test_trie_new_free(void) { Trie *trie; /* Allocate and free an empty trie */ trie = trie_new(); assert(trie != NULL); trie_free(trie); /* Add some values before freeing */ trie = trie_new(); assert(trie_insert(trie, "hello", "there") != 0); assert(trie_insert(trie, "hell", "testing") != 0); assert(trie_insert(trie, "testing", "testing") != 0); assert(trie_insert(trie, "", "asfasf") != 0); trie_free(trie); /* Add a value, remove it and then free */ trie = trie_new(); assert(trie_insert(trie, "hello", "there") != 0); assert(trie_remove(trie, "hello") != 0); trie_free(trie); /* Test out of memory scenario */ alloc_test_set_limit(0); trie = trie_new(); assert(trie == NULL); }
trie_destroy(trie, NULL); TSET() TEST(simple_remove) char *key = "cat"; struct trie *trie = trie_create(); struct trie_iter *iter = trie_insert(trie, key, key); ASSERT_STR_EQUAL(trie_key(iter), key); ASSERT_STR_EQUAL(trie_value(iter), key); iter = trie_lookup(trie, key); ASSERT_STR_EQUAL(trie_key(iter), key); ASSERT_STR_EQUAL(trie_value(iter), key); char *value = trie_remove(trie, key); ASSERT_STR_EQUAL(value, key); iter = trie_lookup(trie, key); ASSERT_EQUAL(iter, NULL); trie_destroy(trie, NULL); TSET() TEST(insert_null) struct trie *root = trie_create(); struct trie_iter *iter = trie_insert(root, NULL, NULL); ASSERT_EQUAL(iter, NULL); trie_destroy(root, dealloc); TSET()
int dictionary_delete(struct dictionary *dict, const wchar_t *word) { return trie_remove(dict->tree, word); }