diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-04-05 22:19:15 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-04-05 22:19:15 +0100 |
| commit | c290560c84e06d3ccff1c983dd577923ff9af78f (patch) | |
| tree | e414373b2dd6291765e5244ec11cdbabda8f8823 | |
| parent | fbd61014d3fcf520359a2b9906cdaacc801c2d5c (diff) | |
Split out tests for evaluating int values
| -rw-r--r-- | patio-to-cranelift/src/codegen/values.rs | 41 |
1 files changed, 26 insertions, 15 deletions
diff --git a/patio-to-cranelift/src/codegen/values.rs b/patio-to-cranelift/src/codegen/values.rs index ac2c5682..abc43459 100644 --- a/patio-to-cranelift/src/codegen/values.rs +++ b/patio-to-cranelift/src/codegen/values.rs @@ -75,10 +75,7 @@ pub(crate) fn eval_int_value(value: &PatioIntValue, target_triple: &target_lexic mod data_value_to_bytes_test { use std::str::FromStr; - use crate::patio::{ - types::PatioDataLayout, - values::{PatioDataValue, PatioIntValue}, - }; + use crate::patio::values::{PatioDataValue, PatioIntValue}; fn data_value_to_bytes(data_value: PatioDataValue) -> Box<[u8]> { super::data_value_to_bytes( @@ -131,36 +128,50 @@ mod data_value_to_bytes_test { assert_eq!(bytes.as_ref(), &[0x78, 0x56, 0x34, 0x12]); } +} + +#[cfg(test)] +mod eval_int_value_test { + use std::str::FromStr; + + use crate::patio::{types::PatioDataLayout, values::PatioIntValue}; + + fn eval_int_value(int_value: PatioIntValue) -> i32 { + super::eval_int_value( + &int_value, + &target_lexicon::Triple::from_str("x86_64-unknown-linux-gnu").unwrap(), + ) + } #[test] fn int_value_literal_is_used_verbatim() { - let data_value = PatioDataValue::Int32(PatioIntValue::Literal { value: 0x12345678 }); + let int_value = PatioIntValue::Literal { value: 0x12345678 }; - let bytes = data_value_to_bytes(data_value); + let value = eval_int_value(int_value); - assert_eq!(bytes.as_ref(), &[0x78, 0x56, 0x34, 0x12]); + assert_eq!(value, 0x12345678); } #[test] fn int_can_have_size_of_layout() { - let data_value = PatioDataValue::Int32(PatioIntValue::ByteSizeOf { + let int_value = PatioIntValue::ByteSizeOf { layout: PatioDataLayout::Int32, - }); + }; - let bytes = data_value_to_bytes(data_value); + let value = eval_int_value(int_value); - assert_eq!(bytes.as_ref(), &[4, 0, 0, 0]); + assert_eq!(value, 4); } #[test] fn int_can_have_byte_offset() { - let data_value = PatioDataValue::Int32(PatioIntValue::ByteOffsetOf { + let int_value = PatioIntValue::ByteOffsetOf { layout: PatioDataLayout::array(PatioDataLayout::Int32, 7), element_offsets: vec![5], - }); + }; - let bytes = data_value_to_bytes(data_value); + let value = eval_int_value(int_value); - assert_eq!(bytes.as_ref(), &[20, 0, 0, 0]); + assert_eq!(value, 20); } } |
