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

有帮助吗?

解决方案

我假设你有一个文件列表,例如 test0.in 以及相应的 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 用于多线程测试。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top