diff options
-rw-r--r-- | COPYRIGHT.txt | 2 | ||||
-rw-r--r-- | tests/test_json.h | 166 | ||||
-rw-r--r-- | tests/test_main.cpp | 7 | ||||
-rw-r--r-- | tests/test_object.h | 92 | ||||
-rw-r--r-- | tests/test_rect2.h | 467 | ||||
-rw-r--r-- | thirdparty/README.md | 8 | ||||
-rw-r--r-- | thirdparty/doctest/LICENSE.txt | 2 | ||||
-rw-r--r-- | thirdparty/doctest/doctest.h | 337 | ||||
-rw-r--r-- | thirdparty/doctest/patches/fix-arm64-mac.patch | 18 | ||||
-rw-r--r-- | thirdparty/nanosvg/nanosvg.h | 81 |
10 files changed, 986 insertions, 194 deletions
diff --git a/COPYRIGHT.txt b/COPYRIGHT.txt index 31f51c2854..011514ef04 100644 --- a/COPYRIGHT.txt +++ b/COPYRIGHT.txt @@ -128,7 +128,7 @@ License: Expat Files: ./thirdparty/doctest/ Comment: doctest -Copyright: 2016-2019, Viktor Kirilov +Copyright: 2016-2020, Viktor Kirilov License: Expat Files: ./thirdparty/enet/ diff --git a/tests/test_json.h b/tests/test_json.h new file mode 100644 index 0000000000..fe29e89e06 --- /dev/null +++ b/tests/test_json.h @@ -0,0 +1,166 @@ +/*************************************************************************/ +/* test_json.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +#ifndef TEST_JSON_H +#define TEST_JSON_H + +#include "core/io/json.h" + +#include "thirdparty/doctest/doctest.h" + +namespace TestJSON { + +// NOTE: The current JSON parser accepts many non-conformant strings such as +// single-quoted strings, duplicate commas and trailing commas. +// This is intentionally not tested as users shouldn't rely on this behavior. + +TEST_CASE("[JSON] Parsing single data types") { + // Parsing a single data type as JSON is valid per the JSON specification. + + JSON json; + Variant result; + String err_str; + int err_line; + + json.parse("null", result, err_str, err_line); + CHECK_MESSAGE( + err_line == 0, + "Parsing `null` as JSON should parse successfully."); + CHECK_MESSAGE( + result == Variant(), + "Parsing a double quoted string as JSON should return the expected value."); + + json.parse("true", result, err_str, err_line); + CHECK_MESSAGE( + err_line == 0, + "Parsing boolean `true` as JSON should parse successfully."); + CHECK_MESSAGE( + result, + "Parsing boolean `true` as JSON should return the expected value."); + + json.parse("false", result, err_str, err_line); + CHECK_MESSAGE( + err_line == 0, + "Parsing boolean `false` as JSON should parse successfully."); + CHECK_MESSAGE( + !result, + "Parsing boolean `false` as JSON should return the expected value."); + + // JSON only has a floating-point number type, no integer type. + // This is why we use `is_equal_approx()` for the comparison. + json.parse("123456", result, err_str, err_line); + CHECK_MESSAGE( + err_line == 0, + "Parsing an integer number as JSON should parse successfully."); + CHECK_MESSAGE( + Math::is_equal_approx(result, 123'456), + "Parsing an integer number as JSON should return the expected value."); + + json.parse("0.123456", result, err_str, err_line); + CHECK_MESSAGE( + err_line == 0, + "Parsing a floating-point number as JSON should parse successfully."); + CHECK_MESSAGE( + Math::is_equal_approx(result, 0.123456), + "Parsing a floating-point number as JSON should return the expected value."); + + json.parse("\"hello\"", result, err_str, err_line); + CHECK_MESSAGE( + err_line == 0, + "Parsing a double quoted string as JSON should parse successfully."); + CHECK_MESSAGE( + result == "hello", + "Parsing a double quoted string as JSON should return the expected value."); +} + +TEST_CASE("[JSON] Parsing arrays") { + JSON json; + Variant result; + String err_str; + int err_line; + + // JSON parsing fails if it's split over several lines (even if leading indentation is removed). + json.parse( + R"(["Hello", "world.", "This is",["a","json","array.",[]], "Empty arrays ahoy:", [[["Gotcha!"]]]])", + result, err_str, err_line); + + const Array array = result; + CHECK_MESSAGE( + err_line == 0, + "Parsing a JSON array should parse successfully."); + CHECK_MESSAGE( + array[0] == "Hello", + "The parsed JSON should contain the expected values."); + const Array sub_array = array[3]; + CHECK_MESSAGE( + sub_array.size() == 4, + "The parsed JSON should contain the expected values."); + CHECK_MESSAGE( + sub_array[1] == "json", + "The parsed JSON should contain the expected values."); + CHECK_MESSAGE( + sub_array[3].hash() == Array().hash(), + "The parsed JSON should contain the expected values."); + const Array deep_array = Array(Array(array[5])[0])[0]; + CHECK_MESSAGE( + deep_array[0] == "Gotcha!", + "The parsed JSON should contain the expected values."); +} + +TEST_CASE("[JSON] Parsing objects (dictionaries)") { + JSON json; + Variant result; + String err_str; + int err_line; + + json.parse( + R"({"name": "Godot Engine", "is_free": true, "bugs": null, "apples": {"red": 500, "green": 0, "blue": -20}, "empty_object": {}})", + result, err_str, err_line); + + const Dictionary dictionary = result; + CHECK_MESSAGE( + dictionary["name"] == "Godot Engine", + "The parsed JSON should contain the expected values."); + CHECK_MESSAGE( + dictionary["is_free"], + "The parsed JSON should contain the expected values."); + CHECK_MESSAGE( + dictionary["bugs"] == Variant(), + "The parsed JSON should contain the expected values."); + CHECK_MESSAGE( + Math::is_equal_approx(Dictionary(dictionary["apples"])["blue"], -20), + "The parsed JSON should contain the expected values."); + CHECK_MESSAGE( + dictionary["empty_object"].hash() == Dictionary().hash(), + "The parsed JSON should contain the expected values."); +} +} // namespace TestJSON + +#endif // TEST_JSON_H diff --git a/tests/test_main.cpp b/tests/test_main.cpp index cd7f1d3eac..4388654d08 100644 --- a/tests/test_main.cpp +++ b/tests/test_main.cpp @@ -42,15 +42,18 @@ #include "test_expression.h" #include "test_gradient.h" #include "test_gui.h" +#include "test_json.h" #include "test_list.h" #include "test_math.h" #include "test_method_bind.h" #include "test_node_path.h" #include "test_oa_hash_map.h" +#include "test_object.h" #include "test_ordered_hash_map.h" #include "test_pck_packer.h" #include "test_physics_2d.h" #include "test_physics_3d.h" +#include "test_rect2.h" #include "test_render.h" #include "test_shader_lang.h" #include "test_string.h" @@ -112,10 +115,6 @@ int test_main(int argc, char *argv[]) { test_context.applyCommandLine(test_args.size(), doctest_args); - test_context.setOption("order-by", "name"); - test_context.setOption("abort-after", 5); - test_context.setOption("no-breaks", true); - for (int x = 0; x < test_args.size(); x++) { delete[] doctest_args[x]; } diff --git a/tests/test_object.h b/tests/test_object.h new file mode 100644 index 0000000000..6fef2576e7 --- /dev/null +++ b/tests/test_object.h @@ -0,0 +1,92 @@ +/*************************************************************************/ +/* test_object.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +#ifndef TEST_OBJECT_H +#define TEST_OBJECT_H + +#include "core/object/object.h" + +#include "thirdparty/doctest/doctest.h" + +namespace TestObject { + +TEST_CASE("[Object] Core getters") { + Object object; + + CHECK_MESSAGE( + object.is_class("Object"), + "is_class() should return the expected value."); + CHECK_MESSAGE( + object.get_class() == "Object", + "The returned class should match the expected value."); + CHECK_MESSAGE( + object.get_class_name() == "Object", + "The returned class name should match the expected value."); + CHECK_MESSAGE( + object.get_class_static() == "Object", + "The returned static class should match the expected value."); + CHECK_MESSAGE( + object.get_save_class() == "Object", + "The returned save class should match the expected value."); +} + +TEST_CASE("[Object] Metadata") { + const String meta_path = "hello/world complex métadata\n\n\t\tpath"; + Object object; + + object.set_meta(meta_path, Color(0, 1, 0)); + CHECK_MESSAGE( + Color(object.get_meta(meta_path)).is_equal_approx(Color(0, 1, 0)), + "The returned object metadata after setting should match the expected value."); + + List<String> meta_list; + object.get_meta_list(&meta_list); + CHECK_MESSAGE( + meta_list.size() == 1, + "The metadata list should only contain 1 item after adding one metadata item."); + + object.remove_meta(meta_path); + // Also try removing nonexistent metadata (it should do nothing, without printing an error message). + object.remove_meta("I don't exist"); + ERR_PRINT_OFF; + CHECK_MESSAGE( + object.get_meta(meta_path) == Variant(), + "The returned object metadata after removing should match the expected value."); + ERR_PRINT_ON; + + List<String> meta_list2; + object.get_meta_list(&meta_list2); + CHECK_MESSAGE( + meta_list2.size() == 0, + "The metadata list should contain 0 items after removing all metadata items."); +} +} // namespace TestObject + +#endif // TEST_OBJECT_H diff --git a/tests/test_rect2.h b/tests/test_rect2.h new file mode 100644 index 0000000000..aefceb1128 --- /dev/null +++ b/tests/test_rect2.h @@ -0,0 +1,467 @@ +/*************************************************************************/ +/* test_rect2.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 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. */ +/*************************************************************************/ + +#ifndef TEST_RECT2_H +#define TEST_RECT2_H + +#include "core/math/rect2.h" + +#include "thirdparty/doctest/doctest.h" + +namespace TestRect2 { +// We also test Rect2i here, for consistency with the source code where Rect2 +// and Rect2i are defined in the same file. + +// Rect2 + +TEST_CASE("[Rect2] Constructor methods") { + const Rect2 rect = Rect2(0, 100, 1280, 720); + const Rect2 rect_vector = Rect2(Vector2(0, 100), Vector2(1280, 720)); + const Rect2 rect_copy_rect = Rect2(rect); + const Rect2 rect_copy_recti = Rect2(Rect2i(0, 100, 1280, 720)); + + CHECK_MESSAGE( + rect == rect_vector, + "Rect2s created with the same dimensions but by different methods should be equal."); + CHECK_MESSAGE( + rect == rect_copy_rect, + "Rect2s created with the same dimensions but by different methods should be equal."); + CHECK_MESSAGE( + rect == rect_copy_recti, + "Rect2s created with the same dimensions but by different methods should be equal."); +} + +TEST_CASE("[Rect2] String conversion") { + // Note: This also depends on the Vector2 string representation. + CHECK_MESSAGE( + String(Rect2(0, 100, 1280, 720)) == "0, 100, 1280, 720", + "The string representation should match the expected value."); +} + +TEST_CASE("[Rect2] Basic getters") { + const Rect2 rect = Rect2(0, 100, 1280, 720); + CHECK_MESSAGE( + rect.get_position().is_equal_approx(Vector2(0, 100)), + "get_position() should return the expected value."); + CHECK_MESSAGE( + rect.get_size().is_equal_approx(Vector2(1280, 720)), + "get_size() should return the expected value."); + CHECK_MESSAGE( + rect.get_end().is_equal_approx(Vector2(1280, 820)), + "get_end() should return the expected value."); +} + +TEST_CASE("[Rect2] Basic setters") { + Rect2 rect = Rect2(0, 100, 1280, 720); + rect.set_end(Vector2(4000, 4000)); + CHECK_MESSAGE( + rect.is_equal_approx(Rect2(0, 100, 4000, 3900)), + "set_end() should result in the expected Rect2."); + + rect = Rect2(0, 100, 1280, 720); + rect.set_position(Vector2(4000, 4000)); + CHECK_MESSAGE( + rect.is_equal_approx(Rect2(4000, 4000, 1280, 720)), + "set_position() should result in the expected Rect2."); + + rect = Rect2(0, 100, 1280, 720); + rect.set_size(Vector2(4000, 4000)); + CHECK_MESSAGE( + rect.is_equal_approx(Rect2(0, 100, 4000, 4000)), + "set_size() should result in the expected Rect2."); +} + +TEST_CASE("[Rect2] Area getters") { + CHECK_MESSAGE( + Math::is_equal_approx(Rect2(0, 100, 1280, 720).get_area(), 921'600), + "get_area() should return the expected value."); + CHECK_MESSAGE( + Math::is_equal_approx(Rect2(0, 100, -1280, -720).get_area(), 921'600), + "get_area() should return the expected value."); + CHECK_MESSAGE( + Math::is_equal_approx(Rect2(0, 100, 1280, -720).get_area(), -921'600), + "get_area() should return the expected value."); + CHECK_MESSAGE( + Math::is_equal_approx(Rect2(0, 100, -1280, 720).get_area(), -921'600), + "get_area() should return the expected value."); + CHECK_MESSAGE( + Math::is_zero_approx(Rect2(0, 100, 0, 720).get_area()), + "get_area() should return the expected value."); + + CHECK_MESSAGE( + !Rect2(0, 100, 1280, 720).has_no_area(), + "has_no_area() should return the expected value on Rect2 with an area."); + CHECK_MESSAGE( + Rect2(0, 100, 0, 500).has_no_area(), + "has_no_area() should return the expected value on Rect2 with no area."); + CHECK_MESSAGE( + Rect2(0, 100, 500, 0).has_no_area(), + "has_no_area() should return the expected value on Rect2 with no area."); + CHECK_MESSAGE( + Rect2(0, 100, 0, 0).has_no_area(), + "has_no_area() should return the expected value on Rect2 with no area."); +} + +TEST_CASE("[Rect2] Absolute coordinates") { + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).abs().is_equal_approx(Rect2(0, 100, 1280, 720)), + "abs() should return the expected Rect2."); + CHECK_MESSAGE( + Rect2(0, -100, 1280, 720).abs().is_equal_approx(Rect2(0, -100, 1280, 720)), + "abs() should return the expected Rect2."); + CHECK_MESSAGE( + Rect2(0, -100, -1280, -720).abs().is_equal_approx(Rect2(-1280, -820, 1280, 720)), + "abs() should return the expected Rect2."); + CHECK_MESSAGE( + Rect2(0, 100, -1280, 720).abs().is_equal_approx(Rect2(-1280, 100, 1280, 720)), + "abs() should return the expected Rect2."); +} + +TEST_CASE("[Rect2] Clipping") { + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).clip(Rect2(0, 300, 100, 100)).is_equal_approx(Rect2(0, 300, 100, 100)), + "clip() with fully enclosed Rect2 should return the expected result."); + // The resulting Rect2 is 100 pixels high because the first Rect2 is vertically offset by 100 pixels. + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).clip(Rect2(1200, 700, 100, 100)).is_equal_approx(Rect2(1200, 700, 80, 100)), + "clip() with partially enclosed Rect2 should return the expected result."); + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).clip(Rect2(-4000, -4000, 100, 100)).is_equal_approx(Rect2()), + "clip() with non-enclosed Rect2 should return the expected result."); +} + +TEST_CASE("[Rect2] Enclosing") { + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).encloses(Rect2(0, 300, 100, 100)), + "clip() with fully contained Rect2 should return the expected result."); + CHECK_MESSAGE( + !Rect2(0, 100, 1280, 720).encloses(Rect2(1200, 700, 100, 100)), + "clip() with partially contained Rect2 should return the expected result."); + CHECK_MESSAGE( + !Rect2(0, 100, 1280, 720).encloses(Rect2(-4000, -4000, 100, 100)), + "clip() with non-contained Rect2 should return the expected result."); +} + +TEST_CASE("[Rect2] Expanding") { + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).expand(Vector2(500, 600)).is_equal_approx(Rect2(0, 100, 1280, 720)), + "expand() with contained Vector2 should return the expected result."); + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).expand(Vector2(0, 0)).is_equal_approx(Rect2(0, 0, 1280, 820)), + "expand() with non-contained Vector2 should return the expected result."); +} + +TEST_CASE("[Rect2] Growing") { + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).grow(100).is_equal_approx(Rect2(-100, 0, 1480, 920)), + "grow() with positive value should return the expected Rect2."); + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).grow(-100).is_equal_approx(Rect2(100, 200, 1080, 520)), + "grow() with negative value should return the expected Rect2."); + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).grow(-4000).is_equal_approx(Rect2(4000, 4100, -6720, -7280)), + "grow() with large negative value should return the expected Rect2."); + + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).grow_individual(100, 200, 300, 400).is_equal_approx(Rect2(-100, -100, 1680, 1320)), + "grow_individual() with positive values should return the expected Rect2."); + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).grow_individual(-100, 200, 300, -400).is_equal_approx(Rect2(100, -100, 1480, 520)), + "grow_individual() with positive and negative values should return the expected Rect2."); + + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).grow_margin(MARGIN_TOP, 500).is_equal_approx(Rect2(0, -400, 1280, 1220)), + "grow_margin() with positive value should return the expected Rect2."); + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).grow_margin(MARGIN_TOP, -500).is_equal_approx(Rect2(0, 600, 1280, 220)), + "grow_margin() with negative value should return the expected Rect2."); +} + +TEST_CASE("[Rect2] Has point") { + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).has_point(Vector2(500, 600)), + "has_point() with contained Vector2 should return the expected result."); + CHECK_MESSAGE( + !Rect2(0, 100, 1280, 720).has_point(Vector2(0, 0)), + "has_point() with non-contained Vector2 should return the expected result."); + + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).has_point(Vector2(0, 110)), + "has_point() with positive Vector2 on left edge should return the expected result."); + CHECK_MESSAGE( + !Rect2(0, 100, 1280, 720).has_point(Vector2(1280, 110)), + "has_point() with positive Vector2 on right edge should return the expected result."); + + CHECK_MESSAGE( + Rect2(-4000, 100, 1280, 720).has_point(Vector2(-4000, 110)), + "has_point() with negative Vector2 on left edge should return the expected result."); + CHECK_MESSAGE( + !Rect2(-4000, 100, 1280, 720).has_point(Vector2(-2720, 110)), + "has_point() with negative Vector2 on right edge should return the expected result."); +} + +TEST_CASE("[Rect2] Intersection") { + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).intersects(Rect2(0, 300, 100, 100)), + "intersects() with fully enclosed Rect2 should return the expected result."); + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).intersects(Rect2(1200, 700, 100, 100)), + "intersects() with partially enclosed Rect2 should return the expected result."); + CHECK_MESSAGE( + !Rect2(0, 100, 1280, 720).intersects(Rect2(-4000, -4000, 100, 100)), + "intersects() with non-enclosed Rect2 should return the expected result."); +} + +TEST_CASE("[Rect2] Merging") { + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).merge(Rect2(0, 300, 100, 100)).is_equal_approx(Rect2(0, 100, 1280, 720)), + "merge() with fully enclosed Rect2 should return the expected result."); + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).merge(Rect2(1200, 700, 100, 100)).is_equal_approx(Rect2(0, 100, 1300, 720)), + "merge() with partially enclosed Rect2 should return the expected result."); + CHECK_MESSAGE( + Rect2(0, 100, 1280, 720).merge(Rect2(-4000, -4000, 100, 100)).is_equal_approx(Rect2(-4000, -4000, 5280, 4820)), + "merge() with non-enclosed Rect2 should return the expected result."); +} + +// Rect2i + +TEST_CASE("[Rect2i] Constructor methods") { + Rect2i recti = Rect2i(0, 100, 1280, 720); + Rect2i recti_vector = Rect2i(Vector2i(0, 100), Vector2i(1280, 720)); + Rect2i recti_copy_recti = Rect2i(recti); + Rect2i recti_copy_rect = Rect2i(Rect2(0, 100, 1280, 720)); + + CHECK_MESSAGE( + recti == recti_vector, + "Rect2is created with the same dimensions but by different methods should be equal."); + CHECK_MESSAGE( + recti == recti_copy_recti, + "Rect2is created with the same dimensions but by different methods should be equal."); + CHECK_MESSAGE( + recti == recti_copy_rect, + "Rect2is created with the same dimensions but by different methods should be equal."); +} + +TEST_CASE("[Rect2i] String conversion") { + // Note: This also depends on the Vector2 string representation. + CHECK_MESSAGE( + String(Rect2i(0, 100, 1280, 720)) == "0, 100, 1280, 720", + "The string representation should match the expected value."); +} + +TEST_CASE("[Rect2i] Basic getters") { + const Rect2i rect = Rect2i(0, 100, 1280, 720); + CHECK_MESSAGE( + rect.get_position() == Vector2i(0, 100), + "get_position() should return the expected value."); + CHECK_MESSAGE( + rect.get_size() == Vector2i(1280, 720), + "get_size() should return the expected value."); + CHECK_MESSAGE( + rect.get_end() == Vector2i(1280, 820), + "get_end() should return the expected value."); +} + +TEST_CASE("[Rect2i] Basic setters") { + Rect2i rect = Rect2i(0, 100, 1280, 720); + rect.set_end(Vector2i(4000, 4000)); + CHECK_MESSAGE( + rect == Rect2i(0, 100, 4000, 3900), + "set_end() should result in the expected Rect2i."); + + rect = Rect2i(0, 100, 1280, 720); + rect.set_position(Vector2i(4000, 4000)); + CHECK_MESSAGE( + rect == Rect2i(4000, 4000, 1280, 720), + "set_position() should result in the expected Rect2i."); + + rect = Rect2i(0, 100, 1280, 720); + rect.set_size(Vector2i(4000, 4000)); + CHECK_MESSAGE( + rect == Rect2i(0, 100, 4000, 4000), + "set_size() should result in the expected Rect2i."); +} + +TEST_CASE("[Rect2i] Area getters") { + CHECK_MESSAGE( + Math::is_equal_approx(Rect2i(0, 100, 1280, 720).get_area(), 921'600), + "get_area() should return the expected value."); + CHECK_MESSAGE( + Math::is_equal_approx(Rect2i(0, 100, -1280, -720).get_area(), 921'600), + "get_area() should return the expected value."); + CHECK_MESSAGE( + Math::is_equal_approx(Rect2i(0, 100, 1280, -720).get_area(), -921'600), + "get_area() should return the expected value."); + CHECK_MESSAGE( + Math::is_equal_approx(Rect2i(0, 100, -1280, 720).get_area(), -921'600), + "get_area() should return the expected value."); + CHECK_MESSAGE( + Math::is_zero_approx(Rect2i(0, 100, 0, 720).get_area()), + "get_area() should return the expected value."); + + CHECK_MESSAGE( + !Rect2i(0, 100, 1280, 720).has_no_area(), + "has_no_area() should return the expected value on Rect2i with an area."); + CHECK_MESSAGE( + Rect2i(0, 100, 0, 500).has_no_area(), + "has_no_area() should return the expected value on Rect2i with no area."); + CHECK_MESSAGE( + Rect2i(0, 100, 500, 0).has_no_area(), + "has_no_area() should return the expected value on Rect2i with no area."); + CHECK_MESSAGE( + Rect2i(0, 100, 0, 0).has_no_area(), + "has_no_area() should return the expected value on Rect2i with no area."); +} + +TEST_CASE("[Rect2i] Absolute coordinates") { + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).abs() == Rect2i(0, 100, 1280, 720), + "abs() should return the expected Rect2i."); + CHECK_MESSAGE( + Rect2i(0, -100, 1280, 720).abs() == Rect2i(0, -100, 1280, 720), + "abs() should return the expected Rect2i."); + CHECK_MESSAGE( + Rect2i(0, -100, -1280, -720).abs() == Rect2i(-1280, -820, 1280, 720), + "abs() should return the expected Rect2i."); + CHECK_MESSAGE( + Rect2i(0, 100, -1280, 720).abs() == Rect2i(-1280, 100, 1280, 720), + "abs() should return the expected Rect2i."); +} + +TEST_CASE("[Rect2i] Clipping") { + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).clip(Rect2i(0, 300, 100, 100)) == Rect2i(0, 300, 100, 100), + "clip() with fully enclosed Rect2i should return the expected result."); + // The resulting Rect2i is 100 pixels high because the first Rect2i is vertically offset by 100 pixels. + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).clip(Rect2i(1200, 700, 100, 100)) == Rect2i(1200, 700, 80, 100), + "clip() with partially enclosed Rect2i should return the expected result."); + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).clip(Rect2i(-4000, -4000, 100, 100)) == Rect2i(), + "clip() with non-enclosed Rect2i should return the expected result."); +} + +TEST_CASE("[Rect2i] Enclosing") { + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).encloses(Rect2i(0, 300, 100, 100)), + "clip() with fully contained Rect2i should return the expected result."); + CHECK_MESSAGE( + !Rect2i(0, 100, 1280, 720).encloses(Rect2i(1200, 700, 100, 100)), + "clip() with partially contained Rect2i should return the expected result."); + CHECK_MESSAGE( + !Rect2i(0, 100, 1280, 720).encloses(Rect2i(-4000, -4000, 100, 100)), + "clip() with non-contained Rect2i should return the expected result."); +} + +TEST_CASE("[Rect2i] Expanding") { + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).expand(Vector2i(500, 600)) == Rect2i(0, 100, 1280, 720), + "expand() with contained Vector2i should return the expected result."); + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).expand(Vector2i(0, 0)) == Rect2i(0, 0, 1280, 820), + "expand() with non-contained Vector2i should return the expected result."); +} + +TEST_CASE("[Rect2i] Growing") { + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).grow(100) == Rect2i(-100, 0, 1480, 920), + "grow() with positive value should return the expected Rect2i."); + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).grow(-100) == Rect2i(100, 200, 1080, 520), + "grow() with negative value should return the expected Rect2i."); + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).grow(-4000) == Rect2i(4000, 4100, -6720, -7280), + "grow() with large negative value should return the expected Rect2i."); + + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).grow_individual(100, 200, 300, 400) == Rect2i(-100, -100, 1680, 1320), + "grow_individual() with positive values should return the expected Rect2i."); + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).grow_individual(-100, 200, 300, -400) == Rect2i(100, -100, 1480, 520), + "grow_individual() with positive and negative values should return the expected Rect2i."); + + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).grow_margin(MARGIN_TOP, 500) == Rect2i(0, -400, 1280, 1220), + "grow_margin() with positive value should return the expected Rect2i."); + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).grow_margin(MARGIN_TOP, -500) == Rect2i(0, 600, 1280, 220), + "grow_margin() with negative value should return the expected Rect2i."); +} + +TEST_CASE("[Rect2i] Has point") { + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).has_point(Vector2i(500, 600)), + "has_point() with contained Vector2i should return the expected result."); + CHECK_MESSAGE( + !Rect2i(0, 100, 1280, 720).has_point(Vector2i(0, 0)), + "has_point() with non-contained Vector2i should return the expected result."); + + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).has_point(Vector2(0, 110)), + "has_point() with positive Vector2 on left edge should return the expected result."); + CHECK_MESSAGE( + !Rect2i(0, 100, 1280, 720).has_point(Vector2(1280, 110)), + "has_point() with positive Vector2 on right edge should return the expected result."); + + CHECK_MESSAGE( + Rect2i(-4000, 100, 1280, 720).has_point(Vector2(-4000, 110)), + "has_point() with negative Vector2 on left edge should return the expected result."); + CHECK_MESSAGE( + !Rect2i(-4000, 100, 1280, 720).has_point(Vector2(-2720, 110)), + "has_point() with negative Vector2 on right edge should return the expected result."); +} + +TEST_CASE("[Rect2i] Intersection") { + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).intersects(Rect2i(0, 300, 100, 100)), + "intersects() with fully enclosed Rect2i should return the expected result."); + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).intersects(Rect2i(1200, 700, 100, 100)), + "intersects() with partially enclosed Rect2i should return the expected result."); + CHECK_MESSAGE( + !Rect2i(0, 100, 1280, 720).intersects(Rect2i(-4000, -4000, 100, 100)), + "intersects() with non-enclosed Rect2i should return the expected result."); +} + +TEST_CASE("[Rect2i] Merging") { + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).merge(Rect2i(0, 300, 100, 100)) == Rect2i(0, 100, 1280, 720), + "merge() with fully enclosed Rect2i should return the expected result."); + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).merge(Rect2i(1200, 700, 100, 100)) == Rect2i(0, 100, 1300, 720), + "merge() with partially enclosed Rect2i should return the expected result."); + CHECK_MESSAGE( + Rect2i(0, 100, 1280, 720).merge(Rect2i(-4000, -4000, 100, 100)) == Rect2i(-4000, -4000, 5280, 4820), + "merge() with non-enclosed Rect2i should return the expected result."); +} +} // namespace TestRect2 + +#endif // TEST_RECT2_H diff --git a/thirdparty/README.md b/thirdparty/README.md index f590acaa0b..f4f3aad0fc 100644 --- a/thirdparty/README.md +++ b/thirdparty/README.md @@ -76,15 +76,11 @@ Files extracted from upstream source: ## doctest - Upstream: https://github.com/onqtam/doctest -- Version: 1c8da00 (2.4.0) +- Version: 8424be5 (2.4.1) - License: MIT Extracted from .zip provided. Extracted license and header only. -Important: Some files have Godot-made changes. -They are marked with `// -- GODOT start --` and `// -- GODOT end --` -comments. - ## enet - Upstream: http://enet.bespin.org @@ -435,7 +431,7 @@ Collection of single-file libraries used in Godot components. ## nanosvg - Upstream: https://github.com/memononen/nanosvg -- Version: git (25241c5a8f8451d41ab1b02ab2d865b01600d949, 2019) +- Version: git (3e403ec72a9145cbbcc6c63d94a4caf079aafec2, 2020) - License: zlib Files extracted from the upstream source: diff --git a/thirdparty/doctest/LICENSE.txt b/thirdparty/doctest/LICENSE.txt index a204721468..50a358cd1b 100644 --- a/thirdparty/doctest/LICENSE.txt +++ b/thirdparty/doctest/LICENSE.txt @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2016-2019 Viktor Kirilov +Copyright (c) 2016-2020 Viktor Kirilov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/thirdparty/doctest/doctest.h b/thirdparty/doctest/doctest.h index e4fed12767..acbe6cd321 100644 --- a/thirdparty/doctest/doctest.h +++ b/thirdparty/doctest/doctest.h @@ -4,7 +4,7 @@ // // doctest.h - the lightest feature-rich C++ single-header testing framework for unit tests and TDD // -// Copyright (c) 2016-2019 Viktor Kirilov +// Copyright (c) 2016-2020 Viktor Kirilov // // Distributed under the MIT Software License // See accompanying file LICENSE.txt or copy at @@ -48,8 +48,8 @@ #define DOCTEST_VERSION_MAJOR 2 #define DOCTEST_VERSION_MINOR 4 -#define DOCTEST_VERSION_PATCH 0 -#define DOCTEST_VERSION_STR "2.4.0" +#define DOCTEST_VERSION_PATCH 1 +#define DOCTEST_VERSION_STR "2.4.1" #define DOCTEST_VERSION \ (DOCTEST_VERSION_MAJOR * 10000 + DOCTEST_VERSION_MINOR * 100 + DOCTEST_VERSION_PATCH) @@ -301,11 +301,15 @@ DOCTEST_MSVC_SUPPRESS_WARNING(26812) // Prefer 'enum class' over 'enum' #define DOCTEST_NOINLINE __declspec(noinline) #define DOCTEST_UNUSED #define DOCTEST_ALIGNMENT(x) -#else // MSVC +#elif DOCTEST_CLANG && DOCTEST_CLANG < DOCTEST_COMPILER(3, 5, 0) +#define DOCTEST_NOINLINE +#define DOCTEST_UNUSED +#define DOCTEST_ALIGNMENT(x) +#else #define DOCTEST_NOINLINE __attribute__((noinline)) #define DOCTEST_UNUSED __attribute__((unused)) #define DOCTEST_ALIGNMENT(x) __attribute__((aligned(x))) -#endif // MSVC +#endif #ifndef DOCTEST_NORETURN #define DOCTEST_NORETURN [[noreturn]] @@ -355,14 +359,20 @@ DOCTEST_MSVC_SUPPRESS_WARNING(26812) // Prefer 'enum class' over 'enum' #ifndef DOCTEST_BREAK_INTO_DEBUGGER // should probably take a look at https://github.com/scottt/debugbreak -#ifdef DOCTEST_PLATFORM_MAC -// -- GODOT start -- +#ifdef DOCTEST_PLATFORM_LINUX +#if defined(__GNUC__) && (defined(__i386) || defined(__x86_64)) +// Break at the location of the failing check if possible +#define DOCTEST_BREAK_INTO_DEBUGGER() __asm__("int $3\n" : :) +#else +#include <signal.h> +#define DOCTEST_BREAK_INTO_DEBUGGER() raise(SIGTRAP) +#endif +#elif defined(DOCTEST_PLATFORM_MAC) #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) #define DOCTEST_BREAK_INTO_DEBUGGER() __asm__("int $3\n" : :) #else #define DOCTEST_BREAK_INTO_DEBUGGER() __asm__("brk #0"); #endif -// -- GODOT end -- #elif DOCTEST_MSVC #define DOCTEST_BREAK_INTO_DEBUGGER() __debugbreak() #elif defined(__MINGW32__) @@ -371,7 +381,7 @@ extern "C" __declspec(dllimport) void __stdcall DebugBreak(); DOCTEST_GCC_SUPPRESS_WARNING_POP #define DOCTEST_BREAK_INTO_DEBUGGER() ::DebugBreak() #else // linux -#define DOCTEST_BREAK_INTO_DEBUGGER() ((void)0) +#define DOCTEST_BREAK_INTO_DEBUGGER() (static_cast<void>(0)) #endif // linux #endif // DOCTEST_BREAK_INTO_DEBUGGER @@ -381,6 +391,9 @@ DOCTEST_GCC_SUPPRESS_WARNING_POP #endif // DOCTEST_CONFIG_USE_IOSFWD #ifdef DOCTEST_CONFIG_USE_STD_HEADERS +#ifndef DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS +#define DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS +#endif // DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS #include <iosfwd> #include <cstddef> #include <ostream> @@ -746,7 +759,6 @@ struct ContextOptions //!OCLINT too many fields }; namespace detail { -#if defined(DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING) || defined(DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS) template <bool CONDITION, typename TYPE = void> struct enable_if {}; @@ -754,7 +766,6 @@ namespace detail { template <typename TYPE> struct enable_if<true, TYPE> { typedef TYPE type; }; -#endif // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING) || DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS // clang-format off template<class T> struct remove_reference { typedef T type; }; @@ -763,6 +774,14 @@ namespace detail { template<class T> struct remove_const { typedef T type; }; template<class T> struct remove_const<const T> { typedef T type; }; +#ifdef DOCTEST_CONFIG_INCLUDE_TYPE_TRAITS + template<class T> struct is_enum : public std::is_enum<T> {}; + template<class T> struct underlying_type : public std::underlying_type<T> {}; +#else + // Use compiler intrinsics + template<class T> struct is_enum { constexpr static bool value = __is_enum(T); }; + template<class T> struct underlying_type { typedef __underlying_type(T) type; }; +#endif // clang-format on template <typename T> @@ -777,12 +796,12 @@ namespace detail { template<class, class = void> struct check { - static constexpr auto value = false; + static constexpr bool value = false; }; template<class T> struct check<T, decltype(os() << val<T>(), void())> { - static constexpr auto value = true; + static constexpr bool value = true; }; } // namespace has_insertion_operator_impl @@ -851,7 +870,7 @@ struct StringMaker<R C::*> } }; -template <typename T> +template <typename T, typename detail::enable_if<!detail::is_enum<T>::value, bool>::type = true> String toString(const DOCTEST_REF_WRAP(T) value) { return StringMaker<T>::convert(value); } @@ -878,6 +897,12 @@ DOCTEST_INTERFACE String toString(int long long in); DOCTEST_INTERFACE String toString(int long long unsigned in); DOCTEST_INTERFACE String toString(std::nullptr_t in); +template <typename T, typename detail::enable_if<detail::is_enum<T>::value, bool>::type = true> +String toString(const DOCTEST_REF_WRAP(T) value) { + typedef typename detail::underlying_type<T>::type UT; + return toString(static_cast<UT>(value)); +} + #if DOCTEST_MSVC >= DOCTEST_COMPILER(19, 20, 0) // see this issue on why this is needed: https://github.com/onqtam/doctest/issues/183 DOCTEST_INTERFACE String toString(const std::string& in); @@ -1289,12 +1314,12 @@ namespace detail { template <class L, class R> struct RelationalComparator<n, L, R> { bool operator()(const DOCTEST_REF_WRAP(L) lhs, const DOCTEST_REF_WRAP(R) rhs) const { return op(lhs, rhs); } }; // clang-format on - DOCTEST_BINARY_RELATIONAL_OP(0, eq) - DOCTEST_BINARY_RELATIONAL_OP(1, ne) - DOCTEST_BINARY_RELATIONAL_OP(2, gt) - DOCTEST_BINARY_RELATIONAL_OP(3, lt) - DOCTEST_BINARY_RELATIONAL_OP(4, ge) - DOCTEST_BINARY_RELATIONAL_OP(5, le) + DOCTEST_BINARY_RELATIONAL_OP(0, doctest::detail::eq) + DOCTEST_BINARY_RELATIONAL_OP(1, doctest::detail::ne) + DOCTEST_BINARY_RELATIONAL_OP(2, doctest::detail::gt) + DOCTEST_BINARY_RELATIONAL_OP(3, doctest::detail::lt) + DOCTEST_BINARY_RELATIONAL_OP(4, doctest::detail::ge) + DOCTEST_BINARY_RELATIONAL_OP(5, doctest::detail::le) struct DOCTEST_INTERFACE ResultBuilder : public AssertData { @@ -1421,9 +1446,9 @@ namespace detail { } catch(T ex) { // NOLINT res = m_translateFunction(ex); //!OCLINT parameter reassignment return true; - } catch(...) {} //!OCLINT - empty catch statement -#endif // DOCTEST_CONFIG_NO_EXCEPTIONS - ((void)res); // to silence -Wunused-parameter + } catch(...) {} //!OCLINT - empty catch statement +#endif // DOCTEST_CONFIG_NO_EXCEPTIONS + static_cast<void>(res); // to silence -Wunused-parameter return false; } @@ -2189,37 +2214,37 @@ int registerReporter(const char* name, int priority, bool isReporter) { #ifdef DOCTEST_CONFIG_NO_EXCEPTIONS_BUT_WITH_ALL_ASSERTS -#define DOCTEST_WARN_THROWS(...) ((void)0) -#define DOCTEST_CHECK_THROWS(...) ((void)0) -#define DOCTEST_REQUIRE_THROWS(...) ((void)0) -#define DOCTEST_WARN_THROWS_AS(expr, ...) ((void)0) -#define DOCTEST_CHECK_THROWS_AS(expr, ...) ((void)0) -#define DOCTEST_REQUIRE_THROWS_AS(expr, ...) ((void)0) -#define DOCTEST_WARN_THROWS_WITH(expr, ...) ((void)0) -#define DOCTEST_CHECK_THROWS_WITH(expr, ...) ((void)0) -#define DOCTEST_REQUIRE_THROWS_WITH(expr, ...) ((void)0) -#define DOCTEST_WARN_THROWS_WITH_AS(expr, with, ...) ((void)0) -#define DOCTEST_CHECK_THROWS_WITH_AS(expr, with, ...) ((void)0) -#define DOCTEST_REQUIRE_THROWS_WITH_AS(expr, with, ...) ((void)0) -#define DOCTEST_WARN_NOTHROW(...) ((void)0) -#define DOCTEST_CHECK_NOTHROW(...) ((void)0) -#define DOCTEST_REQUIRE_NOTHROW(...) ((void)0) - -#define DOCTEST_WARN_THROWS_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_CHECK_THROWS_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_REQUIRE_THROWS_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_WARN_THROWS_AS_MESSAGE(expr, ex, msg) ((void)0) -#define DOCTEST_CHECK_THROWS_AS_MESSAGE(expr, ex, msg) ((void)0) -#define DOCTEST_REQUIRE_THROWS_AS_MESSAGE(expr, ex, msg) ((void)0) -#define DOCTEST_WARN_THROWS_WITH_MESSAGE(expr, with, msg) ((void)0) -#define DOCTEST_CHECK_THROWS_WITH_MESSAGE(expr, with, msg) ((void)0) -#define DOCTEST_REQUIRE_THROWS_WITH_MESSAGE(expr, with, msg) ((void)0) -#define DOCTEST_WARN_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) ((void)0) -#define DOCTEST_CHECK_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) ((void)0) -#define DOCTEST_REQUIRE_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) ((void)0) -#define DOCTEST_WARN_NOTHROW_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_CHECK_NOTHROW_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_REQUIRE_NOTHROW_MESSAGE(expr, msg) ((void)0) +#define DOCTEST_WARN_THROWS(...) (static_cast<void>(0)) +#define DOCTEST_CHECK_THROWS(...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_THROWS(...) (static_cast<void>(0)) +#define DOCTEST_WARN_THROWS_AS(expr, ...) (static_cast<void>(0)) +#define DOCTEST_CHECK_THROWS_AS(expr, ...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_THROWS_AS(expr, ...) (static_cast<void>(0)) +#define DOCTEST_WARN_THROWS_WITH(expr, ...) (static_cast<void>(0)) +#define DOCTEST_CHECK_THROWS_WITH(expr, ...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_THROWS_WITH(expr, ...) (static_cast<void>(0)) +#define DOCTEST_WARN_THROWS_WITH_AS(expr, with, ...) (static_cast<void>(0)) +#define DOCTEST_CHECK_THROWS_WITH_AS(expr, with, ...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_THROWS_WITH_AS(expr, with, ...) (static_cast<void>(0)) +#define DOCTEST_WARN_NOTHROW(...) (static_cast<void>(0)) +#define DOCTEST_CHECK_NOTHROW(...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_NOTHROW(...) (static_cast<void>(0)) + +#define DOCTEST_WARN_THROWS_MESSAGE(expr, msg) (static_cast<void>(0)) +#define DOCTEST_CHECK_THROWS_MESSAGE(expr, msg) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_THROWS_MESSAGE(expr, msg) (static_cast<void>(0)) +#define DOCTEST_WARN_THROWS_AS_MESSAGE(expr, ex, msg) (static_cast<void>(0)) +#define DOCTEST_CHECK_THROWS_AS_MESSAGE(expr, ex, msg) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_THROWS_AS_MESSAGE(expr, ex, msg) (static_cast<void>(0)) +#define DOCTEST_WARN_THROWS_WITH_MESSAGE(expr, with, msg) (static_cast<void>(0)) +#define DOCTEST_CHECK_THROWS_WITH_MESSAGE(expr, with, msg) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_THROWS_WITH_MESSAGE(expr, with, msg) (static_cast<void>(0)) +#define DOCTEST_WARN_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) (static_cast<void>(0)) +#define DOCTEST_CHECK_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) (static_cast<void>(0)) +#define DOCTEST_WARN_NOTHROW_MESSAGE(expr, msg) (static_cast<void>(0)) +#define DOCTEST_CHECK_NOTHROW_MESSAGE(expr, msg) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_NOTHROW_MESSAGE(expr, msg) (static_cast<void>(0)) #else // DOCTEST_CONFIG_NO_EXCEPTIONS_BUT_WITH_ALL_ASSERTS @@ -2310,86 +2335,86 @@ int registerReporter(const char* name, int priority, bool isReporter) { #define DOCTEST_REGISTER_REPORTER(name, priority, reporter) #define DOCTEST_REGISTER_LISTENER(name, priority, reporter) -#define DOCTEST_INFO(x) ((void)0) -#define DOCTEST_CAPTURE(x) ((void)0) -#define DOCTEST_ADD_MESSAGE_AT(file, line, x) ((void)0) -#define DOCTEST_ADD_FAIL_CHECK_AT(file, line, x) ((void)0) -#define DOCTEST_ADD_FAIL_AT(file, line, x) ((void)0) -#define DOCTEST_MESSAGE(x) ((void)0) -#define DOCTEST_FAIL_CHECK(x) ((void)0) -#define DOCTEST_FAIL(x) ((void)0) - -#define DOCTEST_WARN(...) ((void)0) -#define DOCTEST_CHECK(...) ((void)0) -#define DOCTEST_REQUIRE(...) ((void)0) -#define DOCTEST_WARN_FALSE(...) ((void)0) -#define DOCTEST_CHECK_FALSE(...) ((void)0) -#define DOCTEST_REQUIRE_FALSE(...) ((void)0) - -#define DOCTEST_WARN_MESSAGE(cond, msg) ((void)0) -#define DOCTEST_CHECK_MESSAGE(cond, msg) ((void)0) -#define DOCTEST_REQUIRE_MESSAGE(cond, msg) ((void)0) -#define DOCTEST_WARN_FALSE_MESSAGE(cond, msg) ((void)0) -#define DOCTEST_CHECK_FALSE_MESSAGE(cond, msg) ((void)0) -#define DOCTEST_REQUIRE_FALSE_MESSAGE(cond, msg) ((void)0) - -#define DOCTEST_WARN_THROWS(...) ((void)0) -#define DOCTEST_CHECK_THROWS(...) ((void)0) -#define DOCTEST_REQUIRE_THROWS(...) ((void)0) -#define DOCTEST_WARN_THROWS_AS(expr, ...) ((void)0) -#define DOCTEST_CHECK_THROWS_AS(expr, ...) ((void)0) -#define DOCTEST_REQUIRE_THROWS_AS(expr, ...) ((void)0) -#define DOCTEST_WARN_THROWS_WITH(expr, ...) ((void)0) -#define DOCTEST_CHECK_THROWS_WITH(expr, ...) ((void)0) -#define DOCTEST_REQUIRE_THROWS_WITH(expr, ...) ((void)0) -#define DOCTEST_WARN_THROWS_WITH_AS(expr, with, ...) ((void)0) -#define DOCTEST_CHECK_THROWS_WITH_AS(expr, with, ...) ((void)0) -#define DOCTEST_REQUIRE_THROWS_WITH_AS(expr, with, ...) ((void)0) -#define DOCTEST_WARN_NOTHROW(...) ((void)0) -#define DOCTEST_CHECK_NOTHROW(...) ((void)0) -#define DOCTEST_REQUIRE_NOTHROW(...) ((void)0) - -#define DOCTEST_WARN_THROWS_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_CHECK_THROWS_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_REQUIRE_THROWS_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_WARN_THROWS_AS_MESSAGE(expr, ex, msg) ((void)0) -#define DOCTEST_CHECK_THROWS_AS_MESSAGE(expr, ex, msg) ((void)0) -#define DOCTEST_REQUIRE_THROWS_AS_MESSAGE(expr, ex, msg) ((void)0) -#define DOCTEST_WARN_THROWS_WITH_MESSAGE(expr, with, msg) ((void)0) -#define DOCTEST_CHECK_THROWS_WITH_MESSAGE(expr, with, msg) ((void)0) -#define DOCTEST_REQUIRE_THROWS_WITH_MESSAGE(expr, with, msg) ((void)0) -#define DOCTEST_WARN_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) ((void)0) -#define DOCTEST_CHECK_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) ((void)0) -#define DOCTEST_REQUIRE_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) ((void)0) -#define DOCTEST_WARN_NOTHROW_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_CHECK_NOTHROW_MESSAGE(expr, msg) ((void)0) -#define DOCTEST_REQUIRE_NOTHROW_MESSAGE(expr, msg) ((void)0) - -#define DOCTEST_WARN_EQ(...) ((void)0) -#define DOCTEST_CHECK_EQ(...) ((void)0) -#define DOCTEST_REQUIRE_EQ(...) ((void)0) -#define DOCTEST_WARN_NE(...) ((void)0) -#define DOCTEST_CHECK_NE(...) ((void)0) -#define DOCTEST_REQUIRE_NE(...) ((void)0) -#define DOCTEST_WARN_GT(...) ((void)0) -#define DOCTEST_CHECK_GT(...) ((void)0) -#define DOCTEST_REQUIRE_GT(...) ((void)0) -#define DOCTEST_WARN_LT(...) ((void)0) -#define DOCTEST_CHECK_LT(...) ((void)0) -#define DOCTEST_REQUIRE_LT(...) ((void)0) -#define DOCTEST_WARN_GE(...) ((void)0) -#define DOCTEST_CHECK_GE(...) ((void)0) -#define DOCTEST_REQUIRE_GE(...) ((void)0) -#define DOCTEST_WARN_LE(...) ((void)0) -#define DOCTEST_CHECK_LE(...) ((void)0) -#define DOCTEST_REQUIRE_LE(...) ((void)0) - -#define DOCTEST_WARN_UNARY(...) ((void)0) -#define DOCTEST_CHECK_UNARY(...) ((void)0) -#define DOCTEST_REQUIRE_UNARY(...) ((void)0) -#define DOCTEST_WARN_UNARY_FALSE(...) ((void)0) -#define DOCTEST_CHECK_UNARY_FALSE(...) ((void)0) -#define DOCTEST_REQUIRE_UNARY_FALSE(...) ((void)0) +#define DOCTEST_INFO(x) (static_cast<void>(0)) +#define DOCTEST_CAPTURE(x) (static_cast<void>(0)) +#define DOCTEST_ADD_MESSAGE_AT(file, line, x) (static_cast<void>(0)) +#define DOCTEST_ADD_FAIL_CHECK_AT(file, line, x) (static_cast<void>(0)) +#define DOCTEST_ADD_FAIL_AT(file, line, x) (static_cast<void>(0)) +#define DOCTEST_MESSAGE(x) (static_cast<void>(0)) +#define DOCTEST_FAIL_CHECK(x) (static_cast<void>(0)) +#define DOCTEST_FAIL(x) (static_cast<void>(0)) + +#define DOCTEST_WARN(...) (static_cast<void>(0)) +#define DOCTEST_CHECK(...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE(...) (static_cast<void>(0)) +#define DOCTEST_WARN_FALSE(...) (static_cast<void>(0)) +#define DOCTEST_CHECK_FALSE(...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_FALSE(...) (static_cast<void>(0)) + +#define DOCTEST_WARN_MESSAGE(cond, msg) (static_cast<void>(0)) +#define DOCTEST_CHECK_MESSAGE(cond, msg) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_MESSAGE(cond, msg) (static_cast<void>(0)) +#define DOCTEST_WARN_FALSE_MESSAGE(cond, msg) (static_cast<void>(0)) +#define DOCTEST_CHECK_FALSE_MESSAGE(cond, msg) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_FALSE_MESSAGE(cond, msg) (static_cast<void>(0)) + +#define DOCTEST_WARN_THROWS(...) (static_cast<void>(0)) +#define DOCTEST_CHECK_THROWS(...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_THROWS(...) (static_cast<void>(0)) +#define DOCTEST_WARN_THROWS_AS(expr, ...) (static_cast<void>(0)) +#define DOCTEST_CHECK_THROWS_AS(expr, ...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_THROWS_AS(expr, ...) (static_cast<void>(0)) +#define DOCTEST_WARN_THROWS_WITH(expr, ...) (static_cast<void>(0)) +#define DOCTEST_CHECK_THROWS_WITH(expr, ...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_THROWS_WITH(expr, ...) (static_cast<void>(0)) +#define DOCTEST_WARN_THROWS_WITH_AS(expr, with, ...) (static_cast<void>(0)) +#define DOCTEST_CHECK_THROWS_WITH_AS(expr, with, ...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_THROWS_WITH_AS(expr, with, ...) (static_cast<void>(0)) +#define DOCTEST_WARN_NOTHROW(...) (static_cast<void>(0)) +#define DOCTEST_CHECK_NOTHROW(...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_NOTHROW(...) (static_cast<void>(0)) + +#define DOCTEST_WARN_THROWS_MESSAGE(expr, msg) (static_cast<void>(0)) +#define DOCTEST_CHECK_THROWS_MESSAGE(expr, msg) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_THROWS_MESSAGE(expr, msg) (static_cast<void>(0)) +#define DOCTEST_WARN_THROWS_AS_MESSAGE(expr, ex, msg) (static_cast<void>(0)) +#define DOCTEST_CHECK_THROWS_AS_MESSAGE(expr, ex, msg) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_THROWS_AS_MESSAGE(expr, ex, msg) (static_cast<void>(0)) +#define DOCTEST_WARN_THROWS_WITH_MESSAGE(expr, with, msg) (static_cast<void>(0)) +#define DOCTEST_CHECK_THROWS_WITH_MESSAGE(expr, with, msg) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_THROWS_WITH_MESSAGE(expr, with, msg) (static_cast<void>(0)) +#define DOCTEST_WARN_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) (static_cast<void>(0)) +#define DOCTEST_CHECK_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_THROWS_WITH_AS_MESSAGE(expr, with, ex, msg) (static_cast<void>(0)) +#define DOCTEST_WARN_NOTHROW_MESSAGE(expr, msg) (static_cast<void>(0)) +#define DOCTEST_CHECK_NOTHROW_MESSAGE(expr, msg) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_NOTHROW_MESSAGE(expr, msg) (static_cast<void>(0)) + +#define DOCTEST_WARN_EQ(...) (static_cast<void>(0)) +#define DOCTEST_CHECK_EQ(...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_EQ(...) (static_cast<void>(0)) +#define DOCTEST_WARN_NE(...) (static_cast<void>(0)) +#define DOCTEST_CHECK_NE(...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_NE(...) (static_cast<void>(0)) +#define DOCTEST_WARN_GT(...) (static_cast<void>(0)) +#define DOCTEST_CHECK_GT(...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_GT(...) (static_cast<void>(0)) +#define DOCTEST_WARN_LT(...) (static_cast<void>(0)) +#define DOCTEST_CHECK_LT(...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_LT(...) (static_cast<void>(0)) +#define DOCTEST_WARN_GE(...) (static_cast<void>(0)) +#define DOCTEST_CHECK_GE(...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_GE(...) (static_cast<void>(0)) +#define DOCTEST_WARN_LE(...) (static_cast<void>(0)) +#define DOCTEST_CHECK_LE(...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_LE(...) (static_cast<void>(0)) + +#define DOCTEST_WARN_UNARY(...) (static_cast<void>(0)) +#define DOCTEST_CHECK_UNARY(...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_UNARY(...) (static_cast<void>(0)) +#define DOCTEST_WARN_UNARY_FALSE(...) (static_cast<void>(0)) +#define DOCTEST_CHECK_UNARY_FALSE(...) (static_cast<void>(0)) +#define DOCTEST_REQUIRE_UNARY_FALSE(...) (static_cast<void>(0)) #endif // DOCTEST_CONFIG_DISABLE @@ -3742,8 +3767,8 @@ namespace { DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wdeprecated-declarations") void color_to_stream(std::ostream& s, Color::Enum code) { - ((void)s); // for DOCTEST_CONFIG_COLORS_NONE or DOCTEST_CONFIG_COLORS_WINDOWS - ((void)code); // for DOCTEST_CONFIG_COLORS_NONE + static_cast<void>(s); // for DOCTEST_CONFIG_COLORS_NONE or DOCTEST_CONFIG_COLORS_WINDOWS + static_cast<void>(code); // for DOCTEST_CONFIG_COLORS_NONE #ifdef DOCTEST_CONFIG_COLORS_ANSI if(g_no_colors || (isatty(STDOUT_FILENO) == false && getContextOptions()->force_colors == false)) @@ -3849,7 +3874,28 @@ namespace detail { #ifdef DOCTEST_IS_DEBUGGER_ACTIVE bool isDebuggerActive() { return DOCTEST_IS_DEBUGGER_ACTIVE(); } #else // DOCTEST_IS_DEBUGGER_ACTIVE -#ifdef DOCTEST_PLATFORM_MAC +#ifdef DOCTEST_PLATFORM_LINUX + class ErrnoGuard { + public: + ErrnoGuard() : m_oldErrno(errno) {} + ~ErrnoGuard() { errno = m_oldErrno; } + private: + int m_oldErrno; + }; + // See the comments in Catch2 for the reasoning behind this implementation: + // https://github.com/catchorg/Catch2/blob/v2.13.1/include/internal/catch_debugger.cpp#L79-L102 + bool isDebuggerActive() { + ErrnoGuard guard; + std::ifstream in("/proc/self/status"); + for(std::string line; std::getline(in, line);) { + static const int PREFIX_LEN = 11; + if(line.compare(0, PREFIX_LEN, "TracerPid:\t") == 0) { + return line.length() > PREFIX_LEN && line[PREFIX_LEN] != '0'; + } + } + return false; + } +#elif defined(DOCTEST_PLATFORM_MAC) // The following function is taken directly from the following technical note: // https://developer.apple.com/library/archive/qa/qa1361/_index.html // Returns true if the current process is being debugged (either @@ -5460,25 +5506,28 @@ DOCTEST_CLANG_SUPPRESS_WARNING_POP separator_to_stream(); s << std::dec; + auto totwidth = int(std::ceil(log10((std::max(p.numTestCasesPassingFilters, static_cast<unsigned>(p.numAsserts))) + 1))); + auto passwidth = int(std::ceil(log10((std::max(p.numTestCasesPassingFilters - p.numTestCasesFailed, static_cast<unsigned>(p.numAsserts - p.numAssertsFailed))) + 1))); + auto failwidth = int(std::ceil(log10((std::max(p.numTestCasesFailed, static_cast<unsigned>(p.numAssertsFailed))) + 1))); const bool anythingFailed = p.numTestCasesFailed > 0 || p.numAssertsFailed > 0; - s << Color::Cyan << "[doctest] " << Color::None << "test cases: " << std::setw(6) + s << Color::Cyan << "[doctest] " << Color::None << "test cases: " << std::setw(totwidth) << p.numTestCasesPassingFilters << " | " << ((p.numTestCasesPassingFilters == 0 || anythingFailed) ? Color::None : Color::Green) - << std::setw(6) << p.numTestCasesPassingFilters - p.numTestCasesFailed << " passed" + << std::setw(passwidth) << p.numTestCasesPassingFilters - p.numTestCasesFailed << " passed" << Color::None << " | " << (p.numTestCasesFailed > 0 ? Color::Red : Color::None) - << std::setw(6) << p.numTestCasesFailed << " failed" << Color::None << " | "; + << std::setw(failwidth) << p.numTestCasesFailed << " failed" << Color::None << " |"; if(opt.no_skipped_summary == false) { const int numSkipped = p.numTestCases - p.numTestCasesPassingFilters; - s << (numSkipped == 0 ? Color::None : Color::Yellow) << std::setw(6) << numSkipped + s << " " << (numSkipped == 0 ? Color::None : Color::Yellow) << numSkipped << " skipped" << Color::None; } s << "\n"; - s << Color::Cyan << "[doctest] " << Color::None << "assertions: " << std::setw(6) + s << Color::Cyan << "[doctest] " << Color::None << "assertions: " << std::setw(totwidth) << p.numAsserts << " | " << ((p.numAsserts == 0 || anythingFailed) ? Color::None : Color::Green) - << std::setw(6) << (p.numAsserts - p.numAssertsFailed) << " passed" << Color::None - << " | " << (p.numAssertsFailed > 0 ? Color::Red : Color::None) << std::setw(6) + << std::setw(passwidth) << (p.numAsserts - p.numAssertsFailed) << " passed" << Color::None + << " | " << (p.numAssertsFailed > 0 ? Color::Red : Color::None) << std::setw(failwidth) << p.numAssertsFailed << " failed" << Color::None << " |\n"; s << Color::Cyan << "[doctest] " << Color::None << "Status: " << (p.numTestCasesFailed > 0 ? Color::Red : Color::Green) diff --git a/thirdparty/doctest/patches/fix-arm64-mac.patch b/thirdparty/doctest/patches/fix-arm64-mac.patch deleted file mode 100644 index f78014534f..0000000000 --- a/thirdparty/doctest/patches/fix-arm64-mac.patch +++ /dev/null @@ -1,18 +0,0 @@ -diff --git a/thirdparty/doctest/doctest.h b/thirdparty/doctest/doctest.h -index 9444698286..e4fed12767 100644 ---- a/thirdparty/doctest/doctest.h -+++ b/thirdparty/doctest/doctest.h -@@ -356,7 +356,13 @@ DOCTEST_MSVC_SUPPRESS_WARNING(26812) // Prefer 'enum class' over 'enum' - #ifndef DOCTEST_BREAK_INTO_DEBUGGER - // should probably take a look at https://github.com/scottt/debugbreak - #ifdef DOCTEST_PLATFORM_MAC -+// -- GODOT start -- -+#if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) - #define DOCTEST_BREAK_INTO_DEBUGGER() __asm__("int $3\n" : :) -+#else -+#define DOCTEST_BREAK_INTO_DEBUGGER() __asm__("brk #0"); -+#endif -+// -- GODOT end -- - #elif DOCTEST_MSVC - #define DOCTEST_BREAK_INTO_DEBUGGER() __debugbreak() - #elif defined(__MINGW32__) diff --git a/thirdparty/nanosvg/nanosvg.h b/thirdparty/nanosvg/nanosvg.h index e5f6900614..4c03ee5893 100644 --- a/thirdparty/nanosvg/nanosvg.h +++ b/thirdparty/nanosvg/nanosvg.h @@ -225,11 +225,6 @@ static int nsvg__isdigit(char c) return c >= '0' && c <= '9'; } -static int nsvg__isnum(char c) -{ - return strchr("0123456789+-.eE", c) != 0; -} - static NSVG_INLINE float nsvg__minf(float a, float b) { return a < b ? a : b; } static NSVG_INLINE float nsvg__maxf(float a, float b) { return a > b ? a : b; } @@ -736,9 +731,11 @@ static void nsvg__lineTo(NSVGparser* p, float x, float y) static void nsvg__cubicBezTo(NSVGparser* p, float cpx1, float cpy1, float cpx2, float cpy2, float x, float y) { - nsvg__addPoint(p, cpx1, cpy1); - nsvg__addPoint(p, cpx2, cpy2); - nsvg__addPoint(p, x, y); + if (p->npts > 0) { + nsvg__addPoint(p, cpx1, cpy1); + nsvg__addPoint(p, cpx2, cpy2); + nsvg__addPoint(p, x, y); + } } static NSVGattrib* nsvg__getAttr(NSVGparser* p) @@ -808,7 +805,9 @@ static float nsvg__convertToPixels(NSVGparser* p, NSVGcoordinate c, float orig, static NSVGgradientData* nsvg__findGradientData(NSVGparser* p, const char* id) { NSVGgradientData* grad = p->gradients; - while (grad) { + if (id == NULL || *id == '\0') + return NULL; + while (grad != NULL) { if (strcmp(grad->id, id) == 0) return grad; grad = grad->next; @@ -825,19 +824,26 @@ static NSVGgradient* nsvg__createGradient(NSVGparser* p, const char* id, const f NSVGgradient* grad; float ox, oy, sw, sh, sl; int nstops = 0; + int refIter; data = nsvg__findGradientData(p, id); if (data == NULL) return NULL; // TODO: use ref to fill in all unset values too. ref = data; + refIter = 0; while (ref != NULL) { + NSVGgradientData* nextRef = NULL; if (stops == NULL && ref->stops != NULL) { stops = ref->stops; nstops = ref->nstops; break; } - ref = nsvg__findGradientData(p, ref->ref); + nextRef = nsvg__findGradientData(p, ref->ref); + if (nextRef == ref) break; // prevent infite loops on malformed data + ref = nextRef; + refIter++; + if (refIter > 32) break; // prevent infite loops on malformed data } if (stops == NULL) return NULL; @@ -1040,6 +1046,10 @@ static void nsvg__addPath(NSVGparser* p, char closed) if (closed) nsvg__lineTo(p, p->pts[0], p->pts[1]); + // Expect 1 + N*3 points (N = number of cubic bezier segments). + if ((p->npts % 3) != 1) + return; + path = (NSVGpath*)malloc(sizeof(NSVGpath)); if (path == NULL) goto error; memset(path, 0, sizeof(NSVGpath)); @@ -1458,6 +1468,15 @@ static int nsvg__parseUnits(const char* units) return NSVG_UNITS_USER; } +static int nsvg__isCoordinate(const char* s) +{ + // optional sign + if (*s == '-' || *s == '+') + s++; + // must have at least one digit, or start by a dot + return (nsvg__isdigit(*s) || *s == '.'); +} + static NSVGcoordinate nsvg__parseCoordinateRaw(const char* str) { NSVGcoordinate coord = {0, NSVG_UNITS_USER}; @@ -1597,25 +1616,32 @@ static int nsvg__parseRotate(float* xform, const char* str) static void nsvg__parseTransform(float* xform, const char* str) { float t[6]; + int len; nsvg__xformIdentity(xform); while (*str) { if (strncmp(str, "matrix", 6) == 0) - str += nsvg__parseMatrix(t, str); + len = nsvg__parseMatrix(t, str); else if (strncmp(str, "translate", 9) == 0) - str += nsvg__parseTranslate(t, str); + len = nsvg__parseTranslate(t, str); else if (strncmp(str, "scale", 5) == 0) - str += nsvg__parseScale(t, str); + len = nsvg__parseScale(t, str); else if (strncmp(str, "rotate", 6) == 0) - str += nsvg__parseRotate(t, str); + len = nsvg__parseRotate(t, str); else if (strncmp(str, "skewX", 5) == 0) - str += nsvg__parseSkewX(t, str); + len = nsvg__parseSkewX(t, str); else if (strncmp(str, "skewY", 5) == 0) - str += nsvg__parseSkewY(t, str); + len = nsvg__parseSkewY(t, str); else{ ++str; continue; } + if (len != 0) { + str += len; + } else { + ++str; + continue; + } nsvg__xformPremultiply(xform, t); } @@ -1876,8 +1902,11 @@ static int nsvg__getArgsPerElement(char cmd) case 'a': case 'A': return 7; + case 'z': + case 'Z': + return 0; } - return 0; + return -1; } static void nsvg__pathMoveTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel) @@ -2187,6 +2216,7 @@ static void nsvg__parsePath(NSVGparser* p, const char** attr) float args[10]; int nargs; int rargs = 0; + char initPoint; float cpx, cpy, cpx2, cpy2; const char* tmp[4]; char closedFlag; @@ -2209,13 +2239,14 @@ static void nsvg__parsePath(NSVGparser* p, const char** attr) nsvg__resetPath(p); cpx = 0; cpy = 0; cpx2 = 0; cpy2 = 0; + initPoint = 0; closedFlag = 0; nargs = 0; while (*s) { s = nsvg__getNextPathItem(s, item); if (!*item) break; - if (nsvg__isnum(item[0])) { + if (cmd != '\0' && nsvg__isCoordinate(item)) { if (nargs < 10) args[nargs++] = (float)nsvg__atof(item); if (nargs >= rargs) { @@ -2228,6 +2259,7 @@ static void nsvg__parsePath(NSVGparser* p, const char** attr) cmd = (cmd == 'm') ? 'l' : 'L'; rargs = nsvg__getArgsPerElement(cmd); cpx2 = cpx; cpy2 = cpy; + initPoint = 1; break; case 'l': case 'L': @@ -2277,7 +2309,6 @@ static void nsvg__parsePath(NSVGparser* p, const char** attr) } } else { cmd = item[0]; - rargs = nsvg__getArgsPerElement(cmd); if (cmd == 'M' || cmd == 'm') { // Commit path. if (p->npts > 0) @@ -2286,7 +2317,11 @@ static void nsvg__parsePath(NSVGparser* p, const char** attr) nsvg__resetPath(p); closedFlag = 0; nargs = 0; - } else if (cmd == 'Z' || cmd == 'z') { + } else if (initPoint == 0) { + // Do not allow other commands until initial point has been set (moveTo called once). + cmd = '\0'; + } + if (cmd == 'Z' || cmd == 'z') { closedFlag = 1; // Commit path. if (p->npts > 0) { @@ -2302,6 +2337,12 @@ static void nsvg__parsePath(NSVGparser* p, const char** attr) closedFlag = 0; nargs = 0; } + rargs = nsvg__getArgsPerElement(cmd); + if (rargs == -1) { + // Command not recognized + cmd = '\0'; + rargs = 0; + } } } // Commit path. |