summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2017-12-28 20:43:30 +1300
committerDavid Phillips <david@sighup.nz>2017-12-28 20:43:30 +1300
commitb6a3052c3ebde6114ec6166971b3072460c44814 (patch)
tree9a36552c07e6fea22c597616229ffd79e437aef2
parent7823d9c7e4f3c0485e8d96e44e0d94541762c6d1 (diff)
downloadhence-b6a3052c3ebde6114ec6166971b3072460c44814.tar.xz
Add more tests, fix misc bugs found
-rw-r--r--parser.c9
-rw-r--r--simulator.c1
-rw-r--r--test/Makefile2
-rwxr-xr-xtest/test-duplicate-inputbin0 -> 18808 bytes
-rw-r--r--test/test-duplicate-input.c13
-rwxr-xr-xtest/test-short-keywordbin0 -> 19008 bytes
-rw-r--r--test/test-short-keyword.c52
7 files changed, 74 insertions, 3 deletions
diff --git a/parser.c b/parser.c
index 5b96739..822441c 100644
--- a/parser.c
+++ b/parser.c
@@ -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
new file mode 100755
index 0000000..236c6fc
--- /dev/null
+++ b/test/test-duplicate-input
Binary files differ
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
new file mode 100755
index 0000000..39f58dc
--- /dev/null
+++ b/test/test-short-keyword
Binary files differ
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;
+}