Ejemplo n.º 1
0
void test_hash_table_remove(void)
{
	HashTable *hash_table;
	char buf[10];

	hash_table = generate_hash_table();

	assert(hash_table_num_entries(hash_table) == NUM_TEST_VALUES);
	sprintf(buf, "%i", 5000);
	assert(hash_table_lookup(hash_table, buf) != NULL);

	/* Remove an entry */

	hash_table_remove(hash_table, buf);

	/* Check entry counter */

	assert(hash_table_num_entries(hash_table) == 9999);

	/* Check that NULL is returned now */

	assert(hash_table_lookup(hash_table, buf) == NULL);

	/* Try removing a non-existent entry */

	sprintf(buf, "%i", -1);
	hash_table_remove(hash_table, buf);

	assert(hash_table_num_entries(hash_table) == 9999);

	hash_table_free(hash_table);
}
Ejemplo n.º 2
0
void test_hash_table_iterating_remove(void)
{
	HashTable *hash_table;
	HashTableIterator iterator;
	char buf[10];
	char *val;
	HashTablePair pair;
	int count;
	unsigned int removed;
	int i;

	hash_table = generate_hash_table();

	/* Iterate over all values in the table */

	count = 0;
	removed = 0;

	hash_table_iterate(hash_table, &iterator);

	while (hash_table_iter_has_more(&iterator)) {

		/* Read the next value */

		pair = hash_table_iter_next(&iterator);
		val = pair.value;

		/* Remove every hundredth entry */

		if ((atoi(val) % 100) == 0) {
			hash_table_remove(hash_table, val);
			++removed;
		}

		++count;
	}

	/* Check counts */

	assert(removed == 100);
	assert(count == NUM_TEST_VALUES);

	assert(hash_table_num_entries(hash_table)
	       == NUM_TEST_VALUES - removed);

	/* Check all entries divisible by 100 were really removed */

	for (i=0; i<NUM_TEST_VALUES; ++i) {
		sprintf(buf, "%i", i);

		if (i % 100 == 0) {
			assert(hash_table_lookup(hash_table, buf) == NULL);
		} else {
			assert(hash_table_lookup(hash_table, buf) != NULL);
		}
	}

	hash_table_free(hash_table);
}
Ejemplo n.º 3
0
void load_hashtable_list( ){

		char *word = NULL, *entry_word = NULL;
	
		entry_word = gtk_entry_get_text ( mydata.input_word );	
		//	word = g_strchomp( g_strchug( g_strdown( entry_word ) ) );
		//  it has caused a Pango: index out of bound bug so use instead
		word = g_strdown( entry_word );
	
	//
		if( ! isalpha( word[0] ) ){
			//g_print("on_input_word_changed(): enter proper word\n");
			// ************** show proper warning to enter proper word *****************
//
			gtk_statusbar_pop ( mydata.statusbar, mydata.statusbar_context_id );
			gtk_statusbar_push (GTK_STATUSBAR (mydata.statusbar), mydata.statusbar_context_id, "Please enter/select proper english word");
//
			return;
		}								
		//check if hash table is created for word[0] of not
		g_sprintf( file_loaded_name, APP_DBS"%c.db", g_ascii_tolower( word[0] ) );
		//g_print("on_input_word_changed(): filename = %s\n",file_loaded_name);
		
		if( mydata.hash_tab[ g_ascii_toupper(word[0]) - 'A' ] == NULL ){
			//remove the prev word entries
			//remove_all_from_list ();
			//g_print("on_input_word_changed(): generating hash table\n");
			mydata.hash_tab[ g_ascii_toupper(word[0]) - 'A' ] = generate_hash_table( file_loaded_name );
			mydata.list_store_arr[ g_ascii_toupper(word[0]) - 'A' ] = mydata.list_store;
			list_loaded = g_ascii_toupper(word[0]) - 'A';
			//check if hash table is created or not by generate_hash_table() func call				 
			if( mydata.hash_tab[ g_ascii_toupper(word[0]) - 'A' ] == NULL ){
				g_print("on_input_word_changed():\nafter trying to generate_hash_table\nfile: %s could not be located and loaded\n", file_loaded_name );
			}						 
		}else{
			//g_print("on_input_word_changed(): %d", list_loaded );
			if( list_loaded != (g_ascii_toupper(word[0]) - 'A') ){
				//remove_all_from_list ();
//************   we have change here to fasten************ -------------->>>>>>>>>
//load list from pointer else populate it			
				if( mydata.list_store_arr[ g_ascii_toupper(word[0]) - 'A' ] == NULL ){
					populate_list_only( file_loaded_name );
					//g_print("on_input_word_changed(): list populated from file\n" );
				}else{					
					mydata.list_store = mydata.list_store_arr[ g_ascii_toupper(word[0]) - 'A' ];
//					following may be necessary		  
					gtk_tree_view_set_model ( mydata.treeview, GTK_TREE_MODEL(mydata.list_store) );
					//g_print("on_input_word_changed(): list used from previous list\n" );
				}
				
				list_loaded = (g_ascii_toupper(word[0]) - 'A');
			}
		}	
	//
	
}
Ejemplo n.º 4
0
void test_hash_table_iterating(void)
{
	HashTable *hash_table;
	HashTableIterator iterator;
	int count;

	hash_table = generate_hash_table();

	/* Iterate over all values in the table */

	count = 0;

	hash_table_iterate(hash_table, &iterator);

	while (hash_table_iter_has_more(&iterator)) {
		hash_table_iter_next(&iterator);

		++count;
	}

	assert(count == NUM_TEST_VALUES);

	/* Test iter_next after iteration has completed. */

	assert(hash_table_iter_next(&iterator) == HASH_TABLE_NULL);

	hash_table_free(hash_table);

	/* Test iterating over an empty table */

	hash_table = hash_table_new(int_hash, int_equal);
	
	hash_table_iterate(hash_table, &iterator);

	assert(hash_table_iter_has_more(&iterator) == 0);

	hash_table_free(hash_table);
}
Ejemplo n.º 5
0
void test_hash_table_insert_lookup(void)
{
	HashTable *hash_table;
	char buf[10];
	char *value;
	int i;

	/* Generate a hash table */

	hash_table = generate_hash_table();

	assert(hash_table_num_entries(hash_table) == NUM_TEST_VALUES);

	/* Check all values */

	for (i=0; i<NUM_TEST_VALUES; ++i) {
		sprintf(buf, "%i", i);
		value = hash_table_lookup(hash_table, buf);

		assert(strcmp(value, buf) == 0);
	}

	/* Lookup on invalid values returns NULL */

	sprintf(buf, "%i", -1);
	assert(hash_table_lookup(hash_table, buf) == NULL);
	sprintf(buf, "%i", NUM_TEST_VALUES);
	assert(hash_table_lookup(hash_table, buf) == NULL);

	/* Insert overwrites existing entries with the same key */

	sprintf(buf, "%i", 12345);
	hash_table_insert(hash_table, buf, strdup("hello world"));
	value = hash_table_lookup(hash_table, buf);
	assert(strcmp(value, "hello world") == 0);

	hash_table_free(hash_table);
}
Ejemplo n.º 6
0
void
on_input_word_activate (GtkEntry *entry, gpointer user_data)
{

	char *word = NULL, *entry_word = NULL;
	
	entry_word = gtk_entry_get_text ( mydata.input_word );	
	word = g_strchomp( g_strchug( g_strdown( entry_word ) ) );

	if( strlen( word ) == 0 ){
		//g_print("on_input_word_activate():word is NULL\n");
		//avoid some latency by commenting below
//
		gtk_statusbar_pop ( mydata.statusbar, mydata.statusbar_context_id );
		gtk_statusbar_push (GTK_STATUSBAR (mydata.statusbar), mydata.statusbar_context_id, "Please enter a word");
//		remove_all_from_list();	// empty the list as there is no word to search for									
//		if we empty the list it causes a bug of not loading the list of same word although
//		hash table is still there
	}else{
		//it is cause of latency
//		g_print("on_input_word_activate():word= %s\n", word );
		if( ! isalpha( word[0] ) ){
			//g_print("on_input_word_activate(): enter proper word\n");
			// *************** show proper warning to enter proper word **********************
//
			gtk_statusbar_pop ( mydata.statusbar, mydata.statusbar_context_id );
			gtk_statusbar_push (GTK_STATUSBAR (mydata.statusbar), mydata.statusbar_context_id, "Please enter proper english word");
//
			return;
		}								
		//check if hash table is created for word[0] of not
		g_sprintf( file_loaded_name, APP_DBS"%c.db", g_ascii_tolower( word[0] ) );
//		g_print("on_input_word_activate(): filename = %s\n",file_loaded_name);
		
		if( mydata.hash_tab[ g_ascii_toupper(word[0]) - 'A' ] == NULL ){
			//remove the prev word entries
			//remove_all_from_list ();
//			g_print("on_input_word_activate(): generating hash table\n");
			mydata.hash_tab[ g_ascii_toupper(word[0]) - 'A' ] = generate_hash_table( file_loaded_name );
			mydata.list_store_arr[ g_ascii_toupper(word[0]) - 'A' ] = mydata.list_store;
			list_loaded = g_ascii_toupper(word[0]) - 'A';
			//check if hash table is created or not by generate_hash_table() func call				 
			if( mydata.hash_tab[ g_ascii_toupper(word[0]) - 'A' ] == NULL ){
				g_print("on_input_word_activate():\nafter trying to generate_hash_table\nfile: %s could not be located and loaded\n", file_loaded_name );
			}						 
		}else{
			//g_print("on_input_word_activate(): %d", list_loaded );
			if( list_loaded != (g_ascii_toupper(word[0]) - 'A') ){				
				//
				if( mydata.list_store_arr[ g_ascii_toupper(word[0]) - 'A' ] == NULL ){
					populate_list_only( file_loaded_name );
					//g_print("on_input_word_changed(): list populated from file\n" );
				}else{					
					mydata.list_store = mydata.list_store_arr[ g_ascii_toupper(word[0]) - 'A' ];
//					following may be necessary		  
					gtk_tree_view_set_model ( mydata.treeview, GTK_TREE_MODEL(mydata.list_store) );
					//g_print("on_input_word_changed(): list used from previous list\n" );
				}
				
				list_loaded = (g_ascii_toupper(word[0]) - 'A');
				//
			}
		}	
			
	}
	
	on_find_clicked();
	
}