Skip to content

Commit

Permalink
test(codegen): add tests for as_bytes_and_ptrs and has_const_value
Browse files Browse the repository at this point in the history
Wodann committed Oct 25, 2020
1 parent b27718b commit 3e66099
Showing 2 changed files with 62 additions and 3 deletions.
16 changes: 14 additions & 2 deletions crates/mun_codegen/src/mock.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,8 @@ use crate::{
db::{CodeGenDatabase, CodeGenDatabaseStorage},
OptimizationLevel,
};
use hir::{FileId, RelativePathBuf, SourceDatabase, SourceRoot, SourceRootId};
use hir::{FileId, HirDatabase, RelativePathBuf, SourceDatabase, SourceRoot, SourceRootId};
use mun_target::spec::Target;
use parking_lot::Mutex;
use std::sync::Arc;

@@ -15,7 +16,6 @@ use std::sync::Arc;
hir::HirDatabaseStorage,
CodeGenDatabaseStorage
)]
#[derive(Default)]
pub(crate) struct MockDatabase {
storage: salsa::Storage<Self>,
events: Mutex<Option<Vec<salsa::Event>>>,
@@ -60,6 +60,18 @@ impl hir::Upcast<dyn CodeGenDatabase> for MockDatabase {
}
}

impl Default for MockDatabase {
fn default() -> Self {
let mut db: MockDatabase = MockDatabase {
storage: Default::default(),
events: Default::default(),
};
db.set_optimization_level(OptimizationLevel::Default);
db.set_target(Target::host_target().unwrap());
db
}
}

impl MockDatabase {
/// Creates a database from the given text.
pub fn with_single_file(text: &str) -> (MockDatabase, FileId) {
49 changes: 48 additions & 1 deletion crates/mun_codegen/src/value/mod.rs
Original file line number Diff line number Diff line change
@@ -103,7 +103,7 @@ pub trait TransparentValue<'ink> {
/// Contains either a value converted to bytes or a pointer to the value.
///
/// This is used for generating constant enum types.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum BytesOrPtr<'ink> {
Bytes(Vec<u8>),
UntypedPtr(PointerValue<'ink>),
@@ -452,3 +452,50 @@ where
Value::from_raw(self.as_target_value(context).value)
}
}

#[cfg(test)]
mod tests {
use super::{AsBytesAndPtrs, HasConstValue};
use crate::{code_gen::CodeGenContext, mock::MockDatabase, value::IrTypeContext};
use bytemuck::from_bytes;
use std::mem::size_of;

macro_rules! test_as_bytes_and_ptrs_primitive {
($($ty:ty),+) => {
$(
paste::item! {
#[test]
fn [<test_has_const_value_ $ty>]() {
assert_eq!($ty::has_const_value(), true);
}
}
paste::item! {
#[test]
fn [<test_as_bytes_and_ptrs_ $ty>]() {
let bytes: Vec<u8> = (0..(size_of::<$ty>() as u8)).collect();
let value = from_bytes::<$ty>(&bytes);

let inkwell_context = inkwell::context::Context::create();

let db = MockDatabase::default();
let codegen_context = CodeGenContext::new(&inkwell_context, &db);

let target_data = codegen_context.target_machine.get_target_data();
let type_context = IrTypeContext {
context: &inkwell_context,
target_data: &target_data,
struct_types: &codegen_context.rust_types,
};

assert_eq!(
value.as_bytes_and_ptrs(&type_context),
vec![bytes.into()],
);
}
}
)+
};
}

test_as_bytes_and_ptrs_primitive!(u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);
}

0 comments on commit 3e66099

Please sign in to comment.