summaryrefslogtreecommitdiff
path: root/crates/tinywasm/tests/mvp.rs
blob: c6fe43d6e5958bf5a51274c7672dc8ef204c919b (plain)
use std::{
    collections::BTreeMap,
    fmt::{Debug, Formatter},
};

use eyre::{eyre, Result};
use log::debug;
use tinywasm_types::TinyWasmModule;
use wast::{
    lexer::Lexer,
    parser::{self, ParseBuffer},
    QuoteWat, Wast,
};

fn parse_module(mut module: wast::core::Module) -> Result<TinyWasmModule> {
    let parser = tinywasm_parser::Parser::new();
    Ok(parser.parse_module_bytes(module.encode().expect("failed to encode module"))?)
}

#[test]
#[ignore]
fn test_mvp() -> Result<()> {
    let mut test_suite = TestSuite::new();

    wasm_testsuite::MVP_TESTS.iter().for_each(|group| {
        let test_group = test_suite.test_group(group);

        let wast = wasm_testsuite::get_test_wast(group).expect("failed to get test wast");
        let wast = std::str::from_utf8(&wast).expect("failed to convert wast to utf8");

        let mut lexer = Lexer::new(wast);
        // we need to allow confusing unicode characters since they are technically valid wasm
        lexer.allow_confusing_unicode(true);

        let buf = ParseBuffer::new_with_lexer(lexer).expect("failed to create parse buffer");
        let wast_data = parser::parse::<Wast>(&buf).expect("failed to parse wat");

        let mut last_module: Option<TinyWasmModule> = None;
        for (i, directive) in wast_data.directives.into_iter().enumerate() {
            let span = directive.span();
            use wast::WastDirective::*;
            let name = format!("{}-{}", group, i);

            match directive {
                // TODO: needs to support more binary sections
                Wat(QuoteWat::Wat(wast::Wat::Module(module))) => {
                    let result = std::panic::catch_unwind(|| parse_module(module))
                        .map_err(|e| eyre!("failed to parse module: {:?}", e))
                        .and_then(|res| res);

                    match &result {
                        Err(_) => last_module = None,
                        Ok(m) => last_module = Some(m.clone()),
                    }

                    test_group.add_result(&format!("{}-parse", name), span, result.map(|_| ()));
                }

                // these all pass already :)
                AssertMalformed {
                    span,
                    module: QuoteWat::Wat(wast::Wat::Module(module)),
                    message: _,
                } => {
                    let res = std::panic::catch_unwind(|| parse_module(module).map(|_| ()));
                    test_group.add_result(
                        &format!("{}-malformed", name),
                        span,
                        match res {
                            Ok(Ok(_)) => Err(eyre!("expected module to be malformed")),
                            Err(_) | Ok(Err(_)) => Ok(()),
                        },
                    );
                }
                AssertReturn { span, exec, results: _ } => {
                    let Some(module) = last_module.as_ref() else {
                        // println!("no module found for assert_return: {:?}", exec);
                        continue;
                    };

                    let res: Result<Result<()>, _> = std::panic::catch_unwind(|| {
                        let mut store = tinywasm::Store::new();
                        let module = tinywasm::Module::from(module);
                        let instance = module.instantiate(&mut store)?;

                        use wast::WastExecute::*;
                        match exec {
                            Wat(_) => return Result::Ok(()), // not used by the testsuite
                            Get { module: _, global: _ } => return Result::Ok(()),
                            Invoke(invoke) => {
                                for arg in invoke.args {
                                    let arg = get_tinywasm_wasm_value(arg)?;
                                    let _res = instance.get_func(&store, invoke.name)?.call(&mut store, &[arg])?;
                                    // TODO: check the result
                                }
                            }
                        }

                        Ok(())
                    });

                    let res = match res {
                        Err(e) => Err(eyre!("test panicked: {:?}", e)),
                        Ok(Err(e)) => Err(e),
                        Ok(Ok(())) => Ok(()),
                    };

                    test_group.add_result(&format!("{}-return", name), span, res);
                }
                Invoke(m) => {
                    debug!("invoke: {:?}", m);
                }
                // _ => test_group.add_result(
                //     &format!("{}-unknown", name),
                //     span,
                //     Err(Error::Other("test not implemented".to_string())),
                // ),
                // TODO: implement more test directives
                _ => {}
            }
        }
    });

    if test_suite.failed() {
        eprintln!("\n\nfailed one or more tests:\n{:#?}", test_suite);
        Err(eyre!("failed one or more tests"))
    } else {
        println!("\n\npassed all tests:\n{:#?}", test_suite);
        Ok(())
    }
}

fn get_tinywasm_wasm_value(arg: wast::WastArg) -> Result<tinywasm_types::WasmValue> {
    let wast::WastArg::Core(arg) = arg else {
        return Err(eyre!("unsupported arg type"));
    };

    use tinywasm_types::WasmValue;
    use wast::core::WastArgCore::*;
    Ok(match arg {
        F32(f) => WasmValue::F32(f32::from_bits(f.bits)),
        F64(f) => WasmValue::F64(f64::from_bits(f.bits)),
        I32(i) => WasmValue::I32(i),
        I64(i) => WasmValue::I64(i),
        _ => return Err(eyre!("unsupported arg type")),
    })
}

struct TestSuite(BTreeMap<String, TestGroup>);

impl TestSuite {
    fn new() -> Self {
        Self(BTreeMap::new())
    }

    fn failed(&self) -> bool {
        self.0.values().any(|group| group.stats().1 > 0)
    }

    fn test_group(&mut self, name: &str) -> &mut TestGroup {
        self.0.entry(name.to_string()).or_insert_with(TestGroup::new)
    }
}

impl Debug for TestSuite {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        use owo_colors::OwoColorize;
        let mut total_passed = 0;
        let mut total_failed = 0;

        for (group_name, group) in &self.0 {
            let (group_passed, group_failed) = group.stats();
            total_passed += group_passed;
            total_failed += group_failed;

            writeln!(f, "{}", group_name.bold().underline())?;
            writeln!(f, "  Tests Passed: {}", group_passed.to_string().green())?;
            writeln!(f, "  Tests Failed: {}", group_failed.to_string().red())?;

            // for (test_name, test) in &group.tests {
            //     write!(f, "    {}: ", test_name.bold())?;
            //     match &test.result {
            //         Ok(()) => {
            //             writeln!(f, "{}", "Passed".green())?;
            //         }
            //         Err(e) => {
            //             writeln!(f, "{}", "Failed".red())?;
            //             // writeln!(f, "Error: {:?}", e)?;
            //         }
            //     }
            //     writeln!(f, "      Span: {:?}", test.span)?;
            // }
        }

        writeln!(f, "\n{}", "Total Test Summary:".bold().underline())?;
        writeln!(f, "  Total Tests: {}", (total_passed + total_failed))?;
        writeln!(f, "  Total Passed: {}", total_passed.to_string().green())?;
        writeln!(f, "  Total Failed: {}", total_failed.to_string().red())?;
        Ok(())
    }
}

struct TestGroup {
    tests: BTreeMap<String, TestCase>,
}

impl TestGroup {
    fn new() -> Self {
        Self { tests: BTreeMap::new() }
    }

    fn stats(&self) -> (usize, usize) {
        let mut passed_count = 0;
        let mut failed_count = 0;

        for test in self.tests.values() {
            match test.result {
                Ok(()) => passed_count += 1,
                Err(_) => failed_count += 1,
            }
        }

        (passed_count, failed_count)
    }

    fn add_result(&mut self, name: &str, span: wast::token::Span, result: Result<()>) {
        self.tests.insert(name.to_string(), TestCase { result, _span: span });
    }
}

struct TestCase {
    result: Result<()>,
    _span: wast::token::Span,
}