test_gradient.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*************************************************************************/
  2. /* test_gradient.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef TEST_GRADIENT_H
  31. #define TEST_GRADIENT_H
  32. #include "core/math/color.h"
  33. #include "core/object/class_db.h"
  34. #include "scene/resources/gradient.h"
  35. #include "thirdparty/doctest/doctest.h"
  36. namespace TestGradient {
  37. TEST_CASE("[Gradient] Default gradient") {
  38. // Black-white gradient.
  39. Ref<Gradient> gradient = memnew(Gradient);
  40. CHECK_MESSAGE(
  41. gradient->get_points_count() == 2,
  42. "Default gradient should contain the expected number of points.");
  43. CHECK_MESSAGE(
  44. gradient->get_color_at_offset(0.0).is_equal_approx(Color(0, 0, 0)),
  45. "Default gradient should return the expected interpolated value at offset 0.0.");
  46. CHECK_MESSAGE(
  47. gradient->get_color_at_offset(0.4).is_equal_approx(Color(0.4, 0.4, 0.4)),
  48. "Default gradient should return the expected interpolated value at offset 0.4.");
  49. CHECK_MESSAGE(
  50. gradient->get_color_at_offset(0.8).is_equal_approx(Color(0.8, 0.8, 0.8)),
  51. "Default gradient should return the expected interpolated value at offset 0.8.");
  52. CHECK_MESSAGE(
  53. gradient->get_color_at_offset(1.0).is_equal_approx(Color(1, 1, 1)),
  54. "Default gradient should return the expected interpolated value at offset 1.0.");
  55. // Out of bounds checks.
  56. CHECK_MESSAGE(
  57. gradient->get_color_at_offset(-1.0).is_equal_approx(Color(0, 0, 0)),
  58. "Default gradient should return the expected interpolated value at offset -1.0.");
  59. CHECK_MESSAGE(
  60. gradient->get_color_at_offset(1234.0).is_equal_approx(Color(1, 1, 1)),
  61. "Default gradient should return the expected interpolated value at offset 1234.0.");
  62. }
  63. TEST_CASE("[Gradient] Custom gradient (points specified in order)") {
  64. // Red-yellow-green gradient (with overbright green).
  65. Ref<Gradient> gradient = memnew(Gradient);
  66. Vector<Gradient::Point> points;
  67. points.push_back({ 0.0, Color(1, 0, 0) });
  68. points.push_back({ 0.5, Color(1, 1, 0) });
  69. points.push_back({ 1.0, Color(0, 2, 0) });
  70. gradient->set_points(points);
  71. CHECK_MESSAGE(
  72. gradient->get_points_count() == 3,
  73. "Custom gradient should contain the expected number of points.");
  74. CHECK_MESSAGE(
  75. gradient->get_color_at_offset(0.0).is_equal_approx(Color(1, 0, 0)),
  76. "Custom gradient should return the expected interpolated value at offset 0.0.");
  77. CHECK_MESSAGE(
  78. gradient->get_color_at_offset(0.25).is_equal_approx(Color(1, 0.5, 0)),
  79. "Custom gradient should return the expected interpolated value at offset 0.25.");
  80. CHECK_MESSAGE(
  81. gradient->get_color_at_offset(0.5).is_equal_approx(Color(1, 1, 0)),
  82. "Custom gradient should return the expected interpolated value at offset 0.5.");
  83. CHECK_MESSAGE(
  84. gradient->get_color_at_offset(0.75).is_equal_approx(Color(0.5, 1.5, 0)),
  85. "Custom gradient should return the expected interpolated value at offset 0.75.");
  86. CHECK_MESSAGE(
  87. gradient->get_color_at_offset(1.0).is_equal_approx(Color(0, 2, 0)),
  88. "Custom gradient should return the expected interpolated value at offset 1.0.");
  89. gradient->remove_point(1);
  90. CHECK_MESSAGE(
  91. gradient->get_points_count() == 2,
  92. "Custom gradient should contain the expected number of points after removing one point.");
  93. CHECK_MESSAGE(
  94. gradient->get_color_at_offset(0.5).is_equal_approx(Color(0.5, 1, 0)),
  95. "Custom gradient should return the expected interpolated value at offset 0.5 after removing point at index 1.");
  96. }
  97. TEST_CASE("[Gradient] Custom gradient (points specified out-of-order)") {
  98. // HSL rainbow with points specified out of order.
  99. // These should be sorted automatically when adding points.
  100. Ref<Gradient> gradient = memnew(Gradient);
  101. Vector<Gradient::Point> points;
  102. points.push_back({ 0.2, Color(1, 0, 0) });
  103. points.push_back({ 0.0, Color(1, 1, 0) });
  104. points.push_back({ 0.8, Color(0, 1, 0) });
  105. points.push_back({ 0.4, Color(0, 1, 1) });
  106. points.push_back({ 1.0, Color(0, 0, 1) });
  107. points.push_back({ 0.6, Color(1, 0, 1) });
  108. gradient->set_points(points);
  109. CHECK_MESSAGE(
  110. gradient->get_points_count() == 6,
  111. "Custom out-of-order gradient should contain the expected number of points.");
  112. CHECK_MESSAGE(
  113. gradient->get_color_at_offset(0.0).is_equal_approx(Color(1, 1, 0)),
  114. "Custom out-of-order gradient should return the expected interpolated value at offset 0.0.");
  115. CHECK_MESSAGE(
  116. gradient->get_color_at_offset(0.3).is_equal_approx(Color(0.5, 0.5, 0.5)),
  117. "Custom out-of-order gradient should return the expected interpolated value at offset 0.3.");
  118. CHECK_MESSAGE(
  119. gradient->get_color_at_offset(0.6).is_equal_approx(Color(1, 0, 1)),
  120. "Custom out-of-order gradient should return the expected interpolated value at offset 0.6.");
  121. CHECK_MESSAGE(
  122. gradient->get_color_at_offset(1.0).is_equal_approx(Color(0, 0, 1)),
  123. "Custom out-of-order gradient should return the expected interpolated value at offset 1.0.");
  124. gradient->remove_point(0);
  125. CHECK_MESSAGE(
  126. gradient->get_points_count() == 5,
  127. "Custom out-of-order gradient should contain the expected number of points after removing one point.");
  128. // The color will be clamped to the nearest point (which is at offset 0.2).
  129. CHECK_MESSAGE(
  130. gradient->get_color_at_offset(0.1).is_equal_approx(Color(1, 0, 0)),
  131. "Custom out-of-order gradient should return the expected interpolated value at offset 0.1 after removing point at index 0.");
  132. }
  133. } // namespace TestGradient
  134. #endif // TEST_GRADIENT_H