From 2acc41d74b882e4062df81c0af45cc64c3642f2b Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Sun, 7 Mar 2021 20:24:19 -0700 Subject: [PATCH] improve Makefile Here are a list of the improvements done to the Makefile. 1. allow tests to be built and run individually from the command-line: Examples of building individual tests: ``` make bin/test1 make bin/test2 ... ``` Running individual tests: ```sh make test1 make test2 make test_compile make test_rand make test_rand_neg # note: you can still run all the tests with make test ``` 2. Because building and running the tests are now in their own rules, the user can run them in parallel now: Build everything in parallel with 5 threads: ``` make -j5 ``` Build and run everything with 5 threads: ``` make -j5 test ``` 3. Moved all outputs to the "bin" directory This simplifies the code in the "clean" target to 1 command. It also simplifies the .gitignore file and make it easy for developers to distinguish between output files and source files. 4. Added re.c as a dependency to all the tests Now if the user modifies re.c, all the tests will be see that they need to be rebuilt. --- .gitignore | 3 +-- Makefile | 44 ++++++++++++++++++++------------------- scripts/regex_test.py | 4 ++-- scripts/regex_test_neg.py | 4 ++-- 4 files changed, 28 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 63a5ace..5578ac1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ *.swp *.pyc -/tests/* -!/tests/*.c +/bin diff --git a/Makefile b/Makefile index 857d2ee..f2c1fe2 100644 --- a/Makefile +++ b/Makefile @@ -16,27 +16,30 @@ PYTHON != if (python --version 2>&1 | grep -q 'Python 2\..*'); then \ # Flags to pass to compiler CFLAGS := -O3 -Wall -Wextra -std=c99 -I. -all: - @$(CC) $(CFLAGS) re.c tests/test1.c -o tests/test1 - @$(CC) $(CFLAGS) re.c tests/test2.c -o tests/test2 - @$(CC) $(CFLAGS) re.c tests/test_rand.c -o tests/test_rand - @$(CC) $(CFLAGS) re.c tests/test_rand_neg.c -o tests/test_rand_neg - @$(CC) $(CFLAGS) re.c tests/test_compile.c -o tests/test_compile +all: bin/test1 bin/test2 bin/test_rand bin/test_rand_neg bin/test_compile + +bin/%: tests/%.c re.c + @mkdir -p bin + @$(CC) $(CFLAGS) re.c $< -o $@ clean: - @rm -f tests/test1 tests/test2 tests/test_rand tests/test_compile - @#@$(foreach test_bin,$(TEST_BINS), rm -f $(test_bin) ; ) - @rm -f a.out - @rm -f *.o + @rm -rf bin +test: test1 test_compile test_rand test_rand_neg test2 -test: all - @$(test $(PYTHON)) +test1: bin/test1 @echo @echo Testing hand-picked regex\'s: - @./tests/test1 + @./bin/test1 + +test_compile: bin/test_compile + @echo @echo Testing handling of invalid regex patterns - @./tests/test_compile + @./bin/test_compile + +test_rand: bin/test_rand + @$(test $(PYTHON)) + @echo @echo Testing patterns against $(NRAND_TESTS) random strings matching the Python implementation and comparing: @echo @$(PYTHON) ./scripts/regex_test.py \\d+\\w?\\D\\d $(NRAND_TESTS) @@ -75,8 +78,9 @@ test: all @$(PYTHON) ./scripts/regex_test.py [\\d] $(NRAND_TESTS) @$(PYTHON) ./scripts/regex_test.py [^\\d] $(NRAND_TESTS) @$(PYTHON) ./scripts/regex_test.py [^-1-4] $(NRAND_TESTS) - @echo - @echo + +test_rand_neg: bin/test_rand_neg + @$(test $(PYTHON)) @echo @echo Testing rejection of patterns against $(NRAND_TESTS) random strings also rejected by the Python implementation: @echo @@ -101,9 +105,7 @@ test: all @$(PYTHON) ./scripts/regex_test_neg.py [012345-9] $(NRAND_TESTS) @$(PYTHON) ./scripts/regex_test_neg.py [0-56789] $(NRAND_TESTS) @$(PYTHON) ./scripts/regex_test_neg.py .*123faerdig $(NRAND_TESTS) - @echo - @echo - @./tests/test2 - @echo - @echo +test2: bin/test2 + @echo + @./bin/test2 diff --git a/scripts/regex_test.py b/scripts/regex_test.py index 4fa98de..1f22709 100755 --- a/scripts/regex_test.py +++ b/scripts/regex_test.py @@ -3,7 +3,7 @@ """ This program generates random text that matches a given regex-pattern. The pattern is given via sys.argv and the generated text is passed to - the binary 'tests/test_rand' to check if the generated text also matches + the binary 'bin/test_rand' to check if the generated text also matches the regex-pattern in the C implementation. The exit-code of the testing program, is used to determine test success. @@ -17,7 +17,7 @@ from subprocess import call -prog = "./tests/test_rand" +prog = "./bin/test_rand" if len(sys.argv) < 2: print("") diff --git a/scripts/regex_test_neg.py b/scripts/regex_test_neg.py index c3daad6..8fa277b 100755 --- a/scripts/regex_test_neg.py +++ b/scripts/regex_test_neg.py @@ -3,7 +3,7 @@ """ This program generates random text that matches a given regex-pattern. The pattern is given via sys.argv and the generated text is passed to - the binary 'tests/test_rand' to check if the generated text also matches + the binary 'bin/test_rand_neg' to check if the generated text also matches the regex-pattern in the C implementation. The exit-code of the testing program, is used to determine test success. @@ -18,7 +18,7 @@ from subprocess import call -prog = "./tests/test_rand_neg" +prog = "./bin/test_rand_neg" if len(sys.argv) < 2: print("")