一些可能有用的测试脚本和 Makefile

file struct

1
2
3
4
5
6
7
8
9
10
11
12
13
.
├── include
│ └── add.h
├── main.cpp
├── Makefile
├── README.md
├── src
│ └── add.cpp
└── tests
├── test_cases.txt
├── test_cnt.cpp
├── test_results.txt
└── tests.sh

Makefile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
CXX = clang++

target = mixplus

includes = $(wildcard ./include/*.h)
src = $(wildcard ./src/*.cpp)
tests = $(wildcard ./tests/*.cpp)
obj = $(src:%.cpp=%.o)
tests_obj = $(tests:%.cpp=%.o)

CXXDEBUG = -g -fsanitize=address -fno-omit-frame-pointer
CXXFLAGS := $(CXXDEBUG) -Iinclude -std=c++20 -Wall -Wno-format -Wno-unused
LDFLAGS := -fsanitize=address

all: $(target)

$(target): $(obj) main.o
@$(CXX) $(LDFLAGS) $(obj) main.o -o $(target)

%.o: %.cpp $(includes)
@$(CXX) $(CXXFLAGS) -c $< -o $@

main.o: main.cpp

.PHONY: run
run: $(target)
@./$(target)

test_target: $(tests_obj) $(obj)
@$(CXX) $(CXXFLAGS) $(tests_obj) $(obj) -o test

.PHONY: test
test: test_target
@./test
@tests/tests.sh

.PHONY: fmt
fmt:
@clang-format --style=LLVM -i src/*.cpp tests/*.cpp include/*.h main.cpp

.PHONY: clean
clean:
@rm -rf $(target) $(obj) $(tests_obj) main.o test

test.sh

read a line from the test cases and exec, compare it with the result

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/bash

TESTS="tests/test_cases.txt"
CASES="tests/test_results.txt"

echo "tests start!"

test_numbers="$(wc -l < $TESTS)"
result_numbers="$(wc -l < $CASES)"
if [ $test_numbers -eq 0 ]; then
echo "No test cases found!"
exit 1
fi
if [ $test_numbers -ne $result_numbers ]; then
echo "Test cases and results are not equal!"
exit 1
fi

for i in `seq 1 $test_numbers` ; do
cmd1="sed -n "$i"p $TESTS"
test_case="$($cmd1)"
result="$($test_case)"
cmd2="sed -n "$i"p $CASES"
test_result="$($cmd2)"
if [ "$result" == "$test_result" ]; then
echo "test $i passed!"
else
echo $result
echo $test_result
echo "test $i failed!"
fi
done

echo "tests success!"

一些可能有用的测试脚本和 Makefile

https://www.yunwei37.com/archives/test_scripts.html

Author

yunwei37

Posted on

2000-03-22

Updated on

2024-12-01

Licensed under

Comments