Back to index

glibc  2.9
tst-fopenloc2.c
Go to the documentation of this file.
00001 #include <errno.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <unistd.h>
00005 #include <wchar.h>
00006 
00007 
00008 static const struct
00009 {
00010   const char *enc;
00011   const char *data;
00012   size_t datalen;
00013   const wchar_t *expected;
00014   size_t expectedlen;
00015 } tests[] =
00016   {
00017     { "UCS-4LE", "a\0\0\0b\0\0\0", 8, L"ab", 2 },
00018     { "UCS-4BE", "\0\0\0a\0\0\0b", 8, L"ab", 2 },
00019   };
00020 #define ntests (sizeof (tests) / sizeof (tests[0]))
00021 
00022 
00023 static int do_test (void);
00024 #define TEST_FUNCTION do_test ()
00025 
00026 static void prepare (void);
00027 #define PREPARE(argc, argv) prepare ();
00028 
00029 #include "../test-skeleton.c"
00030 
00031 
00032 static int fd;
00033 static char *tmpname;
00034 
00035 
00036 static void
00037 prepare (void)
00038 {
00039   fd = create_temp_file ("tst-fopenloc2", &tmpname);
00040   if (fd == -1)
00041     {
00042       puts ("cannot open temp file");
00043       exit (1);
00044     }
00045 }
00046 
00047 
00048 static int
00049 do_test (void)
00050 {
00051   for (int i = 0; i < ntests; ++i)
00052     {
00053       if (ftruncate (fd, 0) != 0)
00054        {
00055          printf ("ftruncate in round %d failed\n", i + 1);
00056          return 1;
00057        }
00058 
00059       if (TEMP_FAILURE_RETRY (write (fd, tests[i].data, tests[i].datalen))
00060          != tests[i].datalen)
00061        {
00062          printf ("write in round %d failed\n", i + 1);
00063          return 1;
00064        }
00065 
00066       if (lseek (fd, 0, SEEK_SET) != 0)
00067        {
00068          printf ("lseek in round %d failed\n", i + 1);
00069          return 1;
00070        }
00071 
00072       char *ccs;
00073       if (asprintf (&ccs, "r,ccs=%s", tests[i].enc) == -1)
00074        {
00075          printf ("asprintf in round %d failed\n", i + 1);
00076          return 1;
00077        }
00078 
00079       FILE *fp = fopen (tmpname, ccs);
00080       if (fp == NULL)
00081        {
00082          printf ("fopen in round %d failed\n", i + 1);
00083          return 1;
00084        }
00085 
00086 #define LINELEN 100
00087       wchar_t line[LINELEN];
00088       if (fgetws (line, LINELEN, fp) != line)
00089        {
00090          printf ("fgetws in round %d failed\n", i + 1);
00091          return 1;
00092        }
00093 
00094       if (wcslen (line) != tests[i].expectedlen)
00095        {
00096          printf ("round %d: expected length %zu, got length %zu\n",
00097                 i + 1, tests[i].expectedlen, wcslen (line));
00098          return 1;
00099        }
00100 
00101       if (wcscmp (tests[i].expected, line) != 0)
00102        {
00103          printf ("round %d: expected L\"%ls\", got L\"%ls\"\n",
00104                 i + 1, tests[i].expected, line);
00105          return 1;
00106        }
00107 
00108       fclose (fp);
00109 
00110       free (ccs);
00111     }
00112 
00113   close (fd);
00114 
00115   return 0;
00116 }