Skip to content

Commit

Permalink
test(runtime-c-api) Use calloc and simplify code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Sep 6, 2019
1 parent a67ce53 commit 1f1ecb7
Showing 1 changed file with 34 additions and 77 deletions.
111 changes: 34 additions & 77 deletions lib/runtime-c-api/tests/test-exports.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "../wasmer.h"
#include <assert.h>
#include <stdint.h>
#include <string.h>

int main()
{
Expand Down Expand Up @@ -43,17 +44,8 @@ int main()

wasmer_byte_array name_bytes = wasmer_export_name(export);

assert(name_bytes.bytes_len == 3);

char expected[] = {'s', 'u', 'm'};

printf("Read export name:\n");

for (uint32_t idx = 0; idx < name_bytes.bytes_len; idx++) {
printf("%c\n", name_bytes.bytes[idx]);

assert(name_bytes.bytes[idx] == expected[idx]);
}
assert(name_bytes.bytes_len == sizeof("sum") - 1);
assert(memcmp(name_bytes.bytes, "sum", sizeof("sum") - 1) == 0);

printf("Check arity\n");

Expand All @@ -68,15 +60,15 @@ int main()

printf("Check signature\n");

wasmer_value_tag *input_types = (wasmer_value_tag *) malloc(sizeof(wasmer_value_tag) * inputs_arity);
wasmer_export_func_params(exported_function, input_types , inputs_arity);
wasmer_value_tag *input_types = (wasmer_value_tag *) calloc(inputs_arity, sizeof(wasmer_value_tag));
wasmer_export_func_params(exported_function, input_types, inputs_arity);

assert(input_types[0] == WASM_I32);
assert(input_types[1] == WASM_I32);

free(input_types);

wasmer_value_tag *output_types = (wasmer_value_tag *) malloc(sizeof(wasmer_value_tag) * outputs_arity);
wasmer_value_tag *output_types = (wasmer_value_tag *) calloc(outputs_arity, sizeof(wasmer_value_tag));
wasmer_export_func_returns(exported_function, output_types, outputs_arity);

assert(output_types[0] == WASM_I32);
Expand Down Expand Up @@ -118,13 +110,8 @@ int main()
const wasmer_export_func_t *exported_function = wasmer_export_to_func(export);
wasmer_byte_array name_bytes = wasmer_export_name(export);

assert(name_bytes.bytes_len == 7);

char expected[] = {'a', 'r', 'i', 't', 'y', '_', '0'};

for (uint32_t idx = 0; idx < name_bytes.bytes_len; idx++) {
assert(name_bytes.bytes[idx] == expected[idx]);
}
assert(name_bytes.bytes_len == sizeof("arity_0") - 1);
assert(memcmp(name_bytes.bytes, "arity_0", sizeof("arity_0") - 1) == 0);

uint32_t inputs_arity;
wasmer_export_func_params_arity(exported_function, &inputs_arity);
Expand All @@ -135,7 +122,7 @@ int main()
assert(inputs_arity == 0);
assert(outputs_arity == 1);

wasmer_value_tag *output_types = (wasmer_value_tag *) malloc(sizeof(wasmer_value_tag) * outputs_arity);
wasmer_value_tag *output_types = (wasmer_value_tag *) calloc(outputs_arity, sizeof(wasmer_value_tag));
wasmer_export_func_returns(exported_function, output_types, outputs_arity);

assert(output_types[0] == WASM_I32);
Expand Down Expand Up @@ -166,13 +153,8 @@ int main()
const wasmer_export_func_t *exported_function = wasmer_export_to_func(export);
wasmer_byte_array name_bytes = wasmer_export_name(export);

assert(name_bytes.bytes_len == 7);

char expected[] = {'i', '3', '2', '_', 'i', '3', '2'};

for (uint32_t idx = 0; idx < name_bytes.bytes_len; idx++) {
assert(name_bytes.bytes[idx] == expected[idx]);
}
assert(name_bytes.bytes_len == sizeof("i32_i32") - 1);
assert(memcmp(name_bytes.bytes, "i32_i32", sizeof("i32_i32") - 1) == 0);

uint32_t inputs_arity;
wasmer_export_func_params_arity(exported_function, &inputs_arity);
Expand All @@ -183,14 +165,14 @@ int main()
assert(inputs_arity == 1);
assert(outputs_arity == 1);

wasmer_value_tag *input_types = (wasmer_value_tag *) malloc(sizeof(wasmer_value_tag) * inputs_arity);
wasmer_export_func_params(exported_function, input_types , inputs_arity);
wasmer_value_tag *input_types = (wasmer_value_tag *) calloc(inputs_arity, sizeof(wasmer_value_tag));
wasmer_export_func_params(exported_function, input_types, inputs_arity);

assert(input_types[0] == WASM_I32);

free(input_types);

wasmer_value_tag *output_types = (wasmer_value_tag *) malloc(sizeof(wasmer_value_tag) * outputs_arity);
wasmer_value_tag *output_types = (wasmer_value_tag *) calloc(outputs_arity, sizeof(wasmer_value_tag));
wasmer_export_func_returns(exported_function, output_types, outputs_arity);

assert(output_types[0] == WASM_I32);
Expand Down Expand Up @@ -224,13 +206,8 @@ int main()
const wasmer_export_func_t *exported_function = wasmer_export_to_func(export);
wasmer_byte_array name_bytes = wasmer_export_name(export);

assert(name_bytes.bytes_len == 7);

char expected[] = {'i', '6', '4', '_', 'i', '6', '4'};

for (uint32_t idx = 0; idx < name_bytes.bytes_len; idx++) {
assert(name_bytes.bytes[idx] == expected[idx]);
}
assert(name_bytes.bytes_len == sizeof("i64_i64") - 1);
assert(memcmp(name_bytes.bytes, "i64_i64", sizeof("i64_i64") - 1) == 0);

uint32_t inputs_arity;
wasmer_export_func_params_arity(exported_function, &inputs_arity);
Expand All @@ -241,14 +218,14 @@ int main()
assert(inputs_arity == 1);
assert(outputs_arity == 1);

wasmer_value_tag *input_types = (wasmer_value_tag *) malloc(sizeof(wasmer_value_tag) * inputs_arity);
wasmer_export_func_params(exported_function, input_types , inputs_arity);
wasmer_value_tag *input_types = (wasmer_value_tag *) calloc(inputs_arity, sizeof(wasmer_value_tag));
wasmer_export_func_params(exported_function, input_types, inputs_arity);

assert(input_types[0] == WASM_I64);

free(input_types);

wasmer_value_tag *output_types = (wasmer_value_tag *) malloc(sizeof(wasmer_value_tag) * outputs_arity);
wasmer_value_tag *output_types = (wasmer_value_tag *) calloc(outputs_arity, sizeof(wasmer_value_tag));
wasmer_export_func_returns(exported_function, output_types, outputs_arity);

assert(output_types[0] == WASM_I64);
Expand Down Expand Up @@ -282,13 +259,8 @@ int main()
const wasmer_export_func_t *exported_function = wasmer_export_to_func(export);
wasmer_byte_array name_bytes = wasmer_export_name(export);

assert(name_bytes.bytes_len == 7);

char expected[] = {'f', '3', '2', '_', 'f', '3', '2'};

for (uint32_t idx = 0; idx < name_bytes.bytes_len; idx++) {
assert(name_bytes.bytes[idx] == expected[idx]);
}
assert(name_bytes.bytes_len == sizeof("f32_f32") - 1);
assert(memcmp(name_bytes.bytes, "f32_f32", sizeof("f32_f32") - 1) == 0);

uint32_t inputs_arity;
wasmer_export_func_params_arity(exported_function, &inputs_arity);
Expand All @@ -299,14 +271,14 @@ int main()
assert(inputs_arity == 1);
assert(outputs_arity == 1);

wasmer_value_tag *input_types = (wasmer_value_tag *) malloc(sizeof(wasmer_value_tag) * inputs_arity);
wasmer_export_func_params(exported_function, input_types , inputs_arity);
wasmer_value_tag *input_types = (wasmer_value_tag *) calloc(inputs_arity, sizeof(wasmer_value_tag));
wasmer_export_func_params(exported_function, input_types, inputs_arity);

assert(input_types[0] == WASM_F32);

free(input_types);

wasmer_value_tag *output_types = (wasmer_value_tag *) malloc(sizeof(wasmer_value_tag) * outputs_arity);
wasmer_value_tag *output_types = (wasmer_value_tag *) calloc(outputs_arity, sizeof(wasmer_value_tag));
wasmer_export_func_returns(exported_function, output_types, outputs_arity);

assert(output_types[0] == WASM_F32);
Expand Down Expand Up @@ -339,13 +311,8 @@ int main()
const wasmer_export_func_t *exported_function = wasmer_export_to_func(export);
wasmer_byte_array name_bytes = wasmer_export_name(export);

assert(name_bytes.bytes_len == 7);

char expected[] = {'f', '6', '4', '_', 'f', '6', '4'};

for (uint32_t idx = 0; idx < name_bytes.bytes_len; idx++) {
assert(name_bytes.bytes[idx] == expected[idx]);
}
assert(name_bytes.bytes_len == sizeof("f64_f64") - 1);
assert(memcmp(name_bytes.bytes, "f64_f64", sizeof("f64_f64") - 1) == 0);

uint32_t inputs_arity;
wasmer_export_func_params_arity(exported_function, &inputs_arity);
Expand All @@ -356,14 +323,14 @@ int main()
assert(inputs_arity == 1);
assert(outputs_arity == 1);

wasmer_value_tag *input_types = (wasmer_value_tag *) malloc(sizeof(wasmer_value_tag) * inputs_arity);
wasmer_export_func_params(exported_function, input_types , inputs_arity);
wasmer_value_tag *input_types = (wasmer_value_tag *) calloc(inputs_arity, sizeof(wasmer_value_tag));
wasmer_export_func_params(exported_function, input_types, inputs_arity);

assert(input_types[0] == WASM_F64);

free(input_types);

wasmer_value_tag *output_types = (wasmer_value_tag *) malloc(sizeof(wasmer_value_tag) * outputs_arity);
wasmer_value_tag *output_types = (wasmer_value_tag *) calloc(outputs_arity, sizeof(wasmer_value_tag));
wasmer_export_func_returns(exported_function, output_types, outputs_arity);

assert(output_types[0] == WASM_F64);
Expand Down Expand Up @@ -396,13 +363,8 @@ int main()
const wasmer_export_func_t *exported_function = wasmer_export_to_func(export);
wasmer_byte_array name_bytes = wasmer_export_name(export);

assert(name_bytes.bytes_len == 6);

char expected[] = {'s', 't', 'r', 'i', 'n', 'g'};

for (uint32_t idx = 0; idx < name_bytes.bytes_len; idx++) {
assert(name_bytes.bytes[idx] == expected[idx]);
}
assert(name_bytes.bytes_len == sizeof("string") - 1);
assert(memcmp(name_bytes.bytes, "string", sizeof("string") - 1) == 0);

uint32_t inputs_arity;
wasmer_export_func_params_arity(exported_function, &inputs_arity);
Expand All @@ -413,7 +375,7 @@ int main()
assert(inputs_arity == 0);
assert(outputs_arity == 1);

wasmer_value_tag *output_types = (wasmer_value_tag *) malloc(sizeof(wasmer_value_tag) * outputs_arity);
wasmer_value_tag *output_types = (wasmer_value_tag *) calloc(outputs_arity, sizeof(wasmer_value_tag));
wasmer_export_func_returns(exported_function, output_types, outputs_arity);

assert(output_types[0] == WASM_I32);
Expand Down Expand Up @@ -444,13 +406,8 @@ int main()
const wasmer_export_func_t *exported_function = wasmer_export_to_func(export);
wasmer_byte_array name_bytes = wasmer_export_name(export);

assert(name_bytes.bytes_len == 4);

char expected[] = {'v', 'o' , 'i', 'd'};

for (uint32_t idx = 0; idx < name_bytes.bytes_len; idx++) {
assert(name_bytes.bytes[idx] == expected[idx]);
}
assert(name_bytes.bytes_len == sizeof("void") - 1);
assert(memcmp(name_bytes.bytes, "void", sizeof("void") - 1) == 0);

uint32_t inputs_arity;
wasmer_export_func_params_arity(exported_function, &inputs_arity);
Expand Down

0 comments on commit 1f1ecb7

Please sign in to comment.