test-getline.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00047 f = fopen ("test-getline.txt", "wb");
00048 if (!f || fwrite ("a\nbc\nd\0f", 1, 8, f) != 8 || fclose (f) != 0)
00049 {
00050 fputs ("Failed to create sample file.\n", stderr);
00051 remove ("test-getline.txt");
00052 return 1;
00053 }
00054 f = fopen ("test-getline.txt", "rb");
00055 if (!f)
00056 {
00057 fputs ("Failed to reopen sample file.\n", stderr);
00058 remove ("test-getline.txt");
00059 return 1;
00060 }
00061
00062
00063 result = getline (&line, &len, f);
00064 ASSERT (result == 2);
00065 ASSERT (strcmp (line, "a\n") == 0);
00066 ASSERT (2 < len);
00067
00068
00069 free (line);
00070 line = malloc (1);
00071 len = 0;
00072 result = getline (&line, &len, f);
00073 ASSERT (result == 3);
00074 ASSERT (strcmp (line, "bc\n") == 0);
00075 ASSERT (3 < len);
00076
00077
00078 result = getline (&line, &len, f);
00079 ASSERT (result == 3);
00080 ASSERT (memcmp (line, "d\0f", 4) == 0);
00081 ASSERT (3 < len);
00082
00083 result = getline (&line, &len, f);
00084 ASSERT (result == -1);
00085
00086 free (line);
00087 fclose (f);
00088 remove ("test-getline.txt");
00089 return 0;
00090 }