如何在unix环境中使用.in和.out文件进行测试

How to use .in and .out files for testing in a unix environment?

本文关键字:out 文件 测试 in unix 环境      更新时间:2023-10-16

我有C++程序,还有一堆.in和.out文件用于预制测试。我想知道如何执行我的c++程序并将其输入其中。

我假设您有一个文件列表,如test0.in和一个corrosconding test1.out。也许你想要这样的Makefile:

#Put rules/variables to actually make the program
SRC = test.cpp
TARGET = program
INPUTS = $(shell ls *.in)
OUTPUTS = $(patsubst %.in, %.out, $(INPUTS))
TESTS = $(patsubst %.in, %.test, $(INPUTS))
#This build rule is just for showcase.  You will probably need your own, more sophisticated build rules
$(TARGET): $(SRC)
    g++ $^ -o $@ -std=c++11
.PHONY: test
test: $(TESTS)
%.test : %.in %.out $(TARGET)
    @./$(TARGET) < $*.in > $@;
    @if cmp -s $@ $*.out; then 
        echo "PASS: $*"; 
    else 
        echo "FAIL: $*"; 
    fi
    @rm $@

然后,只需键入make test -j进行多线程测试。