@@ -315,6 +315,122 @@ static int test_mp_fread_fwrite(void)
315315 return EXIT_FAILURE ;
316316}
317317
318+ #if (!(defined LTM_NOTHING ) && !(defined MP_NO_FILE ) && (defined __GLIBC__ ))
319+ #include <printf.h>
320+ #define LTM_TEST_BUFSIZ 1024
321+ static int test_mp_printf_extension (void )
322+ {
323+ FILE * test_file = NULL ;
324+
325+ char line_buffer [LTM_TEST_BUFSIZ ] = {0 };
326+ bool write_only = false;
327+ size_t slen = 0 ;
328+ int characters_printed = 0 ;
329+ char * fgets_return ;
330+ int idx = 0 ;
331+
332+ const char * test_values [2 ] = {
333+ "4DDCFDE0D20EF8663B34D19F829FDD" ,
334+ "-51D9769BDAE5B38121F2A31D881E5F"
335+ };
336+ const char * test_strings [12 ] = {
337+ "Right aligned AAA 404289102523688521157725445716877277 BBB\n" ,
338+ "Left aligned AAA 404289102523688521157725445716877277 BBB\n" ,
339+ "hex with right align AAA 0x4DDCFDE0D20EF8663B34D19F829FDD BBB\n" ,
340+ "hex with left align AAA 0x4DDCFDE0D20EF8663B34D19F829FDD BBB\n" ,
341+ "Right aligned AAA -424986725583297217766029037085924959 BBB\n" ,
342+ "Left aligned AAA -424986725583297217766029037085924959 BBB\n" ,
343+ "hex with right align AAA -0x51D9769BDAE5B38121F2A31D881E5F BBB\n" ,
344+ "hex with left align AAA -0x51D9769BDAE5B38121F2A31D881E5F BBB\n" ,
345+ "Right aligned AAA 0 BBB\n" ,
346+ "Left aligned AAA 0 BBB\n" ,
347+ "hex with right align AAA 0x0 BBB\n" ,
348+ "hex with left align AAA 0x0 BBB\n"
349+ };
350+
351+ const char * print_strings [12 ] = {
352+ "Right aligned AAA %50N BBB\n" ,
353+ "Left aligned AAA %-50N BBB\n" ,
354+ "hex with right align AAA %#50N BBB\n" ,
355+ "hex with left align AAA %#-50N BBB\n" ,
356+ /* at idx == 4 mp_exch(&p,&q); */
357+ "Right aligned AAA %50N BBB\n" ,
358+ "Left aligned AAA %-50N BBB\n" ,
359+ "hex with right align AAA %#50N BBB\n" ,
360+ "hex with left align AAA %#-50N BBB\n" ,
361+ /* at idx == 8 mp_zero(&p); */
362+ "Right aligned AAA %50N BBB\n" ,
363+ "Left aligned AAA %-50N BBB\n" ,
364+ "hex with right align AAA %#50N BBB\n" ,
365+ "hex with left align AAA %#-50N BBB\n" ,
366+ };
367+
368+
369+ mp_int p , q ;
370+
371+ test_file = fopen ("ltm_testing_mp_fprintf_88a43603fcfc2f7e7c6646cd4b89180a" , "w+" );
372+ if (test_file == NULL ) {
373+ /* use logfile instead to have at least sth. in case of an error */
374+ test_file = stdout ;
375+ write_only = true;
376+ }
377+
378+ DOR (mp_init_multi (& p , & q , NULL ));
379+
380+ DO (mp_read_radix (& p , test_values [0 ], 16 ));
381+ DO (mp_read_radix (& q , test_values [1 ], 16 ));
382+
383+ DO (mp_printf_extension ());
384+
385+ for (idx = 0 ; idx < 12 ; idx ++ ) {
386+ if (idx == 4 ) {
387+ mp_exch (& p ,& q );
388+ }
389+ if (idx == 8 ) {
390+ mp_zero (& p );
391+ }
392+ characters_printed = fprintf (test_file , print_strings [idx ], & p );
393+ slen = strlen (test_strings [idx ]);
394+ if ((characters_printed - (int )slen ) != 0 ) {
395+ fprintf (stderr , "%d test_mp_print_extension: failed to print o:%zu t:%d\n" ,
396+ idx , slen , characters_printed );
397+ goto LBL_ERR ;
398+ }
399+ if (!write_only ) {
400+ rewind (test_file );
401+ fgets_return = fgets (line_buffer , LTM_TEST_BUFSIZ , test_file );
402+ if (fgets_return == NULL ) {
403+ fprintf (stderr , "%d test_mp_fprintf: failed to read from file\n" , idx );
404+ goto LBL_ERR ;
405+ }
406+ if (strcmp (line_buffer , test_strings [idx ]) != 0 ) {
407+ fprintf (stderr , "test_mp_fprintf: file content is not equal to test string #%d\n" ,idx );
408+ goto LBL_ERR ;
409+ }
410+ }
411+ /* Clear file content */
412+ test_file = freopen ("ltm_testing_mp_fprintf_88a43603fcfc2f7e7c6646cd4b89180a" ,"w+" , test_file );
413+ if (test_file == NULL ) {
414+ /* use logfile instead to have at least sth. in case of an error */
415+ test_file = stdout ;
416+ write_only = true;
417+ }
418+ }
419+
420+ mp_clear_multi (& p , & q , NULL );
421+ fclose (test_file );
422+ if (remove ("ltm_testing_mp_fprintf_88a43603fcfc2f7e7c6646cd4b89180a" ) != 0 ) {
423+ fprintf (stderr , "Could not delete file ltm_testing_mp_fprintf_88a43603fcfc2f7e7c6646cd4b89180a\n" );
424+ }
425+ return EXIT_SUCCESS ;
426+ LBL_ERR :
427+ mp_clear_multi (& p , & q , NULL );
428+ fclose (test_file );
429+ /* We don't delete the testfile in case of error, conrtent might be helpful. */
430+ return EXIT_FAILURE ;
431+ }
432+ #endif
433+
318434static mp_err very_random_source (void * out , size_t size )
319435{
320436 memset (out , 0xff , size );
@@ -1576,6 +1692,9 @@ static int test_mp_log(void)
15761692 DO (mp_rand (& a , i ));
15771693 for (j = 1 ; j < ((i /2 )+ 1 ); j ++ ) {
15781694 DO (mp_rand (& base , j ));
1695+ if (mp_cmp_d (& base ,2u ) == MP_LT ) {
1696+ continue ;
1697+ }
15791698 DO (mp_log (& a , & base , & lb ));
15801699 DO (mp_expt_n (& base , lb , & bn ));
15811700 /* "bn" must be smaller than or equal to "a" at this point. */
@@ -2453,6 +2572,9 @@ static int unit_tests(int argc, char **argv)
24532572 T1 (mp_dr_reduce , MP_DR_REDUCE ),
24542573 T2 (mp_pack_unpack ,MP_PACK , MP_UNPACK ),
24552574 T2 (mp_fread_fwrite , MP_FREAD , MP_FWRITE ),
2575+ #if (!(defined LTM_NOTHING ) && !(defined MP_NO_FILE ) && (defined __GLIBC__ ))
2576+ T1 (mp_printf_extension , MP_READ_RADIX ),
2577+ #endif
24562578 T1 (mp_get_u32 , MP_GET_I32 ),
24572579 T1 (mp_get_u64 , MP_GET_I64 ),
24582580 T1 (mp_get_ul , MP_GET_L ),
0 commit comments