Skip to content

Commit f15c667

Browse files
authored
Merge pull request #46 from toppers/add_test_project_of_hako_asset
Add test project of hako_asset library
2 parents 21ab9bf + 89ad504 commit f15c667

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

test/config.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"AssetDumy": [
3+
{
4+
"name": "AssetDumy"
5+
}
6+
]
7+
}

test/hakoAsset-01/CMakeLists.txt

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
# Set the project name and the version of C++ to use.
4+
project(hako_asset_test_project VERSION 1.0 LANGUAGES CXX)
5+
6+
# Set the C++ standard.
7+
set(CMAKE_CXX_STANDARD 11)
8+
set(CMAKE_CXX_STANDARD_REQUIRED True)
9+
10+
set(TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR})
11+
set(HOME_DIR ${TEST_DIR}/../..)
12+
13+
# Download and build Google Test.
14+
include(FetchContent)
15+
FetchContent_Declare(
16+
googletest
17+
GIT_REPOSITORY https://github.com/google/googletest.git
18+
GIT_TAG release-1.10.0
19+
)
20+
FetchContent_MakeAvailable(googletest)
21+
22+
include_directories(
23+
${HOME_DIR}/src/include
24+
${HOME_DIR}/core/src/include
25+
${HOME_DIR}/core/third-party/json/include
26+
)
27+
28+
# Specify the source code for the test and the test code.
29+
set(SOURCE_FILES
30+
${HOME_DIR}/src/assets/src/hako_asset.cpp
31+
${HOME_DIR}/src/assets/src/hako_asset_impl.cpp
32+
./test.cpp
33+
)
34+
35+
link_directories(/usr/local/lib/hakoniwa)
36+
37+
# Generate an executable file.
38+
add_executable(test ${SOURCE_FILES})
39+
40+
# Link with the Google Test and hakoniwa core library.
41+
target_link_libraries( test
42+
gtest_main
43+
assets
44+
conductor
45+
)

test/hakoAsset-01/test.cpp

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "gtest/gtest.h"
2+
#include <thread>
3+
#include <chrono>
4+
// ここにはテスト対象のヘッダーファイルをインクルードします。
5+
#include "hako_asset.h"
6+
#include "hako_conductor.h"
7+
8+
// コールバック関数の実装
9+
static int my_on_initialize(hako_asset_context_t* context)
10+
{
11+
std::cout << "INFO: my_on_initialize enter" << std::endl;
12+
return 0;
13+
}
14+
15+
static int my_on_reset(hako_asset_context_t* context)
16+
{
17+
std::cout << "INFO: my_on_reset enter" << std::endl;
18+
return 0;
19+
}
20+
21+
static int my_on_simulation_step(hako_asset_context_t* context)
22+
{
23+
std::cout << "INFO: on_simulation_step enter: " << hako_asset_simulation_time() << std::endl;
24+
std::cout << "INFO: sleep 1sec" << std::endl;
25+
usleep(1000*1000);
26+
std::cout << "INFO: on_simulation_step exit" << std::endl;
27+
return 0;
28+
}
29+
30+
hako_asset_callbacks_t callbacks = {
31+
.on_initialize = my_on_initialize,
32+
.on_simulation_step = my_on_simulation_step,
33+
.on_reset = my_on_reset
34+
};
35+
36+
hako_time_t delta_time_usec = 1000 * 1000 * 100;
37+
38+
void run_hako_cmd_sequence() {
39+
std::this_thread::sleep_for(std::chrono::seconds(1));
40+
std::system("/usr/local/bin/hakoniwa/hako-cmd start");
41+
std::this_thread::sleep_for(std::chrono::seconds(10));
42+
std::system("/usr/local/bin/hakoniwa/hako-cmd stop");
43+
std::this_thread::sleep_for(std::chrono::seconds(1));
44+
std::system("/usr/local/bin/hakoniwa/hako-cmd reset");
45+
}
46+
47+
TEST(HakoCApiTest, AssetRegist) {
48+
hako_conductor_start(delta_time_usec, delta_time_usec);
49+
int result = hako_asset_register("AssetDumy", "../../config.json", &callbacks, 1000, HAKO_ASSET_MODEL_CONTROLLER);
50+
ASSERT_EQ(result, 0);
51+
}
52+
53+
TEST(HakoCApiTest, AssetStart) {
54+
// Execute the external command to launch the sandbox in a separate thread
55+
std::thread t(run_hako_cmd_sequence);
56+
t.detach();
57+
58+
int result = hako_asset_start();
59+
ASSERT_EQ(result, EINTR); // Resetで終了するのでEINTRが返る
60+
}
61+
62+
TEST(HakoCApiTest, ConductorStop) {
63+
hako_conductor_stop();
64+
// ASSERT_EQ(result, 0);
65+
}
66+
67+
68+
69+
int main(int argc, char **argv) {
70+
::testing::InitGoogleTest(&argc, argv);
71+
return RUN_ALL_TESTS();
72+
}

0 commit comments

Comments
 (0)