|
Packit Service |
4684c1 |
/* Test of getdelim() function.
|
|
Packit Service |
4684c1 |
Copyright (C) 2007-2020 Free Software Foundation, Inc.
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
This program is free software; you can redistribute it and/or modify
|
|
Packit Service |
4684c1 |
it under the terms of the GNU General Public License as published by
|
|
Packit Service |
4684c1 |
the Free Software Foundation; either version 3, or (at your option)
|
|
Packit Service |
4684c1 |
any later version.
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
This program is distributed in the hope that it will be useful,
|
|
Packit Service |
4684c1 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
4684c1 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
4684c1 |
GNU General Public License for more details.
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
You should have received a copy of the GNU General Public License
|
|
Packit Service |
4684c1 |
along with this program; if not, see <https://www.gnu.org/licenses/>. */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Written by Eric Blake <ebb9@byu.net>, 2007. */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include <config.h>
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include <stdio.h>
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include "signature.h"
|
|
Packit Service |
4684c1 |
SIGNATURE_CHECK (getdelim, ssize_t, (char **, size_t *, int, FILE *));
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include <stdlib.h>
|
|
Packit Service |
4684c1 |
#include <string.h>
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include "macros.h"
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
main (void)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
FILE *f;
|
|
Packit Service |
4684c1 |
char *line;
|
|
Packit Service |
4684c1 |
size_t len;
|
|
Packit Service |
4684c1 |
ssize_t result;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Create test file. */
|
|
Packit Service |
4684c1 |
f = fopen ("test-getdelim.txt", "wb");
|
|
Packit Service |
4684c1 |
if (!f || fwrite ("anAnbcnd\0f", 1, 10, f) != 10 || fclose (f) != 0)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
fputs ("Failed to create sample file.\n", stderr);
|
|
Packit Service |
4684c1 |
remove ("test-getdelim.txt");
|
|
Packit Service |
4684c1 |
return 1;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
f = fopen ("test-getdelim.txt", "rb");
|
|
Packit Service |
4684c1 |
if (!f)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
fputs ("Failed to reopen sample file.\n", stderr);
|
|
Packit Service |
4684c1 |
remove ("test-getdelim.txt");
|
|
Packit Service |
4684c1 |
return 1;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Test initial allocation, which must include trailing NUL. */
|
|
Packit Service |
4684c1 |
line = NULL;
|
|
Packit Service |
4684c1 |
len = 0;
|
|
Packit Service |
4684c1 |
result = getdelim (&line, &len, 'n', f);
|
|
Packit Service |
4684c1 |
ASSERT (result == 2);
|
|
Packit Service |
4684c1 |
ASSERT (strcmp (line, "an") == 0);
|
|
Packit Service |
4684c1 |
ASSERT (2 < len);
|
|
Packit Service |
4684c1 |
free (line);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Test initial allocation again, with line = NULL and len != 0. */
|
|
Packit Service |
4684c1 |
line = NULL;
|
|
Packit Service |
4684c1 |
len = (size_t)(~0) / 4;
|
|
Packit Service |
4684c1 |
result = getdelim (&line, &len, 'n', f);
|
|
Packit Service |
4684c1 |
ASSERT (result == 2);
|
|
Packit Service |
4684c1 |
ASSERT (strcmp (line, "An") == 0);
|
|
Packit Service |
4684c1 |
ASSERT (2 < len);
|
|
Packit Service |
4684c1 |
free (line);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Test growth of buffer. */
|
|
Packit Service |
4684c1 |
line = malloc (1);
|
|
Packit Service |
4684c1 |
len = 1;
|
|
Packit Service |
4684c1 |
result = getdelim (&line, &len, 'n', f);
|
|
Packit Service |
4684c1 |
ASSERT (result == 3);
|
|
Packit Service |
4684c1 |
ASSERT (strcmp (line, "bcn") == 0);
|
|
Packit Service |
4684c1 |
ASSERT (3 < len);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Test embedded NULs and EOF behavior. */
|
|
Packit Service |
4684c1 |
result = getdelim (&line, &len, 'n', f);
|
|
Packit Service |
4684c1 |
ASSERT (result == 3);
|
|
Packit Service |
4684c1 |
ASSERT (memcmp (line, "d\0f", 4) == 0);
|
|
Packit Service |
4684c1 |
ASSERT (3 < len);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
result = getdelim (&line, &len, 'n', f);
|
|
Packit Service |
4684c1 |
ASSERT (result == -1);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
free (line);
|
|
Packit Service |
4684c1 |
fclose (f);
|
|
Packit Service |
4684c1 |
remove ("test-getdelim.txt");
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|