summaryrefslogtreecommitdiff
path: root/src/unit-tests
diff options
context:
space:
mode:
Diffstat (limited to 'src/unit-tests')
-rw-r--r--src/unit-tests/lexer/lexer.test.scm89
-rw-r--r--src/unit-tests/parser/parser.test.scm30
-rw-r--r--src/unit-tests/utils/t-factory.test.scm19
3 files changed, 138 insertions, 0 deletions
diff --git a/src/unit-tests/lexer/lexer.test.scm b/src/unit-tests/lexer/lexer.test.scm
new file mode 100644
index 0000000..807dbec
--- /dev/null
+++ b/src/unit-tests/lexer/lexer.test.scm
@@ -0,0 +1,89 @@
+;; -*- compile-command: "guile -L ./src ./src/unit-tests/lexer/lexer.test.scm"; -*-
+(use-modules (srfi srfi-64)
+ (modules lexer lexer))
+
+(define (read-this str)
+ (set-current-input-port
+ (open-input-string str))
+ str)
+
+
+(test-begin "lexer-harness")
+
+
+(test-equal (read-this "")
+ '()
+ (read-tokens))
+
+(test-equal (read-this "(")
+ '(left-paren)
+ (read-tokens))
+
+(test-equal (read-this "((")
+ '(left-paren left-paren)
+ (read-tokens))
+
+(test-equal (read-this "( )")
+ '(left-paren right-paren)
+ (read-tokens))
+
+(test-equal (read-this "( {;} {((};})")
+ '(left-paren open-brace semi-colon close-brace open-brace left-paren left-paren close-brace semi-colon close-brace right-paren)
+ (read-tokens))
+
+(test-equal (read-this "1")
+ '(1)
+ (read-tokens))
+
+(test-equal (read-this "~+-")
+ '(complement add sub)
+ (read-tokens))
+
+(test-equal (read-this "---")
+ '(decrement sub)
+ (read-tokens))
+
+(test-equal (read-this "+--")
+ '(add decrement)
+ (read-tokens))
+
+(test-equal (read-this "0")
+ '(0)
+ (read-tokens))
+
+(test-equal (read-this "0011001")
+ '(11001)
+ (read-tokens))
+
+(test-equal (read-this "12 {34")
+ '(12 open-brace 34)
+ (read-tokens))
+
+(test-equal (read-this "34;")
+ '(34 semi-colon)
+ (read-tokens))
+
+(test-error (read-this "3.4")
+ (read-tokens))
+
+(test-equal (read-this "a")
+ '("a")
+ (read-tokens))
+
+(test-equal (read-this "a_2")
+ '("a_2")
+ (read-tokens))
+
+(test-error (read-this "1foo")
+ (read-tokens))
+
+(test-equal (read-this "void")
+ '(void)
+ (read-tokens))
+
+(test-equal (read-this "int main(void) {return 2;}")
+ '(int "main" left-paren void right-paren open-brace return 2 semi-colon close-brace)
+ (read-tokens))
+
+
+(test-end "lexer-harness")
diff --git a/src/unit-tests/parser/parser.test.scm b/src/unit-tests/parser/parser.test.scm
new file mode 100644
index 0000000..b3573d0
--- /dev/null
+++ b/src/unit-tests/parser/parser.test.scm
@@ -0,0 +1,30 @@
+;; -*- compile-command: "guile -L ./src ./src/unit-tests/parser/parser.test.scm"; -*-
+(use-modules (srfi srfi-64)
+ (modules parser parser))
+
+
+(test-begin "parser-harness")
+
+
+(test-equal "trivial function main 2"
+ '(program (function (identifier "main") (return (constant 2))))
+ (p-program '(int "main" left-paren void right-paren open-brace return 2 semi-colon close-brace)))
+
+(test-equal "trivial function foo 4"
+ '(program (function (identifier "foo") (return (constant 4))))
+ (p-program '(int "foo" left-paren void right-paren open-brace return 4 semi-colon close-brace)))
+
+(test-error "trivial function bad double return"
+ (p-program '(int "foo" left-paren void right-paren open-brace return return 4 semi-colon close-brace)))
+
+(test-error "trivial function bad parens"
+ (p-program '(int "foo" right-paren void left-paren open-brace return 4 semi-colon close-brace)))
+
+(test-error "trivial function bad int parameter"
+ (p-program '(int "foo" left-paren int right-paren open-brace return 4 semi-colon close-brace)))
+
+(test-error "trivial function incomplete function"
+ (p-program '(int "foo" left-paren void right-paren open-brace return)))
+
+
+(test-end "parser-harness")
diff --git a/src/unit-tests/utils/t-factory.test.scm b/src/unit-tests/utils/t-factory.test.scm
new file mode 100644
index 0000000..eda983b
--- /dev/null
+++ b/src/unit-tests/utils/t-factory.test.scm
@@ -0,0 +1,19 @@
+;; -*- compile-command: "guile -L ./src ./src/unit-tests/utils/t-factory.test.scm"; -*-
+(use-modules (srfi srfi-64)
+ (modules ast assembly-tree)
+ (modules utils t-factory))
+
+
+(test-begin "t-factory-harness")
+
+
+(test-equal "make first temporary"
+ 't.101
+ (temporary-name (make-t)))
+
+(test-equal "make second temporary"
+ 't.102
+ (temporary-name (make-t)))
+
+
+(test-end "t-factory-harness")