Wasm: change TypeSection tests to unit tests rather than integration tests

This commit is contained in:
Brian Carroll 2022-01-08 15:17:52 +00:00
parent 8e7f398e50
commit d88b86e884
2 changed files with 44 additions and 20 deletions

View file

@ -752,22 +752,48 @@ impl<'a> WasmModule<'a> {
} }
} }
/// Assertion to run on the generated Wasm module in every test #[cfg(test)]
pub fn test_assert_preload<'a>(arena: &'a Bump, wasm_module: &WasmModule<'a>) { mod tests {
test_assert_types_preload(arena, &wasm_module.types); use super::*;
} use bumpalo::{self, collections::Vec, Bump};
fn test_assert_types_preload<'a>(arena: &'a Bump, original: &TypeSection<'a>) { fn test_assert_types_preload<'a>(arena: &'a Bump, original: &TypeSection<'a>) {
// Serialize the Type section that we built from Roc code // Serialize the Type section that we built from Roc code
let mut original_serialized = Vec::with_capacity_in(original.bytes.len() + 10, arena); let mut original_serialized = Vec::with_capacity_in(6 + original.bytes.len(), arena);
original.serialize(&mut original_serialized); original.serialize(&mut original_serialized);
debug_assert!(original_serialized[0] == SectionId::Type as u8); debug_assert!(original_serialized[0] == SectionId::Type as u8);
// Reconstruct a new TypeSection by "pre-loading" the bytes of the original! // Reconstruct a new TypeSection by "pre-loading" the bytes of the original!
let body = &original_serialized[6..]; let body = &original_serialized[6..];
let preloaded = TypeSection::preload(arena, body); let preloaded = TypeSection::preload(arena, body);
debug_assert_eq!(original.offsets, preloaded.offsets); debug_assert_eq!(original.offsets, preloaded.offsets);
debug_assert_eq!(original.bytes, preloaded.bytes); debug_assert_eq!(original.bytes, preloaded.bytes);
}
#[test]
fn test_type_section() {
use ValueType::*;
let arena = &Bump::new();
let signatures = [
Signature {
param_types: bumpalo::vec![in arena],
ret_type: None,
},
Signature {
param_types: bumpalo::vec![in arena; I32, I64, F32, F64],
ret_type: None,
},
Signature {
param_types: bumpalo::vec![in arena; I32, I32, I32],
ret_type: Some(I32),
},
];
let mut section = TypeSection::new(arena, signatures.len());
for sig in signatures {
section.insert(sig);
}
test_assert_types_preload(arena, &section);
}
} }

View file

@ -135,8 +135,6 @@ fn compile_roc_to_wasm_bytes<'a, T: Wasm32TestResult>(
T::insert_test_wrapper(arena, &mut wasm_module, TEST_WRAPPER_NAME, main_fn_index); T::insert_test_wrapper(arena, &mut wasm_module, TEST_WRAPPER_NAME, main_fn_index);
roc_gen_wasm::wasm_module::sections::test_assert_preload(arena, &wasm_module);
let needs_linking = !wasm_module.import.entries.is_empty(); let needs_linking = !wasm_module.import.entries.is_empty();
let mut app_module_bytes = std::vec::Vec::with_capacity(4096); let mut app_module_bytes = std::vec::Vec::with_capacity(4096);