Skip to content

Commit

Permalink
[llvm] Add test to compile and run a simple fibonacci program
Browse files Browse the repository at this point in the history
  • Loading branch information
vaivaswatha committed Jul 7, 2024
1 parent fb3882c commit 545950d
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ syn = { version = "2.0.66", features = ["derive"] }
combine = "4.6.7"
thiserror = "1.0.61"
linkme = "0.3"

tempfile = "3"
2 changes: 2 additions & 0 deletions pliron-llvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ thiserror.workspace = true
linkme.workspace = true
inkwell = { version = "0", features = ["llvm17-0"] }
rustc-hash.workspace = true
assert_cmd = { version = "2" }

[dev-dependencies]
expect-test.workspace = true
tempfile.workspace = true
90 changes: 90 additions & 0 deletions pliron-llvm/tests/compile_run.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//! Tests that compile code and run it.
use std::path::PathBuf;

use assert_cmd::Command;
use expect_test::expect;
use pliron::Lazy;
use tempfile::tempdir;

static RESOURCES_DIR: pliron::Lazy<PathBuf> = Lazy::new(|| {
[env!("CARGO_MANIFEST_DIR"), "tests", "resources"]
.iter()
.collect()
});

#[test]
fn test_fib() {
// Create a tempdir() to place the temporary compiled files.
let tmp_dir = tempdir().unwrap();

// clang -c -emit-llvm -o $tmp/fib.bc tests/resources/fib.c
let mut cmd = Command::new("clang-17");
let compile_fib = cmd
.current_dir(&*RESOURCES_DIR)
.args([
"-c",
"-emit-llvm",
"-o",
tmp_dir.path().join("fib.bc").to_str().unwrap(),
"fib.c",
])
.output()
.expect("failed to execute clang to compile fib.c");
assert!(
compile_fib.status.success(),
"{}",
String::from_utf8(compile_fib.stderr).unwrap()
);

// llvm-opt -S -i $tmp/fib.bc -o $tmp/fib.opt.ll
let mut cmd = Command::cargo_bin("llvm-opt").unwrap();
let compile_fib = cmd
.current_dir(&*RESOURCES_DIR)
.args([
"-S",
"-i",
tmp_dir.path().join("fib.bc").to_str().unwrap(),
"-o",
tmp_dir.path().join("fib.out.ll").to_str().unwrap(),
])
.output()
.expect("failed to execute llvm-opt to compile fib.bc to fib.out.ll");
assert!(
compile_fib.status.success(),
"{}",
String::from_utf8(compile_fib.stderr).unwrap()
);

// clang -o $tmp/fib $tmp/fib.out.ll tests/resources/fib-main.c
let mut cmd = Command::new("clang-17");
let compile_fib = cmd
.current_dir(&*RESOURCES_DIR)
.args([
"-o",
tmp_dir.path().join("fib").to_str().unwrap(),
tmp_dir.path().join("fib.out.ll").to_str().unwrap(),
"fib-main.c",
])
.output()
.expect("failed to execute clang to compile fib-main.c");
assert!(
compile_fib.status.success(),
"{}",
String::from_utf8(compile_fib.stderr).unwrap()
);

// $tmp/fib
let mut cmd = Command::new(tmp_dir.path().join("fib"));
let run_fib = cmd.output().expect("Error executing compiled fib program");
assert!(run_fib.status.success());
let fib_output = String::from_utf8(run_fib.stdout).unwrap();
expect![[r#"
fib(0): 0
fib(1): 0
fib(2): 1
fib(3): 1
fib(4): 2
"#]]
.assert_eq(&fib_output);
}
14 changes: 14 additions & 0 deletions pliron-llvm/tests/resources/fib-main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdio.h>

extern int fib(int n);

int main() {

printf("fib(0): %d\n", fib(0));
printf("fib(1): %d\n", fib(1));
printf("fib(2): %d\n", fib(2));
printf("fib(3): %d\n", fib(3));
printf("fib(4): %d\n", fib(4));

return 0;
}
19 changes: 19 additions & 0 deletions pliron-llvm/tests/resources/fib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
int fib(int n) {
if (n == 1) {
return 0;
}
if (n == 2) {
return 1;
}

int prev2 = 0;
int prev1 = 1;

int cur;
for (int i = 3; i <= n; i++) {
cur = prev1 + prev2;
prev2 = prev1;
prev1 = cur;
}
return cur;
}

0 comments on commit 545950d

Please sign in to comment.