move tests to its own modules
This commit is contained in:
@@ -50,3 +50,49 @@ impl ConstPool {
|
||||
self.insert(ConstantValue::String(value))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::ir_core::ids::ConstId;
|
||||
|
||||
#[test]
|
||||
fn test_const_pool_deduplication() {
|
||||
let mut pool = ConstPool::new();
|
||||
|
||||
let id1 = pool.insert(ConstantValue::Int(42));
|
||||
let id2 = pool.insert(ConstantValue::String("hello".to_string()));
|
||||
let id3 = pool.insert(ConstantValue::Int(42));
|
||||
|
||||
assert_eq!(id1, id3);
|
||||
assert_ne!(id1, id2);
|
||||
assert_eq!(pool.constants.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_const_pool_deterministic_assignment() {
|
||||
let mut pool = ConstPool::new();
|
||||
|
||||
let id0 = pool.insert(ConstantValue::Int(10));
|
||||
let id1 = pool.insert(ConstantValue::Int(20));
|
||||
let id2 = pool.insert(ConstantValue::Int(30));
|
||||
|
||||
assert_eq!(id0, ConstId(0));
|
||||
assert_eq!(id1, ConstId(1));
|
||||
assert_eq!(id2, ConstId(2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_const_pool_serialization() {
|
||||
let mut pool = ConstPool::new();
|
||||
pool.insert(ConstantValue::Int(42));
|
||||
pool.insert(ConstantValue::String("test".to_string()));
|
||||
pool.insert(ConstantValue::Float(3.14));
|
||||
|
||||
let json = serde_json::to_string_pretty(&pool).unwrap();
|
||||
|
||||
assert!(json.contains("\"Int\": 42"));
|
||||
assert!(json.contains("\"String\": \"test\""));
|
||||
assert!(json.contains("\"Float\": 3.14"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,3 +17,86 @@ pub use function::*;
|
||||
pub use block::*;
|
||||
pub use instr::*;
|
||||
pub use terminator::*;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json;
|
||||
|
||||
#[test]
|
||||
fn test_ir_core_manual_construction() {
|
||||
let mut const_pool = ConstPool::new();
|
||||
const_pool.insert(ConstantValue::String("hello".to_string()));
|
||||
|
||||
let program = Program {
|
||||
const_pool,
|
||||
modules: vec![Module {
|
||||
name: "main".to_string(),
|
||||
functions: vec![Function {
|
||||
id: FunctionId(10),
|
||||
name: "entry".to_string(),
|
||||
params: vec![],
|
||||
return_type: Type::Void,
|
||||
blocks: vec![Block {
|
||||
id: 0,
|
||||
instrs: vec![
|
||||
Instr::PushConst(ConstId(0)),
|
||||
Instr::Call(FunctionId(11), 0),
|
||||
],
|
||||
terminator: Terminator::Return,
|
||||
}],
|
||||
}],
|
||||
}],
|
||||
};
|
||||
|
||||
let json = serde_json::to_string_pretty(&program).unwrap();
|
||||
|
||||
let expected = r#"{
|
||||
"const_pool": {
|
||||
"constants": [
|
||||
{
|
||||
"String": "hello"
|
||||
}
|
||||
]
|
||||
},
|
||||
"modules": [
|
||||
{
|
||||
"name": "main",
|
||||
"functions": [
|
||||
{
|
||||
"id": 10,
|
||||
"name": "entry",
|
||||
"params": [],
|
||||
"return_type": "Void",
|
||||
"blocks": [
|
||||
{
|
||||
"id": 0,
|
||||
"instrs": [
|
||||
{
|
||||
"PushConst": 0
|
||||
},
|
||||
{
|
||||
"Call": [
|
||||
11,
|
||||
0
|
||||
]
|
||||
}
|
||||
],
|
||||
"terminator": "Return"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}"#;
|
||||
assert_eq!(json, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ir_core_ids() {
|
||||
assert_eq!(serde_json::to_string(&FunctionId(1)).unwrap(), "1");
|
||||
assert_eq!(serde_json::to_string(&ConstId(2)).unwrap(), "2");
|
||||
assert_eq!(serde_json::to_string(&TypeId(3)).unwrap(), "3");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user