|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 3 | +// |
| 4 | +// Integration tests for Chapeliser CLI. |
| 5 | + |
| 6 | +use std::fs; |
| 7 | + |
| 8 | +#[test] |
| 9 | +fn test_init_creates_manifest() { |
| 10 | + let dir = tempfile::tempdir().unwrap(); |
| 11 | + let manifest = dir.path().join("chapeliser.toml"); |
| 12 | + |
| 13 | + chapeliser::manifest::init_manifest(dir.path().to_str().unwrap()).unwrap(); |
| 14 | + assert!(manifest.exists(), "chapeliser.toml should be created"); |
| 15 | + |
| 16 | + let content = fs::read_to_string(&manifest).unwrap(); |
| 17 | + assert!(content.contains("[workload]")); |
| 18 | + assert!(content.contains("partition")); |
| 19 | + assert!(content.contains("gather")); |
| 20 | +} |
| 21 | + |
| 22 | +#[test] |
| 23 | +fn test_validate_good_manifest() { |
| 24 | + let m = chapeliser::manifest::load_manifest("examples/panic-attacker/chapeliser.toml").unwrap(); |
| 25 | + chapeliser::manifest::validate(&m).unwrap(); |
| 26 | + assert_eq!(m.workload.name, "mass-panic"); |
| 27 | + assert_eq!(m.workload.partition, "per-item"); |
| 28 | + assert_eq!(m.workload.gather, "merge"); |
| 29 | + assert_eq!(m.scaling.max_nodes, 64); |
| 30 | +} |
| 31 | + |
| 32 | +#[test] |
| 33 | +fn test_generate_produces_files() { |
| 34 | + let dir = tempfile::tempdir().unwrap(); |
| 35 | + let output = dir.path().join("generated"); |
| 36 | + |
| 37 | + let m = chapeliser::manifest::load_manifest("examples/panic-attacker/chapeliser.toml").unwrap(); |
| 38 | + chapeliser::codegen::generate_all(&m, output.to_str().unwrap()).unwrap(); |
| 39 | + |
| 40 | + // Check all 4 artifacts exist |
| 41 | + assert!(output.join("chapel/mass_panic_distributed.chpl").exists()); |
| 42 | + assert!(output.join("zig/mass_panic_ffi.zig").exists()); |
| 43 | + assert!(output.join("include/mass_panic_chapeliser.h").exists()); |
| 44 | + assert!(output.join("build.sh").exists()); |
| 45 | + |
| 46 | + // Check Chapel file contains expected content |
| 47 | + let chpl = fs::read_to_string(output.join("chapel/mass_panic_distributed.chpl")).unwrap(); |
| 48 | + assert!(chpl.contains("coforall")); |
| 49 | + assert!(chpl.contains("mass_panic")); |
| 50 | + assert!(chpl.contains("c_process_item")); |
| 51 | + |
| 52 | + // Check Zig file contains expected exports |
| 53 | + let zig = fs::read_to_string(output.join("zig/mass_panic_ffi.zig")).unwrap(); |
| 54 | + assert!(zig.contains("export fn c_process_item")); |
| 55 | + assert!(zig.contains("mass_panic_process_item")); |
| 56 | + |
| 57 | + // Check C header contains guards and function declarations |
| 58 | + let h = fs::read_to_string(output.join("include/mass_panic_chapeliser.h")).unwrap(); |
| 59 | + assert!(h.contains("#ifndef CHAPELISER_MASS_PANIC_H")); |
| 60 | + assert!(h.contains("mass_panic_process_item")); |
| 61 | + assert!(h.contains("mass_panic_serialize")); |
| 62 | +} |
| 63 | + |
| 64 | +#[test] |
| 65 | +fn test_validate_rejects_bad_partition() { |
| 66 | + let toml = r#" |
| 67 | + [workload] |
| 68 | + name = "test" |
| 69 | + entry = "src/lib.rs::func" |
| 70 | + partition = "invalid_strategy" |
| 71 | + gather = "merge" |
| 72 | +
|
| 73 | + [data] |
| 74 | + input-type = "Vec<Item>" |
| 75 | + output-type = "Vec<Result>" |
| 76 | +
|
| 77 | + [scaling] |
| 78 | + min-nodes = 1 |
| 79 | + max-nodes = 4 |
| 80 | + grain-size = 10 |
| 81 | + "#; |
| 82 | + |
| 83 | + let m: chapeliser::manifest::Manifest = toml::from_str(toml).unwrap(); |
| 84 | + let result = chapeliser::manifest::validate(&m); |
| 85 | + assert!(result.is_err()); |
| 86 | + assert!(result.unwrap_err().to_string().contains("Unknown partition strategy")); |
| 87 | +} |
| 88 | + |
| 89 | +#[test] |
| 90 | +fn test_all_partition_strategies_generate() { |
| 91 | + let strategies = ["per-item", "chunk", "adaptive", "spatial", "keyed"]; |
| 92 | + |
| 93 | + for strategy in &strategies { |
| 94 | + let toml_str = format!(r#" |
| 95 | + [workload] |
| 96 | + name = "test-{strategy}" |
| 97 | + entry = "src/lib.rs::func" |
| 98 | + partition = "{strategy}" |
| 99 | + gather = "merge" |
| 100 | +
|
| 101 | + [data] |
| 102 | + input-type = "Vec<Item>" |
| 103 | + output-type = "Vec<Result>" |
| 104 | +
|
| 105 | + [scaling] |
| 106 | + min-nodes = 1 |
| 107 | + max-nodes = 4 |
| 108 | + grain-size = 10 |
| 109 | + "#); |
| 110 | + |
| 111 | + let m: chapeliser::manifest::Manifest = toml::from_str(&toml_str).unwrap(); |
| 112 | + chapeliser::manifest::validate(&m).unwrap(); |
| 113 | + |
| 114 | + let dir = tempfile::tempdir().unwrap(); |
| 115 | + chapeliser::codegen::generate_all(&m, dir.path().to_str().unwrap()) |
| 116 | + .unwrap_or_else(|e| panic!("Failed to generate for strategy {strategy}: {e}")); |
| 117 | + |
| 118 | + let safe_name = format!("test_{}", strategy.replace('-', "_")); |
| 119 | + let chpl_path = dir.path().join(format!("chapel/{safe_name}_distributed.chpl")); |
| 120 | + assert!(chpl_path.exists(), "Chapel file missing for strategy {strategy}: expected {}", chpl_path.display()); |
| 121 | + } |
| 122 | +} |
0 commit comments