~ubuntu-branches/ubuntu/trusty/libprelude/trusty

« back to all changes in this revision

Viewing changes to libmissing/tests/test-getdelim.c

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2009-04-29 11:31:50 UTC
  • mfrom: (1.1.12 upstream) (2.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090429113150-qw9oxc1e50ldljck
Tags: 0.9.22-2ubuntu1
* Merge from debian unstable, remaining changes:
  - Build-Depend on libltdl7-dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Test of getdelim() function.
 
2
   Copyright (C) 2007-2008 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
          fflush (stderr);                                                   \
 
33
          abort ();                                                          \
 
34
        }                                                                    \
 
35
    }                                                                        \
 
36
  while (0)
 
37
 
 
38
int
 
39
main (int argc, char **argv)
 
40
{
 
41
  FILE *f;
 
42
  char *line = NULL;
 
43
  size_t len = 0;
 
44
  ssize_t result;
 
45
 
 
46
  /* Create test file.  */
 
47
  f = fopen ("test-getdelim.txt", "wb");
 
48
  if (!f || fwrite ("anbcnd\0f", 1, 8, f) != 8 || fclose (f) != 0)
 
49
    {
 
50
      fputs ("Failed to create sample file.\n", stderr);
 
51
      remove ("test-getdelim.txt");
 
52
      return 1;
 
53
    }
 
54
  f = fopen ("test-getdelim.txt", "rb");
 
55
  if (!f)
 
56
    {
 
57
      fputs ("Failed to reopen sample file.\n", stderr);
 
58
      remove ("test-getdelim.txt");
 
59
      return 1;
 
60
    }
 
61
 
 
62
  /* Test initial allocation, which must include trailing NUL.  */
 
63
  result = getdelim (&line, &len, 'n', f);
 
64
  ASSERT (result == 2);
 
65
  ASSERT (strcmp (line, "an") == 0);
 
66
  ASSERT (2 < len);
 
67
 
 
68
  /* Test growth of buffer.  */
 
69
  free (line);
 
70
  line = malloc (1);
 
71
  len = 1;
 
72
  result = getdelim (&line, &len, 'n', f);
 
73
  ASSERT (result == 3);
 
74
  ASSERT (strcmp (line, "bcn") == 0);
 
75
  ASSERT (3 < len);
 
76
 
 
77
  /* Test embedded NULs and EOF behavior.  */
 
78
  result = getdelim (&line, &len, 'n', f);
 
79
  ASSERT (result == 3);
 
80
  ASSERT (memcmp (line, "d\0f", 4) == 0);
 
81
  ASSERT (3 < len);
 
82
 
 
83
  result = getdelim (&line, &len, 'n', f);
 
84
  ASSERT (result == -1);
 
85
 
 
86
  free (line);
 
87
  fclose (f);
 
88
  remove ("test-getdelim.txt");
 
89
  return 0;
 
90
}