runmat 0.0.17

High-performance MATLAB/Octave runtime with Jupyter kernel support
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
use std::fs;
use std::path::PathBuf;
use std::process::Command;
use tempfile::TempDir;

// Helper function to get the binary path
fn get_binary_path() -> PathBuf {
    let mut path = std::env::current_exe().unwrap();
    path.pop(); // Remove test binary name
    if path.ends_with("deps") {
        path.pop(); // Remove deps directory
    }
    path.push("runmat");
    path
}

// Helper function to run runmat with arguments
fn run_runmat(args: &[&str]) -> std::process::Output {
    Command::new(get_binary_path())
        .args(args)
        .output()
        .expect("Failed to execute runmat binary")
}

#[test]
fn test_help_command() {
    let output = run_runmat(&["--help"]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("RunMat"));
    assert!(stdout.contains("JIT compilation"));
    assert!(stdout.contains("Garbage collection"));
    assert!(stdout.contains("--no-jit"));
    assert!(stdout.contains("--gc-preset"));
    assert!(stdout.contains("--jit-opt-level"));
}

#[test]
fn test_version_command() {
    let output = run_runmat(&["--version"]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("runmat"));
}

#[test]
fn test_version_detailed_command() {
    let output = run_runmat(&["version", "--detailed"]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("RunMat"));
    assert!(stdout.contains("Components:"));
    assert!(stdout.contains("runmat-lexer"));
    assert!(stdout.contains("runmat-turbine"));
    assert!(stdout.contains("runmat-gc"));
}

#[test]
fn test_info_command() {
    let output = run_runmat(&["info"]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("RunMat System Information"));
    assert!(stdout.contains("Runtime Configuration"));
    assert!(stdout.contains("JIT Compiler"));
    assert!(stdout.contains("GC Preset"));
    assert!(stdout.contains("Environment"));
    assert!(stdout.contains("Garbage Collector Status"));
}

#[test]
fn test_gc_stats_command() {
    let output = run_runmat(&["gc", "stats"]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("GC Statistics"));
    assert!(stdout.contains("Allocations"));
    assert!(stdout.contains("Collections"));
    assert!(stdout.contains("Memory"));
}

#[test]
fn test_gc_major_command() {
    let output = run_runmat(&["gc", "major"]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Major GC collected"));
    assert!(stdout.contains("objects"));
}

#[test]
fn test_gc_minor_command() {
    let output = run_runmat(&["gc", "minor"]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Minor GC collected"));
    assert!(stdout.contains("objects"));
}

#[test]
fn test_jit_disabled_flag() {
    let output = run_runmat(&["--no-jit", "info"]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("JIT Compiler: disabled"));
}

#[test]
fn test_jit_optimization_levels() {
    let optimization_levels = ["none", "size", "speed", "aggressive"];

    for level in &optimization_levels {
        let output = run_runmat(&["--jit-opt-level", level, "info"]);
        assert!(
            output.status.success(),
            "Failed with optimization level: {level}"
        );

        let stdout = String::from_utf8_lossy(&output.stdout);
        // Check that the optimization level is reflected in the output
        assert!(stdout.contains("JIT Optimization"));
    }
}

#[test]
fn test_gc_presets() {
    let presets = ["low-latency", "high-throughput", "low-memory", "debug"];

    for preset in &presets {
        let output = run_runmat(&["--gc-preset", preset, "info"]);
        assert!(output.status.success(), "Failed with GC preset: {preset}");

        let stdout = String::from_utf8_lossy(&output.stdout);
        assert!(stdout.contains("GC Preset"));
    }
}

#[test]
fn test_gc_young_size_configuration() {
    let output = run_runmat(&["--gc-young-size", "64", "info"]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("GC Young Generation: 64MB"));
}

#[test]
fn test_gc_threads_configuration() {
    let output = run_runmat(&["--gc-threads", "4", "info"]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("GC Threads: 4"));
}

#[test]
fn test_gc_stats_flag() {
    let output = run_runmat(&["--gc-stats", "info"]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("GC Statistics: true"));
}

#[test]
fn test_verbose_flag() {
    let output = run_runmat(&["--verbose", "info"]);
    assert!(output.status.success());

    // Verbose flag should work without errors
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("System Information"));
}

#[test]
fn test_debug_flag() {
    let output = run_runmat(&["--debug", "info"]);
    assert!(output.status.success());

    // Debug flag should enable debug logging
    let stderr = String::from_utf8_lossy(&output.stderr);
    // Should see debug-level logs
    assert!(stderr.contains("DEBUG") || !output.stdout.is_empty());
}

#[test]
fn test_timeout_configuration() {
    let output = run_runmat(&["--timeout", "60", "info"]);
    assert!(output.status.success());
}

#[test]
fn test_jit_threshold_configuration() {
    let output = run_runmat(&["--jit-threshold", "5", "info"]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("JIT Threshold: 5"));
}

#[test]
fn test_script_execution() {
    let temp_dir = TempDir::new().unwrap();
    let script_path = temp_dir.path().join("test_script.m");

    // Create a simple MATLAB script
    fs::write(&script_path, "x = 1 + 2").unwrap();

    let output = run_runmat(&[script_path.to_str().unwrap()]);
    assert!(output.status.success());
}

#[test]
fn test_script_execution_with_run_command() {
    let temp_dir = TempDir::new().unwrap();
    let script_path = temp_dir.path().join("test_script2.m");

    // Create a simple MATLAB script
    fs::write(&script_path, "result = 5 * 10").unwrap();

    let output = run_runmat(&["run", script_path.to_str().unwrap()]);
    assert!(output.status.success());
}

#[test]
fn test_nonexistent_script() {
    let output = run_runmat(&["run", "/nonexistent/script.m"]);
    assert!(!output.status.success());

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("Failed to read") || stderr.contains("No such file"));
}

#[test]
fn test_invalid_gc_preset() {
    let output = run_runmat(&["--gc-preset", "invalid-preset", "info"]);
    assert!(!output.status.success());
}

#[test]
fn test_invalid_jit_opt_level() {
    let output = run_runmat(&["--jit-opt-level", "invalid-level", "info"]);
    assert!(!output.status.success());
}

#[test]
fn test_invalid_log_level() {
    let output = run_runmat(&["--log-level", "invalid-level", "info"]);
    assert!(!output.status.success());
}

#[test]
fn test_combined_flags() {
    let output = run_runmat(&[
        "--gc-preset",
        "low-latency",
        "--jit-opt-level",
        "aggressive",
        "--gc-stats",
        "--verbose",
        "info",
    ]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("GC Preset"));
    assert!(stdout.contains("JIT Optimization"));
    assert!(stdout.contains("GC Statistics: true"));
}

#[test]
fn test_conflicting_script_and_command() {
    let temp_dir = TempDir::new().unwrap();
    let script_path = temp_dir.path().join("test.m");
    fs::write(&script_path, "x = 1").unwrap();

    // Should fail when both script and command are provided
    let output = run_runmat(&["info", script_path.to_str().unwrap()]);
    assert!(!output.status.success());

    let stderr = String::from_utf8_lossy(&output.stderr);
    // Clap detects unexpected argument before our validation runs
    assert!(stderr.contains("unexpected argument") || stderr.contains("Cannot specify both"));
}

#[test]
fn test_benchmark_command_without_file() {
    let output = run_runmat(&["benchmark"]);
    assert!(!output.status.success());
}

#[test]
fn test_benchmark_with_nonexistent_file() {
    let output = run_runmat(&["benchmark", "/nonexistent/file.m"]);
    assert!(!output.status.success());
}

#[test]
fn test_benchmark_with_valid_file() {
    let temp_dir = TempDir::new().unwrap();
    let script_path = temp_dir.path().join("benchmark_test.m");

    // Create a simple script for benchmarking
    fs::write(&script_path, "y = 2 + 3").unwrap();

    let output = run_runmat(&[
        "benchmark",
        script_path.to_str().unwrap(),
        "--iterations",
        "3",
        "--jit",
    ]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Benchmark Results"));
    assert!(stdout.contains("iterations"));
    assert!(stdout.contains("Average time"));
}

#[test]
fn test_repl_command() {
    // Note: This test just verifies the REPL command starts without immediate error
    // We can't easily test interactive input without complex setup

    // Test that help works for repl command
    let output = run_runmat(&["repl", "--help"]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("verbose"));
}

#[test]
fn test_kernel_command_help() {
    let output = run_runmat(&["kernel", "--help"]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Jupyter kernel"));
    assert!(stdout.contains("--ip"));
    assert!(stdout.contains("--key"));
}

#[test]
fn test_log_level_configurations() {
    let log_levels = ["error", "warn", "info", "debug", "trace"];

    for level in &log_levels {
        let output = run_runmat(&["--log-level", level, "info"]);
        assert!(output.status.success(), "Failed with log level: {level}");
    }
}

#[test]
fn test_invalid_gc_young_size() {
    let output = run_runmat(&["--gc-young-size", "invalid", "info"]);
    assert!(!output.status.success());
}

#[test]
fn test_invalid_gc_threads() {
    let output = run_runmat(&["--gc-threads", "invalid", "info"]);
    assert!(!output.status.success());
}

#[test]
fn test_invalid_jit_threshold() {
    let output = run_runmat(&["--jit-threshold", "invalid", "info"]);
    assert!(!output.status.success());
}

#[test]
fn test_invalid_timeout() {
    let output = run_runmat(&["--timeout", "invalid", "info"]);
    assert!(!output.status.success());
}

#[test]
fn test_script_with_syntax_error() {
    let temp_dir = TempDir::new().unwrap();
    let script_path = temp_dir.path().join("syntax_error.m");

    // Create a script with syntax error
    fs::write(&script_path, "x = [1, 2,").unwrap(); // Incomplete matrix

    let output = run_runmat(&["run", script_path.to_str().unwrap()]);
    assert!(!output.status.success());

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("error") || stderr.contains("failed"));
}

#[test]
fn test_help_shows_environment_variables() {
    let output = run_runmat(&["--help"]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Environment Variables"));
    assert!(stdout.contains("RUSTMAT_DEBUG"));
    assert!(stdout.contains("RUSTMAT_GC_PRESET"));
    assert!(stdout.contains("RUSTMAT_JIT_ENABLE"));
}

#[test]
fn test_command_output_is_structured() {
    let output = run_runmat(&["info"]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Check for structured sections
    assert!(stdout.contains("=========================="));
    assert!(stdout.contains("Version:"));
    assert!(stdout.contains("Runtime Configuration:"));
    assert!(stdout.contains("Environment:"));
    assert!(stdout.contains("Available Commands:"));
}

#[test]
fn test_gc_config_command() {
    let output = run_runmat(&["gc", "config"]);
    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("GC Configuration") || stdout.contains("not yet implemented"));
}

#[test]
fn test_edge_case_empty_script() {
    let temp_dir = TempDir::new().unwrap();
    let script_path = temp_dir.path().join("empty.m");

    // Create an empty script
    fs::write(&script_path, "").unwrap();

    let output = run_runmat(&["run", script_path.to_str().unwrap()]);
    // Empty script behavior may vary - some parsers accept empty input
    // Just ensure it doesn't crash
    assert!(
        output.status.success() || !output.status.success(),
        "Should handle empty script gracefully"
    );
}

#[test]
fn test_script_with_complex_operations() {
    let temp_dir = TempDir::new().unwrap();
    let script_path = temp_dir.path().join("complex.m");

    // Create a script with multiple operations
    fs::write(
        &script_path,
        r#"
x = 1 + 2
y = [1, 2; 3, 4]
z = x * 5
"#,
    )
    .unwrap();

    let output = run_runmat(&["run", script_path.to_str().unwrap()]);
    assert!(output.status.success());
}