简体   繁体   English

如何遍历Makefile中的n个测试用例?

[英]How To Loop through n Test cases In Makefile?

I want my Makefile to automate my testing for me. 我希望我的Makefile为我自动化测试。 Basically I would have a bunch of test cases that my code runs on. 基本上,我会在代码上运行一堆测试用例。 I want the user to specify the number of test cases rather than that being hard coded in. 我希望用户指定测试用例的数量,而不是进行硬编码。

Basically I want something like this: 基本上我想要这样的东西:

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

And turn it into something like this: 并将其变成这样:

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;
}

Thanks :) 谢谢 :)

Now updated to answer the question properly 现在已更新,可以正确回答问题

What you might want to do (by the look of it) is have your makefile do various things for you: 您可能想要做的(看起来)是让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)

Use by: 使用者:

  • make build - build main make build -建立主要
  • make test - run all tests that are found upto 100 (the max can be changed) make test -运行发现的所有测试,最多100次(可以更改的最大值)
  • make test NUM_TESTS=3 - run the first 3 tests (if they exist found) make test NUM_TESTS=3运行前3个测试(如果找到了)
  • make test NUM_TESTS=3 -j6 - same as before but run 6 parallel jobs (or use -j for as many parallel jobs as possible) - ie run the tests in parallel make test NUM_TESTS=3 -j6与以前相同,但运行6个并行作业(或使用-j进行尽可能多的并行作业)-即并行运行测试

Explanation: The pattern rule will generate a file output*.txt depending on the file test*.txt. 说明:模式规则将根据文件test * .txt生成文件output * .txt。 But we want to call the rule outputX.txt for this we generate a list of output files by searching for all output files (in variable TEST_OUTPUTS ) and then selecting the number of tests we want. 但是我们要为此调用规则outputX.txt ,通过搜索所有输出文件(在TEST_OUTPUTS变量中),然后选择所需的测试数量,来生成输出文件列表。 We can do this by passing in a variable or if we don't pass in a variable then it does upto 100 tests (or whatever you set the maximum to be. 我们可以通过传入一个变量来完成此操作,或者如果我们不传入一个变量,那么它最多可以进行100次测试(或您设置的最大值)。

Note: I did not run this, so I would consider is to be pseudo code, but it should be pretty close) 注意:我没有运行它,所以我认为是伪代码,但是应该很接近)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM