Пример #1
0
int main(void)
{
    //OC_comp_fp_t f_ptr;
    struct Ordered_container* container_ptr = OC_create_container(&thing_comp_f);

    OC_insert(container_ptr, make_thing(5));
    OC_insert(container_ptr, make_thing(3));
    OC_insert(container_ptr, make_thing(4));
    OC_insert(container_ptr, make_thing(1));
    OC_insert(container_ptr, make_thing(2));
    OC_insert(container_ptr, make_thing(99));
    OC_insert(container_ptr, make_thing(-2));

    for (int i = 0; i < 7; ++i)
    {
        printf("Element %d: %d\n", i, *(int*)(*(container_ptr->array + i)));
    }

    printf("size = %d\n", OC_get_size(container_ptr));
    printf("Empty = %d\n", OC_empty(container_ptr));

    int search_num = 5;
    int* data_ptr = OC_find_item(container_ptr, &search_num);
    printf("Found data: %d\n", *(int*)OC_get_data_ptr(data_ptr));
    search_num = 99;
    printf("Apply_if_arg returned: %d\n", 
           OC_apply_if_arg(container_ptr, (OC_apply_if_arg_fp_t)apply_if_arg_tester, &search_num));
    data_ptr = OC_find_item_arg(container_ptr, &search_num, alt_thing_comp_f);
    printf("Found data: %d\n", *(int*)OC_get_data_ptr(data_ptr));

    printf("Things: %d\n", g_num_things);
    OC_apply(container_ptr, &delete_thing);
    OC_clear(container_ptr);
    printf("Things: %d\n", g_num_things);
    printf("size = %d\n", OC_get_size(container_ptr));
    printf("Empty = %d\n", OC_empty(container_ptr));

    OC_destroy_container(container_ptr);

    void* test = malloc(8);

    printf("Old way %p   New Way %p\n", *(void**)test, test);

    return 0;
}
Пример #2
0
static void clear_library( struct Ordered_container* lib_title, struct Ordered_container* lib_ID, struct Ordered_container* catalog, char* output )
{
    if ( strlen( output ) > 0 && OC_apply_if( catalog, is_Collection_not_empty ) )
    {
        print_error( "Cannot clear all records unless all collections are empty!\n" );
    }
    else
    {
        reset_Record_ID_counter();
        
        /* clear lib_ID instead of calling clear_container b/c
         it points to the same data as lib_title */
        OC_clear( lib_ID );
        
        /* clear the container and destroy the records */
        clear_container(lib_title, ( void(*)(void*) )destroy_Record, output );
    }
}
Пример #3
0
int main(void)
{
    //OC_comp_fp_t f_ptr;
    struct Ordered_container* container_ptr = OC_create_container(&thing_comp_f);

    OC_insert(container_ptr, make_thing(5));
    OC_insert(container_ptr, make_thing(3));
    OC_insert(container_ptr, make_thing(4));
    OC_insert(container_ptr, make_thing(1));
    OC_insert(container_ptr, make_thing(2));
    OC_insert(container_ptr, make_thing(99));
    OC_insert(container_ptr, make_thing(-2));

    printf("size = %d\n", OC_get_size(container_ptr));
    printf("Empty = %d\n", OC_empty(container_ptr));
    Print_container(container_ptr, sizeof(int));

    int search_num = 5;
    int* data_ptr = OC_find_item(container_ptr, &search_num);
    printf("Found data: %d\n", *(int*)OC_get_data_ptr(data_ptr));
    search_num = 99;
    printf("Apply_if_arg returned: %d\n", 
           OC_apply_if_arg(container_ptr, (OC_apply_if_arg_fp_t)apply_if_arg_tester, &search_num));
    data_ptr = OC_find_item_arg(container_ptr, &search_num, alt_thing_comp_f);
    printf("Found data: %d\n", *(int*)OC_get_data_ptr(data_ptr));

    printf("Things: %d\n", g_num_things);
    OC_apply(container_ptr, &delete_thing);
    OC_clear(container_ptr);
    printf("Things: %d\n", g_num_things);
    printf("size = %d\n", OC_get_size(container_ptr));
    printf("Empty = %d\n", OC_empty(container_ptr));
    Print_container(container_ptr, sizeof(int));

    OC_destroy_container(container_ptr);

    return 0;
}
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;
}
int main(int argc, const char * argv[]) {
    
    int a = 1, b = 3, c = 5, d = 7,
        e = 2, f = 4, g = 6, h = 8;
    void *ptr1 = &a, *ptr2 = &b, *ptr3 = &c, *ptr4 = &d,
         *ptr5 = &e, *ptr6 = &f, *ptr7 = &g, *ptr8 = &h;
    
    struct Ordered_container* container1 = OC_create_container( (OC_comp_fp_t)compare_int_ptr);
    
    
    assert(container1);
    OC_insert(container1, ptr3);
    print_containter(container1); // 5
    OC_insert(container1, ptr2);
    print_containter(container1); // 3 5
    OC_insert(container1, ptr6);   // 4
    print_containter(container1);  // 3 4 5
    OC_insert(container1, ptr4);
    print_containter(container1); // 3 4 5 7
    OC_insert(container1, ptr1);
    print_containter(container1); // 1 3 4 5 7
    OC_insert(container1, ptr8);
    print_containter(container1); // 1 3 4 5 7 8
    
    printf("%d\n",OC_get_size(container1)); // 6
    printf("%d\n",OC_empty(container1));    // 0
    OC_clear(container1);
    printf("%d\n",OC_get_size(container1));
    
    
    OC_insert(container1, ptr3);
    print_containter(container1); // 5
    OC_insert(container1, ptr2);
    print_containter(container1); // 3 5
    OC_insert(container1, ptr4);
    print_containter(container1); // 3 5 7
    OC_insert(container1, ptr1);
    print_containter(container1); // 1 3 5 7
    OC_insert(container1, ptr8);
    print_containter(container1); // 1 3 5 7 8
    
    
    //OC_destroy_container(container1);
    
    void* found_item_ptr1 = OC_find_item(container1, ptr4);
    
    if (!found_item_ptr1) {
        printf("found_item_ptr not found");
    } else {
        printf("found_item_ptr");
        printf(" and it's %d\n", *(int*)OC_get_data_ptr(found_item_ptr1));
    }
    
    OC_delete_item(container1, found_item_ptr1);
    print_containter(container1); // 1 3 5 8

    void* found_item_ptr2 = OC_find_item(container1, ptr1);
    OC_delete_item(container1, found_item_ptr2);
    void* found_item_ptr3 = OC_find_item(container1, ptr8);
    OC_delete_item(container1, found_item_ptr3);
    
    print_containter(container1); // 3 5
    
    OC_insert(container1, ptr6);   // 4
    print_containter(container1);  // 3 4 5
    OC_insert(container1, ptr5);   // 2
    print_containter(container1);  // 2 3 4 5
    printf("%d\n",OC_get_size(container1)); // 4

    OC_apply(container1, (OC_apply_fp_t)print_int); // 2 3 4 5
    printf("\n");
    

    

    
    
    return 0;
    
    
}
Пример #7
0
/* clears a Container as well as applies the destructor to all the data it pointed to */
static void clear_container( struct Ordered_container* c_ptr , OC_apply_fp_t destructor, char* output )
{
    OC_apply( c_ptr, destructor );
    OC_clear( c_ptr );
    printf( "%s", output );
}