From 6a2a62cd89a6e0db9a43549273c8253580aca480 Mon Sep 17 00:00:00 2001 From: YiYing He Date: Mon, 1 Feb 2021 19:53:52 +0800 Subject: [PATCH] [Test] Add memory page limitation test. --- test/CMakeLists.txt | 1 + test/memlimit/CMakeLists.txt | 13 ++++++++++ test/memlimit/MemLimitTest.cpp | 44 ++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 test/memlimit/CMakeLists.txt create mode 100644 test/memlimit/MemLimitTest.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index fa05d61..e295d3a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -13,6 +13,7 @@ add_subdirectory(externref) add_subdirectory(expected) add_subdirectory(span) add_subdirectory(po) +add_subdirectory(memlimit) if(BUILD_COVERAGE) setup_target_for_coverage_gcovr_html( diff --git a/test/memlimit/CMakeLists.txt b/test/memlimit/CMakeLists.txt new file mode 100644 index 0000000..2b03965 --- /dev/null +++ b/test/memlimit/CMakeLists.txt @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: Apache-2.0 + +add_executable(ssvmMemLimitTests + MemLimitTest.cpp +) + +add_test(ssvmMemLimitTests ssvmMemLimitTests) + +target_link_libraries(ssvmMemLimitTests + PRIVATE + utilGoogleTest + ssvmVM +) diff --git a/test/memlimit/MemLimitTest.cpp b/test/memlimit/MemLimitTest.cpp new file mode 100644 index 0000000..57a58fa --- /dev/null +++ b/test/memlimit/MemLimitTest.cpp @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: Apache-2.0 +#include "common/configure.h" +#include "runtime/instance/memory.h" + +#include "gtest/gtest.h" + +namespace { + +TEST(MemLimitTest, Limit__Pages) { + using MemInst = SSVM::Runtime::Instance::MemoryInstance; + SSVM::Configure Conf; + Conf.setMaxMemoryPage(256); + + SSVM::AST::Limit Lim1(257); + MemInst Inst1(Lim1, Conf.getMaxMemoryPage()); + ASSERT_TRUE(Inst1.getDataPtr() == nullptr); + + MemInst Inst2(Lim1); + ASSERT_FALSE(Inst2.getDataPtr() == nullptr); + + SSVM::AST::Limit Lim2(1); + MemInst Inst3(Lim2, Conf.getMaxMemoryPage()); + ASSERT_FALSE(Inst3.getDataPtr() == nullptr); + ASSERT_FALSE(Inst3.growPage(256)); + ASSERT_TRUE(Inst3.growPage(255)); + + MemInst Inst4(Lim2); + ASSERT_FALSE(Inst4.getDataPtr() == nullptr); + ASSERT_TRUE(Inst4.growPage(256)); + + SSVM::AST::Limit Lim3(1, 128); + MemInst Inst5(Lim3, Conf.getMaxMemoryPage()); + ASSERT_FALSE(Inst5.getDataPtr() == nullptr); + ASSERT_FALSE(Inst5.growPage(128)); + ASSERT_TRUE(Inst5.growPage(127)); +} + +} // namespace + +GTEST_API_ int main(int argc, char **argv) { + SSVM::Log::setErrorLoggingLevel(); + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} -- GitLab