Skip to content

Commit

Permalink
Unit test for constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jakelang committed Mar 13, 2019
1 parent a72043e commit 82a426e
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions bindings/rust/evmc-vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ pub extern crate evmc_sys;
pub use evmc_sys as ffi;

// TODO: Add helpers for host interface
// TODO: Add convenient helpers for evmc_result
// TODO: Add convenient helpers for evmc_execute
// TODO: Add a derive macro here for creating evmc_create

//pub fn destroy_result(&mut evmc_result)

/// EVMC result structure.
pub struct EvmcResult(Box<ffi::evmc_result>);

Expand Down Expand Up @@ -38,7 +35,9 @@ impl EvmcResult {

pub fn output(&self) -> Option<&[u8]> {
if !self.0.output_data.is_null() && self.0.output_size != 0 {
Some(unsafe { std::slice::from_raw_parts::<u8>(self.0.output_data, self.0.output_size) })
Some(unsafe {
std::slice::from_raw_parts::<u8>(self.0.output_data, self.0.output_size)
})
} else {
None
}
Expand All @@ -55,3 +54,24 @@ extern "C" fn release_result(result: *const ffi::evmc_result) {
Box::from_raw(result as *mut ffi::evmc_result);
}
}

#[cfg(test)]
mod tests {
use super::*;
use evmc_sys as ffi;
use std::mem::discriminant;

#[test]
fn constructor() {
let result = EvmcResult::new(
ffi::evmc_status_code::EVMC_SUCCESS,
420,
Some(&[0xde, 0xad, 0xbe, 0xef]),
);
assert!(result.status() == ffi::evmc_status_code::EVMC_SUCCESS);
assert!(result.gasleft() == 420);
assert!(result.output().is_some());
assert!(result.output().unwrap().len() == 4);
assert!(result.output().unwrap() == [0xde, 0xad, 0xbe, 0xef]);
}
}

0 comments on commit 82a426e

Please sign in to comment.