Blame libio/tst_swprintf.c

Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <wchar.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
static wchar_t buf[100];
Packit 6c4009
#define nbuf (sizeof (buf) / sizeof (buf[0]))
Packit 6c4009
static const struct
Packit 6c4009
{
Packit 6c4009
  size_t n;
Packit 6c4009
  const char *str;
Packit 6c4009
  ssize_t exp;
Packit 6c4009
} tests[] =
Packit 6c4009
  {
Packit 6c4009
    { nbuf, "hello world", 11 },
Packit 6c4009
    { 0, "hello world", -1 },
Packit 6c4009
    { 0, "", -1 },
Packit 6c4009
    { nbuf, "", 0 }
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  size_t n;
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  puts ("test 1");
Packit 6c4009
  n = swprintf (buf, nbuf, L"Hello %s", "world");
Packit 6c4009
  if (n != 11)
Packit 6c4009
    {
Packit 6c4009
      printf ("incorrect return value: %zd instead of 11\n", n);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else if (wcscmp (buf, L"Hello world") != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("incorrect string: L\"%ls\" instead of L\"Hello world\"\n", buf);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  puts ("test 2");
Packit 6c4009
  n = swprintf (buf, nbuf, L"Is this >%g< 3.1?", 3.1);
Packit 6c4009
  if (n != 18)
Packit 6c4009
    {
Packit 6c4009
      printf ("incorrect return value: %zd instead of 18\n", n);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else if (wcscmp (buf, L"Is this >3.1< 3.1?") != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("incorrect string: L\"%ls\" instead of L\"Is this >3.1< 3.1?\"\n",
Packit 6c4009
	      buf);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (n = 0; n < sizeof (tests) / sizeof (tests[0]); ++n)
Packit 6c4009
    {
Packit 6c4009
      ssize_t res = swprintf (buf, tests[n].n, L"%s", tests[n].str);
Packit 6c4009
Packit 6c4009
      if (tests[n].exp < 0 && res >= 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("swprintf (buf, %Zu, L\"%%s\", \"%s\") expected to fail\n",
Packit 6c4009
		  tests[n].n, tests[n].str);
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
      else if (tests[n].exp >= 0 && tests[n].exp != res)
Packit 6c4009
	{
Packit 6c4009
	  printf ("swprintf (buf, %Zu, L\"%%s\", \"%s\") expected to return %Zd, but got %Zd\n",
Packit 6c4009
		  tests[n].n, tests[n].str, tests[n].exp, res);
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	printf ("swprintf (buf, %Zu, L\"%%s\", \"%s\") OK\n",
Packit 6c4009
		tests[n].n, tests[n].str);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (swprintf (buf, nbuf, L"%.0s", "foo") != 0
Packit 6c4009
      || wcslen (buf) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("swprintf (buf, %Zu, L\"%%.0s\", \"foo\") create some output\n",
Packit 6c4009
	      nbuf);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (swprintf (buf, nbuf, L"%.0ls", L"foo") != 0
Packit 6c4009
      || wcslen (buf) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("swprintf (buf, %Zu, L\"%%.0ls\", L\"foo\") create some output\n",
Packit 6c4009
	      nbuf);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}