Skip to content

Commit

Permalink
test(c-api) Add test case for wat2wasm.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Oct 6, 2020
1 parent 264ed83 commit c3efc06
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/c-api/tests/wasm_c_api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ add_executable(wasm-c-api-serialize wasm-c-api/example/serialize.c)
add_executable(wasm-c-api-trap wasm-c-api/example/trap.c)

# Our additional tests.
add_executable(test-wasi test-wasi.c)
add_executable(test-early-exit test-early-exit.c)
add_executable(test-memory test-memory.c)
add_executable(test-wasi test-wasi.c)
add_executable(test-wat2wasm test-wat2wasm.c)

include_directories(wasm-c-api/include)
include_directories(../../)
Expand Down Expand Up @@ -133,11 +134,6 @@ add_test(NAME wasm-c-api-trap
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/wasm-c-api/example/
)

set_property(TARGET test-wasi PROPERTY C_STANDARD 11)
target_link_libraries(test-wasi general ${WASMER_LIB})
target_compile_options(test-wasi PRIVATE ${COMPILER_OPTIONS})
add_test(test-wasi test-wasi)

set_property(TARGET test-early-exit PROPERTY C_STANDARD 11)
target_link_libraries(test-early-exit general ${WASMER_LIB})
target_compile_options(test-early-exit PRIVATE ${COMPILER_OPTIONS})
Expand All @@ -147,3 +143,13 @@ set_property(TARGET test-memory PROPERTY C_STANDARD 11)
target_link_libraries(test-memory general ${WASMER_LIB})
target_compile_options(test-memory PRIVATE ${COMPILER_OPTIONS})
add_test(test-memory test-memory)

set_property(TARGET test-wasi PROPERTY C_STANDARD 11)
target_link_libraries(test-wasi general ${WASMER_LIB})
target_compile_options(test-wasi PRIVATE ${COMPILER_OPTIONS})
add_test(test-wasi test-wasi)

set_property(TARGET test-wat2wasm PROPERTY C_STANDARD 11)
target_link_libraries(test-wat2wasm general ${WASMER_LIB})
target_compile_options(test-wat2wasm PRIVATE ${COMPILER_OPTIONS})
add_test(test-wat2wasm test-wat2wasm)
52 changes: 52 additions & 0 deletions lib/c-api/tests/wasm_c_api/test-wat2wasm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include "wasmer_wasm.h"

#define own

int main(int argc, const char* argv[]) {
// Initialize.
printf("Initializing...\n");
wasm_engine_t* engine = wasm_engine_new();
wasm_store_t* store = wasm_store_new(engine);

// Getting Wasm.
printf("Compiling WAT to Wasm...\n");

wasm_byte_vec_t wat = {
.data = "(module)",
.size = 8,
};
wasm_byte_vec_t *wasm = wat2wasm(&wat);

if (!wasm) {
printf("> Error compiler WAT to Wasm!\n");
return 1;
}

if (wasm->size != 8) {
printf("The Wasm size is incorrect!\n");
return 1;
}

if (!(wasm->data[0] == 0 &&
wasm->data[1] == 'a' &&
wasm->data[2] == 's' &&
wasm->data[3] == 'm' &&
wasm->data[4] == 1 &&
wasm->data[5] == 0 &&
wasm->data[6] == 0 &&
wasm->data[7] == 0)) {
printf("The Wasm data is incorrect!\n");
return 1;
}

wasm_byte_vec_delete(wasm);

// All done.
printf("Done.\n");
return 0;
}

0 comments on commit c3efc06

Please sign in to comment.