Skip to content

Commit

Permalink
Added emtests
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Dec 11, 2018
1 parent 44a745e commit 5796b17
Show file tree
Hide file tree
Showing 15 changed files with 8,450 additions and 75 deletions.
61 changes: 58 additions & 3 deletions build/emtests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,79 @@
//! - Compile using emcc and get the .wasm from it (wasm)
//! - Generate the test that will compare the output of running the .wasm file
//! with wasmer with the expected output
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process::Command;


static BANNER: &str = "// Rust test file autogenerated with cargo build (build/emtests.rs).
// Please do NOT modify it by hand, as it will be reseted on next build.\n";

const TESTS: [&str; 1] = [
"emtests/localtime.c"
"emtests/printf.c"
];

pub fn compile(file: &str) -> String {
let mut output_path = PathBuf::from(file);
output_path.set_extension("out");
let output_str = output_path.to_str().unwrap();

// Compile to .out
Command::new("cc")
.arg(file)
.arg("-o")
.arg(output_str)
.output()
.expect("failed to execute process");

// Get the result of .out
let output = Command::new(output_str)
.arg(output_str)
.output()
.expect("failed to execute process");

// Remove executable
fs::remove_file(output_str).unwrap();

let mut output_path = PathBuf::from(file);
output_path.set_extension("js");
let output_str = output_path.to_str().unwrap();

// Compile to wasm
Command::new("emcc")
.arg(file)
.arg("-s").arg("WASM=1")
.arg("-o")
.arg(output_str)
.output()
.expect("failed to execute process");

// Remove js file
fs::remove_file(output_str).unwrap();

let mut output_path = PathBuf::from(file);
output_path.set_extension("output");
let module_name = output_path.file_stem().unwrap().to_str().unwrap().to_owned();

let output_str = output_path.to_str().unwrap();

// Write the output to file
fs::write(output_str, output.stdout).expect("Unable to write file");

module_name

// panic!("OUTPUT: {:?}", output);
}

pub fn build() {
let rust_test_modpath = concat!(env!("CARGO_MANIFEST_DIR"), "/src/emtests/mod.rs");

let mut modules: Vec<String> = Vec::new();
// modules.reserve_exact(TESTS.len());

for test in TESTS.iter() {
let moudle_name = compile(test);
modules.push(format!("mod {};", moudle_name));
}
modules.insert(0, BANNER.to_string());
// We add an empty line
modules.push("".to_string());
Expand Down
12 changes: 12 additions & 0 deletions emtests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ This directory contains tests for unit testing each of the functions/syscalls th
If you want to generate the wasm files, you will just need to:

```
make emtests
```

This process will do something similar to:

```
cc localtime.c -o localtime.out
# Execute the out file and save its output
./localtime.out > ./localtime.output
rm localtime.out
# Generate the .wasm file
emcc localtime.c -o localtime.js
# Delte the js file, as we don't need it
rm localtime.js
Expand Down
5 changes: 0 additions & 5 deletions emtests/localtime.output

This file was deleted.

Loading

0 comments on commit 5796b17

Please sign in to comment.