gc_test.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "gtest/gtest.h"
  6. #include "bh_platform.h"
  7. #include "bh_read_file.h"
  8. #include "wasm_export.h"
  9. class WasmGCTest : public testing::Test
  10. {
  11. private:
  12. std::string get_binary_path()
  13. {
  14. char cwd[1024] = { 0 };
  15. if (readlink("/proc/self/exe", cwd, 1024) <= 0) {
  16. return NULL;
  17. }
  18. char *path_end = strrchr(cwd, '/');
  19. if (path_end != NULL) {
  20. *path_end = '\0';
  21. }
  22. return std::string(cwd);
  23. }
  24. protected:
  25. void SetUp()
  26. {
  27. CWD = get_binary_path();
  28. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  29. init_args.mem_alloc_type = Alloc_With_Pool;
  30. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  31. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  32. ASSERT_EQ(wasm_runtime_full_init(&init_args), true);
  33. cleanup = true;
  34. }
  35. void TearDown()
  36. {
  37. if (cleanup) {
  38. wasm_runtime_destroy();
  39. }
  40. }
  41. public:
  42. bool load_wasm_file(const char *wasm_file)
  43. {
  44. const char *file;
  45. unsigned char *wasm_file_buf;
  46. uint32 wasm_file_size;
  47. file = strdup((CWD + "/" + wasm_file).c_str());
  48. wasm_file_buf =
  49. (unsigned char *)bh_read_file_to_buffer(file, &wasm_file_size);
  50. if (!wasm_file_buf)
  51. return false;
  52. module = wasm_runtime_load(wasm_file_buf, wasm_file_size, error_buf,
  53. sizeof(error_buf));
  54. if (!module)
  55. return false;
  56. return true;
  57. }
  58. public:
  59. std::string CWD;
  60. RuntimeInitArgs init_args;
  61. wasm_module_t module = NULL;
  62. wasm_module_inst_t module_inst = NULL;
  63. wasm_function_inst_t func_inst = NULL;
  64. wasm_exec_env_t exec_env = NULL;
  65. char error_buf[128];
  66. char global_heap_buf[512 * 1024];
  67. bool cleanup = true;
  68. };
  69. TEST_F(WasmGCTest, Test_app1)
  70. {
  71. ASSERT_TRUE(load_wasm_file("test1.wasm"));
  72. ASSERT_TRUE(load_wasm_file("test2.wasm"));
  73. ASSERT_TRUE(load_wasm_file("test3.wasm"));
  74. ASSERT_TRUE(load_wasm_file("test4.wasm"));
  75. ASSERT_TRUE(load_wasm_file("test5.wasm"));
  76. ASSERT_TRUE(load_wasm_file("test6.wasm"));
  77. ASSERT_TRUE(load_wasm_file("struct1.wasm"));
  78. ASSERT_TRUE(load_wasm_file("struct2.wasm"));
  79. ASSERT_TRUE(load_wasm_file("struct3.wasm"));
  80. ASSERT_TRUE(load_wasm_file("func1.wasm"));
  81. ASSERT_TRUE(load_wasm_file("func2.wasm"));
  82. }