// Author: Douglas Wilhelm Harder // Copyright (c) 2012 by Douglas Wilhelm Harder. All rights reserved. /************************************************* * This tests if the queue data structure * works by randomly pushing or popping entries * onto or off of the queue. * * The integers are pushed onto the queue in * order and should, therefore, come off in * order, too. * * The order of pushes and pops is random, but * a push is, initially, three times more likely * than a pop. This makes sure that the resizing * capability works while ensuring that at least * a few pops occur between each resize. *************************************************/ #include #include "Queue.h" #define N 10000 int main() { struct Queue q; init( &q, 16 ); int a[N]; int i; for ( i = 0; i < N; ++i ) { a[i] = i; } int j = 0; for ( i = 0; i < 5*N; ++i ) { if ( (rand() % 4) == 0 ) { if ( front( &q ) != NULL ) { printf( "%d\n", *((int *)front( &q )) ); pop( &q ); } } else { if ( j < N ) { push( &q, &(a[j]) ); ++j; } } } deinit( &q ); return 0; }