-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_maxbf.c
257 lines (207 loc) · 7.13 KB
/
test_maxbf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* Test MaxBF. Some test cases taken from http://brainfuck.org/, written by
* Daniel B Cristofani (cristofdathevanetdotcom) and shared under the Creative
* Commons Attribution-ShareAlike 4.0 International License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "minunit.h"
/*** Mock functions ***/
#define TEST_BUF_SIZE 1000
char mock_input_buf[TEST_BUF_SIZE] = { 0 };
char mock_output_buf[TEST_BUF_SIZE] = { 0 };
int (*old_fgetc)(FILE *stream) = fgetc;
static int mock_fgetc(FILE *stream)
{
// If we are reading from stdin, we should use the mock input stream.
// Otherwise, we should use the actual stream. This is because fgetc is used
// to read characters in from the program file as well as reading in
// characters from the program input stream.
if (stream == stdin) {
int result = (int)mock_input_buf[0];
// Shift input one to the left.
memmove(mock_input_buf, &mock_input_buf[1], TEST_BUF_SIZE - 1);
if (result == 0) return EOF;
return result;
} else {
return old_fgetc(stream);
}
}
#undef fgetc
#define fgetc(stream) mock_fgetc(stream)
static int mock_fputc(int c, FILE *stream)
{
size_t len = strlen(mock_output_buf);
mock_output_buf[len] = (char)c;
mock_output_buf[len + 1] = '\0';
return c;
}
#undef fputc
#define fputc(c, stream) mock_fputc(c, stream)
/*** File to test. ***/
#include "maxbf.c"
int tests_run = 0; // Number of tests run.
int tmpnam_calls = 0; // Track calls to tmpnam to compare against TMP_MAX.
/*** Setup functions ***/
FILE *create_file_from_string(const char *s)
{
char file_name[L_tmpnam];
if (tmpnam_calls == TMP_MAX || tmpnam(file_name) == NULL) {
goto error;
}
tmpnam_calls++;
FILE *fp = fopen(file_name, "w+");
if (fp == NULL) {
goto error;
}
if (fputs(s, fp) == EOF) {
fclose(fp);
goto error;
}
fseek(fp, 0L, SEEK_SET);
return fp;
error:
puts("Could not generate temporary file for tests.");
exit(EXIT_FAILURE);
}
void buf_cleanup(void)
{
// These buffers are defined along with the mock functions in maxbf.c.
mock_input_buf[0] = '\0';
mock_output_buf[0] = '\0';
}
/*** Utilities ***/
/**
* Check the interpreter's output given certain inputs.
* @param program The Brainfuck program to run as a string.
* @param input The input string fed into the program. Can be NULL.
* @param debug_enabled Whether the # command should be enabled.
* @param expected_output The string to compare the output of the program to.
* Can be NULL.
* @param expected_status The expected ExecutionStatus value returned by the
* interpreter.
* @return True only if the expected output and status match the values returned
* by the interpreter.
*/
bool test_interpreter(const char *program, const char *input, bool debug_enabled,
const char *expected_output, ExecutionStatus expected_status)
{
FILE *fp = create_file_from_string(program);
struct interpreter_config config = {.input_file=NULL, .output_file=NULL,
.debug_enabled=debug_enabled};
if (input != NULL) {
strcpy(mock_input_buf, input);
}
ExecutionStatus status = execute_brainfuck_from_stream(fp, stdin, stdout, &config);
bool result;
if (expected_output == NULL) {
result = status == expected_status;
} else {
result = strcmp(mock_output_buf, expected_output) == 0
&& status == expected_status;
}
// Cleanup.
fclose(fp);
buf_cleanup();
return result;
}
/*** Test cases ***/
static char *test_hello_world()
{
bool result = test_interpreter(">++++++++[<+++++++++>-]<.>++++[<+++++++>-]<"
"+.+++++++..+++.>>++++++[<+++++++>-]<++.----"
"--------.>++++++[<+++++++++>-]<+.<.+++.----"
"--.--------.>>>++++[<++++++++>-]<+.",
NULL, false, "Hello, World!", STATUS_OK);
mu_assert("Error, Hello world failed.", result);
return 0;
}
static char *test_array_size()
{
bool result = test_interpreter("++++[>++++++<-]>[>+++++>+++++++<<-]>>++++<["
"[>[[>>+<<-]<]>>>-]>-[>+>+<<-]>]+++++[>+++++"
"++<<++>-]>.<<.", NULL, false, "#\n", STATUS_OK);
mu_assert("Error, Check for cell 30,000 failed.", result);
return 0;
}
static char *test_left_bound()
{
bool result = test_interpreter("<", NULL, false, NULL, STATUS_ERR_LBOUND);
mu_assert("Error, Moving left from cell 0 did not result in an error.", result);
return 0;
}
static char *test_improper_nesting_1()
{
bool result = test_interpreter("[[][][[]]", NULL, false, NULL, STATUS_ERR_NESTING);
mu_assert("Error, Improper nesting (extra [) did not cause an error.", result);
return 0;
}
static char *test_improper_nesting_2()
{
bool result = test_interpreter("[[]][[]", NULL, false, NULL, STATUS_ERR_NESTING);
mu_assert("Error, Improper nesting (extra ]) did not cause an error.", result);
return 0;
}
static char *test_ignore_characters()
{
bool result = test_interpreter("abcd[efg]123?", NULL, false, NULL, STATUS_OK);
mu_assert("Error, Invalid characters were not ignored.", result);
return 0;
}
static char *test_bracket_skipping()
{
bool result = test_interpreter("[This: < and this [<] shouldn't cause an er"
"ror]", NULL, false, NULL, STATUS_OK);
mu_assert("Error, Valid nesting caused an error.", result);
return 0;
}
static char *test_obscure_problems()
{
bool result = test_interpreter("[]++++++++++[>>+>+>++++++[<<+<+++>>>-]<<<<-"
"]\"A*$\";?@![#>>+<<]>[>>]<<<<[>++<[-]]>.>.",
NULL, false, "H\n", STATUS_OK);
mu_assert("Error, Check for several obscure errors failed.", result);
return 0;
}
static char *test_input()
{
bool result = test_interpreter(",.,.,,.>,.", "Y\n&?.", false, "Y\n?.", STATUS_OK);
mu_assert("Error, Input check failed.", result);
return 0;
}
static char *test_io()
{
// Input: Newline + EOF.
bool result = test_interpreter(">,>+++++++++,>+++++++++++[<++++++<++++++<+>"
">>-]<<.>.<<-.>.>.<<.", "\n", false, "LB\nLB\n",
STATUS_OK);
mu_assert("Error, I/O check failed.", result);
return 0;
}
/*** Test Execution. ***/
static char *all_tests()
{
mu_run_test(test_hello_world);
mu_run_test(test_array_size);
mu_run_test(test_left_bound);
mu_run_test(test_improper_nesting_1);
mu_run_test(test_improper_nesting_2);
mu_run_test(test_ignore_characters);
mu_run_test(test_bracket_skipping);
mu_run_test(test_obscure_problems);
mu_run_test(test_input);
mu_run_test(test_io);
return 0;
}
int main(void)
{
char *result = all_tests();
if (result != 0) {
printf("%s\n", result);
} else {
printf("ALL TESTS PASSED\n");
}
printf("Tests run: %d\n", tests_run);
return result != 0;
}