133eb0b6dSopenharmony_ci/*
233eb0b6dSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
333eb0b6dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
433eb0b6dSopenharmony_ci * you may not use this file except in compliance with the License.
533eb0b6dSopenharmony_ci * You may obtain a copy of the License at
633eb0b6dSopenharmony_ci *
733eb0b6dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
833eb0b6dSopenharmony_ci *
933eb0b6dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1033eb0b6dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1133eb0b6dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1233eb0b6dSopenharmony_ci * See the License for the specific language governing permissions and
1333eb0b6dSopenharmony_ci * limitations under the License.
1433eb0b6dSopenharmony_ci */
1533eb0b6dSopenharmony_ci
1633eb0b6dSopenharmony_ci#include "test.h"
1733eb0b6dSopenharmony_ci
1833eb0b6dSopenharmony_ci#include "jerryscript_native_engine.h"
1933eb0b6dSopenharmony_ci
2033eb0b6dSopenharmony_cistatic NativeEngine* g_nativeEngine = nullptr;
2133eb0b6dSopenharmony_cistatic constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
2233eb0b6dSopenharmony_ciNativeEngineTest::NativeEngineTest()
2333eb0b6dSopenharmony_ci{
2433eb0b6dSopenharmony_ci    engine_ = g_nativeEngine;
2533eb0b6dSopenharmony_ci}
2633eb0b6dSopenharmony_ci
2733eb0b6dSopenharmony_ciNativeEngineTest::~NativeEngineTest() {}
2833eb0b6dSopenharmony_ci
2933eb0b6dSopenharmony_cistatic void* context_alloc_fn(size_t size, void* cb_data)
3033eb0b6dSopenharmony_ci{
3133eb0b6dSopenharmony_ci    (void)cb_data;
3233eb0b6dSopenharmony_ci    size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
3333eb0b6dSopenharmony_ci    return malloc(newSize);
3433eb0b6dSopenharmony_ci}
3533eb0b6dSopenharmony_cistatic void LoopNewThread(void* data)
3633eb0b6dSopenharmony_ci{
3733eb0b6dSopenharmony_ci    g_nativeEngine->Loop(LOOP_DEFAULT);
3833eb0b6dSopenharmony_ci}
3933eb0b6dSopenharmony_ciint main(int argc, char** argv)
4033eb0b6dSopenharmony_ci{
4133eb0b6dSopenharmony_ci    testing::GTEST_FLAG(output) = "xml:./";
4233eb0b6dSopenharmony_ci    testing::InitGoogleTest(&argc, argv);
4333eb0b6dSopenharmony_ci    // allocate 50MB space
4433eb0b6dSopenharmony_ci    jerry_context_t* ctx_p = jerry_create_context(1024 * 1024 * 50, context_alloc_fn, NULL);
4533eb0b6dSopenharmony_ci    jerry_port_default_set_current_context(ctx_p);
4633eb0b6dSopenharmony_ci    jerry_init(jerry_init_flag_t::JERRY_INIT_EMPTY);
4733eb0b6dSopenharmony_ci    g_nativeEngine = new JerryScriptNativeEngine(0); // default instance id 0
4833eb0b6dSopenharmony_ci    uv_thread_t tid;
4933eb0b6dSopenharmony_ci    uv_thread_create(&tid, LoopNewThread, nullptr);
5033eb0b6dSopenharmony_ci    int ret = RUN_ALL_TESTS();
5133eb0b6dSopenharmony_ci    uv_thread_join(&tid);
5233eb0b6dSopenharmony_ci    delete g_nativeEngine;
5333eb0b6dSopenharmony_ci    g_nativeEngine = nullptr;
5433eb0b6dSopenharmony_ci    jerry_cleanup();
5533eb0b6dSopenharmony_ci    free(ctx_p);
5633eb0b6dSopenharmony_ci    return ret;
5733eb0b6dSopenharmony_ci}
58