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