test-getdelim.c

Go to the documentation of this file.
00001 /* Test of getdelim() function.
00002    Copyright (C) 2007-2008 Free Software Foundation, Inc.
00003 
00004    This program is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 3, or (at your option)
00007    any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program; if not, write to the Free Software Foundation,
00016    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
00017 
00018 /* Written by Eric Blake <ebb9@byu.net>, 2007.  */
00019 
00020 #include <config.h>
00021 
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 
00026 #define ASSERT(expr) \
00027   do                                                                         \
00028     {                                                                        \
00029       if (!(expr))                                                           \
00030         {                                                                    \
00031           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
00032           fflush (stderr);                                                   \
00033           abort ();                                                          \
00034         }                                                                    \
00035     }                                                                        \
00036   while (0)
00037 
00038 int
00039 main (int argc, char **argv)
00040 {
00041   FILE *f;
00042   char *line = NULL;
00043   size_t len = 0;
00044   ssize_t result;
00045 
00046   /* Create test file.  */
00047   f = fopen ("test-getdelim.txt", "wb");
00048   if (!f || fwrite ("anbcnd\0f", 1, 8, f) != 8 || fclose (f) != 0)
00049     {
00050       fputs ("Failed to create sample file.\n", stderr);
00051       remove ("test-getdelim.txt");
00052       return 1;
00053     }
00054   f = fopen ("test-getdelim.txt", "rb");
00055   if (!f)
00056     {
00057       fputs ("Failed to reopen sample file.\n", stderr);
00058       remove ("test-getdelim.txt");
00059       return 1;
00060     }
00061 
00062   /* Test initial allocation, which must include trailing NUL.  */
00063   result = getdelim (&line, &len, 'n', f);
00064   ASSERT (result == 2);
00065   ASSERT (strcmp (line, "an") == 0);
00066   ASSERT (2 < len);
00067 
00068   /* Test growth of buffer.  */
00069   free (line);
00070   line = malloc (1);
00071   len = 1;
00072   result = getdelim (&line, &len, 'n', f);
00073   ASSERT (result == 3);
00074   ASSERT (strcmp (line, "bcn") == 0);
00075   ASSERT (3 < len);
00076 
00077   /* Test embedded NULs and EOF behavior.  */
00078   result = getdelim (&line, &len, 'n', f);
00079   ASSERT (result == 3);
00080   ASSERT (memcmp (line, "d\0f", 4) == 0);
00081   ASSERT (3 < len);
00082 
00083   result = getdelim (&line, &len, 'n', f);
00084   ASSERT (result == -1);
00085 
00086   free (line);
00087   fclose (f);
00088   remove ("test-getdelim.txt");
00089   return 0;
00090 }

Generated on Tue Oct 21 18:28:19 2008 for gsasl by  doxygen 1.5.6