2017-11-18 19:09:20 +03:00
/* SPDX-License-Identifier: LGPL-2.1+ */
2013-07-05 08:25:26 +04:00
# include <stdio.h>
# include <stdlib.h>
2018-01-11 02:39:12 +03:00
# include <string.h>
2013-07-05 08:25:26 +04:00
typedef const char * ( * lookup_t ) ( int ) ;
typedef int ( * reverse_t ) ( const char * ) ;
static inline void _test_table ( const char * name ,
lookup_t lookup ,
reverse_t reverse ,
2013-07-16 20:01:01 +04:00
int size ,
bool sparse ) {
2015-02-02 04:24:31 +03:00
int i , boring = 0 ;
2013-07-05 08:25:26 +04:00
2013-07-16 20:01:01 +04:00
for ( i = - 1 ; i < size + 1 ; i + + ) {
2013-07-05 08:25:26 +04:00
const char * val = lookup ( i ) ;
2013-07-14 01:36:03 +04:00
int rev ;
2013-07-05 08:25:26 +04:00
2015-02-02 04:24:31 +03:00
if ( val ) {
2013-07-05 08:25:26 +04:00
rev = reverse ( val ) ;
2015-02-02 04:24:31 +03:00
boring = 0 ;
} else {
2013-07-14 01:36:03 +04:00
rev = reverse ( " --no-such--value---- " ) ;
2015-02-02 04:24:31 +03:00
boring + = i > = 0 ;
}
if ( boring < 1 | | i = = size )
printf ( " %s: %d → %s → %d \n " , name , i , val , rev ) ;
else if ( boring = = 1 )
printf ( " %*s ... \n " , ( int ) strlen ( name ) , " " ) ;
2013-07-05 08:25:26 +04:00
2014-10-28 20:07:20 +03:00
assert_se ( ! ( i > = 0 & & i < size ?
sparse ? rev ! = i & & rev ! = - 1 : val = = NULL | | rev ! = i :
val ! = NULL | | rev ! = - 1 ) ) ;
2013-07-05 08:25:26 +04:00
}
}
# define test_table(lower, upper) \
2013-07-16 20:01:01 +04:00
_test_table ( STRINGIFY ( lower ) , lower # # _to_string , lower # # _from_string , _ # # upper # # _MAX , false )
2014-07-10 06:29:24 +04:00
# define test_table_sparse(lower, upper) \
_test_table ( STRINGIFY ( lower ) , lower # # _to_string , lower # # _from_string , _ # # upper # # _MAX , true )