Skip to content

Commit

Permalink
parameterize parse_opcode test
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Mar 1, 2023
1 parent 584e16d commit 578d312
Showing 1 changed file with 45 additions and 187 deletions.
232 changes: 45 additions & 187 deletions src/gen/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,196 +107,54 @@ fn opcode_tester_with_assert_before(a: &mut Allocator, val: &[u8]) -> Option<Con
parse_opcode(&a, v, ENABLE_ASSERT_BEFORE)
}

#[test]
fn test_parse_opcode() {
let mut a = Allocator::new();
assert_eq!(
opcode_tester(&mut a, &[AGG_SIG_UNSAFE]),
Some(AGG_SIG_UNSAFE)
);
assert_eq!(opcode_tester(&mut a, &[AGG_SIG_ME]), Some(AGG_SIG_ME));
assert_eq!(opcode_tester(&mut a, &[CREATE_COIN]), Some(CREATE_COIN));
assert_eq!(opcode_tester(&mut a, &[RESERVE_FEE]), Some(RESERVE_FEE));
assert_eq!(
opcode_tester(&mut a, &[CREATE_COIN_ANNOUNCEMENT]),
Some(CREATE_COIN_ANNOUNCEMENT)
);
assert_eq!(
opcode_tester(&mut a, &[ASSERT_COIN_ANNOUNCEMENT]),
Some(ASSERT_COIN_ANNOUNCEMENT)
);
assert_eq!(
opcode_tester(&mut a, &[CREATE_PUZZLE_ANNOUNCEMENT]),
Some(CREATE_PUZZLE_ANNOUNCEMENT)
);
assert_eq!(
opcode_tester(&mut a, &[ASSERT_PUZZLE_ANNOUNCEMENT]),
Some(ASSERT_PUZZLE_ANNOUNCEMENT)
);
assert_eq!(
opcode_tester(&mut a, &[ASSERT_MY_COIN_ID]),
Some(ASSERT_MY_COIN_ID)
);
assert_eq!(
opcode_tester(&mut a, &[ASSERT_MY_PARENT_ID]),
Some(ASSERT_MY_PARENT_ID)
);
assert_eq!(
opcode_tester(&mut a, &[ASSERT_MY_PUZZLEHASH]),
Some(ASSERT_MY_PUZZLEHASH)
);
assert_eq!(
opcode_tester(&mut a, &[ASSERT_MY_AMOUNT]),
Some(ASSERT_MY_AMOUNT)
);
assert_eq!(
opcode_tester(&mut a, &[ASSERT_SECONDS_RELATIVE]),
Some(ASSERT_SECONDS_RELATIVE)
);
assert_eq!(
opcode_tester(&mut a, &[ASSERT_SECONDS_ABSOLUTE]),
Some(ASSERT_SECONDS_ABSOLUTE)
);
assert_eq!(
opcode_tester(&mut a, &[ASSERT_HEIGHT_RELATIVE]),
Some(ASSERT_HEIGHT_RELATIVE)
);
assert_eq!(
opcode_tester(&mut a, &[ASSERT_HEIGHT_ABSOLUTE]),
Some(ASSERT_HEIGHT_ABSOLUTE)
);
assert_eq!(
opcode_tester(&mut a, &[ASSERT_BEFORE_SECONDS_RELATIVE]),
None
);
assert_eq!(
opcode_tester(&mut a, &[ASSERT_BEFORE_SECONDS_ABSOLUTE]),
None
);
assert_eq!(
opcode_tester(&mut a, &[ASSERT_BEFORE_HEIGHT_RELATIVE]),
None
);
assert_eq!(
opcode_tester(&mut a, &[ASSERT_BEFORE_HEIGHT_ABSOLUTE]),
None
);
assert_eq!(opcode_tester(&mut a, &[ASSERT_CONCURRENT_SPEND]), None);
assert_eq!(opcode_tester(&mut a, &[ASSERT_CONCURRENT_PUZZLE]), None);

assert_eq!(opcode_tester(&mut a, &[ALWAYS_TRUE]), Some(ALWAYS_TRUE));
// leading zeros are not allowed, it makes it a different value
assert_eq!(opcode_tester(&mut a, &[ASSERT_HEIGHT_ABSOLUTE, 0]), None);
assert_eq!(opcode_tester(&mut a, &[0, ASSERT_HEIGHT_ABSOLUTE]), None);
assert_eq!(opcode_tester(&mut a, &[0]), None);

// test with the ENABLE_ASSERT_BEFORE flag as well

assert_eq!(
opcode_tester_with_assert_before(&mut a, &[AGG_SIG_UNSAFE]),
Some(AGG_SIG_UNSAFE)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[AGG_SIG_ME]),
Some(AGG_SIG_ME)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[CREATE_COIN]),
Some(CREATE_COIN)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[RESERVE_FEE]),
Some(RESERVE_FEE)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[CREATE_COIN_ANNOUNCEMENT]),
Some(CREATE_COIN_ANNOUNCEMENT)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[ASSERT_COIN_ANNOUNCEMENT]),
Some(ASSERT_COIN_ANNOUNCEMENT)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[CREATE_PUZZLE_ANNOUNCEMENT]),
Some(CREATE_PUZZLE_ANNOUNCEMENT)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[ASSERT_PUZZLE_ANNOUNCEMENT]),
Some(ASSERT_PUZZLE_ANNOUNCEMENT)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[ASSERT_MY_COIN_ID]),
Some(ASSERT_MY_COIN_ID)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[ASSERT_MY_PARENT_ID]),
Some(ASSERT_MY_PARENT_ID)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[ASSERT_MY_PUZZLEHASH]),
Some(ASSERT_MY_PUZZLEHASH)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[ASSERT_MY_AMOUNT]),
Some(ASSERT_MY_AMOUNT)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[ASSERT_SECONDS_RELATIVE]),
Some(ASSERT_SECONDS_RELATIVE)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[ASSERT_SECONDS_ABSOLUTE]),
Some(ASSERT_SECONDS_ABSOLUTE)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[ASSERT_HEIGHT_RELATIVE]),
Some(ASSERT_HEIGHT_RELATIVE)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[ASSERT_HEIGHT_ABSOLUTE]),
Some(ASSERT_HEIGHT_ABSOLUTE)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[ASSERT_BEFORE_SECONDS_RELATIVE]),
Some(ASSERT_BEFORE_SECONDS_RELATIVE)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[ASSERT_BEFORE_SECONDS_ABSOLUTE]),
Some(ASSERT_BEFORE_SECONDS_ABSOLUTE)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[ASSERT_BEFORE_HEIGHT_RELATIVE]),
Some(ASSERT_BEFORE_HEIGHT_RELATIVE)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[ASSERT_BEFORE_HEIGHT_ABSOLUTE]),
Some(ASSERT_BEFORE_HEIGHT_ABSOLUTE)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[ASSERT_CONCURRENT_SPEND]),
Some(ASSERT_CONCURRENT_SPEND)
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[ASSERT_CONCURRENT_PUZZLE]),
Some(ASSERT_CONCURRENT_PUZZLE)
);
#[cfg(test)]
use rstest::rstest;

assert_eq!(
opcode_tester_with_assert_before(&mut a, &[ALWAYS_TRUE]),
Some(ALWAYS_TRUE)
);
// leading zeros are not allowed, it makes it a different value
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[ASSERT_HEIGHT_ABSOLUTE, 0]),
None
);
assert_eq!(
opcode_tester_with_assert_before(&mut a, &[0, ASSERT_HEIGHT_ABSOLUTE]),
None
);
assert_eq!(opcode_tester_with_assert_before(&mut a, &[0]), None);
#[cfg(test)]
#[rstest]
// leading zeros are not allowed, it makes it a different value
#[case(&[ASSERT_HEIGHT_ABSOLUTE, 0], None, None)]
#[case(&[0, ASSERT_HEIGHT_ABSOLUTE], None, None)]
#[case(&[0], None, None)]
// all condition codes
#[case(&[AGG_SIG_UNSAFE], Some(AGG_SIG_UNSAFE), Some(AGG_SIG_UNSAFE))]
#[case(&[AGG_SIG_ME], Some(AGG_SIG_ME), Some(AGG_SIG_ME))]
#[case(&[CREATE_COIN], Some(CREATE_COIN), Some(CREATE_COIN))]
#[case(&[RESERVE_FEE], Some(RESERVE_FEE), Some(RESERVE_FEE))]
#[case(&[CREATE_COIN_ANNOUNCEMENT], Some(CREATE_COIN_ANNOUNCEMENT), Some(CREATE_COIN_ANNOUNCEMENT))]
#[case(&[ASSERT_COIN_ANNOUNCEMENT], Some(ASSERT_COIN_ANNOUNCEMENT), Some(ASSERT_COIN_ANNOUNCEMENT))]
#[case(&[CREATE_PUZZLE_ANNOUNCEMENT], Some(CREATE_PUZZLE_ANNOUNCEMENT), Some(CREATE_PUZZLE_ANNOUNCEMENT))]
#[case(&[ASSERT_PUZZLE_ANNOUNCEMENT], Some(ASSERT_PUZZLE_ANNOUNCEMENT), Some(ASSERT_PUZZLE_ANNOUNCEMENT))]
#[case(&[ASSERT_CONCURRENT_SPEND], None, Some(ASSERT_CONCURRENT_SPEND))]
#[case(&[ASSERT_CONCURRENT_PUZZLE], None, Some(ASSERT_CONCURRENT_PUZZLE))]
#[case(&[ASSERT_MY_COIN_ID], Some(ASSERT_MY_COIN_ID), Some(ASSERT_MY_COIN_ID))]
#[case(&[ASSERT_MY_PARENT_ID], Some(ASSERT_MY_PARENT_ID), Some(ASSERT_MY_PARENT_ID))]
#[case(&[ASSERT_MY_PUZZLEHASH], Some(ASSERT_MY_PUZZLEHASH), Some(ASSERT_MY_PUZZLEHASH))]
#[case(&[ASSERT_MY_AMOUNT], Some(ASSERT_MY_AMOUNT), Some(ASSERT_MY_AMOUNT))]
#[case(&[ASSERT_SECONDS_RELATIVE],Some(ASSERT_SECONDS_RELATIVE) , Some(ASSERT_SECONDS_RELATIVE))]
#[case(&[ASSERT_SECONDS_ABSOLUTE],Some(ASSERT_SECONDS_ABSOLUTE) , Some(ASSERT_SECONDS_ABSOLUTE))]
#[case(&[ASSERT_HEIGHT_RELATIVE], Some(ASSERT_HEIGHT_RELATIVE), Some(ASSERT_HEIGHT_RELATIVE))]
#[case(&[ASSERT_HEIGHT_ABSOLUTE], Some(ASSERT_HEIGHT_ABSOLUTE), Some(ASSERT_HEIGHT_ABSOLUTE))]
#[case(&[ASSERT_BEFORE_SECONDS_RELATIVE], None, Some(ASSERT_BEFORE_SECONDS_RELATIVE))]
#[case(&[ASSERT_BEFORE_SECONDS_ABSOLUTE], None, Some(ASSERT_BEFORE_SECONDS_ABSOLUTE))]
#[case(&[ASSERT_BEFORE_HEIGHT_RELATIVE], None, Some(ASSERT_BEFORE_HEIGHT_RELATIVE))]
#[case(&[ASSERT_BEFORE_HEIGHT_ABSOLUTE], None, Some(ASSERT_BEFORE_HEIGHT_ABSOLUTE))]
#[case(&[ALWAYS_TRUE], Some(ALWAYS_TRUE), Some(ALWAYS_TRUE))]

fn test_parse_opcode(
#[case] input: &[u8],
#[case] expected: Option<ConditionOpcode>,
#[case] expected2: Option<ConditionOpcode>,
) {
let mut a = Allocator::new();
assert_eq!(opcode_tester(&mut a, input), expected);
assert_eq!(opcode_tester_with_assert_before(&mut a, input), expected2);
}

#[test]
fn test_parse_invalid_opcode() {
// a pair is never a valid condition
let mut a = Allocator::new();
let v1 = a.new_atom(&[0]).unwrap();
let v2 = a.new_atom(&[0]).unwrap();
let p = a.new_pair(v1, v2).unwrap();
Expand Down

0 comments on commit 578d312

Please sign in to comment.