From e98452b0c7deeffcca51b7ddccd17caa59b47902 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Tue, 5 Jan 2016 19:23:40 +0100 Subject: [PATCH] Tests for Builtin. --- src/builtin.rs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/builtin.rs b/src/builtin.rs index 48558c608..2b7d4199c 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -20,7 +20,9 @@ impl Builtin { /// Create a new object from a builtin-function name with a linear cost associated with input size. pub fn from_named_linear(name: &str, base_cost: usize, word_cost: usize) -> Option { new_builtin_exec(name).map(|b| { - let cost = Box::new(move|s: usize| -> U256 {U256::from(base_cost) + U256::from(word_cost) * U256::from((s + 31) / 32)}); + let cost = Box::new(move|s: usize| -> U256 { + U256::from(base_cost) + U256::from(word_cost) * U256::from((s + 31) / 32) + }); Self::new(cost, b) }) } @@ -126,4 +128,34 @@ fn identity() { f(&i[..], &mut o8[..]); assert_eq!(i, o8[..4]); assert_eq!([255u8; 4], o8[4..]); +} + +#[test] +fn from_named_linear() { + let b = Builtin::from_named_linear("identity", 10, 20).unwrap(); + assert_eq!((*b.cost)(0), U256::from(10)); + assert_eq!((*b.cost)(1), U256::from(30)); + assert_eq!((*b.cost)(32), U256::from(30)); + assert_eq!((*b.cost)(33), U256::from(50)); + + let i = [0u8, 1, 2, 3]; + let mut o = [255u8; 4]; + (*b.execute)(&i[..], &mut o[..]); + assert_eq!(i, o); +} + +#[test] +fn from_json() { + let text = "{ \"name\": \"identity\", \"linear\": {\"base\": 10, \"word\": 20} }"; + let json = Json::from_str(text).unwrap(); + let b = Builtin::from_json(&json).unwrap(); + assert_eq!((*b.cost)(0), U256::from(10)); + assert_eq!((*b.cost)(1), U256::from(30)); + assert_eq!((*b.cost)(32), U256::from(30)); + assert_eq!((*b.cost)(33), U256::from(50)); + + let i = [0u8, 1, 2, 3]; + let mut o = [255u8; 4]; + (*b.execute)(&i[..], &mut o[..]); + assert_eq!(i, o); } \ No newline at end of file