/* * intlist_test.c * * Copyright (c) 2013-2018 * * Source code released under the GPL version 2 */ #include #include #include #include "intlist.h" char progname[] = "intlist_test"; char version[] = "0.1"; #define check( a, b ) { \ if ( !(a) ) { \ fprintf( stderr, "Failed %s (%s) in %s() line %d\n", #a, b, __FUNCTION__, __LINE__ );\ return 1; \ } \ } #define check_len( a, b ) if ( !_check_len( a, b, __FUNCTION__, __LINE__ ) ) return 1; int _check_len( intlist *a, int expected, const char *fn, int line ) { if ( a->n == expected ) return 1; fprintf( stderr, "Failed: %s() line %d: Expected intlist length of %d, found %d\n", fn, line, expected, a->n ); return 0; } #define check_entry( a, b, c ) if ( !_check_entry( a, b, c, __FUNCTION__, __LINE__ ) ) return 1; int _check_entry( intlist *a, int n, int expected, const char *fn, int line ) { int m; m = intlist_get( a, n ); if ( m == expected ) return 1; fprintf( stderr, "Failed: %s() line %d: Expected intlist element %d to be %d, found %d\n", fn, line, n, expected, m ); return 0; } /* * void intlist_init( intlist *il ); */ int test_init( void ) { intlist il; intlist_init( &il ); check_len( &il, 0 ); intlist_free( &il ); return 0; } /* * void intlist_init_fill( intlist *il, int n, int value ); */ #define COUNT (150) int test_init_fill( void ) { int i, status; intlist il; status = intlist_init_fill( &il, COUNT, 3121 ); check( (status==INTLIST_OK), "intlist_init_fill() should return INTLIST_OK" ); check_len( &il, COUNT ); for ( i=0; i=0&&m<99), "intlist_randomize() shouldn't remove values" ); } intlist_free( &a ); return 0; } /* * void intlist_sort( intlist *il ); */ int test_sort( void ) { intlist a; int i; intlist_init_range( &a, 100, 0, -1 ); check_len( &a, 100 ); check_entry( &a, 0, 100 ); check_entry( &a, 99, 1 ); intlist_sort( &a ); for ( i=0; i<100; ++i ) check_entry( &a, i, i+1 ); intlist_free( &a ); return 0; } /* * int intlist_fill( intlist *il, int n, int value ); */ #define COUNT (1011) int test_fill( void ) { int i, status; intlist a; intlist_init( &a ); status = intlist_fill( &a, COUNT, 51221 ); check( (status==INTLIST_OK), "intlist_fill() should return INTLIST_OK" ); check_len( &a, COUNT ); for ( i=0; i=0&&m<=COUNT), "intlist_find() should find valid entries" ); } for ( i=-100;i<0; ++i ) { m = intlist_find( &a, i ); check( (m<0||m>=COUNT), "intlist_find() should not find invalid entries" ); } for ( i=COUNT;i=COUNT), "intlist_find() should not find invalid entries" ); } intlist_free( &a ); return 0; } /* * int intlist_find_or_add( intlist *il, int searchvalue ); */ int test_find_or_add( void ) { int i, m, n; intlist a; intlist_init_range( &a, 0, 150, 10 ); check_len( &a, 15 ); check_entry( &a, 0, 0 ); check_entry( &a, 14, 140 ); for ( i=0; i<150; i+=10 ) { m = intlist_find_or_add( &a, i ); check( (m>=0&&m<15), "intlist_find_or_add() should find existing entries" ); } n = a.n; for ( i=5; i<155; i+=10 ) { n++; m = intlist_find( &a, i ); check( (m<0||m>=n), "intlist_find() should not find missing entries" ); m = intlist_find_or_add( &a, i ); check_len( &a, n ); check_entry( &a, n-1, i ); check( (m==n-1), "intlist_find_or_add() should find added entries" ); } check_len( &a, 30 ); intlist_free( &a ); return 0; } /* * void intlist_empty( intlist *il ); */ int test_empty( void ) { intlist a; int status; status = intlist_init_fill( &a, 100, 0 ); check( (status==INTLIST_OK), "intlist_init_fill() should return INTLIST_OK" ); check_len( &a, 100 ); intlist_empty( &a ); check_len( &a, 0 ); status = intlist_fill( &a, 150, 10 ); check( (status==INTLIST_OK), "intlist_fill() should return INTLIST_OK" ); check_len( &a, 150 ); intlist_empty( &a ); check_len( &a, 0 ); intlist_free( &a ); return 0; } /* * int intlist_copy( intlist *to, intlist *from ); */ int test_copy( void ) { int i, status; intlist a, b; status = intlist_init_fill( &a, 110, 11 ); check( (status==INTLIST_OK), "intlist_init_fill() should return INTLIST_OK" ); check_len( &a, 110 ); for ( i=0; i<110; ++i ) check_entry( &a, i, 11 ); status = intlist_init_range( &b, 0, 60, 1 ); check( (status==INTLIST_OK), "intlist_init_range() should return INTLIST_OK" ); check_len( &b, 60 ); for ( i=0; i<60; ++i ) check_entry( &b, i, i ); status = intlist_copy( &b, &a ); check( (status==INTLIST_OK), "intlist_copy() should return INTLIST_OK" ); check_len( &a, 110 ); for ( i=0; i<110; ++i ) check_entry( &a, i, 11 ); check_len( &b, 110 ); for ( i=0; i<110; ++i ) check_entry( &b, i, 11 ); intlist_free( &a ); intlist_free( &b ); return 0; } /* * intlist * intlist_dup( intlist *from ); */ int test_dup( void ) { intlist a, *b; int i, status; status = intlist_init_range( &a, 0, 60, 1 ); check( (status==INTLIST_OK), "intlist_init_range() should return INTLIST_OK" ); check_len( &a, 60 ); for ( i=0; i<60; ++i ) check_entry( &a, i, i ); b = intlist_dup( &a ); check( (b!=NULL), "intlist_dup() should not return NULL" ); check_len( b, 60 ); for ( i=0; i<60; ++i ) check_entry( b, i, i ); intlist_free( &a ); intlist_delete( b ); return 0; } /* * int intlist_get( intlist *il, int pos ); */ int test_get( void ) { int i, m, status; intlist a; status = intlist_init_range( &a, 0, 60, 1 ); check( (status==INTLIST_OK), "intlist_init_range() should return INTLIST_OK" ); check_len( &a, 60 ); for ( i=0; i<60; ++i ) { m = intlist_get( &a, i ); check( (m==i), "intlist_get() should return value of the entry" ); } intlist_free( &a ); return 0; } /* * int intlist_set( intlist *il, int pos ); */ int test_set( void ) { int i, m, status; intlist a; status = intlist_init_range( &a, 0, 60, 1 ); check( (status==INTLIST_OK), "intlist_init_range() should return INTLIST_OK" ); check_len( &a, 60 ); for ( i=0; i<30; ++i ) { status = intlist_set( &a, i, -10 ); check( (status==INTLIST_OK), "intlist_set() should return INTLIST_OK" ); check_len( &a, 60 ); } for ( i=0; i<60; ++i ) { m = intlist_get( &a, i ); if ( i<30 ) { check( (m==-10), "intlist_get() should return value of the entry" ); } else { check( (m==i), "intlist_get() should return value of the entry" ); } } intlist_free( &a ); return 0; } /* * int intlist_append( intlist *to, intlist *from ); */ int test_append( void ) { int i, status; intlist a, b; status = intlist_init_range( &a, 0, 60, 1 ); check( (status==INTLIST_OK), "intlist_init_range() should return INTLIST_OK" ); check_len( &a, 60 ); status = intlist_init_range( &b, 60, 90, 1 ); check( (status==INTLIST_OK), "intlist_init_range() should return INTLIST_OK" ); check_len( &b, 30 ); status = intlist_append( &a, &b ); check( (status==INTLIST_OK), "intlist_append() should return INTLIST_OK" ); check_len( &a, 90 ); for ( i=0; i