Skip to content

Commit

Permalink
Add list lowering tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ospencer committed Jan 26, 2022
1 parent 3597d40 commit d9cf1f0
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/runtime/lists/exports.wit
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ list-result: function() -> list<u8>
list-result2: function() -> string
list-result3: function() -> list<string>

list-roundtrip: function(a: list<u8>) -> list<u8>
string-roundtrip: function(a: string) -> string
3 changes: 3 additions & 0 deletions tests/runtime/lists/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def list_result2(self) -> str:
def list_result3(self) -> List[str]:
return ['hello,', 'world!']

def list_roundtrip(self, a: bytes) -> bytes:
return a

def string_roundtrip(self, a: str) -> str:
return a

Expand Down
4 changes: 4 additions & 0 deletions tests/runtime/lists/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ impl Imports for MyImports {
vec!["hello,".to_string(), "world!".to_string()]
}

fn list_roundtrip(&mut self, list: &[u8]) -> Vec<u8> {
list.to_vec()
}

fn string_roundtrip(&mut self, s: &str) -> String {
s.to_string()
}
Expand Down
7 changes: 7 additions & 0 deletions tests/runtime/lists/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ async function run() {
},
listResult2() { return 'hello!'; },
listResult3() { return ['hello,', 'world!']; },
listRoundtrip(x) { return x; },
stringRoundtrip(x) { return x; },

unalignedRoundtrip1(u16, u32, u64, flag32, flag64) {
Expand Down Expand Up @@ -119,6 +120,12 @@ async function run() {
assert.deepStrictEqual(Array.from(wasm.listResult()), [1, 2, 3, 4, 5]);
assert.deepStrictEqual(wasm.listResult2(), "hello!");
assert.deepStrictEqual(wasm.listResult3(), ["hello,", "world!"]);

const buffer = new ArrayBuffer(8);
(new Uint8Array(buffer)).set(new Uint8Array([1, 2, 3, 4]), 2);
// Create a view of the four bytes in the middle of the buffer
const view = new Uint8Array(buffer, 2, 4);
assert.deepStrictEqual(Array.from(wasm.listRoundtrip(view)), [1, 2, 3, 4]);

assert.deepStrictEqual(wasm.stringRoundtrip("x"), "x");
assert.deepStrictEqual(wasm.stringRoundtrip(""), "");
Expand Down
2 changes: 2 additions & 0 deletions tests/runtime/lists/imports.wit
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ list-minmax32: function(a: list<u32>, b: list<s32>) -> (list<u32>, list<s32>)
list-minmax64: function(a: list<u64>, b: list<s64>) -> (list<u64>, list<s64>)
list-minmax-float: function(a: list<f32>, b: list<f64>) -> (list<f32>, list<f64>)

list-roundtrip: function(a: list<u8>) -> list<u8>

string-roundtrip: function(a: string) -> string

unaligned-roundtrip1: function(a: list<u16>, b: list<u32>, c: list<u64>, d: list<flag32>, e: list<flag64>)
Expand Down
4 changes: 4 additions & 0 deletions tests/runtime/lists/wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ void exports_list_result3(exports_list_string_t *ret0) {
exports_string_dup(&ret0->ptr[1], "world!");
}

void exports_list_roundtrip(exports_list_u8_t *a, exports_list_u8_t *ret0) {
*ret0 = *a;
}

void exports_string_roundtrip(exports_string_t *a, exports_string_t *ret0) {
*ret0 = *a;
}
4 changes: 4 additions & 0 deletions tests/runtime/lists/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ impl exports::Exports for Exports {
vec!["hello,".to_string(), "world!".to_string()]
}

fn list_roundtrip(x: Vec<u8>) -> Vec<u8> {
x.clone()
}

fn string_roundtrip(x: String) -> String {
x.clone()
}
Expand Down

0 comments on commit d9cf1f0

Please sign in to comment.