Debian Codegraph @

terps/scare/sxmain.c

raw symbols SCIP
1 /* vi: set ts=2 shiftwidth=2 expandtab:
2 *
3 * Copyright (C) 2003-2008 Simon Baldwin and Mark J. Tilford
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
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
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 * USA
18 */
19
20 /*
21 * Module notes:
22 *
23 * o In order to validate all its arguments, this module creates and retains
24 * a copy of every valid game encountered, only freeing them all at the end.
25 * This could make it very memory-hungry when running a large number of
26 * games and scripts. The alternative is to only create games as needed,
27 * and free them once used, but it's nice to fully validate all command line
28 * arguments first, and work afterwards, so for now that's what's done.
29 *
30 * o ...Alternatively, we could validate by creating the game, destroy it,
31 * and then re-parse it later when running the test script. Unfortunately,
32 * parsing an Adrift game can be lengthy (~seconds), so paying this price
33 * twice isn't too attractive either.
34 *
35 * o For now, then, if running lots of test, run in batches of ten or so.
36 */
37
38 #include <assert.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #include "scare.h"
45 #include "sxprotos.h"
46
47
48 /*
49 * main()
50 *
51 * Validate the command line, and each argument as a game to be run.
52 * Execute scripts for each, and return with an error code if any test fails.
53 */
54 int
55 main (int argc, const char *argv[])
56 {
57 const sc_char *const program = argv[0];
58 sc_bool is_verbose = FALSE, is_tracing = FALSE;
59 const sc_char *trace_flags;
60 sx_test_descriptor_t *tests;
61 sc_int count, index_, errors;
62 assert (argc > 0 && argv);
63
64 /* Get options and validate the command line. */
65 if (argc > 1
66 && (strcmp (argv[1], "-v") == 0 || strcmp (argv[1], "-vv") == 0))
67 {
68 is_verbose = TRUE;
69 is_tracing = (strcmp (argv[1], "-vv") == 0);
70 argc--;
71 argv++;
72 }
73
74 if (is_verbose)
75 {
76 sx_trace ("--- %s Test Suite [Adrift %ld compatible]\n",
77 sc_scare_version (), sc_scare_emulation ());
78 if (argc < 2)
79 return EXIT_SUCCESS;
80 }
81 else if (argc < 2)
82 {
83 fprintf (stderr, "Usage: %s [-v | -vv] test [test...]\n", program);
84 return EXIT_FAILURE;
85 }
86
87 /* Ensure that the interpreter is in the Latin1 locale, and stays there. */
88 if (!sc_set_locale ("Latin1"))
89 {
90 fprintf (stderr, "%s: failed to set locale\n", program);
91 return EXIT_FAILURE;
92 }
93
94 /*
95 * Force test reproducibility. Because game construction may use random
96 * numbers, we also need to remember to reseed this before constructing
97 * each game, and then again before running each.
98 */
99 sc_set_portable_random (TRUE);
100
101 /* Set verbosity and tracing for other modules. */
102 scr_set_verbose (is_verbose);
103 stub_debug_trace (is_tracing);
104 trace_flags = getenv ("SC_TRACE_FLAGS");
105 if (trace_flags)
106 sc_set_trace_flags (strtoul (trace_flags, NULL, 0));
107
108 /* Create an array of test descriptors large enough for all tests. */
109 tests = sx_malloc ((argc - 1) * sizeof (*tests));
110
111 /* Validate each test argument by opening a game and script for it. */
112 count = 0;
113 for (index_ = 1; index_ < argc; index_++)
114 {
115 const sc_char *name;
116 FILE *stream;
117 sx_script script;
118 sc_game game;
119
120 name = argv[index_];
121
122 script = sx_fopen (name, "scr", "r");
123 if (!script)
124 {
125 fprintf (stderr, "%s: %s.scr: %s\n", program, name, strerror (errno));
126 continue;
127 }
128
129 stream = sx_fopen (name, "taf", "rb");
130 if (!stream)
131 {
132 fprintf (stderr, "%s: %s.taf: %s\n", program, name, strerror (errno));
133 fclose (script);
134 continue;
135 }
136
137 sc_reseed_random_sequence (1);
138 game = sc_game_from_stream (stream);
139 fclose (stream);
140 if (!game)
141 {
142 fprintf (stderr,
143 "%s: %s.taf: Unable to decode Adrift game\n", program, name);
144 fclose (script);
145 continue;
146 }
147
148 tests[count].name = name;
149 tests[count].script = script;
150 tests[count].game = game;
151 count++;
152 }
153
154 /* Run the available tests and report results. */
155 if (count > 0)
156 errors = test_run_game_tests (tests, count, is_verbose);
157 else
158 errors = 1;
159
160 /* Clean up allocations and opened files. */
161 for (index_ = 0; index_ < count; index_++)
162 {
163 fclose (tests[index_].script);
164 sc_free_game (tests[index_].game);
165 }
166 sx_free (tests);
167
168 /* Report results overall. */
169 printf ("%s [%ld test%s, %ld error%s]\n",
170 errors > 0 ? "FAIL" : "PASS",
171 count, count == 1 ? "" : "s", errors, errors == 1 ? "" : "s");
172
173 return errors > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
174 }