int main(void)
{
	/* test with some Point objects - the container items hold their addresses, not the actual Point structs! */
	/* "brace initialization" is convenient for structure types */
	struct Point s1 = {1, 1};
	struct Point s2 = {1, 2};
	struct Point s3 = {2, 1};
	struct Point s4 = {2, 2};
	struct Point probe1 = {1, 2};
	struct Point probe2 = {99, 99};
		
	struct Ordered_container * container;
	
	container = OC_create_container(compare_Points);
	
	printf("size is %d\n", OC_get_size(container));
	OC_apply(container, print_as_Point);
	
	/* fill the container with s1, s2, s3, s4 but insert in this order: s3, s1, s4, s2,
	and see if the points get put in the right order. */
	
	printf("\ninserting (%d, %d)\n", s3.x, s3.y);
	OC_insert(container, &s3); 
	printf("size is %d\n", OC_get_size(container));
	OC_apply(container,print_as_Point);
	
	printf("\ninserting (%d, %d)\n", s1.x, s1.y);
	OC_insert(container, &s1);
	printf("size is %d\n",OC_get_size(container));
	OC_apply(container, print_as_Point);
	
	printf("\ninserting (%d, %d)\n", s4.x, s4.y);
	OC_insert(container, &s4);
	printf("size is %d\n", OC_get_size(container));
	OC_apply(container, print_as_Point);

	printf("\ninserting (%d, %d)\n", s2.x, s2.y);
	OC_insert(container, &s2);
	printf("size is %d\n", OC_get_size(container));
	OC_apply(container, print_as_Point);

	/* use find_item and delete_item */
	find_and_remove(container, probe1);
	printf("size is %d\n", OC_get_size(container));
	OC_apply(container, print_as_Point);

	find_and_remove(container, probe2);
	printf("size is %d\n", OC_get_size(container));
	OC_apply(container, print_as_Point);
	
	/* demo another apply function - note function pointer cast is not needed for this one */
	{
		int odd_or_even_value = 42;
		OC_apply_arg(container, demo_func, (void *)&odd_or_even_value);
		odd_or_even_value = 3;
		OC_apply_arg(container, demo_func, (void *)&odd_or_even_value);
	}

	OC_clear(container);
	
	printf("size is %d\n", OC_get_size(container));
	OC_apply(container, print_as_Point);
	
	OC_destroy_container(container);
	/* using the pointer "container" is undefined at this point */
	
	printf("Done\n");
	return 0;
}
int main( void )
{
    int i; 
    int cur_size_container = 0;
    
    char* test_string[] = { "t5", "t6", "t3", "t9", "t7", "t1", "t2" };
    int size_test = 7;
    
    char* s1 = "s1";
    /*char* s2 = "s2";
    char* s3 = "s3";
    char* s4 = "s4";
    char* s5 = "s5";
    char* s6 = "s6";
    
    char* probe1 = "s3";
    char* probe2 = "s99";
    
    char* probe3 = "t2";
    char* probe4 = "t11";*/
    
    void* node;
    
    struct Ordered_container * container;

    printf("Starting test\nCreating\n" );

    /* create the container */
    container = OC_create_container((int (*)(const void*, const void*))compare_string);

    for (  i = 0 ; i < size_test ; ++i )
    {

        printf("Inserting %s\n", test_string[ i ] );
        OC_insert( container, test_string[ i ] );
        cur_size_container = OC_get_size( container );
        printf("\tsize is %d\n", cur_size_container );
        /* make sure it has the correct size */
        assert( ( i + 1 ) == cur_size_container );
        
        if ( /*( i % 3 ) == 1 */ 1 )
        {
            OC_apply( container, (void (*)(void*))print_as_string) ;
        }
    }
    
    assert( OC_apply_if( container, (int (*)(void*))right_form ) == 0  );
    
    printf( "Inserting s1\n " );
    OC_insert( container, s1 );
    OC_apply( container, (void (*)(void*))print_as_string) ;
    
    assert( OC_apply_if( container, (int (*)(void*))right_form ) == ('s' - 't' ) );
    
    node = OC_find_item( container, s1 );
    
    OC_apply( container, (void (*)(void*))print_as_string) ;
    
    /* check the correct pointer is returned */
    printf( "\nAfter adding s1\n" );
    assert( OC_get_data_ptr( node ) == s1 );
    
    /* check to make sure delete is working */
    OC_delete_item( container, node );
    assert( OC_get_size( container ) == size_test );


    /* check clear function */
    OC_clear( container );
    assert( OC_empty( container ) != 0 );
    

    
    /* rebuild array to run more tests */
    for (  i = 0 ; i < size_test ; ++i )
    {
        OC_insert( container, test_string[ i ] );
        cur_size_container = OC_get_size( container );
        /* make sure it has the correct size */
        assert( ( i + 1 ) == cur_size_container );
    }

    /* removing one element at a time */
    for( i = 0 ; i < size_test ; ++i )
    {
        printf( "%d: removing %s\n", i , test_string[i] );
        OC_apply( container, (void (*)(void*))print_as_string) ;
        find_and_remove( container, test_string[i] );
        assert( ( size_test - i - 1 ) == OC_get_size( container ) );
    }
    
    /* clean up memory */
    /* run with valgrind to ensure no mem leaks */
    OC_destroy_container( container );

    return 0;
}
예제 #3
0
bool LLXferManager::validateFileForTransfer(const std::string& filename)
{
	return find_and_remove(mExpectedTransfers, filename);
}
예제 #4
0
int sfs_delete(char *pathname)
{
	char** tokens;
	uint32_t* parent_location = NULL;
	locations index_block = {NULL};
	locations inode_loc = (locations) calloc(2, sizeof(uint32_t));
	int type = -1;

	if(strlen(pathname) == 1)
	{
		print_error(INVALID_PATH);
		return -1;
	}

	/*
	 * Parse the pathname
	 */
	tokens = tokenize_path(pathname);
	if(tokens == NULL)
	{
		print_error(INVALID_PATH);
		return 0;
	}

	/*
	 * Traverse the file system to find the desired inode
	 * get the inode above the one you desired
	 */
	parent_location = traverse_file_system(tokens, true);

	if(parent_location == NULL)
	{
		/*
		 * Parent not found
		 */
		print_error(PARENT_NOT_FOUND);
		return -1;
	}

	/*if (parent_location[0] = get_root() && tokens[parent_location[1]] == NULL)
	{
		print_error(INVALID_PARAMETER);
		return -1;
	}*/

	index_block = iterate_index(get_index_block(parent_location[0]), NULL);
	if(index_block == NULL)
	{
		/*
		 * Empty or no index block
		 */
		print_error(INDEX_ALLOCATION_ERROR);
		return 0;
	}

	/*
	 * Find the inode of the file or directory with the given file name in the
	 * given index block list
	 */
	inode_loc[0] = find_inode(index_block, tokens[parent_location[1]]);

	if(inode_loc[0] == NULL)
	{
		/*
		 * Inode not found
		 */
		print_error(FILE_NOT_FOUND);
		return 0;
	}

	/*
	 * Check if it is a directory
	 */
	type = get_type(inode_loc[0]);


	index_block = iterate_index(get_index_block(inode_loc[0]), NULL);

	if(index_block == NULL)
	{
		/*
		 * Invalid index block
		 */
		print_error(INDEX_ALLOCATION_ERROR);
		return -1;
	}

	if (type == 1)
	{
		if(index_block[0] != NULL)
		{
			/*
			 * Directory has children
			 */
			print_error(DIRECTORY_HAS_CHILDREN);
			return 0;
		}
	}

	/*
	 * Delete all swoft entries for the given file
	 */
	printf("Number swoft entries remove: %d\n",find_and_remove(inode_loc[0]));

	if(unlink_inode_from_parent(parent_location[0], inode_loc[0]) < 0)
	{
		if(reset_fbl() == NULL)
		{
			print_error(ERROR_UPDATING_FBL);
			return -2;
		}

		print_error(ERROR_BLOCK_LINKAGE);
		return -1;
	}

	else if(type == 0)
	{
		/*
		 * Delete the data blocks (each time update the free_block list)
		 */
		if(update_fbl(NULL, index_block) == NULL)
		{
			print_error(ERROR_UPDATING_FBL);
			return -1;
		}
	}

	/*
	 * Delete the index blocks (each time update the free_block list)
	 */
	if(update_fbl(NULL, index_block_locations(get_index_block(inode_loc[0]), NULL)) == NULL)
	{
		print_error(ERROR_UPDATING_FBL);
		return -1;
	}

	/*
	 * Delete the Inode block (update the free_block list)
	 */
	if(update_fbl(NULL, inode_loc) == NULL)
	{
		print_error(ERROR_UPDATING_FBL);
		return -1;
	}

	/*
	 * Sync FBL, write FBL in memory to disk
	 */
	if(sync_fbl() == NULL)
	{
		print_error(ERROR_UPDATING_FBL);
		return -1;
	}

	/*
	 * return value > 0 then the file was deleted successfully.
	 * return value <= 0 then the file failed to be delete.
	 */
	print_error(SUCCESS);
	return 1;
}
예제 #5
0
bool LLXferManager::validateFileForRequest(const std::string& filename)
{
	return find_and_remove(mExpectedRequests, filename);
}