ccode_runner 0.3.7

Run/compiles files and executes them efficiently
Documentation
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use ccode_runner::ExecutionLimits;
use ccode_runner::lang_runner::{language_name::LanguageName, program_store::ProgramStore};

#[test]
fn test_time_limit_exceeded_infinite_loop_python() {
    let program_text = r#"
while True:
    pass
"#;

    let limits = ExecutionLimits::new().with_time_limit(1000); // 1 second timeout

    let result = ProgramStore::new_from_text_with_limits(
        program_text,
        program_text,
        LanguageName::Python,
        LanguageName::Python,
        false,
        limits,
    );

    assert!(result.is_ok(), "ProgramStore creation should succeed");
    let program = result.unwrap();

    let run_result = program.run_codes_and_compare_output("");
    assert!(run_result.is_err(), "Should fail due to timeout");

    // The error can be either CodeRunFailed (wrapping timeout) or direct timeout
    // Both are acceptable as the timeout is being enforced
    let error = run_result.unwrap_err();
    let error_msg = format!("{}", error);
    assert!(
        error_msg.contains("time limit")
            || error_msg.contains("exceeded")
            || error_msg.contains("CodeRunFailed"),
        "Error should indicate failure, got: {}",
        error_msg
    );
}

#[test]
fn test_time_limit_not_exceeded_quick_program_python() {
    let program_text = r#"
print("Hello, World!")
"#;

    let limits = ExecutionLimits::new().with_time_limit(5000); // 5 second timeout

    let result = ProgramStore::new_from_text_with_limits(
        program_text,
        program_text,
        LanguageName::Python,
        LanguageName::Python,
        false,
        limits,
    );

    assert!(result.is_ok(), "ProgramStore creation should succeed");
    let program = result.unwrap();

    let run_result = program.run_codes_and_compare_output("");
    assert!(run_result.is_ok(), "Should succeed within time limit");

    let (matched, expected, actual) = run_result.unwrap();
    assert!(matched);
    assert_eq!(expected.trim(), "Hello, World!");
    assert_eq!(actual.trim(), "Hello, World!");
}

#[test]
fn test_time_limit_exceeded_infinite_loop_c() {
    let program_text = r#"
int main() {
    while (1) {}
    return 0;
}
"#;

    let limits = ExecutionLimits::new().with_time_limit(1000); // 1 second timeout

    let result = ProgramStore::new_from_text_with_limits(
        program_text,
        program_text,
        LanguageName::C,
        LanguageName::C,
        false,
        limits,
    );

    assert!(result.is_ok(), "ProgramStore creation should succeed");
    let program = result.unwrap();

    let run_result = program.run_codes_and_compare_output("");
    assert!(run_result.is_err(), "Should fail due to timeout");
}

#[test]
fn test_time_limit_not_exceeded_quick_program_c() {
    let program_text = r#"
#include <stdio.h>
int main() {
    printf("Hello, World!\n");
    return 0;
}
"#;

    let limits = ExecutionLimits::new().with_time_limit(5000); // 5 second timeout

    let result = ProgramStore::new_from_text_with_limits(
        program_text,
        program_text,
        LanguageName::C,
        LanguageName::C,
        false,
        limits,
    );

    assert!(result.is_ok(), "ProgramStore creation should succeed");
    let program = result.unwrap();

    let run_result = program.run_codes_and_compare_output("");
    assert!(run_result.is_ok(), "Should succeed within time limit");

    let (matched, expected, actual) = run_result.unwrap();
    assert!(matched);
    assert_eq!(expected.trim(), "Hello, World!");
    assert_eq!(actual.trim(), "Hello, World!");
}

#[test]
fn test_backwards_compatibility_no_limits() {
    let program_text = r#"
print("Backwards compatible!")
"#;

    let program = ProgramStore::new_from_text(
        program_text,
        program_text,
        LanguageName::Python,
        LanguageName::Python,
        false,
    )
    .unwrap();

    let (matched, expected, actual) = program
        .run_codes_and_compare_output("")
        .expect("Failed to run program");

    assert!(matched);
    assert_eq!(expected.trim(), "Backwards compatible!");
    assert_eq!(actual.trim(), "Backwards compatible!");
}

#[test]
fn test_execution_limits_builder_pattern() {
    let limits = ExecutionLimits::new()
        .with_time_limit(2000)
        .with_memory_limit(100 * 1024 * 1024);

    assert_eq!(limits.time_limit_ms, Some(2000));
    assert_eq!(limits.memory_limit_bytes, Some(100 * 1024 * 1024));
}

#[test]
fn test_execution_limits_default() {
    let limits = ExecutionLimits::default();

    assert_eq!(limits.time_limit_ms, None);
    assert_eq!(limits.memory_limit_bytes, None);
}

#[test]
fn test_time_limit_with_stdin() {
    let program_text = r#"
n = int(input())
print(n * n)
"#;

    let limits = ExecutionLimits::new().with_time_limit(5000); // 5 second timeout

    let result = ProgramStore::new_from_text_with_limits(
        program_text,
        program_text,
        LanguageName::Python,
        LanguageName::Python,
        false,
        limits,
    );

    assert!(result.is_ok(), "ProgramStore creation should succeed");
    let program = result.unwrap();

    let run_result = program.run_codes_and_compare_output("10");
    assert!(run_result.is_ok(), "Should succeed with stdin input");

    let (matched, expected, actual) = run_result.unwrap();
    assert!(matched);
    assert_eq!(expected.trim(), "100");
    assert_eq!(actual.trim(), "100");
}

#[test]
fn test_memory_limit_only_no_timeout() {
    // Test program that runs quickly but with memory limit set
    let program_text = r#"
print("Quick program with memory limit")
"#;

    let limits = ExecutionLimits::new().with_memory_limit(100 * 1024 * 1024); // 100MB

    let result = ProgramStore::new_from_text_with_limits(
        program_text,
        program_text,
        LanguageName::Python,
        LanguageName::Python,
        false,
        limits,
    );

    assert!(result.is_ok(), "ProgramStore creation should succeed");
    let program = result.unwrap();

    let run_result = program.run_codes_and_compare_output("");
    assert!(
        run_result.is_ok(),
        "Should succeed with memory limit on quick program"
    );

    let (matched, expected, _) = run_result.unwrap();
    assert!(matched);
    assert_eq!(expected.trim(), "Quick program with memory limit");
}

#[test]
fn test_combined_time_and_memory_limits() {
    // Test with both time and memory limits
    let program_text = r#"
import sys
print("Program with both limits")
"#;

    let limits = ExecutionLimits::new()
        .with_time_limit(5000)
        .with_memory_limit(100 * 1024 * 1024);

    let result = ProgramStore::new_from_text_with_limits(
        program_text,
        program_text,
        LanguageName::Python,
        LanguageName::Python,
        false,
        limits,
    );

    assert!(result.is_ok(), "ProgramStore creation should succeed");
    let program = result.unwrap();

    let run_result = program.run_codes_and_compare_output("");
    assert!(run_result.is_ok(), "Should succeed with both limits");

    let (matched, expected, _) = run_result.unwrap();
    assert!(matched);
    assert_eq!(expected.trim(), "Program with both limits");
}

#[test]
fn test_memory_limit_with_rust() {
    // Test memory limit with a compiled language
    let program_text = r#"
fn main() {
    println!("Rust with memory limit");
}
"#;

    let limits = ExecutionLimits::new()
        .with_time_limit(5000)
        .with_memory_limit(50 * 1024 * 1024); // 50MB

    let result = ProgramStore::new_from_text_with_limits(
        program_text,
        program_text,
        LanguageName::Rust,
        LanguageName::Rust,
        false,
        limits,
    );

    assert!(result.is_ok(), "ProgramStore creation should succeed");
    let program = result.unwrap();

    let run_result = program.run_codes_and_compare_output("");
    assert!(
        run_result.is_ok(),
        "Should succeed with memory limit on Rust"
    );

    let (matched, expected, _) = run_result.unwrap();
    assert!(matched);
    assert_eq!(expected.trim(), "Rust with memory limit");
}

#[test]
fn test_only_time_limit_no_memory() {
    // Test with only time limit, no memory limit
    let program_text = r#"
print("Only time limit")
"#;

    let limits = ExecutionLimits::new().with_time_limit(5000);

    let result = ProgramStore::new_from_text_with_limits(
        program_text,
        program_text,
        LanguageName::Python,
        LanguageName::Python,
        false,
        limits,
    );

    assert!(result.is_ok(), "ProgramStore creation should succeed");
    let program = result.unwrap();

    let run_result = program.run_codes_and_compare_output("");
    assert!(run_result.is_ok(), "Should succeed with time limit only");

    let (matched, expected, _) = run_result.unwrap();
    assert!(matched);
    assert_eq!(expected.trim(), "Only time limit");
}

#[test]
fn test_cpp_with_limits() {
    // Test with C++ to cover compiled language paths
    let program_text = r#"
#include <iostream>
int main() {
    std::cout << "C++ with limits" << std::endl;
    return 0;
}
"#;

    let limits = ExecutionLimits::new()
        .with_time_limit(5000)
        .with_memory_limit(50 * 1024 * 1024);

    let result = ProgramStore::new_from_text_with_limits(
        program_text,
        program_text,
        LanguageName::Cpp,
        LanguageName::Cpp,
        false,
        limits,
    );

    assert!(result.is_ok(), "ProgramStore creation should succeed");
    let program = result.unwrap();

    let run_result = program.run_codes_and_compare_output("");
    assert!(run_result.is_ok(), "Should succeed with limits on C++");

    let (matched, expected, _) = run_result.unwrap();
    assert!(matched);
    assert_eq!(expected.trim(), "C++ with limits");
}

#[test]
#[cfg(unix)] // Memory limits are enforced differently on Unix vs Windows
fn test_memory_limit_exceeded_c() {
    // C program that tries to allocate 10MB of memory
    // We'll set the limit to 2MB, which should cause it to fail
    let program_text = r#"
#include <stdlib.h>
#include <string.h>

int main() {
    // Try to allocate 10MB
    void* mem = malloc(10 * 1024 * 1024);
    if (mem == NULL) {
        return 1;
    }
    // Touch the memory to ensure it's actually allocated
    memset(mem, 0, 10 * 1024 * 1024);
    free(mem);
    return 0;
}
"#;

    let limits = ExecutionLimits::new()
        .with_time_limit(5000) // 5 second timeout to prevent hanging
        .with_memory_limit(2 * 1024 * 1024); // 2MB limit (less than the 10MB allocation)

    let result = ProgramStore::new_from_text_with_limits(
        program_text,
        program_text,
        LanguageName::C,
        LanguageName::C,
        false,
        limits,
    );

    assert!(result.is_ok(), "ProgramStore creation should succeed");
    let program = result.unwrap();

    let run_result = program.run_codes_and_compare_output("");
    // The program should fail due to memory limit
    // Either the malloc fails or the process is terminated
    assert!(
        run_result.is_err(),
        "Should fail due to memory limit exceeded"
    );
}

#[test]
fn test_memory_limit_exceeded_python() {
    // Python program that tries to allocate memory
    // Set a very low limit that Python runtime itself might exceed
    let program_text = r#"
# Try to allocate a large list
data = [0] * (10 * 1024 * 1024)  # 10 million integers
print("Allocated memory")
"#;

    let limits = ExecutionLimits::new()
        .with_time_limit(5000) // 5 second timeout to prevent hanging
        .with_memory_limit(3 * 1024 * 1024); // 3MB limit (Python needs more than this)

    let result = ProgramStore::new_from_text_with_limits(
        program_text,
        program_text,
        LanguageName::Python,
        LanguageName::Python,
        false,
        limits,
    );

    assert!(result.is_ok(), "ProgramStore creation should succeed");
    let program = result.unwrap();

    let run_result = program.run_codes_and_compare_output("");
    // The program should fail due to memory limit
    // On Unix: rlimit will cause it to fail
    // On Windows: the monitor will kill it
    assert!(
        run_result.is_err(),
        "Should fail due to memory limit exceeded"
    );
}