Skip to content

Introduce some snapshot tests of c2rust-transpile #1246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions c2rust-transpile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ tempfile = "3.5.0"
[features]
# Force static linking of LLVM
llvm-static = ["c2rust-ast-exporter/llvm-static"]

[dev-dependencies]
insta = { version = "1.15", features = ["glob"] }
2 changes: 1 addition & 1 deletion c2rust-transpile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type CrateSet = indexmap::IndexSet<ExternCrate>;
type TranspileResult = Result<(PathBuf, PragmaVec, CrateSet), ()>;

/// Configuration settings for the translation process
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct TranspilerConfig {
// Debug output options
pub dump_untyped_context: bool,
Expand Down
50 changes: 50 additions & 0 deletions c2rust-transpile/tests/snapshots.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#[test]
fn transpile() {
let config = c2rust_transpile::TranspilerConfig {
dump_untyped_context: false,
dump_typed_context: false,
pretty_typed_context: false,
dump_function_cfgs: false,
json_function_cfgs: false,
dump_cfg_liveness: false,
dump_structures: false,
verbose: false,
debug_ast_exporter: false,
incremental_relooper: true,
fail_on_multiple: false,
filter: None,
debug_relooper_labels: false,
prefix_function_names: None,
translate_asm: true,
use_c_loop_info: true,
use_c_multiple_info: true,
simplify_structures: true,
panic_on_translator_failure: false,
emit_modules: false,
fail_on_error: false,
replace_unsupported_decls: c2rust_transpile::ReplaceMode::Extern,
translate_valist: true,
overwrite_existing: true,
reduce_type_annotations: false,
reorganize_definitions: false,
enabled_warnings: Default::default(),
emit_no_std: false,
output_dir: None,
translate_const_macros: false,
translate_fn_macros: false,
disable_refactoring: false,
preserve_unused_functions: false,
log_level: log::LevelFilter::Warn,
emit_build_files: false,
binaries: Vec::new(),
};
insta::glob!("snapshots/*.c", |f: &std::path::Path| {
let (_temp_dir, temp_path) =
c2rust_transpile::create_temp_compile_commands(&[f.to_owned()]);
c2rust_transpile::transpile(config.clone(), &temp_path, &[]);
let output_path = f.with_extension("rs");
let output = std::fs::read_to_string(&output_path).unwrap();
std::fs::remove_file(&output_path).unwrap();
insta::assert_snapshot!(&output);
});
}
7 changes: 7 additions & 0 deletions c2rust-transpile/tests/snapshots/factorial.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
unsigned short factorial(unsigned short n) {
unsigned short result = 1;
for (unsigned short i = 1; i < n; i++) {
result *= i;
}
return result;
}
24 changes: 24 additions & 0 deletions c2rust-transpile/tests/snapshots/gotos.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
int sum(int count) {
goto a;

b:
--count;
goto d;

a:;
int x = 0;
goto d;

c:
return x;

d:
if(count <= 0)
goto c;
goto e;

e:
x += count;
goto b;
}

11 changes: 11 additions & 0 deletions c2rust-transpile/tests/snapshots/insertion.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
void insertion_sort(int const n, int * const p) {
for (int i = 1; i < n; i++) {
int const tmp = p[i];
int j = i;
while (j > 0 && p[j-1] > tmp) {
p[j] = p[j-1];
j--;
}
p[j] = tmp;
}
}
26 changes: 26 additions & 0 deletions c2rust-transpile/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
source: c2rust-transpile/tests/snapshots.rs
expression: "&output"
input_file: c2rust-transpile/tests/snapshots/factorial.c
---
#![allow(
dead_code,
mutable_transmutes,
non_camel_case_types,
non_snake_case,
non_upper_case_globals,
unused_assignments,
unused_mut
)]
#[no_mangle]
pub unsafe extern "C" fn factorial(mut n: std::ffi::c_ushort) -> std::ffi::c_ushort {
let mut result: std::ffi::c_ushort = 1 as std::ffi::c_int as std::ffi::c_ushort;
let mut i: std::ffi::c_ushort = 1 as std::ffi::c_int as std::ffi::c_ushort;
while (i as std::ffi::c_int) < n as std::ffi::c_int {
result = (result as std::ffi::c_int * i as std::ffi::c_int)
as std::ffi::c_ushort;
i = i.wrapping_add(1);
i;
}
return result;
}
24 changes: 24 additions & 0 deletions c2rust-transpile/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
source: c2rust-transpile/tests/snapshots.rs
expression: "&output"
input_file: c2rust-transpile/tests/snapshots/gotos.c
---
#![allow(
dead_code,
mutable_transmutes,
non_camel_case_types,
non_snake_case,
non_upper_case_globals,
unused_assignments,
unused_mut
)]
#[no_mangle]
pub unsafe extern "C" fn sum(mut count: std::ffi::c_int) -> std::ffi::c_int {
let mut x: std::ffi::c_int = 0 as std::ffi::c_int;
while !(count <= 0 as std::ffi::c_int) {
x += count;
count -= 1;
count;
}
return x;
}
32 changes: 32 additions & 0 deletions c2rust-transpile/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
source: c2rust-transpile/tests/snapshots.rs
expression: "&output"
input_file: c2rust-transpile/tests/snapshots/insertion.c
---
#![allow(
dead_code,
mutable_transmutes,
non_camel_case_types,
non_snake_case,
non_upper_case_globals,
unused_assignments,
unused_mut
)]
#[no_mangle]
pub unsafe extern "C" fn insertion_sort(n: std::ffi::c_int, p: *mut std::ffi::c_int) {
let mut i: std::ffi::c_int = 1 as std::ffi::c_int;
while i < n {
let tmp: std::ffi::c_int = *p.offset(i as isize);
let mut j: std::ffi::c_int = i;
while j > 0 as std::ffi::c_int
&& *p.offset((j - 1 as std::ffi::c_int) as isize) > tmp
{
*p.offset(j as isize) = *p.offset((j - 1 as std::ffi::c_int) as isize);
j -= 1;
j;
}
*p.offset(j as isize) = tmp;
i += 1;
i;
}
}
Loading