From 6c4ee85f2977400c282bb0f3e086ca2a91aa9cfa Mon Sep 17 00:00:00 2001 From: Leon Stansfield Date: Sat, 3 Aug 2024 18:18:34 +0100 Subject: [PATCH] Add unit tests for HeightMapShape3D Added test cases for constructor and property getters/setters: - Map width, depth, and data - Minimum and maximum height - Update map data from image --- tests/scene/test_height_map_shape_3d.h | 122 +++++++++++++++++++++++++ tests/test_main.cpp | 1 + 2 files changed, 123 insertions(+) create mode 100644 tests/scene/test_height_map_shape_3d.h diff --git a/tests/scene/test_height_map_shape_3d.h b/tests/scene/test_height_map_shape_3d.h new file mode 100644 index 00000000000..60a8db4e58a --- /dev/null +++ b/tests/scene/test_height_map_shape_3d.h @@ -0,0 +1,122 @@ +/**************************************************************************/ +/* test_height_map_shape_3d.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#ifndef TEST_HEIGHT_MAP_SHAPE_3D_H +#define TEST_HEIGHT_MAP_SHAPE_3D_H + +#include "scene/resources/3d/height_map_shape_3d.h" +#include "scene/resources/image_texture.h" + +#include "tests/test_macros.h" +#include "tests/test_utils.h" + +namespace TestHeightMapShape3D { + +TEST_CASE("[SceneTree][HeightMapShape3D] Constructor") { + Ref height_map_shape = memnew(HeightMapShape3D); + CHECK(height_map_shape->get_map_width() == 2); + CHECK(height_map_shape->get_map_depth() == 2); + CHECK(height_map_shape->get_map_data().size() == 4); + CHECK(height_map_shape->get_min_height() == 0.0); + CHECK(height_map_shape->get_max_height() == 0.0); +} + +TEST_CASE("[SceneTree][HeightMapShape3D] set_map_width and get_map_width") { + Ref height_map_shape = memnew(HeightMapShape3D); + height_map_shape->set_map_width(10); + CHECK(height_map_shape->get_map_width() == 10); +} + +TEST_CASE("[SceneTree][HeightMapShape3D] set_map_depth and get_map_depth") { + Ref height_map_shape = memnew(HeightMapShape3D); + height_map_shape->set_map_depth(15); + CHECK(height_map_shape->get_map_depth() == 15); +} + +TEST_CASE("[SceneTree][HeightMapShape3D] set_map_data and get_map_data") { + Ref height_map_shape = memnew(HeightMapShape3D); + Vector map_data; + map_data.push_back(1.0); + map_data.push_back(2.0); + height_map_shape->set_map_data(map_data); + CHECK(height_map_shape->get_map_data().size() == 4.0); + CHECK(height_map_shape->get_map_data()[0] == 0.0); + CHECK(height_map_shape->get_map_data()[1] == 0.0); +} + +TEST_CASE("[SceneTree][HeightMapShape3D] get_min_height") { + Ref height_map_shape = memnew(HeightMapShape3D); + height_map_shape->set_map_width(3); + height_map_shape->set_map_depth(1); + height_map_shape->set_map_data(Vector{ 1.0, 2.0, 0.5 }); + CHECK(height_map_shape->get_min_height() == 0.5); +} + +TEST_CASE("[SceneTree][HeightMapShape3D] get_max_height") { + Ref height_map_shape = memnew(HeightMapShape3D); + height_map_shape->set_map_width(3); + height_map_shape->set_map_depth(1); + height_map_shape->set_map_data(Vector{ 1.0, 2.0, 0.5 }); + CHECK(height_map_shape->get_max_height() == 2.0); +} + +TEST_CASE("[SceneTree][HeightMapShape3D] update_map_data_from_image") { + // Create a HeightMapShape3D instance. + Ref height_map_shape = memnew(HeightMapShape3D); + + // Create a mock image with FORMAT_R8 and set its data. + Vector image_data; + image_data.push_back(0); + image_data.push_back(128); + image_data.push_back(255); + image_data.push_back(64); + + Ref image = memnew(Image); + image->set_data(2, 2, false, Image::FORMAT_R8, image_data); + + height_map_shape->update_map_data_from_image(image, 0.0, 10.0); + + // Check the map data. + Vector expected_map_data = { 0.0, 5.0, 10.0, 2.5 }; + Vector actual_map_data = height_map_shape->get_map_data(); + real_t tolerance = 0.1; + + for (int i = 0; i < expected_map_data.size(); ++i) { + CHECK(Math::abs(actual_map_data[i] - expected_map_data[i]) < tolerance); + } + + // Check the min and max heights. + CHECK(height_map_shape->get_min_height() == 0.0); + CHECK(height_map_shape->get_max_height() == 10.0); +} + +} // namespace TestHeightMapShape3D + +#endif // TEST_HEIGHT_MAP_SHAPE_3D_H diff --git a/tests/test_main.cpp b/tests/test_main.cpp index 502aed6a6ea..4487167dbad 100644 --- a/tests/test_main.cpp +++ b/tests/test_main.cpp @@ -155,6 +155,7 @@ #include "tests/scene/test_arraymesh.h" #include "tests/scene/test_camera_3d.h" +#include "tests/scene/test_height_map_shape_3d.h" #include "tests/scene/test_path_3d.h" #include "tests/scene/test_path_follow_3d.h" #include "tests/scene/test_primitives.h"