@@ -9,6 +9,39 @@ fn common_compile_args() -> &'static [&'static str] {
9
9
& [ "-DONLINE_JUDGE" , "-O2" , "-w" , "-fmax-errors=3" ]
10
10
}
11
11
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
+
12
45
#[ test]
13
46
fn test_help ( ) {
14
47
let mut cmd = assert_cmd:: Command :: cargo_bin ( BIN_NAME ) . unwrap ( ) ;
@@ -40,48 +73,60 @@ int main()
40
73
"# ,
41
74
)
42
75
. unwrap ( ) ;
76
+ compile_c ( & test_root) ;
77
+
43
78
test_root. child ( "stdin" ) . touch ( ) . unwrap ( ) ;
79
+ let config_path = "config.toml" ;
80
+ create_config ( & test_root, config_path) ;
44
81
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] )
50
85
. assert ( )
51
86
. success ( ) ;
52
87
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
- }
70
88
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
+ )
73
115
. 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) ;
74
121
75
122
let mut cmd = assert_cmd:: Command :: cargo_bin ( BIN_NAME ) . unwrap ( ) ;
76
123
cmd. current_dir ( test_root. path ( ) )
77
- . args ( [ "--env-path" , "config.toml" ] )
124
+ . args ( [ "--env-path" , config_path ] )
78
125
. assert ( )
79
126
. success ( ) ;
80
127
81
- test_root
82
- . child ( "stdout" )
83
- . assert ( predicate:: str:: diff ( "hello, world!\n " ) ) ;
128
+ test_root. child ( "stdout" ) . assert ( predicate:: str:: is_empty ( ) ) ;
84
129
test_root
85
130
. child ( "output" )
86
- . assert ( predicate:: str:: starts_with ( "Exited Normally " ) ) ;
131
+ . assert ( predicate:: str:: starts_with ( "RE " ) ) ;
87
132
}
0 commit comments