|
| 1 | +mod common; |
| 2 | + |
| 3 | +use primitive_types::U256; |
| 4 | +use rand::Rng; |
| 5 | + |
| 6 | +use common::core::{Expect, run}; |
| 7 | + |
| 8 | +use simplicityhl_std::artifacts::u16_convert_test::U16ConvertTestProgram; |
| 9 | +use simplicityhl_std::artifacts::u16_convert_test::derived_u16_convert_test::{ |
| 10 | + U16ConvertTestArguments, U16ConvertTestWitness, |
| 11 | +}; |
| 12 | + |
| 13 | +enum FunctionToTest { |
| 14 | + U16ToU32, |
| 15 | + U16ToU64, |
| 16 | + U16ToU128, |
| 17 | + U16ToU256, |
| 18 | + SplitU16ToU8, |
| 19 | + SafeU16ToU1, |
| 20 | + SafeU16ToU8, |
| 21 | +} |
| 22 | + |
| 23 | +#[inline] |
| 24 | +fn op(o: FunctionToTest) -> u8 { |
| 25 | + o as u8 |
| 26 | +} |
| 27 | + |
| 28 | +fn program() -> U16ConvertTestProgram { |
| 29 | + U16ConvertTestProgram::new(U16ConvertTestArguments {}) |
| 30 | +} |
| 31 | + |
| 32 | +fn build_witness(function: u8, a: u16, expected: [u8; 32]) -> U16ConvertTestWitness { |
| 33 | + U16ConvertTestWitness { |
| 34 | + function_index: function, |
| 35 | + first_arg: a, |
| 36 | + expected, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +mod u16_convert_test { |
| 41 | + use super::*; |
| 42 | + |
| 43 | + #[simplex::test] |
| 44 | + fn u16_convert_test_u16_to_u32(context: simplex::TestContext) -> anyhow::Result<()> { |
| 45 | + let a = rand::thread_rng().gen_range(0..=u16::MAX); |
| 46 | + |
| 47 | + run( |
| 48 | + &context, |
| 49 | + program(), |
| 50 | + build_witness( |
| 51 | + op(FunctionToTest::U16ToU32), |
| 52 | + a, |
| 53 | + U256::from(a).to_big_endian(), |
| 54 | + ), |
| 55 | + Expect::Ok, |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + #[simplex::test] |
| 60 | + fn u16_convert_test_u16_to_u64(context: simplex::TestContext) -> anyhow::Result<()> { |
| 61 | + let a = rand::thread_rng().gen_range(0..=u16::MAX); |
| 62 | + |
| 63 | + run( |
| 64 | + &context, |
| 65 | + program(), |
| 66 | + build_witness( |
| 67 | + op(FunctionToTest::U16ToU64), |
| 68 | + a, |
| 69 | + U256::from(a).to_big_endian(), |
| 70 | + ), |
| 71 | + Expect::Ok, |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + #[simplex::test] |
| 76 | + fn u16_convert_test_u16_to_u128(context: simplex::TestContext) -> anyhow::Result<()> { |
| 77 | + let a = rand::thread_rng().gen_range(0..=u16::MAX); |
| 78 | + |
| 79 | + run( |
| 80 | + &context, |
| 81 | + program(), |
| 82 | + build_witness( |
| 83 | + op(FunctionToTest::U16ToU128), |
| 84 | + a, |
| 85 | + U256::from(a).to_big_endian(), |
| 86 | + ), |
| 87 | + Expect::Ok, |
| 88 | + ) |
| 89 | + } |
| 90 | + |
| 91 | + #[simplex::test] |
| 92 | + fn u16_convert_test_u16_to_u256(context: simplex::TestContext) -> anyhow::Result<()> { |
| 93 | + let a = rand::thread_rng().gen_range(0..=u16::MAX); |
| 94 | + |
| 95 | + run( |
| 96 | + &context, |
| 97 | + program(), |
| 98 | + build_witness( |
| 99 | + op(FunctionToTest::U16ToU256), |
| 100 | + a, |
| 101 | + U256::from(a).to_big_endian(), |
| 102 | + ), |
| 103 | + Expect::Ok, |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + #[simplex::test] |
| 108 | + fn u16_convert_test_split_u16_to_u8(context: simplex::TestContext) -> anyhow::Result<()> { |
| 109 | + let a = rand::thread_rng().gen_range(0..=u16::MAX); |
| 110 | + |
| 111 | + run( |
| 112 | + &context, |
| 113 | + program(), |
| 114 | + build_witness( |
| 115 | + op(FunctionToTest::SplitU16ToU8), |
| 116 | + a, |
| 117 | + U256::from(a).to_big_endian(), |
| 118 | + ), |
| 119 | + Expect::Ok, |
| 120 | + ) |
| 121 | + } |
| 122 | + |
| 123 | + #[simplex::test] |
| 124 | + fn u16_convert_test_safe_u16_to_u1(context: simplex::TestContext) -> anyhow::Result<()> { |
| 125 | + let a = rand::thread_rng().gen_range(0..=1); |
| 126 | + |
| 127 | + run( |
| 128 | + &context, |
| 129 | + program(), |
| 130 | + build_witness( |
| 131 | + op(FunctionToTest::SafeU16ToU1), |
| 132 | + a, |
| 133 | + U256::from(a).to_big_endian(), |
| 134 | + ), |
| 135 | + Expect::Ok, |
| 136 | + ) |
| 137 | + } |
| 138 | + |
| 139 | + #[simplex::test] |
| 140 | + fn u16_convert_test_safe_u16_to_u1_overflow( |
| 141 | + context: simplex::TestContext, |
| 142 | + ) -> anyhow::Result<()> { |
| 143 | + let a = rand::thread_rng().gen_range(2..=u16::MAX); |
| 144 | + |
| 145 | + run( |
| 146 | + &context, |
| 147 | + program(), |
| 148 | + build_witness( |
| 149 | + op(FunctionToTest::SafeU16ToU1), |
| 150 | + a, |
| 151 | + U256::from(a).to_big_endian(), |
| 152 | + ), |
| 153 | + Expect::AssertFailed, |
| 154 | + ) |
| 155 | + } |
| 156 | + |
| 157 | + #[simplex::test] |
| 158 | + fn u16_convert_test_safe_u16_to_u8(context: simplex::TestContext) -> anyhow::Result<()> { |
| 159 | + let a = rand::thread_rng().gen_range(0..=u8::MAX as u16); |
| 160 | + |
| 161 | + run( |
| 162 | + &context, |
| 163 | + program(), |
| 164 | + build_witness( |
| 165 | + op(FunctionToTest::SafeU16ToU8), |
| 166 | + a, |
| 167 | + U256::from(a).to_big_endian(), |
| 168 | + ), |
| 169 | + Expect::Ok, |
| 170 | + ) |
| 171 | + } |
| 172 | + |
| 173 | + #[simplex::test] |
| 174 | + fn u16_convert_test_safe_u16_to_u8_overflow( |
| 175 | + context: simplex::TestContext, |
| 176 | + ) -> anyhow::Result<()> { |
| 177 | + let a = rand::thread_rng().gen_range(u8::MAX as u16 + 1..=u16::MAX); |
| 178 | + |
| 179 | + run( |
| 180 | + &context, |
| 181 | + program(), |
| 182 | + build_witness( |
| 183 | + op(FunctionToTest::SafeU16ToU8), |
| 184 | + a, |
| 185 | + U256::from(a).to_big_endian(), |
| 186 | + ), |
| 187 | + Expect::AssertFailed, |
| 188 | + ) |
| 189 | + } |
| 190 | +} |
0 commit comments