Bladeren bron

Added unit testing framework.

Taddeus Kroes 14 jaren geleden
bovenliggende
commit
2672776710
11 gewijzigde bestanden met toevoegingen van 103 en 19 verwijderingen
  1. 4 0
      .gitignore
  2. 24 0
      Makefile
  3. 3 0
      base.mk
  4. 0 19
      src/Makefile
  5. 0 0
      src/__init__.py
  6. 3 0
      test.py
  7. 0 0
      tests/__init__.py
  8. BIN
      tests/__init__.pyc
  9. 47 0
      tests/rules.mk
  10. 22 0
      tests/test_utils.py
  11. BIN
      tests/test_utils.pyc

+ 4 - 0
.gitignore

@@ -1,3 +1,7 @@
 *.swp
 *.pdf
 *~
+.coverage
+coverage/
+build/
+src/Makefile_old

+ 24 - 0
Makefile

@@ -0,0 +1,24 @@
+BUILD=build/
+
+# Fix pdflatex search path
+TGT_DIR :=
+TGT_DOC :=
+
+# Default target is 'all'. The 'build' target is defined here so that all
+# sub rules.mk can add prerequisites to the 'build' target.
+all:
+build:
+
+d := tests/
+include base.mk
+include $(d)/rules.mk
+
+.PHONY: doc
+
+all: doc build
+
+clean:
+	rm -rf $(CLEAN)
+
+$(TGT_DIR):
+	mkdir -p $(TGT_DIR)

+ 3 - 0
base.mk

@@ -0,0 +1,3 @@
+b := $(BUILD)$(d)
+TGT_DIR := $(TGT_DIR) $(b)
+CLEAN := $(CLEAN) $(b)

+ 0 - 19
src/Makefile

@@ -1,19 +0,0 @@
-CC=gcc
-LEX=lex
-YACC=yacc
-
-CFLAGS = -std=c99 -pedantic -Wall -D_POSIX_SOURCE -D_GNU_SOURCE $(PIC) -pipe
-
-all: peephole
-
-peephole: y.tab.o lex.yy.o
-	$(CC) -o $@ $^ -lm
-
-y.tab.c y.tab.h: peephole.y
-	$(YACC) -d $^
-
-lex.yy.c: peephole.l
-	$(LEX) $^
-
-clean:
-	rm -f lex.yy.* y.tab.* *.o peephole parser.out parsetab.py

+ 0 - 0
src/__init__.py


+ 3 - 0
test.py

@@ -0,0 +1,3 @@
+from testrunner import main
+import sys
+main(sys.argv[1:])

+ 0 - 0
tests/__init__.py


BIN
tests/__init__.pyc


+ 47 - 0
tests/rules.mk

@@ -0,0 +1,47 @@
+TESTS=$(wildcard tests/test_*.py)
+COVERAGE_OUTPUT_DIR := coverage
+PROFILER_OUTPUT_DIR := profiles
+OMIT := /usr/share/pyshared/*,/usr/lib/pymodules/python2.7/sympy/*,test*,*__init__.py
+
+ifeq ($(findstring python-coverage,$(wildcard /usr/bin/*)), python-coverage)
+COVERAGE=/usr/bin/python-coverage
+RM=rm -rf
+else
+COVERAGE=/usr/bin/coverage
+endif
+
+CLEAN := $(CLEAN) $(COVERAGE_OUTPUT_DIR) $(PROFILER_OUTPUT_DIR)
+TGT_DIR := $(TGT_DIR) $(PROFILER_OUTPUT_DIR)
+
+.PHONY: test coverage $(TESTS)
+
+test: $(TESTS) build
+
+ifeq ($(findstring python-coverage,$(wildcard /usr/bin/*)), python-coverage)
+coverage: ${COVERAGE} build
+	${COVERAGE} erase
+	${RM} ${COVERAGE_OUTPUT_DIR}/*
+	mkdir ${COVERAGE_OUTPUT_DIR} 2>/dev/null || true
+	for t in ${TESTS}; do \
+		echo $$t; \
+		${COVERAGE} -x test.py $$t; \
+		${COVERAGE} combine; \
+	done
+	${COVERAGE} html --omit=${OMIT} -d ${COVERAGE_OUTPUT_DIR}
+else
+coverage: ${COVERAGE} build
+	mkdir ${COVERAGE_OUTPUT_DIR} 2>/dev/null || true
+	${COVERAGE} erase
+	for t in ${TESTS}; do \
+		echo $$t; \
+		${COVERAGE} --omit ${OMIT} -x test.py $$t; \
+		${COVERAGE} --omit ${OMIT} -c; \
+	done
+	${COVERAGE} html --omit ${OMIT} --dir ${COVERAGE_OUTPUT_DIR}
+endif
+
+${COVERAGE}:
+	@echo "Install package 'python-coverage' to generate a coverage report."
+	@echo "On Debian/Ubuntu use: sudo apt-get install python-coverage"; false
+
+$(TESTS): build; @python -m testrunner $@

+ 22 - 0
tests/test_utils.py

@@ -0,0 +1,22 @@
+import unittest
+
+from src.utils import Statement as S, Block as B, find_leaders, \
+        find_basic_blocks
+
+
+class TestUtils(unittest.TestCase):
+
+    def setUp(self):
+        pass
+
+    def test_find_leaders(self):
+        add = S('command', 'add', '$1', '$2', '$3')
+        s = [add, S('command', 'j', 'foo'), add, add, S('label', 'foo')]
+        self.assertEqual(find_leaders(s), [0, 2, 4])
+
+    def test_find_basic_blocks(self):
+        add = S('command', 'add', '$1', '$2', '$3')
+        s = [add, S('command', 'j', 'foo'), add, add, S('label', 'foo')]
+        self.assertEqual(map(lambda b: b.statements, find_basic_blocks(s)), \
+                [B(s[:2]).statements, B(s[2:4]).statements, \
+                 B(s[4:]).statements])

BIN
tests/test_utils.pyc