void MaxHeapSuite_TestCreationExtraction( MaxHeapSuiteData* data ) {
   Index    ii=0;

   /* These initial totals due to set up above */
   pcu_check_true( data->heap->numHeapElements == NUM_INITIAL_DATA );
   pcu_check_true( data->heap->numArrayElements == NUM_INITIAL_DATA );

   for( ii=0; ii<NUM_INITIAL_DATA; ii++ ){
      /* Since we are always extracting the max, expect the order to be reversed */
      pcu_check_true( *(int*)MaxHeap_Extract( data->heap ) == data->dataArray[(NUM_INITIAL_DATA-1)-ii] );
   }

   pcu_check_true( data->heap->numHeapElements == 0 );
   pcu_check_true( data->heap->numArrayElements == NUM_INITIAL_DATA );
}
void MaxHeapSuite_TestInsertionExtraction( MaxHeapSuiteData* data ) {
   Index    ii=0;

   /*Inserting more entries into the Heap*/
   for( ii=NUM_INITIAL_DATA; ii<NUM_DATA; ii++ ){
      MaxHeap_Insert( data->heap, &(data->dataArray[ii]) );
   }
   
   pcu_check_true( data->heap->numHeapElements == NUM_DATA );
   pcu_check_true( data->heap->numArrayElements == NUM_DATA );

   for( ii=0; ii<NUM_DATA; ii++ ){
      /* Since we are always extracting the max, expect the order to be reversed */
      pcu_check_true( *(int*)MaxHeap_Extract( data->heap ) == data->dataArray[(NUM_DATA-1)-ii] );
   }

   pcu_check_true( data->heap->numHeapElements == 0 );
   pcu_check_true( data->heap->numArrayElements == NUM_DATA );
}
Exemplo n.º 3
0
int main( int argc, char* argv[] ) {
	MPI_Comm			CommWorld;
	int				rank;
	int				numProcessors;
	int				procToWatch;
	int **keys;
	
	MaxHeap *heap;
	int i = 0;
	Stream *myStream = NULL;
	
	/* Initialise MPI, get world info */
	MPI_Init( &argc, &argv );
	MPI_Comm_dup( MPI_COMM_WORLD, &CommWorld );
	MPI_Comm_size( CommWorld, &numProcessors );
	MPI_Comm_rank( CommWorld, &rank );

	BaseFoundation_Init( &argc, &argv );
	BaseIO_Init( &argc, &argv );
	BaseContainer_Init( &argc, &argv );

	if( argc >= 2 ) {
		procToWatch = atoi( argv[1] );
	}
	else {
		procToWatch = 0;
	}
	
	if( rank == procToWatch ) {
		
		myStream = Journal_Register( InfoStream_Type, "LinkedListStream" );
		data = Memory_Alloc_Array_Unnamed( int, NUM_DATA );
		keys = Memory_Alloc_Array_Unnamed( int*, NUM_INITIAL_DATA );
		
		Journal_Printf( myStream, "\nCreating the Heap\n" );
		for(i=0; i<NUM_INITIAL_DATA; i++){
			data[i] = i;
			keys[i] = &(data[i]);
		}
		
		heap = MaxHeap_New(
					(void**)(keys), sizeof(int),
					NUM_INITIAL_DATA,
					keySwap,
					compareFunction,
					extendArray );

		Journal_Printf( myStream, "\nPrinting the Heap\n" );
		Stg_Class_Print( heap, myStream );
		
		Journal_Printf( myStream, "\nInserting more entries into the Heap\n" );
		for( i=50; i<NUM_DATA; i++ ){
			data[i] = i;
			MaxHeap_Insert( heap, &(data[i]) );
		}
		
		Journal_Printf( myStream, "\nPrinting the Heap\n" );
		Stg_Class_Print( heap, myStream );
		
		Journal_Printf( myStream, "\nExtracting all the entries in the Heap\n" );
		for( i=0; i<NUM_DATA; i++ ){
			printf( "Heap Max %d\n", *(int*)MaxHeap_Extract( (_Heap*)heap ) );
		}
		
		Journal_Printf( myStream, "\nPrinting the Heap\n" );
		Stg_Class_Print( heap, myStream );
	
		Journal_Printf( myStream, "\nDeleting the Heap\n" );
		Stg_Class_Delete( (void*)heap );
		
		Memory_Free( data );
	}