Skip to content

Commit 09ac7d3

Browse files
committed
Add some basic tests for compare.cc
1 parent e761acb commit 09ac7d3

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Run compare.cc tests
2+
on:
3+
merge_group:
4+
pull_request:
5+
branches:
6+
- main
7+
- '[0-9]+.[0-9]+'
8+
9+
jobs:
10+
compare:
11+
runs-on: ubuntu-24.04
12+
timeout-minutes: 30
13+
container:
14+
image: domjudge/gitlabci:24.04
15+
options: --privileged --cgroupns=host --init
16+
steps:
17+
- uses: actions/checkout@v4
18+
- name: Run the actual compare.cc tests
19+
working-directory: misc-tests
20+
run: ./test_compare.sh

misc-tests/test_compare.sh

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/bin/bash
2+
# shellcheck disable=SC2317
3+
4+
set -e
5+
6+
COMPARE_EXECUTABLE=./compare_test_executable
7+
8+
fail() {
9+
fail=1
10+
msg="$1"
11+
echo -e "\e[31mFAIL: $msg\e[0m" >&2
12+
return 1
13+
}
14+
15+
exec_compare() {
16+
local expected_exit_code=$1
17+
shift
18+
local test_name=$1
19+
shift
20+
21+
echo -n "- $test_name "
22+
23+
# Create input files
24+
echo "does not matter" > judge_in.txt
25+
echo "$1" > judge_ans.txt
26+
echo "$2" > team_out.txt
27+
mkdir -p feedback
28+
shift 2
29+
30+
# Run the compare program
31+
EXIT_CODE=0
32+
$COMPARE_EXECUTABLE judge_in.txt judge_ans.txt feedback "$@" < team_out.txt || EXIT_CODE=$?
33+
34+
# Check expected result
35+
if [ "$EXIT_CODE" -eq "$expected_exit_code" ]; then
36+
echo -e "\e[32m✔\e[0m" >&2
37+
else
38+
fail "$test_name: expected exit code $expected_exit_code, got $EXIT_CODE"
39+
if [ -f feedback/judgemessage.txt ]; then
40+
cat feedback/judgemessage.txt >&2
41+
fi
42+
fi
43+
44+
# Clean up
45+
rm -rf judge_in.txt judge_ans.txt team_out.txt feedback
46+
}
47+
48+
test_identical_files() {
49+
exec_compare 42 "Identical files" "hello world" "hello world"
50+
}
51+
52+
test_different_files() {
53+
exec_compare 43 "Different files" "hello world" "hello there"
54+
}
55+
56+
test_float_within_tolerance() {
57+
exec_compare 42 "Float comparison, within tolerance" "1.000000000" "1.000000001" float_tolerance 1.1e-9
58+
}
59+
60+
test_float_outside_tolerance() {
61+
exec_compare 43 "Float comparison, outside tolerance" "1.000" "1.001" float_tolerance 1e-4
62+
}
63+
64+
test_invalid_float() {
65+
exec_compare 43 "Invalid float (should fail with current isfloat)" "1.0" "1.0a" float_tolerance 1e-9
66+
}
67+
68+
test_case_insensitive_pass() {
69+
exec_compare 42 "Case-insensitive comparison (pass)" "Hello World" "hello world"
70+
}
71+
72+
test_case_sensitive_fail() {
73+
exec_compare 43 "Case-sensitive comparison (fail)" "Hello World" "hello world" case_sensitive
74+
}
75+
76+
test_space_change_sensitive_pass() {
77+
exec_compare 42 "Space change comparison (pass)" "hello world" "hello world"
78+
}
79+
80+
test_space_change_sensitive_fail() {
81+
exec_compare 43 "Space change sensitive comparison (fail)" "hello world" "hello world" space_change_sensitive
82+
}
83+
84+
test_invalid_float_extra_chars() {
85+
exec_compare 43 "Invalid float with extra characters" "1.0" "1.0abc" float_tolerance 1e-9
86+
}
87+
88+
89+
any_test_failed=0
90+
91+
g++ -g -O2 -Wall -std=c++17 ../sql/files/defaultdata/compare/compare.cc -o $COMPARE_EXECUTABLE
92+
93+
for func in $(compgen -o nosort -A function test_); do
94+
fail=0
95+
$func
96+
if [ $fail -ne 0 ]; then
97+
any_test_failed=1
98+
fi
99+
done
100+
101+
rm -f $COMPARE_EXECUTABLE
102+
103+
exit $any_test_failed

0 commit comments

Comments
 (0)