void deleteGroup(struct Ordered_container* rooms, struct Ordered_container* people) { if (OC_apply_if(rooms, (OC_apply_if_fp_t)roomHasMeeting)) { readRestOfLine(); printf("Cannot clear people list unless there are no meetings!\n"); } else { OC_apply(people, (OC_apply_fp_t)destroy_Person); } }
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 ); } }
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; }