Skip to content

Commit 6357cf3

Browse files
committed
add clz test
1 parent 2251a5e commit 6357cf3

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

src/instructions.rs

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ fn compute_block_hash(chain_id: u64, block_number: u64) -> U256 {
279279

280280
#[cfg(test)]
281281
mod tests {
282-
use super::{compute_block_hash, make_scroll_instruction_table};
282+
use super::{clz, compute_block_hash, make_scroll_instruction_table};
283283
use crate::{
284284
builder::{DefaultScrollContext, ScrollContext},
285285
instructions::HISTORY_STORAGE_ADDRESS,
@@ -289,7 +289,7 @@ mod tests {
289289
use revm::{
290290
bytecode::{opcode::*, Bytecode},
291291
database::{EmptyDB, InMemoryDB},
292-
interpreter::Interpreter,
292+
interpreter::{push, InstructionContext, Interpreter},
293293
primitives::{Bytes, U256},
294294
DatabaseRef,
295295
};
@@ -380,4 +380,65 @@ mod tests {
380380
let actual_gas_used = interpreter.gas.used();
381381
assert_eq!(actual_gas_used, expected_gas_used);
382382
}
383+
384+
#[test]
385+
fn test_clz() {
386+
use revm::primitives::uint;
387+
388+
let spec = GALILEO;
389+
let db = EmptyDB::new();
390+
let mut scroll_context = ScrollContext::scroll().with_db(InMemoryDB::new(db));
391+
scroll_context.modify_cfg(|cfg| cfg.spec = spec);
392+
393+
let mut interpreter = Interpreter::default();
394+
395+
struct TestCase {
396+
value: U256,
397+
expected: U256,
398+
}
399+
400+
uint! {
401+
let test_cases = [
402+
TestCase { value: 0x0_U256, expected: 256_U256 },
403+
TestCase { value: 0x1_U256, expected: 255_U256 },
404+
TestCase { value: 0x2_U256, expected: 254_U256 },
405+
TestCase { value: 0x3_U256, expected: 254_U256 },
406+
TestCase { value: 0x4_U256, expected: 253_U256 },
407+
TestCase { value: 0x7_U256, expected: 253_U256 },
408+
TestCase { value: 0x8_U256, expected: 252_U256 },
409+
TestCase { value: 0xff_U256, expected: 248_U256 },
410+
TestCase { value: 0x100_U256, expected: 247_U256 },
411+
TestCase { value: 0xffff_U256, expected: 240_U256 },
412+
TestCase {
413+
value: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256, // U256::MAX
414+
expected: 0_U256,
415+
},
416+
TestCase {
417+
value: 0x8000000000000000000000000000000000000000000000000000000000000000_U256, // 1 << 255
418+
expected: 0_U256,
419+
},
420+
TestCase { // Smallest value with 1 leading zero
421+
value: 0x4000000000000000000000000000000000000000000000000000000000000000_U256, // 1 << 254
422+
expected: 1_U256,
423+
},
424+
TestCase { // Value just below 1 << 255
425+
value: 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_U256,
426+
expected: 1_U256,
427+
},
428+
];
429+
}
430+
431+
for test in test_cases {
432+
push!(interpreter, test.value);
433+
let context =
434+
InstructionContext { host: &mut scroll_context, interpreter: &mut interpreter };
435+
clz(context);
436+
let res = interpreter.stack.pop().unwrap();
437+
assert_eq!(
438+
res, test.expected,
439+
"CLZ for value {:#x} failed. Expected: {}, Got: {}",
440+
test.value, test.expected, res
441+
);
442+
}
443+
}
383444
}

0 commit comments

Comments
 (0)