use decy_core::transpile;
#[test]
fn test_for_loop_array_iteration() {
let c_code = r#"
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += numbers[i];
}
return sum;
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
assert!(!result.is_empty(), "Should generate code");
let unsafe_count = result.matches("unsafe").count();
assert!(unsafe_count <= 3, "Array iteration should minimize unsafe (found {})", unsafe_count);
}
#[test]
fn test_for_loop_array_modification() {
let c_code = r#"
int main() {
int values[10];
for (int i = 0; i < 10; i++) {
values[i] = i * 2;
}
return values[5];
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(
unsafe_count <= 3,
"Array modification should minimize unsafe (found {})",
unsafe_count
);
}
#[test]
fn test_for_loop_with_array_bounds() {
let c_code = r#"
#define ARRAY_SIZE 8
int main() {
int data[ARRAY_SIZE];
int total = 0;
for (int i = 0; i < ARRAY_SIZE; i++) {
data[i] = i + 1;
total += data[i];
}
return total;
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(unsafe_count <= 3, "Bounded loop should minimize unsafe (found {})", unsafe_count);
}
#[test]
fn test_while_loop_array_access() {
let c_code = r#"
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
int i = 0;
int sum = 0;
while (i < 5) {
sum += numbers[i];
i++;
}
return sum;
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(
unsafe_count <= 3,
"While loop array access should minimize unsafe (found {})",
unsafe_count
);
}
#[test]
fn test_while_loop_array_search() {
let c_code = r#"
int main() {
int values[10] = {5, 3, 8, 1, 9, 2, 7, 4, 6, 0};
int i = 0;
int target = 7;
int found = -1;
while (i < 10) {
if (values[i] == target) {
found = i;
break;
}
i++;
}
return found;
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(unsafe_count <= 4, "Search pattern should minimize unsafe (found {})", unsafe_count);
}
#[test]
fn test_nested_loop_2d_array() {
let c_code = r#"
int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int sum = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum += matrix[i][j];
}
}
return sum;
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(
unsafe_count <= 5,
"Nested loops with 2D array should minimize unsafe (found {})",
unsafe_count
);
}
#[test]
fn test_array_out_of_bounds_prevention() {
let c_code = r#"
int main() {
int buffer[5];
// Safe because loop condition matches array size
for (int i = 0; i < 5; i++) {
buffer[i] = i * 10;
}
return buffer[0];
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
let lines = result.lines().count();
let unsafe_per_1000 =
if lines > 0 { (unsafe_count as f64 / lines as f64) * 1000.0 } else { 0.0 };
assert!(
unsafe_per_1000 < 50.0,
"Array bounds should be safe (got {:.2} unsafe/1000 LOC)",
unsafe_per_1000
);
}
#[test]
fn test_array_initialization_in_loop() {
let c_code = r#"
int main() {
int array[100];
for (int i = 0; i < 100; i++) {
array[i] = 0;
}
return 0;
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(
unsafe_count <= 3,
"Array initialization should minimize unsafe (found {})",
unsafe_count
);
}
#[test]
fn test_array_copy_loop() {
let c_code = r#"
int main() {
int source[5] = {1, 2, 3, 4, 5};
int dest[5];
for (int i = 0; i < 5; i++) {
dest[i] = source[i];
}
return dest[4];
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(unsafe_count <= 4, "Array copy should minimize unsafe (found {})", unsafe_count);
}
#[test]
fn test_array_reverse_loop() {
let c_code = r#"
int main() {
int numbers[6] = {1, 2, 3, 4, 5, 6};
for (int i = 0; i < 3; i++) {
int temp = numbers[i];
numbers[i] = numbers[5 - i];
numbers[5 - i] = temp;
}
return numbers[0];
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(unsafe_count <= 5, "Array reverse should minimize unsafe (found {})", unsafe_count);
}
#[test]
fn test_array_max_find_loop() {
let c_code = r#"
int main() {
int values[8] = {23, 45, 12, 67, 34, 89, 56, 78};
int max = values[0];
for (int i = 1; i < 8; i++) {
if (values[i] > max) {
max = values[i];
}
}
return max;
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(unsafe_count <= 4, "Max find should minimize unsafe (found {})", unsafe_count);
}
#[test]
fn test_empty_loop_array() {
let c_code = r#"
int main() {
int array[5] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < 0; i++) {
sum += array[i];
}
return sum; // Should be 0
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(unsafe_count <= 2, "Empty loop should minimize unsafe (found {})", unsafe_count);
}
#[test]
fn test_single_element_array_loop() {
let c_code = r#"
int main() {
int single[1] = {42};
int value = 0;
for (int i = 0; i < 1; i++) {
value = single[i];
}
return value;
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(
unsafe_count <= 2,
"Single element loop should minimize unsafe (found {})",
unsafe_count
);
}
#[test]
fn test_unsafe_minimization_target() {
let c_code = r#"
int main() {
int data[20];
// Initialize
for (int i = 0; i < 20; i++) {
data[i] = i * i;
}
// Sum
int total = 0;
for (int i = 0; i < 20; i++) {
total += data[i];
}
return total;
}
"#;
let result = transpile(c_code).expect("Should transpile");
let unsafe_count = result.matches("unsafe").count();
let lines_of_code = result.lines().count();
let unsafe_per_1000 =
if lines_of_code > 0 { (unsafe_count as f64 / lines_of_code as f64) * 1000.0 } else { 0.0 };
assert!(
unsafe_per_1000 < 50.0,
"Loop+array pattern should minimize unsafe (got {:.2} per 1000 LOC, want <50)",
unsafe_per_1000
);
assert!(result.contains("fn main"), "Should generate main function");
}
#[test]
fn test_transpiled_loop_array_compiles() {
let c_code = r#"
int main() {
int numbers[10];
for (int i = 0; i < 10; i++) {
numbers[i] = i * 2;
}
int result = 0;
for (int i = 0; i < 10; i++) {
result += numbers[i];
}
return result;
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(!result.is_empty(), "Should generate non-empty code");
assert!(result.contains("fn main"), "Should have main function");
let open_braces = result.matches('{').count();
let close_braces = result.matches('}').count();
assert_eq!(
open_braces, close_braces,
"Braces should be balanced: {} open, {} close",
open_braces, close_braces
);
assert!(!result.contains(";;;;"), "Should not have excessive semicolons");
}
#[test]
fn test_loop_array_safety_documentation() {
let c_code = r#"
int main() {
int array[5] = {10, 20, 30, 40, 50};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += array[i];
}
return sum;
}
"#;
let result = transpile(c_code).expect("Should transpile");
assert!(result.contains("fn main"), "Should have main function");
let unsafe_count = result.matches("unsafe").count();
assert!(unsafe_count < 10, "Should have minimal unsafe blocks (found {})", unsafe_count);
}