diff options
Diffstat (limited to 'tests/scene/test_bit_map.h')
-rw-r--r-- | tests/scene/test_bit_map.h | 148 |
1 files changed, 104 insertions, 44 deletions
diff --git a/tests/scene/test_bit_map.h b/tests/scene/test_bit_map.h index 53afdc38f7..3c372b2ac3 100644 --- a/tests/scene/test_bit_map.h +++ b/tests/scene/test_bit_map.h @@ -1,32 +1,32 @@ -/*************************************************************************/ -/* test_bit_map.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* 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. */ -/*************************************************************************/ +/**************************************************************************/ +/* test_bit_map.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_BIT_MAP_H #define TEST_BIT_MAP_H @@ -49,6 +49,8 @@ TEST_CASE("[BitMap] Create bit map") { CHECK(bit_map.get_size() == Size2i(256, 512)); CHECK_MESSAGE(bit_map.get_true_bit_count() == 0, "This will go through the entire bitmask inside of bitmap, thus hopefully checking if the bitmask was correctly set up."); + ERR_PRINT_OFF + dim = Size2i(0, 256); bit_map.create(dim); CHECK_MESSAGE(bit_map.get_size() == Size2i(256, 512), "We should still have the same dimensions as before, because the new dimension is invalid."); @@ -60,6 +62,8 @@ TEST_CASE("[BitMap] Create bit map") { dim = Size2i(46341, 46341); bit_map.create(dim); CHECK_MESSAGE(bit_map.get_size() == Size2i(256, 512), "We should still have the same dimensions as before, because the new dimension is too large (46341*46341=2147488281)."); + + ERR_PRINT_ON } TEST_CASE("[BitMap] Create bit map from image alpha") { @@ -67,6 +71,8 @@ TEST_CASE("[BitMap] Create bit map from image alpha") { BitMap bit_map{}; bit_map.create(dim); + ERR_PRINT_OFF + const Ref<Image> null_img = nullptr; bit_map.create_from_image_alpha(null_img); CHECK_MESSAGE(bit_map.get_size() == Size2i(256, 256), "Bitmap should have its old values because bitmap creation from a nullptr should fail."); @@ -76,15 +82,13 @@ TEST_CASE("[BitMap] Create bit map from image alpha") { bit_map.create_from_image_alpha(empty_img); CHECK_MESSAGE(bit_map.get_size() == Size2i(256, 256), "Bitmap should have its old values because bitmap creation from an empty image should fail."); - Ref<Image> wrong_format_img; - wrong_format_img.instantiate(); - wrong_format_img->create(3, 3, false, Image::Format::FORMAT_DXT1); + Ref<Image> wrong_format_img = Image::create_empty(3, 3, false, Image::Format::FORMAT_DXT1); bit_map.create_from_image_alpha(wrong_format_img); CHECK_MESSAGE(bit_map.get_size() == Size2i(256, 256), "Bitmap should have its old values because converting from a compressed image should fail."); - Ref<Image> img; - img.instantiate(); - img->create(3, 3, false, Image::Format::FORMAT_RGBA8); + ERR_PRINT_ON + + Ref<Image> img = Image::create_empty(3, 3, false, Image::Format::FORMAT_RGBA8); img->set_pixel(0, 0, Color(0, 0, 0, 0)); img->set_pixel(0, 1, Color(0, 0, 0, 0.09f)); img->set_pixel(0, 2, Color(0, 0, 0, 0.25f)); @@ -109,6 +113,8 @@ TEST_CASE("[BitMap] Set bit") { BitMap bit_map{}; // Setting a point before a bit map is created should not crash, because there are checks to see if we are out of bounds. + ERR_PRINT_OFF + bit_map.set_bitv(Point2i(128, 128), true); bit_map.create(dim); @@ -124,12 +130,16 @@ TEST_CASE("[BitMap] Set bit") { bit_map.create(dim); bit_map.set_bitv(Point2i(512, 512), true); CHECK_MESSAGE(bit_map.get_true_bit_count() == 0, "Nothing should change as we were trying to edit a bit outside of the correct range."); + + ERR_PRINT_ON } TEST_CASE("[BitMap] Get bit") { const Size2i dim{ 256, 256 }; BitMap bit_map{}; + ERR_PRINT_OFF + CHECK_MESSAGE(bit_map.get_bitv(Point2i(128, 128)) == false, "Trying to access a bit outside of the BitMap's range should always return false"); bit_map.create(dim); @@ -144,6 +154,8 @@ TEST_CASE("[BitMap] Get bit") { CHECK(bit_map.get_bitv(Point2i(255, 255)) == true); CHECK(bit_map.get_bitv(Point2i(256, 256)) == false); CHECK(bit_map.get_bitv(Point2i(257, 257)) == false); + + ERR_PRINT_ON } TEST_CASE("[BitMap] Set bit rect") { @@ -162,6 +174,9 @@ TEST_CASE("[BitMap] Set bit rect") { reset_bit_map(bit_map); // Checking out of bounds handling. + + ERR_PRINT_OFF + bit_map.set_bit_rect(Rect2i{ 128, 128, 256, 256 }, true); CHECK(bit_map.get_true_bit_count() == 16384); @@ -174,6 +189,8 @@ TEST_CASE("[BitMap] Set bit rect") { bit_map.set_bit_rect(Rect2i{ -128, -128, 512, 512 }, true); CHECK(bit_map.get_true_bit_count() == 65536); + + ERR_PRINT_ON } TEST_CASE("[BitMap] Get true bit count") { @@ -183,7 +200,7 @@ TEST_CASE("[BitMap] Get true bit count") { CHECK(bit_map.get_true_bit_count() == 0); bit_map.create(dim); - CHECK_MESSAGE(bit_map.get_true_bit_count() == 0, "Unitialized bit map should have no true bits"); + CHECK_MESSAGE(bit_map.get_true_bit_count() == 0, "Uninitialized bit map should have no true bits"); bit_map.set_bit_rect(Rect2i{ 0, 0, 256, 256 }, true); CHECK(bit_map.get_true_bit_count() == 65536); bit_map.set_bitv(Point2i{ 0, 0 }, false); @@ -196,12 +213,14 @@ TEST_CASE("[BitMap] Get size") { const Size2i dim{ 256, 256 }; BitMap bit_map{}; - CHECK_MESSAGE(bit_map.get_size() == Size2i(0, 0), "Unitialized bit map should have a size of 0x0"); + CHECK_MESSAGE(bit_map.get_size() == Size2i(0, 0), "Uninitialized bit map should have a size of 0x0"); bit_map.create(dim); CHECK(bit_map.get_size() == Size2i(256, 256)); + ERR_PRINT_OFF bit_map.create(Size2i(-1, 0)); + ERR_PRINT_ON CHECK_MESSAGE(bit_map.get_size() == Size2i(256, 256), "Invalid size should not be accepted by create"); bit_map.create(Size2i(256, 128)); @@ -223,7 +242,9 @@ TEST_CASE("[BitMap] Resize") { CHECK_MESSAGE(bit_map.get_true_bit_count() == 50, "There should be 25 bits in the top left corner, and 25 bits in the bottom right corner"); bit_map.create(dim); + ERR_PRINT_OFF bit_map.resize(Size2i(-1, 128)); + ERR_PRINT_ON CHECK_MESSAGE(bit_map.get_size() == Size2i(128, 128), "When an invalid size is given the bit map will keep its size"); bit_map.create(dim); @@ -238,7 +259,9 @@ TEST_CASE("[BitMap] Resize") { TEST_CASE("[BitMap] Grow and shrink mask") { const Size2i dim{ 256, 256 }; BitMap bit_map{}; - bit_map.grow_mask(100, Rect2i(0, 0, 128, 128)); // Check if method does not crash when working with an uninitialised bit map. + ERR_PRINT_OFF + bit_map.grow_mask(100, Rect2i(0, 0, 128, 128)); // Check if method does not crash when working with an uninitialized bit map. + ERR_PRINT_ON CHECK_MESSAGE(bit_map.get_size() == Size2i(0, 0), "Size should still be equal to 0x0"); bit_map.create(dim); @@ -335,25 +358,27 @@ TEST_CASE("[BitMap] Blit") { Ref<BitMap> blit_bit_map{}; // Testing null reference to blit bit map. + ERR_PRINT_OFF bit_map.blit(blit_pos, blit_bit_map); + ERR_PRINT_ON blit_bit_map.instantiate(); - // Testing if uninitialised blit bit map and uninitialised bit map does not crash + // Testing if uninitialized blit bit map and uninitialized bit map does not crash bit_map.blit(blit_pos, blit_bit_map); - // Testing if uninitialised bit map does not crash + // Testing if uninitialized bit map does not crash blit_bit_map->create(blit_size); bit_map.blit(blit_pos, blit_bit_map); - // Testing if uninitialised bit map does not crash + // Testing if uninitialized bit map does not crash blit_bit_map.unref(); blit_bit_map.instantiate(); CHECK_MESSAGE(blit_bit_map->get_size() == Point2i(0, 0), "Size should be cleared by unref and instance calls."); bit_map.create(bit_map_size); bit_map.blit(Point2i(128, 128), blit_bit_map); - // Testing if both initialised does not crash. + // Testing if both initialized does not crash. blit_bit_map->create(blit_size); bit_map.blit(blit_pos, blit_bit_map); @@ -382,7 +407,9 @@ TEST_CASE("[BitMap] Convert to image") { BitMap bit_map{}; Ref<Image> img; + ERR_PRINT_OFF img = bit_map.convert_to_image(); + ERR_PRINT_ON CHECK_MESSAGE(img.is_valid(), "We should receive a valid Image Object even if BitMap is not created yet"); CHECK_MESSAGE(img->get_format() == Image::FORMAT_L8, "We should receive a valid Image Object even if BitMap is not created yet"); CHECK_MESSAGE(img->get_size() == (Size2i(0, 0)), "Image should have no width or height, because BitMap has not yet been created"); @@ -390,13 +417,13 @@ TEST_CASE("[BitMap] Convert to image") { bit_map.create(dim); img = bit_map.convert_to_image(); CHECK_MESSAGE(img->get_size() == dim, "Image should have the same dimensions as the BitMap"); - CHECK_MESSAGE(img->get_pixel(0, 0).is_equal_approx(Color(0, 0, 0)), "BitMap is intialized to all 0's, so Image should be all black"); + CHECK_MESSAGE(img->get_pixel(0, 0).is_equal_approx(Color(0, 0, 0)), "BitMap is initialized to all 0's, so Image should be all black"); reset_bit_map(bit_map); bit_map.set_bit_rect(Rect2i(0, 0, 128, 128), true); img = bit_map.convert_to_image(); CHECK_MESSAGE(img->get_pixel(0, 0).is_equal_approx(Color(1, 1, 1)), "BitMap's top-left quadrant is all 1's, so Image should be white"); - CHECK_MESSAGE(img->get_pixel(256, 256).is_equal_approx(Color(0, 0, 0)), "All other quadrants were 0's, so these should be black"); + CHECK_MESSAGE(img->get_pixel(255, 255).is_equal_approx(Color(0, 0, 0)), "All other quadrants were 0's, so these should be black"); } TEST_CASE("[BitMap] Clip to polygon") { @@ -404,7 +431,9 @@ TEST_CASE("[BitMap] Clip to polygon") { BitMap bit_map{}; Vector<Vector<Vector2>> polygons; + ERR_PRINT_OFF polygons = bit_map.clip_opaque_to_polygons(Rect2i(0, 0, 128, 128)); + ERR_PRINT_ON CHECK_MESSAGE(polygons.size() == 0, "We should have no polygons, because the BitMap was not initialized"); bit_map.create(dim); @@ -438,6 +467,37 @@ TEST_CASE("[BitMap] Clip to polygon") { polygons = bit_map.clip_opaque_to_polygons(Rect2i(0, 0, 128, 128)); CHECK_MESSAGE(polygons.size() == 1, "We should have exactly 1 polygon"); CHECK_MESSAGE(polygons[0].size() == 6, "The polygon should have exactly 6 points"); + + reset_bit_map(bit_map); + bit_map.set_bit_rect(Rect2i(0, 0, 64, 64), true); + bit_map.set_bit_rect(Rect2i(64, 64, 64, 64), true); + bit_map.set_bit_rect(Rect2i(192, 128, 64, 64), true); + bit_map.set_bit_rect(Rect2i(128, 192, 64, 64), true); + polygons = bit_map.clip_opaque_to_polygons(Rect2i(0, 0, 256, 256)); + CHECK_MESSAGE(polygons.size() == 4, "We should have exactly 4 polygons"); + CHECK_MESSAGE(polygons[0].size() == 4, "The polygon should have exactly 4 points"); + CHECK_MESSAGE(polygons[1].size() == 4, "The polygon should have exactly 4 points"); + CHECK_MESSAGE(polygons[2].size() == 4, "The polygon should have exactly 4 points"); + CHECK_MESSAGE(polygons[3].size() == 4, "The polygon should have exactly 4 points"); + + reset_bit_map(bit_map); + bit_map.set_bit(0, 0, true); + bit_map.set_bit(2, 0, true); + bit_map.set_bit_rect(Rect2i(1, 1, 1, 2), true); + polygons = bit_map.clip_opaque_to_polygons(Rect2i(0, 0, 3, 3)); + CHECK_MESSAGE(polygons.size() == 3, "We should have exactly 3 polygons"); + CHECK_MESSAGE(polygons[0].size() == 4, "The polygon should have exactly 4 points"); + CHECK_MESSAGE(polygons[1].size() == 4, "The polygon should have exactly 4 points"); + CHECK_MESSAGE(polygons[2].size() == 4, "The polygon should have exactly 4 points"); + + reset_bit_map(bit_map); + bit_map.set_bit_rect(Rect2i(0, 0, 2, 1), true); + bit_map.set_bit_rect(Rect2i(0, 2, 3, 1), true); + bit_map.set_bit(0, 1, true); + bit_map.set_bit(2, 1, true); + polygons = bit_map.clip_opaque_to_polygons(Rect2i(0, 0, 4, 4)); + CHECK_MESSAGE(polygons.size() == 1, "We should have exactly 1 polygon"); + CHECK_MESSAGE(polygons[0].size() == 6, "The polygon should have exactly 6 points"); } } // namespace TestBitmap |