我希望我的 Makefile 能够自动执行我的测试。基本上我会有一堆测试用例来运行我的代码。我希望用户指定测试用例的数量,而不是硬编码的数量。
基本上我想要这样的东西:
gcc main.c -o main
./main < test1.txt > output1.txt
./main < test2.txt > output2.txt
./main < test3.txt > output3.txt
./main < test4.txt > output4.txt
.
.
.
./main < test<n>.txt > output<n>.txt #for some value n
把它变成这样:
gcc main.c -o main
#of course this wouldn't be the syntax, but I just need the Makefile version of a loop, where all one has to do is change the n value
for(int i = 0; i < n+1; i++){
./main < test<i>.txt > output<i>.txt;
}
谢谢:)
最佳答案
现已更新以正确回答问题
您可能想要做的(从外观来看)是让您的 makefile 为您做各种事情:
# Target to build "main" its the first target and therefore the default
# call "make" to run
main:
@gcc main.c -o main
# Arbitrary max number of tests, can be overwritten by passing the variable in
NUM_TESTS=100
# Find all the tests, put them into an ordered list, then take the first
# 1 to NUM_TESTS of them. Finally substitute test* for output*
TEST_OUTPUTS=$(subst test,output,$(wordlist 1,$(NUM_TESTS),$(sort $(wildcard test*.txt))))
# Target to do your testing, call "make test NUM_TESTS=3" or "make test"
# to run all tests (up to 100).
.PHONY: test
test: $(TEST_OUTPUTS)
# Pattern rule to run each test - you don't call this directly
# Note: this has a dependency on main so if main is not built it
# will get built first
output%.txt: test%.txt main
@./main < $< > $@
# Target to clean up output files, call "make clean"
.PHONY: clean
clean:
rm -f main
rm -f $(TEST_OUTPUTS)
使用者:
make build
- 构建主make test
- 运行找到的所有测试,最多 100 个(可以更改最大值)make test NUM_TESTS=3
- 运行前 3 个测试(如果找到)make test NUM_TESTS=3 -j6
- 与之前相同,但运行 6 个并行作业(或使用-j
执行尽可能多的并行作业) - 即运行并行测试
说明:
模式规则将根据文件 test*.txt 生成文件输出*.txt。但我们想要调用规则 outputX.txt
,为此我们通过搜索所有输出文件(在变量 TEST_OUTPUTS
中)然后选择输出文件的数量来生成输出文件列表。我们想要的测试。我们可以通过传入一个变量来做到这一点,或者如果我们不传入一个变量,那么它最多会进行 100 次测试(或者您设置的最大值。
注意:我没有运行这个,所以我认为它是伪代码,但它应该非常接近)
关于c - 如何在Makefile中循环遍历n个测试用例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57386910/