#! /bin/sh
. "${srcdir=.}/init.sh"; path_prepend_ . ../src

# Test recognition of D format strings.

cat <<\EOF > f-d-1.data
# Valid: no argument
"abc%%"
# Valid: one any-type argument
"abc%s"
# Valid: one char argument
"abc%c"
# Valid: one bool or integer or char argument
"abc%d"
# Valid: one bool or integer or char argument
"abc%u"
# Valid: one bool or integer or char argument
"abc%b"
# Valid: one bool or integer or char argument
"abc%o"
# Valid: one bool or integer or char or pointer argument
"abc%x"
# Valid: one bool or integer or char or pointer argument
"abc%X"
# Valid: one integer or float argument
"abc%e"
# Valid: one integer or float argument
"abc%E"
# Valid: one integer or float argument
"abc%f"
# Valid: one integer or float argument
"abc%F"
# Valid: one integer or float argument
"abc%g"
# Valid: one integer or float argument
"abc%G"
# Valid: one integer or float argument
"abc%a"
# Valid: one integer or float argument
"abc%A"
# Valid: one bool or integer or float or char or array or irange argument
"abc%r"
# Valid: one array or irange argument
"abc%(%s%)"
# Valid: one associative argument
"abc%(%s%s%)"
# Valid: one argument with flags
"abc%0#g"
# Valid: one argument with width
"abc%2g"
# Valid: one argument with width
"abc%*g"
# Valid: one argument with precision
"abc%.4g"
# Valid: one argument with precision
"abc%.*g"
# Valid: one argument with width and precision
"abc%14.4g"
# Valid: one argument with width and precision
"abc%14.*g"
# Valid: one argument with width and precision
"abc%*.4g"
# Valid: one argument with width and precision
"abc%*.*g"
# Valid: one argument with separator
"abc%,3g"
# Valid: one argument with separator
"abc%,*g"
# Valid: one argument with separator
"abc%,3?g"
# Valid: one argument with separator
"abc%,*?g"
# Valid: one argument with width and separator
"abc%14,3g"
# Valid: one argument with width and separator
"abc%14,*g"
# Valid: one argument with width and separator
"abc%14,3?g"
# Valid: one argument with width and separator
"abc%14,*?g"
# Valid: one argument with width and separator
"abc%*,3g"
# Valid: one argument with width and separator
"abc%*,*g"
# Valid: one argument with width and separator
"abc%*,3?g"
# Valid: one argument with width and separator
"abc%*,*?g"
# Valid: one argument with precision and separator
"abc%.4,3g"
# Valid: one argument with precision and separator
"abc%.4,*g"
# Valid: one argument with precision and separator
"abc%.4,3?g"
# Valid: one argument with precision and separator
"abc%.4,*?g"
# Valid: one argument with precision and separator
"abc%.*,3g"
# Valid: one argument with precision and separator
"abc%.*,*g"
# Valid: one argument with precision and separator
"abc%.*,3?g"
# Valid: one argument with precision and separator
"abc%.*,*?g"
# Valid: one argument with width and precision and separator
"abc%14.4,3g"
# Valid: one argument with width and precision and separator
"abc%14.4,*g"
# Valid: one argument with width and precision and separator
"abc%14.4,3?g"
# Valid: one argument with width and precision and separator
"abc%14.4,*?g"
# Valid: one argument with width and precision and separator
"abc%14.*,3g"
# Valid: one argument with width and precision and separator
"abc%14.*,*g"
# Valid: one argument with width and precision and separator
"abc%14.*,3?g"
# Valid: one argument with width and precision and separator
"abc%14.*,*?g"
# Valid: one argument with width and precision and separator
"abc%*.4,3g"
# Valid: one argument with width and precision and separator
"abc%*.4,*g"
# Valid: one argument with width and precision and separator
"abc%*.4,3?g"
# Valid: one argument with width and precision and separator
"abc%*.4,*?g"
# Valid: one argument with width and precision and separator
"abc%*.*,3g"
# Valid: one argument with width and precision and separator
"abc%*.*,*g"
# Valid: one argument with width and precision and separator
"abc%*.*,3?g"
# Valid: one argument with width and precision and separator
"abc%*.*,*?g"
# Invalid: unterminated
"abc%"
# Invalid: unknown format specifier
"abc%y"
# Invalid: flags after width
"abc%*0g"
# Invalid: flags after precision
"abc%.*0g"
# Invalid: flags after separator
"abc%,*0g"
# Invalid: precision after separator
"abc%,4.2g"
# Valid: null precision
"abc%.f"
# Invalid: twice precision
"abc%.4.2g"
# Invalid: twice separator
"abc%,4,2g"
# Valid: three arguments
"abc%d%u%u"
# Valid: a numbered argument
"abc%1$d"
# Invalid: zero
"abc%0$d"
# Valid: two-digit numbered arguments
"abc%11$def%10$dgh%9$dij%8$dkl%7$dmn%6$dop%5$dqr%4$dst%3$duv%2$dwx%1$dyz"
# Invalid: unterminated number
"abc%1"
# Invalid: flags before number
"abc%+1$d"
# Valid: three arguments, two with same number, no conflict
"abc%1$4x,%2$c,%1$u"
# Valid: three arguments, two with same number, no conflict
"abc%1$4c,%2$c,%1$u"
# Invalid: argument with conflicting types
"abc%1$4c,%2$c,%1$e"
# Valid: mixing of numbered and unnumbered arguments
"abc%d%2$x"
# Valid: mixing of numbered and unnumbered arguments
"abc%2$d%x"
# Valid: numbered argument with constant precision
"abc%1$.9x"
# Valid: mixing of numbered and unnumbered arguments
"abc%1$.*x"
# Valid: missing non-final argument
"abc%2$x%3$s"
# Valid: permutation
"abc%2$ddef%1$d"
# Valid: multiple uses of same argument
"abc%2$xdef%1$Fghi%2$x"
# Valid: one argument with width
"abc%2$#*1$g"
# Valid: one argument with width and precision
"abc%3$*2$.*1$g"
# Valid: one argument with width and precision and separator
"abc%4$*3$.*2$,*g"
# Invalid: zero
"abc%2$*0$.*1$g"
# Valid: compound specifier for array
"abc%(def%sghi%)"
# Valid: compound specifier for associative
"abc%(def%sghi%sjkl%)"
# Invalid: compound specifier that consumes no arguments
"abc%(def%)"
# Invalid: compound specifier that consumes too many arguments
"abc%(def%sghi%sjkl%smno%)"
# Valid: nested compound specifier
"abc%(def%(%s%)ghi%)"
# Valid: nested compound specifier
"abc%(def%(%s%s%)ghi%)"
# Valid: nested compound specifier
"abc%(def%(%s%)ghi%(%u%)jkl%)"
# Valid: nested compound specifier
"abc%(def%(%s%)ghi%(%u%u%)jkl%)"
# Valid: nested compound specifier
"abc%(def%(%s%s%)ghi%(%u%)jkl%)"
# Valid: nested compound specifier
"abc%(def%(%s%s%)ghi%(%u%u%)jkl%)"
EOF

: ${XGETTEXT=xgettext}
n=0
while read comment; do
  read string
  n=`expr $n + 1`
  cat <<EOF > f-d-1-$n.in
void func() { gettext(${string}); }
EOF
  ${XGETTEXT} -L D -o f-d-1-$n.po f-d-1-$n.in || Exit 1
  test -f f-d-1-$n.po || Exit 1
  fail=
  if echo "$comment" | grep 'Valid:' > /dev/null; then
    if grep d-format f-d-1-$n.po > /dev/null; then
      :
    else
      fail=yes
    fi
  else
    if grep d-format f-d-1-$n.po > /dev/null; then
      fail=yes
    else
      :
    fi
  fi
  if test -n "$fail"; then
    echo "Format string recognition error:" 1>&2
    cat f-d-1-$n.in 1>&2
    echo "Got:" 1>&2
    cat f-d-1-$n.po 1>&2
    Exit 1
  fi
  rm -f f-d-1-$n.in f-d-1-$n.po
done < f-d-1.data

Exit 0
