Blame libio/tst-fopenloc2.c

Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <wchar.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
static const struct
Packit 6c4009
{
Packit 6c4009
  const char *enc;
Packit 6c4009
  const char *data;
Packit 6c4009
  size_t datalen;
Packit 6c4009
  const wchar_t *expected;
Packit 6c4009
  size_t expectedlen;
Packit 6c4009
} tests[] =
Packit 6c4009
  {
Packit 6c4009
    { "UCS-4LE", "a\0\0\0b\0\0\0", 8, L"ab", 2 },
Packit 6c4009
    { "UCS-4BE", "\0\0\0a\0\0\0b", 8, L"ab", 2 },
Packit 6c4009
  };
Packit 6c4009
#define ntests (sizeof (tests) / sizeof (tests[0]))
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int do_test (void);
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
Packit 6c4009
static void prepare (void);
Packit 6c4009
#define PREPARE(argc, argv) prepare ();
Packit 6c4009
Packit 6c4009
#include "../test-skeleton.c"
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int fd;
Packit 6c4009
static char *tmpname;
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
prepare (void)
Packit 6c4009
{
Packit 6c4009
  fd = create_temp_file ("tst-fopenloc2", &tmpname);
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("cannot open temp file");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  for (int i = 0; i < ntests; ++i)
Packit 6c4009
    {
Packit 6c4009
      if (ftruncate (fd, 0) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("ftruncate in round %d failed\n", i + 1);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (TEMP_FAILURE_RETRY (write (fd, tests[i].data, tests[i].datalen))
Packit 6c4009
	  != tests[i].datalen)
Packit 6c4009
	{
Packit 6c4009
	  printf ("write in round %d failed\n", i + 1);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (lseek (fd, 0, SEEK_SET) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("lseek in round %d failed\n", i + 1);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      char *ccs;
Packit 6c4009
      if (asprintf (&ccs, "r,ccs=%s", tests[i].enc) == -1)
Packit 6c4009
	{
Packit 6c4009
	  printf ("asprintf in round %d failed\n", i + 1);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      FILE *fp = fopen (tmpname, ccs);
Packit 6c4009
      if (fp == NULL)
Packit 6c4009
	{
Packit 6c4009
	  printf ("fopen in round %d failed\n", i + 1);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
#define LINELEN 100
Packit 6c4009
      wchar_t line[LINELEN];
Packit 6c4009
      if (fgetws (line, LINELEN, fp) != line)
Packit 6c4009
	{
Packit 6c4009
	  printf ("fgetws in round %d failed\n", i + 1);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (wcslen (line) != tests[i].expectedlen)
Packit 6c4009
	{
Packit 6c4009
	  printf ("round %d: expected length %zu, got length %zu\n",
Packit 6c4009
		  i + 1, tests[i].expectedlen, wcslen (line));
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (wcscmp (tests[i].expected, line) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("round %d: expected L\"%ls\", got L\"%ls\"\n",
Packit 6c4009
		  i + 1, tests[i].expected, line);
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      fclose (fp);
Packit 6c4009
Packit 6c4009
      free (ccs);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  close (fd);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}