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