• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500v2-V1.0.0.60_1.0.38/ap/gpl/timemachine/gettext-0.17/gettext-tools/gnulib-tests/
1/* Test of getdelim() function.
2   Copyright (C) 2007 Free Software Foundation, Inc.
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 3, or (at your option)
7   any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software Foundation,
16   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18/* Written by Eric Blake <ebb9@byu.net>, 2007.  */
19
20#include <config.h>
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#define ASSERT(expr) \
27  do									     \
28    {									     \
29      if (!(expr))							     \
30        {								     \
31          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
32          abort ();							     \
33        }								     \
34    }									     \
35  while (0)
36
37int
38main (int argc, char **argv)
39{
40  FILE *f;
41  char *line = NULL;
42  size_t len = 0;
43  ssize_t result;
44
45  /* Create test file.  */
46  f = fopen ("test-getdelim.txt", "wb");
47  if (!f || fwrite ("anbcnd\0f", 1, 8, f) != 8 || fclose (f) != 0)
48    {
49      fputs ("Failed to create sample file.\n", stderr);
50      remove ("test-getdelim.txt");
51      return 1;
52    }
53  f = fopen ("test-getdelim.txt", "rb");
54  if (!f)
55    {
56      fputs ("Failed to reopen sample file.\n", stderr);
57      remove ("test-getdelim.txt");
58      return 1;
59    }
60
61  /* Test initial allocation, which must include trailing NUL.  */
62  result = getdelim (&line, &len, 'n', f);
63  ASSERT (result == 2);
64  ASSERT (strcmp (line, "an") == 0);
65  ASSERT (2 < len);
66
67  /* Test growth of buffer.  */
68  free (line);
69  line = malloc (1);
70  len = 1;
71  result = getdelim (&line, &len, 'n', f);
72  ASSERT (result == 3);
73  ASSERT (strcmp (line, "bcn") == 0);
74  ASSERT (3 < len);
75
76  /* Test embedded NULs and EOF behavior.  */
77  result = getdelim (&line, &len, 'n', f);
78  ASSERT (result == 3);
79  ASSERT (memcmp (line, "d\0f", 4) == 0);
80  ASSERT (3 < len);
81
82  result = getdelim (&line, &len, 'n', f);
83  ASSERT (result == -1);
84
85  free (line);
86  fclose (f);
87  remove ("test-getdelim.txt");
88  return 0;
89}
90