From 221d3b9485a1d84fc4d9f06d864242d3c393d0ba Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Sun, 18 Sep 2022 12:38:53 -0400 Subject: test(tvix/eval): Add proof-of-concept test for Chunk This is pretty boring at the moment, but mostly serves as a foot in the door in the direction of writing more tests Change-Id: Id88eb4ec7e53ebb2d5b5c254c8f45ff750238811 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6637 Autosubmit: grfn Tested-by: BuildkiteCI Reviewed-by: tazjin --- tvix/eval/src/chunk.rs | 14 ++++++++++++++ tvix/eval/src/lib.rs | 2 ++ tvix/eval/src/opcode.rs | 8 ++++---- tvix/eval/src/test_utils.rs | 8 ++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 tvix/eval/src/test_utils.rs (limited to 'tvix/eval/src') diff --git a/tvix/eval/src/chunk.rs b/tvix/eval/src/chunk.rs index 052bf0bf69de..552a4bf6b6d4 100644 --- a/tvix/eval/src/chunk.rs +++ b/tvix/eval/src/chunk.rs @@ -154,3 +154,17 @@ impl Chunk { Ok(()) } } + +#[cfg(test)] +mod tests { + use crate::test_utils::dummy_span; + + use super::*; + + #[test] + fn push_op() { + let mut chunk = Chunk::default(); + chunk.push_op(OpCode::OpNull, dummy_span()); + assert_eq!(chunk.code.last().unwrap(), &OpCode::OpNull); + } +} diff --git a/tvix/eval/src/lib.rs b/tvix/eval/src/lib.rs index 34bb3b57e97c..1c3fea3d5b9f 100644 --- a/tvix/eval/src/lib.rs +++ b/tvix/eval/src/lib.rs @@ -13,6 +13,8 @@ mod warnings; #[cfg(test)] mod properties; #[cfg(test)] +mod test_utils; +#[cfg(test)] mod tests; // Re-export the public interface used by other crates. diff --git a/tvix/eval/src/opcode.rs b/tvix/eval/src/opcode.rs index 9d7fba9ee453..458ea6dd3feb 100644 --- a/tvix/eval/src/opcode.rs +++ b/tvix/eval/src/opcode.rs @@ -5,7 +5,7 @@ use std::ops::{AddAssign, Sub}; /// Index of a constant in the current code chunk. #[repr(transparent)] -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct ConstantIdx(pub usize); /// Index of an instruction in the current code chunk. @@ -39,17 +39,17 @@ pub struct UpvalueIdx(pub usize); /// Offset by which an instruction pointer should change in a jump. #[repr(transparent)] -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct JumpOffset(pub usize); /// Provided count for an instruction (could represent e.g. a number /// of elements). #[repr(transparent)] -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Count(pub usize); #[warn(variant_size_differences)] -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum OpCode { /// Push a constant onto the stack. OpConstant(ConstantIdx), diff --git a/tvix/eval/src/test_utils.rs b/tvix/eval/src/test_utils.rs new file mode 100644 index 000000000000..a7d1c3f96899 --- /dev/null +++ b/tvix/eval/src/test_utils.rs @@ -0,0 +1,8 @@ +use codemap::CodeMap; + +/// Create a dummy [`codemap::Span`] for use in tests +pub(crate) fn dummy_span() -> codemap::Span { + let mut codemap = CodeMap::new(); + let file = codemap.add_file("".to_owned(), "".to_owned()); + file.span +} -- cgit 1.4.1