X-Git-Url: https://git.rrq.au/?a=blobdiff_plain;f=tests%2Fvectortests.c;h=44bb9652db6c8d292eb0db5e8e513be5d211f68f;hb=e938f8c45fd191d96da48f65262e4efcfada7805;hp=b596cfb4cc3813bd7b509d9815b1ad991166fa2a;hpb=6fcd4ffc18696dbf4c11be32837a2035ea5ee92f;p=rrq%2Frrqmisc.git diff --git a/tests/vectortests.c b/tests/vectortests.c index b596cfb..44bb965 100644 --- a/tests/vectortests.c +++ b/tests/vectortests.c @@ -1,22 +1,22 @@ /** - * A sequence of tests of the vector.h functions. + * A sequence of tests of the Vector.h functions. */ #include #include -#include +#include #define OUT(...) fprintf( stderr, __VA_ARGS__ ) // dump an item; return 0 to stop -static void itemdump(const vector_index index,const void *slot) { +static void itemdump(const VectorIndex index,const void *slot) { if ( slot ) { OUT ( "[%ld] %s\n", index, (const char*) slot ); } } // pretend-reclaim item and return data (as int) -static int itemreclaim(vector *pv,vector_index index,void *item,void *data) { +static int itemreclaim(Vector *pv,VectorIndex index,void *item,void *data) { int r = data? 1 : 0; OUT( "reclaim [%ld] (%p) => %d\n", index, item, r ); return r; @@ -28,7 +28,7 @@ static int itemcmp(const void *a,const void *b) { } // Iterator function -static int itemiter(vector_index index,void *item,void *data) { +static int itemiter(VectorIndex index,void *item,void *data) { char *s = ""; if ( item ) { s = (char*) item; @@ -48,13 +48,13 @@ int itemfind(const void *key, const void *item) { return strncmp( a, b, (an [%ld] %p\n", t0[i], index, slot? *slot : 0 ); } - OUT( "vector_prev_used:\n" ); + OUT( "Vector_prev_used:\n" ); for ( i = 0; i < 6; i++ ) { - vector_index index = t0[i]; - slot = vector_prev_used( &v, &index ); + VectorIndex index = t0[i]; + slot = Vector_prev_used( &v, &index ); OUT( " [%d] => [%ld] %p\n", t0[i], index, slot? *slot : 0 ); } item = item75; - vector_set( &v, 75, item ); + Vector_set( &v, 75, item ); OUT( "assigned 75 to %p %s\n", item, (char*)item ); - my_vector_dump( &v, itemdump ); + my_Vector_dump( &v, itemdump ); - OUT( "vector_next_used:\n" ); + OUT( "Vector_next_used:\n" ); for ( i = 0; i < 6; i++ ) { - vector_index index = t0[i]; - slot = vector_next_used( &v, &index ); + VectorIndex index = t0[i]; + slot = Vector_next_used( &v, &index ); OUT( " [%d] => [%ld] %p\n", t0[i], index, slot? *slot : 0 ); } - OUT( "vector_prev_used:\n" ); + OUT( "Vector_prev_used:\n" ); for ( i = 0; i < 6; i++ ) { - vector_index index = t0[i]; - slot = vector_prev_used( &v, &index ); + VectorIndex index = t0[i]; + slot = Vector_prev_used( &v, &index ); OUT( " [%d] => [%ld] %p\n", t0[i], index, slot? *slot : 0 ); } - OUT( "shrinking the vector:\n" ); - // int vector_resize( - // vector *pv, vector_index new_size, - // int (*reclaim)(vector *pv,vector_index index, + OUT( "shrinking the Vector:\n" ); + // int Vector_resize( + // Vector *pv, VectorIndex new_size, + // int (*reclaim)(Vector *pv,VectorIndex index, // void *item, void *data), // void *data ); - i = vector_resize( &v, 50, itemreclaim, (void*)1 ); + i = Vector_resize( &v, 50, itemreclaim, (void*)1 ); OUT( "shrink to 50 (reclaim refused) = %d\n", i ); - i = vector_resize( &v, 50, itemreclaim, (void*)0 ); + i = Vector_resize( &v, 50, itemreclaim, (void*)0 ); OUT( "shrink to 50 (accept reclaim) = %d\n", i ); - i = vector_resize( &v, 508, 0, 0 ); + i = Vector_resize( &v, 508, 0, 0 ); OUT( "grow to 508 (no reclaim) = %d\n", i ); - // void **vector_entry(vector *pv,vector_index index); + // void **Vector_entry(Vector *pv,VectorIndex index); #define SLOTSTR(slot) (slot? ((*slot)? *slot : "(nil)") : "(unassigned)" ) - slot = vector_entry( &v, 24 ); + slot = Vector_entry( &v, 24 ); itemdump( 24, SLOTSTR(slot) ); - slot = vector_entry( &v, 25 ); + slot = Vector_entry( &v, 25 ); itemdump( 25, SLOTSTR(slot) ); - slot = vector_entry( &v, 300 ); + slot = Vector_entry( &v, 300 ); itemdump( 300, SLOTSTR( slot ) ); - //#define vector_size(pv) ((vector_index) (pv)->size) - OUT( "vector size: %ld\n", vector_size( &v ) ); + //#define Vector_size(pv) ((VectorIndex) (pv)->size) + OUT( "Vector size: %ld\n", Vector_size( &v ) ); - // void *vector_get(vector *pv,vector_index index); - // void *vector_get_set(vector *pv,vector_index index,void *value); - item = vector_get( &v, 25 ); + // void *Vector_get(Vector *pv,VectorIndex index); + // void *Vector_get_set(Vector *pv,VectorIndex index,void *value); + item = Vector_get( &v, 25 ); OUT( "old item 25 is %p %s\n", item, (char*)item ); item = "another value"; OUT( "new item 25 is %p %s\n", item, (char*)item ); - item = vector_get_set( &v, 25, item ); + item = Vector_get_set( &v, 25, item ); OUT( "got item 25 as %p %s\n", item, (char*)item ); - item = vector_get( &v, 25 ); + item = Vector_get( &v, 25 ); OUT( "now item 25 is %p %s\n", item, (char*)item ); - // void vector_append(vector *pv,void *value); + // void Vector_append(Vector *pv,void *value); item = "the very last item"; OUT( "appending %p %s\n", item, (char*)item ); - vector_append( &v, item ); - - OUT( "vector size: %ld\n", vector_size( &v ) ); - my_vector_dump( &v, itemdump ); - - vector v2 = { variant, 200, 0 }; - // void vector_copy( - // vector *dst,vector_index di, - // vector *src,vector_index si, - // vector_index n); - vector_copy( &v2, 20, &v, 10, 20 ); - my_vector_dump( &v2, itemdump ); - - vector_resize( &v2, 0, itemreclaim, 0 ); // Reset vector v2 - my_vector_dump( &v2, itemdump ); - - vector_append( &v2, "9 the very last item" ); - vector_append( &v2, "3 the very last item" ); - vector_append( &v2, "4 the very last item" ); - vector_append( &v2, "6 the very last item" ); - vector_append( &v2, "5 the very last item" ); - vector_resize( &v2, vector_size( &v2 ) + 3, 0, 0 ); - vector_append( &v2, "2 the very last item" ); - vector_append( &v2, "8 the very last item" ); - vector_append( &v2, "1 the very last item" ); - vector_append( &v2, 0 ); - vector_append( &v2, "7 the very last item" ); - vector_append( &v2, "0 the very last item" ); - my_vector_dump( &v2, itemdump ); - - // void vector_qsort(vector*,int (*compar)(const void *,const void *)); + Vector_append( &v, item ); + + OUT( "Vector size: %ld\n", Vector_size( &v ) ); + my_Vector_dump( &v, itemdump ); + + Vector v2 = { variant, 200, 0 }; + // void Vector_copy( + // Vector *dst,VectorIndex di, + // Vector *src,VectorIndex si, + // VectorIndex n); + Vector_copy( &v2, 20, &v, 10, 20 ); + my_Vector_dump( &v2, itemdump ); + + Vector_resize( &v2, 0, itemreclaim, 0 ); // Reset Vector v2 + my_Vector_dump( &v2, itemdump ); + + Vector_append( &v2, "9 the very last item" ); + Vector_append( &v2, "3 the very last item" ); + Vector_append( &v2, "4 the very last item" ); + Vector_append( &v2, "6 the very last item" ); + Vector_append( &v2, "5 the very last item" ); + Vector_resize( &v2, Vector_size( &v2 ) + 3, 0, 0 ); + Vector_append( &v2, "2 the very last item" ); + Vector_append( &v2, "8 the very last item" ); + Vector_append( &v2, "1 the very last item" ); + Vector_append( &v2, 0 ); + Vector_append( &v2, "7 the very last item" ); + Vector_append( &v2, "0 the very last item" ); + my_Vector_dump( &v2, itemdump ); + + // void Vector_qsort(Vector*,int (*compar)(const void *,const void *)); OUT( "sorted:" ); - vector_qsort( &v2, itemcmp ); - my_vector_dump( &v2, itemdump ); + Vector_qsort( &v2, itemcmp ); + my_Vector_dump( &v2, itemdump ); - // void vector_iterate(vector *pv, - // vector_index start, - // int (*itemfn)(vector_index,void *item,void *data), + // void Vector_iterate(Vector *pv, + // VectorIndex start, + // int (*itemfn)(VectorIndex,void *item,void *data), // void *data); OUT( "showing slots from 4 to 12\n" ); - vector_iterate( &v2, 4, itemiter, 0 ); + Vector_iterate( &v2, 4, itemiter, 0 ); - // void *vector_bsearch(vector *pv,vector_index *index,const void *key, + // void *Vector_bsearch(Vector *pv,VectorIndex *index,const void *key, // int (*compare)(const void *key, const void *item)); char *pfx[5] = { "4", "9", "0", "3", "10" }; for ( i = 0; i < ( sizeof( pfx ) / sizeof( char* ) ); i++ ) { char *prefix = pfx[i]; - vector_index index = 0; + VectorIndex index = 0; OUT( "lookup prefix \"%s\":\n", prefix ); - item = vector_bsearch( &v2, &index, prefix, itemfind ); + item = Vector_bsearch( &v2, &index, prefix, itemfind ); OUT( "[%ld] %p %s\n", index, item, ( item? (char*)item : "(null)" ) ); } - // Clear out the vectors - (void) vector_resize( &v, 0, itemreclaim, (void*)0 ); - (void) vector_resize( &v2, 0, itemreclaim, (void*)0 ); + // Clear out the Vectors + (void) Vector_resize( &v, 0, itemreclaim, (void*)0 ); + (void) Vector_resize( &v2, 0, itemreclaim, (void*)0 ); } return 0; }