Skip to content

Commit 33986cd

Browse files
committed
test: c code runtime error
1 parent 7cad759 commit 33986cd

File tree

1 file changed

+74
-29
lines changed

1 file changed

+74
-29
lines changed

tests/src/test.rs

+74-29
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,39 @@ fn common_compile_args() -> &'static [&'static str] {
99
&["-DONLINE_JUDGE", "-O2", "-w", "-fmax-errors=3"]
1010
}
1111

12+
fn create_config(test_root: &assert_fs::TempDir, config_path: &str) {
13+
let mut config = toml::toml! {
14+
cwd = "./"
15+
large-stack = true
16+
max-process = 10
17+
memory-limit = 5120000
18+
output-size-limit = 10000
19+
runtime-limit = 1500
20+
lang = 1
21+
};
22+
for f in ["stdin", "stdout", "stderr", "output"] {
23+
config.insert(
24+
f.to_string(),
25+
toml::Value::String(test_root.path().join(f).to_str().unwrap().to_owned()),
26+
);
27+
}
28+
test_root
29+
.child(config_path)
30+
.write_str(toml::to_string(&config).unwrap().as_str())
31+
.unwrap();
32+
}
33+
34+
/// Compile `{test_root}/main.c` into `{test_root}/main`
35+
fn compile_c(test_root: &assert_fs::TempDir) {
36+
let mut compile_cmd = assert_cmd::Command::new("gcc");
37+
compile_cmd
38+
.args(common_compile_args())
39+
.args(["-std=c++11", "main.c", "-o", "main", "-lm"])
40+
.current_dir(test_root.path())
41+
.assert()
42+
.success();
43+
}
44+
1245
#[test]
1346
fn test_help() {
1447
let mut cmd = assert_cmd::Command::cargo_bin(BIN_NAME).unwrap();
@@ -40,48 +73,60 @@ int main()
4073
"#,
4174
)
4275
.unwrap();
76+
compile_c(&test_root);
77+
4378
test_root.child("stdin").touch().unwrap();
79+
let config_path = "config.toml";
80+
create_config(&test_root, config_path);
4481

45-
let mut compile_cmd = assert_cmd::Command::new("gcc");
46-
compile_cmd
47-
.args(common_compile_args())
48-
.args(["-std=c++11", "main.c", "-o", "main", "-lm"])
49-
.current_dir(test_root.path())
82+
let mut cmd = assert_cmd::Command::cargo_bin(BIN_NAME).unwrap();
83+
cmd.current_dir(test_root.path())
84+
.args(["--env-path", config_path])
5085
.assert()
5186
.success();
5287

53-
let mut config = toml::toml! {
54-
cwd = "./"
55-
large-stack = true
56-
max-process = 10
57-
memory-limit = 5120000
58-
output-size-limit = 10000
59-
runtime-limit = 1500
60-
stdin = "./stdin"
61-
stdout = "./stdout"
62-
lang = 1
63-
};
64-
for f in ["stdin", "stdout", "stderr", "output"] {
65-
config.insert(
66-
f.to_string(),
67-
toml::Value::String(test_root.path().join(f).to_str().unwrap().to_owned()),
68-
);
69-
}
7088
test_root
71-
.child("config.toml")
72-
.write_str(toml::to_string(&config).unwrap().as_str())
89+
.child("stdout")
90+
.assert(predicate::str::diff("hello, world!\n"));
91+
test_root
92+
.child("output")
93+
.assert(predicate::str::starts_with("Exited Normally"));
94+
}
95+
96+
#[test]
97+
fn test_c_code_catch_runtime_error() {
98+
let test_root = assert_fs::TempDir::new().unwrap();
99+
test_root
100+
.child("main.c")
101+
.write_str(
102+
r#"
103+
#include <stdio.h>
104+
105+
int main()
106+
{
107+
int a = 1;
108+
int b = 0;
109+
int c = a / b;
110+
printf("%d\n", c);
111+
return 0;
112+
}
113+
"#,
114+
)
73115
.unwrap();
116+
compile_c(&test_root);
117+
118+
test_root.child("stdin").touch().unwrap();
119+
let config_path = "config.toml";
120+
create_config(&test_root, config_path);
74121

75122
let mut cmd = assert_cmd::Command::cargo_bin(BIN_NAME).unwrap();
76123
cmd.current_dir(test_root.path())
77-
.args(["--env-path", "config.toml"])
124+
.args(["--env-path", config_path])
78125
.assert()
79126
.success();
80127

81-
test_root
82-
.child("stdout")
83-
.assert(predicate::str::diff("hello, world!\n"));
128+
test_root.child("stdout").assert(predicate::str::is_empty());
84129
test_root
85130
.child("output")
86-
.assert(predicate::str::starts_with("Exited Normally"));
131+
.assert(predicate::str::starts_with("RE"));
87132
}

0 commit comments

Comments
 (0)