From d0edc42ddae30a69860dada404936993b224fc14 Mon Sep 17 00:00:00 2001
From: Sven Gestegard Robertz <sven.robertz@cs.lth.se>
Date: Fri, 28 Mar 2014 14:12:30 +0100
Subject: [PATCH] added test-cases for compiler error checking of void types

---
 test/Makefile                 | 24 +++++++++++++----
 test/errors/correct/void.lc   | 14 ++++++++++
 test/errors/incorrect/void.lc | 12 +++++++++
 test/test_errors.py           | 49 +++++++++++++++++++++++++++++++++++
 4 files changed, 94 insertions(+), 5 deletions(-)
 create mode 100644 test/errors/correct/void.lc
 create mode 100644 test/errors/incorrect/void.lc
 create mode 100755 test/test_errors.py

diff --git a/test/Makefile b/test/Makefile
index 7401102..491ba7c 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -1,10 +1,10 @@
 TESTS=basic simple nested
 LABCOMM_JAR=../compiler/labComm.jar
-LABCOMM=java -jar $(LABCOMM_JAR) 
+LABCOMM=java -jar $(LABCOMM_JAR)
 
 CFLAGS=-O3 -g -Wall -Werror
 
-all:	
+all:
 
 test: $(TESTS:%=test_%)
 #	PYTHONPATH=../lib/python \
@@ -14,12 +14,12 @@ test: $(TESTS:%=test_%)
 clean distclean:
 	rm -rf gen
 
-.PHONY: test_%
+.PHONY: test_% testErrors
 test_%: gen/%/signatures.py \
 	gen/%/c_relay \
 	gen/%/cs_relay.exe \
 	gen/%/java_relay.class \
-	gen/%/java_code 
+	gen/%/java_code
 	PYTHONPATH=../lib/python ./test_encoder_decoder.py \
 		--signatures=gen/$*/signatures.py \
 		--test tee gen/$*/testdata \
@@ -27,6 +27,20 @@ test_%: gen/%/signatures.py \
 		--test mono gen/$*/cs_relay.exe /dev/stdin /dev/stdout \
 		--test java \\-cp gen/$*:../lib/java/labcomm.jar java_relay \
 			    /dev/stdin /dev/stdout
+
+# test cases for compiler error checking
+.PHONY: compiler_errors testErrorsOK testErrorsNOK
+
+compiler_errors: testErrorsOK testErrorsNOK
+
+# tests that should succeed
+testErrorsOK: $(wildcard errors/correct/*.lc)
+	./test_errors.py --labcomm="$(LABCOMM)" --testOK $^
+
+# tests that should fail
+testErrorsNOK: $(wildcard errors/incorrect/*.lc)
+	./test_errors.py --labcomm="$(LABCOMM)" --testNOK $^
+
 .PRECIOUS: gen/%/.dir
 gen/%/.dir:
 	mkdir -p gen/$*
@@ -52,7 +66,7 @@ gen/%/c_relay.c: gen/%/typeinfo relay_gen_c.py Makefile
 .PRECIOUS: gen/%/c_relay
 gen/%/c_relay: gen/%/c_relay.c gen/%/c_code.c Makefile
 	$(CC) $(CFLAGS) -o $@ $< -I../lib/c -I. -L..//lib/c \
-		gen/$*/c_code.c -llabcomm 
+		gen/$*/c_code.c -llabcomm
 
 # C# relay test rules
 .PRECIOUS: gen/%/cs_code.cs
diff --git a/test/errors/correct/void.lc b/test/errors/correct/void.lc
new file mode 100644
index 0000000..a2d8a84
--- /dev/null
+++ b/test/errors/correct/void.lc
@@ -0,0 +1,14 @@
+typedef void avoid;
+
+sample avoid doavoid;
+
+// examples of errors: void may not be used
+// in structs or arrays
+//
+// sample struct {
+//  int a;
+//  avoid error;
+//} foo;
+//
+//sample void error2[2] ;
+//sample avoid error3[_];
diff --git a/test/errors/incorrect/void.lc b/test/errors/incorrect/void.lc
new file mode 100644
index 0000000..743c4d0
--- /dev/null
+++ b/test/errors/incorrect/void.lc
@@ -0,0 +1,12 @@
+typedef void avoid;
+
+// examples of errors: void may not be used
+// in structs or arrays
+//
+sample struct {
+  int a;
+  avoid error_1;
+} foo;
+
+sample void error_2[2] ;
+sample avoid error_3[_];
diff --git a/test/test_errors.py b/test/test_errors.py
new file mode 100755
index 0000000..24b9627
--- /dev/null
+++ b/test/test_errors.py
@@ -0,0 +1,49 @@
+#!/usr/bin/python
+
+import sys
+import argparse
+import subprocess
+
+# returns true if test fails
+def test_labcomm_compile_OK(lc, args):
+    cmd = args.labcomm.split() + [lc]
+    try:
+        res = subprocess.check_call(cmd)
+        print "sucess!"
+        return False
+    except subprocess.CalledProcessError as ex:
+        print ex.output
+        print ex.returncode
+        return True
+
+def test_labcomm_compile_fail(lc, args):
+    cmd = args.labcomm.split() + [lc]
+    try:
+        res = subprocess.check_call(cmd)
+        print "failed! (should have produced an error)"
+        return True
+    except subprocess.CalledProcessError as ex:
+        print "sucess!"
+        print ex.output
+        print ex.returncode
+        return False;
+
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser(description='Run test of error messages.')
+
+    parser.add_argument('--labcomm');
+    parser.add_argument('--testOK', nargs='*', default=[])
+    parser.add_argument('--testNOK', nargs='*', default=[])
+    args = parser.parse_args()
+
+    fail = False;
+    for test in args.testOK:
+        fail = fail or test_labcomm_compile_OK(test, args)
+        pass
+    for test in args.testNOK:
+        fail = fail or test_labcomm_compile_fail(test, args)
+        pass
+    if fail:
+        print "*** fail ***"
+    else:
+        print "*** success ***"
-- 
GitLab