This source file includes following definitions.
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21 #include <stdio.h>
22
23 #include "signature.h"
24 SIGNATURE_CHECK (getdelim, ssize_t, (char **, size_t *, int, FILE *));
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "macros.h"
30
31 int
32 main (void)
33 {
34 FILE *f;
35 char *line;
36 size_t len;
37 ssize_t result;
38
39
40 f = fopen ("test-getdelim.txt", "wb");
41 if (!f || fwrite ("anAnbcnd\0f", 1, 10, f) != 10 || fclose (f) != 0)
42 {
43 fputs ("Failed to create sample file.\n", stderr);
44 remove ("test-getdelim.txt");
45 return 1;
46 }
47 f = fopen ("test-getdelim.txt", "rb");
48 if (!f)
49 {
50 fputs ("Failed to reopen sample file.\n", stderr);
51 remove ("test-getdelim.txt");
52 return 1;
53 }
54
55
56 line = NULL;
57 len = 0;
58 result = getdelim (&line, &len, 'n', f);
59 ASSERT (result == 2);
60 ASSERT (strcmp (line, "an") == 0);
61 ASSERT (2 < len);
62 free (line);
63
64
65 line = NULL;
66 len = (size_t)(~0) / 4;
67 result = getdelim (&line, &len, 'n', f);
68 ASSERT (result == 2);
69 ASSERT (strcmp (line, "An") == 0);
70 ASSERT (2 < len);
71 free (line);
72
73
74 line = malloc (1);
75 len = 1;
76 result = getdelim (&line, &len, 'n', f);
77 ASSERT (result == 3);
78 ASSERT (strcmp (line, "bcn") == 0);
79 ASSERT (3 < len);
80
81
82 result = getdelim (&line, &len, 'n', f);
83 ASSERT (result == 3);
84 ASSERT (memcmp (line, "d\0f", 4) == 0);
85 ASSERT (3 < len);
86
87 result = getdelim (&line, &len, 'n', f);
88 ASSERT (result == -1);
89
90 free (line);
91 fclose (f);
92 remove ("test-getdelim.txt");
93 return 0;
94 }