URI: 
       tmore tests - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
   DIR commit 4b65e43c54c2ca1394babed18cc02f4fffc22e16
   DIR parent e17e1a71c2923717723f7038d6e123b6452e0b13
  HTML Author: rsc <devnull@localhost>
       Date:   Mon, 22 May 2006 14:56:13 +0000
       
       more tests
       
       Diffstat:
         M src/lib9/mkfile                     |      21 +++++++++++++++++++++
         M src/lib9/testfltfmt.c               |      31 ++++++++++++++++++++++++-------
         M src/lib9/testfmt.c                  |      34 +++++++++++++++++++++++++++++++
         A src/lib9/testprint.c                |      14 ++++++++++++++
       
       4 files changed, 93 insertions(+), 7 deletions(-)
       ---
   DIR diff --git a/src/lib9/mkfile b/src/lib9/mkfile
       t@@ -13,7 +13,9 @@ FMTOFILES=\
                fmt.$O\
                fmtfd.$O\
                fmtfdflush.$O\
       +        fmtlocale.$O\
                fmtlock2.$O\
       +        fmtnull.$O\
                fmtprint.$O\
                fmtquote.$O\
                fmtrune.$O\
       t@@ -170,3 +172,22 @@ HFILES=\
        
        %.$O: utf/%.c
                $CC $CFLAGS utf/$stem.c
       +
       +XLIB=$PLAN9/lib/$LIB
       +
       +testfmt: testfmt.$O $XLIB
       +        $LD -o $target testfmt.$O
       +
       +testfltfmt: testfltfmt.$O $XLIB
       +        $LD -o $target testfltfmt.$O
       +
       +testprint: testprint.$O $XLIB
       +        $LD -o $target testprint.$O
       +
       +# debugging only - should go away (5/22/2006)
       +testgoogfmt: testfltfmt.$O googfmt.$O $XLIB
       +        $LD -o $target testfltfmt.$O googfmt.$O
       +
       +testgoogprint: testprint.$O googfmt.$O $XLIB
       +        $LD -o $target testprint.$O googfmt.$O
       +
   DIR diff --git a/src/lib9/testfltfmt.c b/src/lib9/testfltfmt.c
       t@@ -58,8 +58,14 @@ static int
        numclose(char *num1, char *num2)
        {
                int ndig;
       +        double d1, d2;
                enum { MAXDIG = 15 };
        
       +        d1 = fmtstrtod(num1, 0);
       +        d2 = fmtstrtod(num2, 0);
       +        if(d1 != d2)
       +                return 0;
       +
                ndig = 0;
                while (*num1) {
                        if (*num1 >= '0' && *num1 <= '9') {
       t@@ -126,24 +132,35 @@ doit(int just, int plus, int alt, int zero, int width, int prec, int spec)
                *p = '\0';
        
                for (i = 0; i < sizeof(fmtvals) / sizeof(fmtvals[0]); i++) {
       -                char ref[256], buf[256];
       -                Rune rbuf[256];
       +                char ref[1024], buf[1024];
       +                Rune rbuf[1024];
       +                double d1, d2;
        
                        sprintf(ref, format, fmtvals[i]);
                        snprint(buf, sizeof(buf), format, fmtvals[i]);
                        if (strcmp(ref, buf) != 0
                        && !numclose(ref, buf)) {
       -                        fprintf(stderr, "%s: ref='%s' fmt='%s'\n", format, ref, buf);
       -                        exit(1);
       +                        d1 = fmtstrtod(ref, 0);
       +                        d2 = fmtstrtod(buf, 0);
       +                        fprintf(stderr, "%s: ref='%s'%s fmt='%s'%s\n", 
       +                                format, 
       +                                ref, d1==fmtvals[i] ? "" : " (ref is inexact!)", 
       +                                buf, d2==fmtvals[i] ? "" : " (fmt is inexact!)");
       +                //        exits("oops");
                        }
        
                        /* Check again with output to rune string */
       -                runesnprint(rbuf, 256, format, fmtvals[i]);
       +                runesnprint(rbuf, 1024, format, fmtvals[i]);
                        snprint(buf, sizeof(buf), "%S", rbuf);
                        if (strcmp(ref, buf) != 0
                        && !numclose(ref, buf)) {
       -                        fprintf(stderr, "%s: rune ref='%s' fmt='%s'\n", format, ref, buf);
       -                        exits("oops");
       +                        d1 = fmtstrtod(ref, 0);
       +                        d2 = fmtstrtod(buf, 0);
       +                        fprintf(stderr, "%s: ref='%s'%s fmt='%s'%s\n", 
       +                                format, 
       +                                ref, d1==fmtvals[i] ? "" : " (ref is inexact!)", 
       +                                buf, d2==fmtvals[i] ? "" : " (fmt is inexact!)");
       +                //        exits("oops");
                        }
                }
        }
   DIR diff --git a/src/lib9/testfmt.c b/src/lib9/testfmt.c
       t@@ -44,13 +44,31 @@ mysmprint(char *fmt, ...)
                return fmtstrflush(&f);
        }
        
       +double near1[] = {
       +        0.5,
       +        0.95,
       +        0.995,
       +        0.9995,
       +        0.99995,
       +        0.999995,
       +        0.9999995,
       +        0.99999995,
       +        0.999999995,
       +};
        
        void
        main(int argc, char **argv)
        {
       +        int i, j;
       +
                quotefmtinstall();
                fmtinstall('Z', Zflag);
                fmtinstall(L'\x263a', Zflag);
       +#ifdef PLAN9PORT
       +{ extern int __ifmt(Fmt*);
       +        fmtinstall('i', __ifmt);
       +}
       +#endif
        
                verify(smprint("hello world"), "hello world");
        #ifdef PLAN9PORT
       t@@ -72,6 +90,21 @@ main(int argc, char **argv)
                verify(smprint("%d", 23), "23");
                verify(smprint("%i", 23), "23");
                verify(smprint("%Zi", 1234, 23), "23");
       +        
       +        /* ANSI and their wacky corner cases */
       +        verify(smprint("%.0d", 0), "");
       +        verify(smprint("%.0o", 0), "");
       +        verify(smprint("%.0x", 0), "");
       +        verify(smprint("%#.0o", 0), "0");
       +        verify(smprint("%#.0x", 0), "");
       +        
       +        /* difficult floating point tests that many libraries get wrong */
       +        verify(smprint("%.100f", 1.0), "1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
       +        verify(smprint("%.100g", 1.0), "1");
       +        verify(smprint("%0100f", 1.0), "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001.000000");
       +        for(i=1; i<9; i++)
       +                for(j=0; j<=i; j++)
       +                        verify(smprint("%.*g", j, near1[i]), "1");
        
                /* test $ reorderings */
                verify(smprint("%3$d %4$06d %2$d %1$d", 444, 333, 111, 222), "111 000222 333 444");
       t@@ -107,6 +140,7 @@ main(int argc, char **argv)
        #endif
                verify(mysmprint("%'lld %'lld %'lld", 1LL, 222222222LL, 3333333333333LL), "1 222\xe2\x98\xbb""222\xe2\x98\xbb""22\xe2\x98\xbb""2 333\xe2\x98\xbb""3333\xe2\x98\xbb""333\xe2\x98\xbb""33\xe2\x98\xbb""3");
                verify(mysmprint("%'llx %'llX %'llb", 0x111111111111LL, 0xabcd12345678LL, 112342345LL), "1111:1111:1111 ABCD:1234:5678 110:1011:0010:0011:0101:0100:1001");
       +        verify(mysmprint("%.4f", 3.14159), "3\xe2\x98\xba""1416");
        
                if(failed)
                        sysfatal("tests failed");
   DIR diff --git a/src/lib9/testprint.c b/src/lib9/testprint.c
       t@@ -0,0 +1,14 @@
       +#include <u.h>
       +#include <libc.h>
       +
       +void
       +main(int argc, char **argv)
       +{
       +        char c;
       +        
       +        c = argv[1][strlen(argv[1])-1];
       +        if(c == 'f' || c == 'e' || c == 'g' || c == 'F' || c == 'E' || c == 'G')
       +                print(argv[1], atof(argv[2]));
       +        else if(c == 'x' || c == 'u' || c == 'd' || c == 'c' || c == 'C' || c == 'X')
       +                print(argv[1], atoi(argv[2]));
       +}