Files

153 lines
6.8 KiB
Rust

#[path = "support/identity.rs"]
mod support;
use prometeu_bytecode::{Value, model::ConstantPoolEntry as K};
use prometeu_vm::{HostContext, LogicalFrameEndingReason as End};
use support::*;
#[test]
fn scalar_pbx_execution_matches_independent_c_reference_vectors() {
let mut count = 0;
for row in include_str!("fixtures/hash-conformance.tsv").lines().skip(1) {
let fields: Vec<_> = row.split('\t').collect();
let expected = u64::from_str_radix(fields[2], 16).unwrap() as i64;
let (value, id, bytes) = match fields[0] {
"i64" => (K::Int64(fields[1].parse().unwrap()), 0x4100, 0),
"float" => {
(K::Float64(f64::from_bits(u64::from_str_radix(fields[1], 16).unwrap())), 0x4101, 0)
}
"string" => {
let bytes = hex_bytes(fields[1]);
let size = bytes.len();
(K::String(String::from_utf8(bytes).unwrap()), 0x4102, size)
}
"null" => (K::Null, 0x4102, 0),
_ => continue,
};
let source = format!("PUSH_CONST 0\nINTRINSIC {id}\nRET");
let mut vm = vm(&[(&source, sig(&[], &[0]))], &[value]);
step(&mut vm).unwrap();
let report = vm.run_budget(1, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!(report.reason, End::BudgetExhausted);
assert_eq!(report.cycles_used, 8 + bytes as u64, "{row}");
assert_eq!(&*vm.operand_stack_top(1), &[Value::Int64(expected)], "{row}");
assert_eq!(run(&mut vm).reason, End::EndOfRom);
count += 1;
}
assert_eq!(count, 31);
}
#[test]
fn identities_survive_mutation_and_views_validate_implementation_zero() {
let source = "STRUCT_NEW 5\nSET_LOCAL 0\nGET_LOCAL 0\nREF_IDENTITY_HASH 5\nSET_LOCAL 1\nGET_LOCAL 0\nPUSH_I64 73\nSTRUCT_SET type=5,field=0\nGET_LOCAL 0\nREF_IDENTITY_HASH 5\nGET_LOCAL 1\nEQ\nGET_LOCAL 0\nREF_TO_TRAIT source=5,target=7\nREF_IDENTITY_HASH 7\nGET_LOCAL 1\nEQ\nGET_LOCAL 0\nREF_TO_TRAIT source=5,target=7\nGET_LOCAL 0\nREF_TO_TRAIT source=5,target=8\nREF_IDENTITY_EQ left=7,right=8\nGET_LOCAL 0\nSTRUCT_NEW 5\nREF_IDENTITY_EQ left=5,right=5\nRET";
let mut vm = vm(&[(source, sig(&[], &[2, 2, 2, 2]))], &[]);
assert_eq!(run(&mut vm).reason, End::EndOfRom);
assert_eq!(
&*vm.operand_stack_top(4),
&[Value::Boolean(false), Value::Boolean(true), Value::Boolean(true), Value::Boolean(true)]
);
}
#[test]
fn array_closure_and_random_have_instance_identity() {
for (source, bodies) in [
(
"PUSH_I64 1\nARRAY_NEW 6\nSET_LOCAL 0\nGET_LOCAL 0\nREF_IDENTITY_HASH 6\nSET_LOCAL 1\nGET_LOCAL 0\nPUSH_I64 0\nPUSH_I64 42\nARRAY_SET 6\nGET_LOCAL 0\nREF_IDENTITY_HASH 6\nGET_LOCAL 1\nEQ\nRET",
false,
),
(
"PUSH_I64 42\nINTRINSIC 0x4000\nSET_LOCAL 0\nGET_LOCAL 0\nREF_IDENTITY_HASH 10\nSET_LOCAL 1\nGET_LOCAL 0\nINTRINSIC 0x4001\nPOP\nGET_LOCAL 0\nREF_IDENTITY_HASH 10\nGET_LOCAL 1\nEQ\nRET",
false,
),
(
"MAKE_CLOSURE fn=1,type=9\nSET_LOCAL 0\nGET_LOCAL 0\nGET_LOCAL 0\nREF_IDENTITY_EQ left=9,right=9\nGET_LOCAL 0\nMAKE_CLOSURE fn=1,type=9\nREF_IDENTITY_EQ left=9,right=9\nNOT\nAND\nRET",
true,
),
] {
let mut functions = vec![(source, sig(&[], &[2]))];
if bodies {
functions.push(("RET", sig(&[], &[])));
}
let mut vm = vm(&functions, &[]);
assert_eq!(run(&mut vm).reason, End::EndOfRom);
assert_eq!(&*vm.operand_stack_top(1), &[Value::Boolean(true)]);
}
}
#[test]
fn canonical_null_groups_compare_and_hash_without_instance_faults() {
let source = "PUSH_CONST 0\nPUSH_CONST 0\nREF_IDENTITY_HASH 7\nPUSH_CONST 0\nPUSH_CONST 0\nPUSH_CONST 0\nREF_IDENTITY_EQ left=6,right=7\nRET";
let mut vm = vm(&[(source, sig(&[], &[0, 2]))], &[K::Null]);
assert_eq!(run(&mut vm).reason, End::EndOfRom);
assert_eq!(&*vm.operand_stack_top(2), &[Value::Boolean(true), Value::Int64(0)]);
}
#[test]
fn admission_rejects_ineligible_sources_and_bad_native_ids_even_when_dead() {
for instruction in [
"REF_IDENTITY_HASH 0",
"REF_IDENTITY_HASH 3",
"REF_IDENTITY_HASH 4",
"REF_IDENTITY_HASH 12",
"REF_IDENTITY_HASH 13",
"REF_IDENTITY_HASH 14",
"REF_IDENTITY_HASH 9999",
"REF_IDENTITY_EQ left=5,right=3",
"INTRINSIC 0x41FF",
] {
let text = format!("HALT\n{instruction}");
assert!(image(&[(&text, sig(&[], &[]))], &[]).is_err(), "{instruction}");
}
assert!(image(&[("PUSH_CONST 0\nINTRINSIC 0x4102\nRET", sig(&[], &[0]))], &[K::Null]).is_ok());
for id in [0x1103, 0x4100, 0x4101] {
let text = format!("PUSH_CONST 0\nINTRINSIC {id}\nPOP\nRET");
assert!(image(&[(&text, sig(&[], &[]))], &[K::Null]).is_err(), "{id}");
}
assert!(image(&[("PUSH_I64 3\nREF_IDENTITY_HASH 5\nRET", sig(&[], &[0]))], &[]).is_err());
}
#[test]
fn whole_instruction_budget_admission_preserves_zero_and_overshoot() {
let mut vm = vm(
&[("PUSH_CONST 0\nINTRINSIC 0x4102\nRET", sig(&[], &[0]))],
&[K::String("x".repeat(65))],
);
step(&mut vm).unwrap();
let before = vm.pc();
let zero = vm.run_budget(0, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!(zero.cycles_used, 0);
assert_eq!(vm.pc(), before);
let one = vm.run_budget(1, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!(one.reason, End::BudgetExhausted);
assert_eq!(one.cycles_used, 73);
assert_eq!(one.steps_executed, 1);
}
#[test]
fn every_eligible_absence_has_zero_hash_and_exact_identity_instruction_cost() {
for (ty, width) in [(5, 1), (6, 1), (7, 2), (9, 1), (10, 1)] {
let source = format!("{}REF_IDENTITY_HASH {ty}\nRET", "PUSH_CONST 0\n".repeat(width));
let mut vm = vm(&[(&source, sig(&[], &[0]))], &[K::Null]);
for _ in 0..width {
step(&mut vm).unwrap();
}
let before = vm.pc();
let zero = vm.run_budget(0, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!((zero.cycles_used, zero.steps_executed), (0, 0));
assert_eq!(vm.pc(), before);
let one = vm.run_budget(1, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!((one.cycles_used, one.steps_executed), (8 + width as u64, 1));
assert_eq!(&*vm.operand_stack_top(1), &[Value::Int64(0)]);
}
for source in [
"PUSH_CONST 0\nSTRUCT_NEW 5\nREF_IDENTITY_EQ left=6,right=5\nRET",
"STRUCT_NEW 5\nSTRUCT_NEW 11\nREF_IDENTITY_EQ left=5,right=11\nRET",
] {
let mut vm = vm(&[(source, sig(&[], &[2]))], &[K::Null]);
step(&mut vm).unwrap();
step(&mut vm).unwrap();
let one = vm.run_budget(1, &mut Native, &mut HostContext::new(None)).unwrap();
assert_eq!((one.cycles_used, one.steps_executed), (6, 1));
assert_eq!(&*vm.operand_stack_top(1), &[Value::Boolean(false)]);
}
}