Back to index

glibc  2.9
tst-printfsz.c
Go to the documentation of this file.
00001 /* Based on code by Larry McVoy <lm@neteng.engr.sgi.com>.  */
00002 #include <printf.h>
00003 #include <stdio.h>
00004 #include <string.h>
00005 
00006 #define V       12345678.12345678
00007 
00008 
00009 int
00010 main (int argc, char *argv[])
00011 {
00012   char buf[1024];
00013   int result = 0;
00014 
00015   /* Register the printf handlers.  */
00016   register_printf_function ('b', printf_size, printf_size_info);
00017   register_printf_function ('B', printf_size, printf_size_info);
00018 
00019 
00020   sprintf (buf, "%g %b %B %.0b %.0B %.1b %.1B %8.0b %08.0B",
00021           V, 1025., V, V, V, V, V, V, V, V);
00022   fputs (buf, stdout);
00023   if (strcmp (buf, "\
00024 1.23457e+07 1.001k 12.346M 12m 12M 11.8m 12.3M      12m 0000012M"))
00025     {
00026       result = 1;
00027       fputs (" -> WRONG\n", stdout);
00028     }
00029   else
00030     fputs (" -> OK\n", stdout);
00031 
00032   sprintf (buf, "%b|%B|%-20.2b|%-10.0b|%-10.8b|%-10.2B|",
00033           V, V, V, V, V, V, V, V, V, V, V);
00034   fputs (buf, stdout);
00035   if (strcmp (buf, "\
00036 11.774m|12.346M|11.77m              |12m       |11.77375614m|12.35M    |"))
00037     {
00038       result = 1;
00039       fputs (" -> WRONG\n", stdout);
00040     }
00041   else
00042     fputs (" -> OK\n", stdout);
00043 
00044   sprintf (buf, "%#.0B %*.0b %10.*b %*.*B %10.2B",
00045           V, 2, V, 2, V, 10, 2, V, V);
00046   fputs (buf, stdout);
00047   if (strcmp (buf, "12.M 12m     11.77m     12.35M     12.35M"))
00048     {
00049       result = 1;
00050       fputs (" -> WRONG\n", stdout);
00051     }
00052   else
00053     fputs (" -> OK\n", stdout);
00054 
00055   sprintf (buf, "%6B %6.1B %b %B %b %B",
00056           V, V, 1000.0, 1000.0, 1024.0, 1024.0);
00057   fputs (buf, stdout);
00058   if (strcmp (buf, "12.346M  12.3M 1000.000  1.000K 1.000k 1.024K"))
00059     {
00060       result = 1;
00061       fputs (" -> WRONG\n", stdout);
00062     }
00063   else
00064     fputs (" -> OK\n", stdout);
00065 
00066   return result;
00067 }