#! /bin/sh
#  -*- Mode: Sh -*- 
# ----------------------------------------------------------------------
# deciaml-test --- %d specifier
#
# Author:	      Gary V. Vaughan <gvv@techie.com>
# Maintainer:	      Gary V. Vaughan <gvv@techie.com>
# Created:	      Thu Nov 26 15:50:31 1998
# Last Modified:      Thu Sep 23 23:13:33 1999
#            by:      Gary V. Vaughan <gvv@techie.com>
# ----------------------------------------------------------------------
# @(#) $Id: decimal-test,v 1.4 2001/10/27 17:34:59 bkorb Exp $
# ----------------------------------------------------------------------
# Copyright (C) 1998, 1999 Gary V. Vaughan
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
# As a special exception to the GNU General Public License, if you
# distribute this file as part of a program that also links with and
# uses the libopts library from AutoGen, you may include it under
# the same distribution terms used by the libopts library.

# Code:

# Common definitions
if test -z "$srcdir"; then
    srcdir=`echo "$0" | sed 's,[^/]*$,,'`
    test "$srcdir" = "$0" && srcdir=.
    test -z "$srcdir" && srcdir=.
    test "${VERBOSE+set}" != set && VERBOSE=1
fi
. $srcdir/defs


# this is the output we should expect to see
cat <<\EOF >ok
1 10
*** printfv returned 4 chars.
2 -11
*** printfv returned 5 chars.
3 >20<
*** printfv returned 6 chars.
4 >-22<
*** printfv returned 7 chars.
5 333
*** printfv returned 5 chars.
6 -300
*** printfv returned 6 chars.
7         40
*** printfv returned 12 chars.
8        -44
*** printfv returned 12 chars.
9 55555
*** printfv returned 7 chars.
10 -50000
*** printfv returned 9 chars.
11 66666
*** printfv returned 8 chars.
12 -60000
*** printfv returned 9 chars.
13 0000077777
*** printfv returned 13 chars.
14 -0000077777
*** printfv returned 14 chars.
15 8888
*** printfv returned 7 chars.
16 -8000
*** printfv returned 8 chars.
17 9999
*** printfv returned 7 chars.
18 -9000
*** printfv returned 8 chars.
19 1010
*** printfv returned 7 chars.
20 -1000
*** printfv returned 8 chars.
21 1111
*** printfv returned 7 chars.
22 -01100
*** printfv returned 9 chars.
23 01212
*** printfv returned 8 chars.
24 -01200
*** printfv returned 9 chars.
25 01313
*** printfv returned 8 chars.
26 -001300
*** printfv returned 10 chars.
27 1414.
*** printfv returned 8 chars.
28 -1400.
*** printfv returned 9 chars.
29 1515.
*** printfv returned 8 chars.
30 -1500.
*** printfv returned 9 chars.
31 1616 .
*** printfv returned 9 chars.
32 -1600 .
*** printfv returned 10 chars.
33 +1717
*** printfv returned 8 chars.
34 -1700
*** printfv returned 8 chars.
35  1818
*** printfv returned 8 chars.
36 -1800
*** printfv returned 8 chars.
37  1819
*** printfv returned 8 chars.
38  -1820
*** printfv returned 9 chars.
39 +1821
*** printfv returned 8 chars.
40 +1822
*** printfv returned 8 chars.
41 1919
*** printfv returned 7 chars.
42 2147483647
*** printfv returned 13 chars.
43 -2147483648
*** printfv returned 14 chars.
44 +000001234
*** printfv returned 13 chars.
45 -00234   .
*** printfv returned 13 chars.
46 0000FEED
*** printfv returned 11 chars.
47 00C0FFEE
*** printfv returned 11 chars.
48 FFFFFFFF
*** printfv returned 11 chars.
EOF

cat <<\EOF >errok
EOF

ct=0
exec 3>&2
while read fmt val comment
do
  case "${fmt}" in
  *\%* )
    ct=`expr $ct + 1`
    fmt="`echo \"$fmt\"|sed 's,\\^, ,g'`"
    $SNPRINTFV "$ct $fmt" $val || echo $ct failed
    ;;
  esac
done 2> err > out <<'EOF'

# straight forward integer output
%d 10
%d -11
>%d< 20
>%d< -22

# test width flag
%2d 333
%1d -300
%10d 40
%10d -44

# test precision flag
%.3d 55555
%.3d -50000
%.5d 66666
%.5d -60000
%.10d 77777
%.10d -77777

# test zero padding
%03d 8888
%04d -8000
%0.3d 9999
%0.4d -9000

%04d 1010
%05d -1000
%0.4d 1111
%0.5d -1100

%05d 1212
%06d -1200
%0.5d 1313
%0.6d -1300

# test grouping flag
# TODO!

# test left justfy flag
%-3d. 1414
%-4d. -1400
%-4d. 1515
%-5d. -1500
%-5d. 1616
%-6d. -1600

# test sign flag
%+d 1717
%+d -1700

# test blank flag
%^d 1818
%^d -1800
%^5d 1819
%^6d -1820
%+^d 1821
%^+d 1822

# test long modifier  (these tests fail if sizeof(long) < 4)
%ld 1919L
%ld 2147483647L
%ld -2147483648L

# try a bizarre combination
%+09.3d 1234
%-09.5d. -234
%08lX 0xFEEDUL
%08lX 0xC0FFEEUL
%08lX -1L
EOF

# Test against expected output
if ${CMP} -s out ok; then
    :
else
    echo "ok:" >&2
    cat ok >&2
	diff ok out
    exit 1
fi

# Munge error output to remove leading directories, `lt-' or trailing `.exe'
sed -e "s,^[^:]*[lt-]*snprintfv-test[.ex]*:,snprintfv-test:," err >sederr \
    && mv sederr err

# Show stderr if doesn't match expected output if VERBOSE == 1
if "$CMP" -s err errok; then
    :
else
    echo "err:" >&2
    cat err >&2
    echo "errok:" >&2
    cat errok >&2
    exit 1
fi

# decimal-test ends here
