diff options
-rw-r--r-- | parser.c | 9 | ||||
-rw-r--r-- | simulator.c | 1 | ||||
-rw-r--r-- | test/Makefile | 2 | ||||
-rwxr-xr-x | test/test-duplicate-input | bin | 0 -> 18808 bytes | |||
-rw-r--r-- | test/test-duplicate-input.c | 13 | ||||
-rwxr-xr-x | test/test-short-keyword | bin | 0 -> 19008 bytes | |||
-rw-r--r-- | test/test-short-keyword.c | 52 |
7 files changed, 74 insertions, 3 deletions
@@ -146,9 +146,12 @@ int parse_input(char *str) { strtok(str, " "); str = eat_whitespace(str); + if (strlen(str) == 0) { + emit_error("input label must not be empty"); + return 1; + } emit_info("Add input '%s'\n", str); - gate_input_add(str); - return 0; + return gate_input_add(str); } int @@ -164,6 +167,8 @@ parse_line(char *line) { char *delim = " "; int match = 0; + line[strcspn(line, "\r\n")] = '\0'; + if ( strlen(line) == 0 || (strlen(line) == 1 && isspace(line[0]))) { return 0; diff --git a/simulator.c b/simulator.c index c6462ea..fb5ce8f 100644 --- a/simulator.c +++ b/simulator.c @@ -15,7 +15,6 @@ int main(int argc, char **argv) { gate_init(); while (NULL != fgets(buf, sizeof(buf), fd)) { - buf[strcspn(buf, "\r\n")] = '\0'; if (parse_line(buf)) { return 1; } diff --git a/test/Makefile b/test/Makefile index 053552f..6f04c4a 100644 --- a/test/Makefile +++ b/test/Makefile @@ -2,6 +2,8 @@ CFLAGS += -I../ TESTS = \ test-whitespace-input \ + test-short-keyword \ + test-duplicate-input \ test-logic-and \ test-logic-or \ test-logic-nand \ diff --git a/test/test-duplicate-input b/test/test-duplicate-input Binary files differnew file mode 100755 index 0000000..236c6fc --- /dev/null +++ b/test/test-duplicate-input diff --git a/test/test-duplicate-input.c b/test/test-duplicate-input.c new file mode 100644 index 0000000..8d1b88f --- /dev/null +++ b/test/test-duplicate-input.c @@ -0,0 +1,13 @@ +#include <assert.h> + +#include "gate.h" +#include "parser.h" + +int main(void) { + char test_string[] = "input test\n"; + assert(0 == parse_line(test_string)); + assert(gate_get_input_by_name("test") != NULL); + assert(0 != parse_line(test_string)); + assert(gate_get_input_by_name("test") != NULL); + return 0; +} diff --git a/test/test-short-keyword b/test/test-short-keyword Binary files differnew file mode 100755 index 0000000..39f58dc --- /dev/null +++ b/test/test-short-keyword diff --git a/test/test-short-keyword.c b/test/test-short-keyword.c new file mode 100644 index 0000000..50d44d3 --- /dev/null +++ b/test/test-short-keyword.c @@ -0,0 +1,52 @@ +#include <assert.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> + +#include "gate.h" +#include "parser.h" + +char *failures[] = { + "input\n", + "input \n", +}; + +char *passes[] = { + "input a\n", + "input aaaaaaaa\n", + "\n", + "\n\n", + "\r", + "\r\r", + "\r\n", + "\n\r", + "\n\n\r", + "\r\n\r", +}; + +int main(void) { + size_t i = 0; + char *string = NULL; + + for (i = 0; i < sizeof(failures)/sizeof(failures[0]); i++) { + string = strdup(failures[i]); + if (string == NULL) { + perror("strdup"); + return 1; + } + fprintf(stderr, "Testing xfail '%s'\n", string); + assert(0 != parse_line(string)); + free(string); + } + for (i = 0; i < sizeof(passes)/sizeof(passes[0]); i++) { + string = strdup(passes[i]); + if (string == NULL) { + perror("strdup"); + return 1; + } + fprintf(stderr, "Testing xpass '%s'\n", string); + assert(0 == parse_line(string)); + free(string); + } + return 0; +} |