1 /*
2 * read_test.c
3 *
4 * finding bugs related to reading from an empty file, reading twice
5 * at eof, parsing an empty string, etc.
6 */
7
8 #include "bt_config.h" /* for dmalloc() stuff */
9 #include <stdlib.h>
10
11 #include "testlib.h"
12 #include "my_dmalloc.h"
13
14
main(void)15 int main (void)
16 {
17 char filename[256];
18 FILE * infile;
19 AST * entry;
20 boolean entry_ok,
21 ok = TRUE;;
22
23 bt_initialize ();
24
25 /*
26 * First test -- try to read an entry from an empty file. This
27 * triggers an "unexpected eof" syntax error, and puts the file
28 * at eof -- but doesn't do the eof processing (that's for the next
29 * call).
30 */
31 infile = open_file ("empty.bib", DATA_DIR, filename, 255);
32 CHECK (!feof (infile))
33 entry = bt_parse_entry (infile, filename, 0, &entry_ok);
34 CHECK (feof (infile))
35 CHECK (entry == NULL); /* because no entry found */
36 CHECK (!entry_ok); /* and this causes a syntax error */
37
38 /* Now that we're at eof, read again -- this does the normal eof cleanup */
39 entry = bt_parse_entry (infile, filename, 0, &entry_ok);
40 CHECK (entry == NULL); /* because at eof */
41 CHECK (entry_ok); /* ditto */
42
43 /*
44 * And now do an excess read -- this used to crash the library; now it
45 * just triggers a "usage warning".
46 */
47 entry = bt_parse_entry (infile, filename, 0, &entry_ok);
48 CHECK (entry == NULL);
49 CHECK (entry_ok);
50
51 /*
52 * Try to parse an empty string; should trigger a syntax error (eof
53 * when expected an entry), so entry_ok will be false.
54 */
55 entry = bt_parse_entry_s ("", NULL, 1, 0, &entry_ok);
56 CHECK (entry == NULL);
57 CHECK (! entry_ok);
58
59 /*
60 * Try to parse a string with just junk (nothing entry-like) in it --
61 * should cause syntax error just like the empty string.
62 */
63 entry = bt_parse_entry_s ("this is junk", NULL, 1, 0, &entry_ok);
64 CHECK (entry == NULL);
65 CHECK (! entry_ok);
66
67 /* Tell bt_parse_entry_s() to cleanup after itself */
68 entry = bt_parse_entry_s (NULL, NULL, 1, 0, NULL);
69 CHECK (entry == NULL);
70
71 bt_cleanup ();
72
73 if (! ok)
74 {
75 printf ("Some tests failed\n");
76 exit (1);
77 }
78 else
79 {
80 printf ("All tests successful\n");
81 exit (0);
82 }
83
84 } /* main() */
85