Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Commit

Permalink
Add create and read test case
Browse files Browse the repository at this point in the history
  • Loading branch information
perimeko committed Jun 12, 2020
1 parent f320e99 commit af79627
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions go-owasm/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Span {
cap: data.len(),
}
}

/// Read data from the span.
pub fn read(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
Expand All @@ -39,10 +39,24 @@ mod test {
use super::*;

#[test]
fn test_success_write_span_ok() {
let mut empty_space = vec![0u8; 32];
let mut span = Span{ ptr: empty_space.as_mut_ptr(), len: 0, cap: 32 };
fn test_create_and_read_span_ok() {
let data: Vec<u8> = vec![1, 2, 3, 4, 5];
let span = Span::create(data.as_slice());
let span_data = &span.read();
let span_data_vec = span_data.to_vec();
assert_eq!(span_data_vec.len(), data.len());
assert_eq!(span_data_vec[0], data[0]);
assert_eq!(span_data_vec[1], data[1]);
assert_eq!(span_data_vec[2], data[2]);
assert_eq!(span_data_vec[3], data[3]);
assert_eq!(span_data_vec[4], data[4]);
}

#[test]
fn test_write_span_ok() {
let mut empty_space = vec![0u8; 32];
let mut span = Span{ ptr: empty_space.as_mut_ptr(), len: 0, cap: 32 };

let data: Vec<u8> = vec![1, 2, 3, 4, 5];
assert_eq!(span.write(data.as_slice()), Error::NoError);
assert_eq!(span.len, 5);
Expand All @@ -59,13 +73,11 @@ mod test {
}

#[test]
fn test_success_write_span_fail() {
fn test_write_span_fail() {
let mut empty_space = vec![0u8; 3];
let mut span = Span{ ptr: empty_space.as_mut_ptr(), len: 0, cap: 3 };

let data: Vec<u8> = vec![1, 2, 3, 4, 5];
span.write(data.as_slice());

assert_eq!(span.write(data.as_slice()), Error::SpanExceededCapacityError);
}
}

0 comments on commit af79627

Please sign in to comment.