Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Drop as_uint32() #691

Merged
merged 1 commit into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/unittests/api_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ TEST(api, find_exported_global)

auto opt_global = find_exported_global(*instance, "g1");
ASSERT_TRUE(opt_global);
EXPECT_EQ(as_uint32(*opt_global->value), 0);
EXPECT_EQ(opt_global->value->i32, 0);
EXPECT_EQ(opt_global->type.value_type, ValType::i32);
EXPECT_TRUE(opt_global->type.is_mutable);

Expand Down
8 changes: 3 additions & 5 deletions test/unittests/execute_call_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ TEST(execute_call, imported_function_call_with_arguments)

const auto module = parse(wasm);

auto host_foo = [](Instance&, const Value* args, int) { return Value{as_uint32(args[0]) * 2}; };
auto host_foo = [](Instance&, const Value* args, int) { return Value{args[0].i32 * 2}; };
const auto host_foo_type = module->typesec[0];

auto instance = instantiate(*module, {{host_foo, host_foo_type}});
Expand Down Expand Up @@ -433,12 +433,10 @@ TEST(execute_call, imported_functions_call_indirect)
ASSERT_EQ(module->codesec.size(), 2);

constexpr auto sqr = [](Instance&, const Value* args, int) {
const auto x = as_uint32(args[0]);
return Value{uint64_t{x} * uint64_t{x}};
return Value{uint64_t{args[0].i32} * uint64_t{args[0].i32}};
};
constexpr auto isqrt = [](Instance&, const Value* args, int) {
const auto x = as_uint32(args[0]);
return Value{(11 + uint64_t{x} / 11) / 2};
return Value{(11 + uint64_t{args[0].i32} / 11) / 2};
};

auto instance = instantiate(*module, {{sqr, module->typesec[0]}, {isqrt, module->typesec[0]}});
Expand Down
20 changes: 10 additions & 10 deletions test/unittests/execute_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ TEST(execute, global_set)

auto instance = instantiate(parse(wasm));
EXPECT_THAT(execute(*instance, 0, {}), Result());
EXPECT_EQ(as_uint32(instance->globals[0]), 42);
EXPECT_EQ(instance->globals[0].i32, 42);
}

TEST(execute, global_set_two_globals)
Expand All @@ -283,8 +283,8 @@ TEST(execute, global_set_two_globals)

auto instance = instantiate(parse(wasm));
EXPECT_THAT(execute(*instance, 0, {}), Result());
EXPECT_EQ(as_uint32(instance->globals[0]), 44);
EXPECT_EQ(as_uint32(instance->globals[1]), 45);
EXPECT_EQ(instance->globals[0].i32, 44);
EXPECT_EQ(instance->globals[1].i32, 45);
}

TEST(execute, global_set_imported)
Expand All @@ -303,7 +303,7 @@ TEST(execute, global_set_imported)
auto instance =
instantiate(parse(wasm), {}, {}, {}, {ExternalGlobal{&global_value, {ValType::i32, true}}});
EXPECT_THAT(execute(*instance, 0, {}), Result());
EXPECT_EQ(as_uint32(global_value), 42);
EXPECT_EQ(global_value.i32, 42);
}

TEST(execute, global_set_float)
Expand Down Expand Up @@ -811,7 +811,7 @@ TEST(execute, imported_function)
ASSERT_EQ(module->typesec.size(), 1);

constexpr auto host_foo = [](Instance&, const Value* args, int) {
return Value{as_uint32(args[0]) + as_uint32(args[1])};
return Value{args[0].i32 + args[1].i32};
};

auto instance = instantiate(*module, {{host_foo, module->typesec[0]}});
Expand All @@ -831,10 +831,10 @@ TEST(execute, imported_two_functions)
ASSERT_EQ(module->typesec.size(), 1);

constexpr auto host_foo1 = [](Instance&, const Value* args, int) {
return Value{as_uint32(args[0]) + as_uint32(args[1])};
return Value{args[0].i32 + args[1].i32};
};
constexpr auto host_foo2 = [](Instance&, const Value* args, int) {
return Value{as_uint32(args[0]) * as_uint32(args[1])};
return Value{args[0].i32 * args[1].i32};
};

auto instance =
Expand All @@ -858,10 +858,10 @@ TEST(execute, imported_functions_and_regular_one)
"000a0901070041aa80a8010b");

constexpr auto host_foo1 = [](Instance&, const Value* args, int) {
return Value{as_uint32(args[0]) + as_uint32(args[1])};
return Value{args[0].i32 + args[1].i32};
};
constexpr auto host_foo2 = [](Instance&, const Value* args, int) {
return Value{as_uint32(args[0]) * as_uint32(args[1])};
return Value{args[0].i32 * args[1].i32};
};

const auto module = parse(wasm);
Expand All @@ -888,7 +888,7 @@ TEST(execute, imported_two_functions_different_type)
"0001030201010a0901070042aa80a8010b");

constexpr auto host_foo1 = [](Instance&, const Value* args, int) {
return Value{as_uint32(args[0]) + as_uint32(args[1])};
return Value{args[0].i32 + args[1].i32};
};
constexpr auto host_foo2 = [](Instance&, const Value* args, int) {
return Value{args[0].i64 * args[0].i64};
Expand Down
22 changes: 11 additions & 11 deletions test/unittests/instantiate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ uint32_t call_table_func(Instance& instance, size_t idx)
const auto& elem = (*instance.table)[idx];
const auto res = execute(*elem.instance, elem.func_idx, {}, 0);
EXPECT_TRUE(res.has_value);
return as_uint32(res.value);
return res.value.i32;
}
} // namespace

Expand Down Expand Up @@ -319,7 +319,7 @@ TEST(instantiate, imported_globals)
ASSERT_EQ(instance->imported_globals.size(), 1);
EXPECT_EQ(instance->imported_globals[0].type.value_type, ValType::i32);
EXPECT_TRUE(instance->imported_globals[0].type.is_mutable);
EXPECT_EQ(as_uint32(*instance->imported_globals[0].value), 42);
EXPECT_EQ(instance->imported_globals[0].value->i32, 42);
ASSERT_EQ(instance->globals.size(), 0);
}

Expand All @@ -342,8 +342,8 @@ TEST(instantiate, imported_globals_multiple)
EXPECT_TRUE(instance->imported_globals[0].type.is_mutable);
EXPECT_EQ(instance->imported_globals[1].type.value_type, ValType::i32);
EXPECT_FALSE(instance->imported_globals[1].type.is_mutable);
EXPECT_EQ(as_uint32(*instance->imported_globals[0].value), 42);
EXPECT_EQ(as_uint32(*instance->imported_globals[1].value), 43);
EXPECT_EQ(instance->imported_globals[0].value->i32, 42);
EXPECT_EQ(instance->imported_globals[1].value->i32, 43);
ASSERT_EQ(instance->globals.size(), 0);
}

Expand Down Expand Up @@ -864,7 +864,7 @@ TEST(instantiate, globals_single)
auto instance = instantiate(*module);

ASSERT_EQ(instance->globals.size(), 1);
EXPECT_EQ(as_uint32(instance->globals[0]), 42);
EXPECT_EQ(instance->globals[0].i32, 42);
}

TEST(instantiate, globals_multiple)
Expand All @@ -878,8 +878,8 @@ TEST(instantiate, globals_multiple)
auto instance = instantiate(*module);

ASSERT_EQ(instance->globals.size(), 2);
EXPECT_EQ(as_uint32(instance->globals[0]), 42);
EXPECT_EQ(as_uint32(instance->globals[1]), 43);
EXPECT_EQ(instance->globals[0].i32, 42);
EXPECT_EQ(instance->globals[1].i32, 43);
}

TEST(instantiate, globals_with_imported)
Expand All @@ -898,11 +898,11 @@ TEST(instantiate, globals_with_imported)
auto instance = instantiate(parse(bin), {}, {}, {}, {g});

ASSERT_EQ(instance->imported_globals.size(), 1);
EXPECT_EQ(as_uint32(*instance->imported_globals[0].value), 41);
EXPECT_EQ(instance->imported_globals[0].value->i32, 41);
EXPECT_EQ(instance->imported_globals[0].type.is_mutable, true);
ASSERT_EQ(instance->globals.size(), 2);
EXPECT_EQ(as_uint32(instance->globals[0]), 42);
EXPECT_EQ(as_uint32(instance->globals[1]), 43);
EXPECT_EQ(instance->globals[0].i32, 42);
EXPECT_EQ(instance->globals[1].i32, 43);
}

TEST(instantiate, globals_initialized_from_imported)
Expand All @@ -919,7 +919,7 @@ TEST(instantiate, globals_initialized_from_imported)
auto instance = instantiate(parse(bin), {}, {}, {}, {g});

ASSERT_EQ(instance->globals.size(), 1);
EXPECT_EQ(as_uint32(instance->globals[0]), 42);
EXPECT_EQ(instance->globals[0].i32, 42);
}

TEST(instantiate, globals_float)
Expand Down
14 changes: 7 additions & 7 deletions test/unittests/parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ TEST(parser, global_single_mutable_const_inited)
EXPECT_TRUE(module->globalsec[0].type.is_mutable);
EXPECT_EQ(module->globalsec[0].type.value_type, ValType::i32);
EXPECT_EQ(module->globalsec[0].expression.kind, ConstantExpression::Kind::Constant);
EXPECT_EQ(as_uint32(module->globalsec[0].expression.value.constant), 0x10);
EXPECT_EQ(module->globalsec[0].expression.value.constant.i32, 0x10);
}

TEST(parser, global_multi_global_inited)
Expand Down Expand Up @@ -625,11 +625,11 @@ TEST(parser, global_multi_const_inited)
EXPECT_FALSE(module->globalsec[0].type.is_mutable);
EXPECT_EQ(module->globalsec[0].type.value_type, ValType::i32);
EXPECT_EQ(module->globalsec[0].expression.kind, ConstantExpression::Kind::Constant);
EXPECT_EQ(as_uint32(module->globalsec[0].expression.value.constant), 1);
EXPECT_EQ(module->globalsec[0].expression.value.constant.i32, 1);
EXPECT_TRUE(module->globalsec[1].type.is_mutable);
EXPECT_EQ(module->globalsec[1].type.value_type, ValType::i32);
EXPECT_EQ(module->globalsec[1].expression.kind, ConstantExpression::Kind::Constant);
EXPECT_EQ(as_uint32(module->globalsec[1].expression.value.constant), uint32_t(-1));
EXPECT_EQ(module->globalsec[1].expression.value.constant.i32, -1);
}

TEST(parser, global_float)
Expand Down Expand Up @@ -909,12 +909,12 @@ TEST(parser, element_section)

ASSERT_EQ(module->elementsec.size(), 3);
EXPECT_EQ(module->elementsec[0].offset.kind, ConstantExpression::Kind::Constant);
EXPECT_EQ(as_uint32(module->elementsec[0].offset.value.constant), 1);
EXPECT_EQ(module->elementsec[0].offset.value.constant.i32, 1);
ASSERT_EQ(module->elementsec[0].init.size(), 2);
EXPECT_EQ(module->elementsec[0].init[0], 0);
EXPECT_EQ(module->elementsec[0].init[1], 1);
EXPECT_EQ(module->elementsec[1].offset.kind, ConstantExpression::Kind::Constant);
EXPECT_EQ(as_uint32(module->elementsec[1].offset.value.constant), 2);
EXPECT_EQ(module->elementsec[1].offset.value.constant.i32, 2);
ASSERT_EQ(module->elementsec[1].init.size(), 2);
EXPECT_EQ(module->elementsec[1].init[0], 2);
EXPECT_EQ(module->elementsec[1].init[1], 3);
Expand Down Expand Up @@ -1311,10 +1311,10 @@ TEST(parser, data_section)

ASSERT_EQ(module->datasec.size(), 3);
EXPECT_EQ(module->datasec[0].offset.kind, ConstantExpression::Kind::Constant);
EXPECT_EQ(as_uint32(module->datasec[0].offset.value.constant), 1);
EXPECT_EQ(module->datasec[0].offset.value.constant.i32, 1);
EXPECT_EQ(module->datasec[0].init, "aaff"_bytes);
EXPECT_EQ(module->datasec[1].offset.kind, ConstantExpression::Kind::Constant);
EXPECT_EQ(as_uint32(module->datasec[1].offset.value.constant), 2);
EXPECT_EQ(module->datasec[1].offset.value.constant.i32, 2);
EXPECT_EQ(module->datasec[1].init, "5555"_bytes);
EXPECT_EQ(module->datasec[2].offset.kind, ConstantExpression::Kind::GlobalGet);
EXPECT_EQ(module->datasec[2].offset.value.global_index, 0);
Expand Down
5 changes: 0 additions & 5 deletions test/utils/asserts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,5 @@ std::ostream& operator<<(std::ostream& os, ExecutionResult);

namespace fizzy::test
{
inline uint32_t as_uint32(fizzy::Value value)
{
return value.i32;
}

std::ostream& operator<<(std::ostream& os, const TypedExecutionResult&);
} // namespace fizzy::test